home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 549b.lha / M2P_v1.0_sources / source.lzh / FStorage.mpp < prev    next >
Text File  |  1991-08-10  |  5KB  |  204 lines

  1. (*======================================================================*)
  2.  
  3. IMPLEMENTATION MODULE FStorage;
  4.  
  5. IMPORT SYSTEM;
  6.  
  7. @IF AMIGA THEN IMPORT Memory;
  8. @ELSE          IMPORT Storage;
  9. @END
  10.  
  11. @INCLUDE "MACROS"
  12.  
  13. @NoLongAddressing       (* I really trust this code :-) *)
  14. @NoRangeChecks
  15. @NoVChecks
  16. @NoStackChecks
  17. @NoNILChecks
  18.  
  19. (*----------------------------------------------------------------------*)
  20. (*  Memory Tracking list structure                                      *)
  21. (*----------------------------------------------------------------------*)
  22.  
  23. TYPE  TrackList = POINTER TO TrackNode;
  24.       TrackNode = RECORD
  25.                      Next : TrackList;
  26.                      Size : LONGCARD;
  27.                   END;
  28.  
  29. CONST TrackSize = SYSTEM.TSIZE(TrackNode);
  30.  
  31. VAR   Track : TrackList;
  32.       
  33. (*----------------------------------------------------------------------*)
  34. (*  Type definition for the FreeList.                                   *)
  35. (*----------------------------------------------------------------------*)
  36.  
  37. TYPE ListRec = RECORD
  38.                   Free   :  TrackList;
  39.                   HowMany:  CARDINAL;
  40.                END;
  41.               
  42. CONST BlockSize = 4096;         (* Blocking factor (must be > MaxBlock) *)
  43.       MaxBlock  = 512;          (* Maximum size block using freelist    *)
  44.       Grain     = 8;            (* Allocations sizes are modulus Grain  *)
  45.  
  46. CONST NumFreeLists = MaxBlock DIV Grain;
  47.  
  48. VAR FreeList: ARRAY [1..NumFreeLists] OF ListRec;
  49.            i: CARDINAL;
  50.  
  51. (*----------------------------------------------------------------------*)
  52.  
  53. PROCEDURE ALLOCATE(VAR addr:SYSTEM.ADDRESS; amount: LONGCARD);
  54.  
  55. VAR node : TrackList;
  56.     i:     CARDINAL;
  57.     siz:   SYSTEM.ADDRESS;
  58.  
  59. BEGIN
  60.    IF amount > MaxBlock THEN
  61.       addr:=NIL;
  62.       INC(amount,TrackSize);
  63.       @IF AMIGA THEN
  64.          node:=Memory.AllocMem(amount,Memory.MemReqSet{Memory.MemPublic});
  65.          IF SYSTEM.ADDRESS(node) # SYSTEM.ADDRESS(0) THEN
  66.             node^.Size:=amount;
  67.             addr:=SYSTEM.ADDRESS(node);
  68.             INC(addr,TrackSize);
  69.             node^.Next:=Track;
  70.             Track:=node;
  71.          END;
  72.       @ELSE
  73.          Storage.ALLOCATE(node,amount);
  74.          IF node # NIL THEN
  75.             node^.Size:=amount;
  76.             addr:=SYSTEM.ADDRESS(node);
  77.             INC(addr,TrackSize);
  78.             node^.Next:=Track;
  79.             Track:=node;
  80.          END;
  81.       @END
  82.    ELSIF amount=0 THEN DISPOSEALL; HALT;
  83.    ELSE
  84.       INC(amount,Grain-1);
  85.       i:=CARDINAL(amount) DIV Grain;
  86.       WITH FreeList[i] DO
  87.          IF Free = NIL THEN
  88.             ALLOCATE(node,LONGCARD(BlockSize));
  89.             IF node = NIL THEN
  90.                addr:=NIL;
  91.                RETURN;
  92.             END;
  93.             siz :=SYSTEM.ADDRESS(i*Grain);
  94.             i := 0;
  95.             WHILE i<HowMany DO
  96.                node^.Next:=Free;
  97.                Free:=node;
  98.                node:=SYSTEM.ADDRESS(node)+siz;
  99.                INC(i);
  100.             END;
  101.          END;
  102.          addr:=Free;
  103.          Free:=Free^.Next;
  104.       END;
  105.    END;
  106. END ALLOCATE;
  107.  
  108. (*----------------------------------------------------------------------*)
  109.  
  110. PROCEDURE DEALLOCATE(VAR addr:SYSTEM.ADDRESS; amount: LONGCARD);
  111.  
  112. VAR node,follow: TrackList;
  113.     i:           CARDINAL;
  114.  
  115. BEGIN 
  116.    IF addr # NIL THEN
  117.       IF amount > MaxBlock THEN
  118.          node:=Track;
  119.          DEC(addr,TrackSize);
  120.          WHILE (node # addr) AND (node # NIL) DO
  121.             follow:=node;
  122.             node:=follow^.Next;
  123.          END;
  124.          IF node # NIL THEN
  125.             IF Track=node THEN
  126.                Track:=node^.Next;
  127.             ELSE
  128.                follow^.Next:=node^.Next;
  129.             END;
  130.             @IF AMIGA THEN
  131.                Memory.FreeMem(node,node^.Size);
  132.             @ELSE
  133.                Storage.DEALLOCATE(node,node^.Size);
  134.             @END
  135.          END;
  136.       ELSIF amount # 0 THEN
  137.          node:=addr;
  138.          INC(amount,Grain-1);
  139.          i:=CARDINAL(amount) DIV Grain;
  140.          WITH FreeList[i] DO
  141.             node^.Next:=Free;
  142.             Free:=node;
  143.          END;
  144.       END;
  145.       addr:=NIL;
  146.    END;
  147. END DEALLOCATE;
  148.  
  149. (*----------------------------------------------------------------------*)
  150.  
  151. @NoCopyStrings
  152.  
  153. PROCEDURE DUPLICATE(block: ARRAY OF SYSTEM.BYTE):SYSTEM.ADDRESS;
  154.  
  155. VAR newblock: SYSTEM.ADDRESS;
  156.            i: CARDINAL;
  157.          ptr: POINTER TO SYSTEM.BYTE;
  158.  
  159. BEGIN
  160.    ALLOCATE(newblock,LONGCARD(HIGH(block)+1));
  161.    ptr:=newblock;
  162.    i:=0;
  163.    WHILE (i<=HIGH(block)) DO
  164.       ptr^:=block[i];
  165.       INC(i);
  166.       INC(ptr);
  167.    END;
  168.    RETURN newblock;
  169. END DUPLICATE;
  170.       
  171. (*----------------------------------------------------------------------*)
  172. (*  DISPOSALL   Walks the FreeList, disposing of all allocated memory.  *)
  173. (*----------------------------------------------------------------------*)
  174.  
  175. PROCEDURE DISPOSEALL();
  176.  
  177. VAR next : TrackList;
  178.  
  179. BEGIN
  180.    WHILE Track # NIL DO
  181.       next:=Track^.Next;
  182.       @IF AMIGA THEN
  183.          Memory.FreeMem(Track,Track^.Size);
  184.       @ELSE
  185.          Storage.DEALLOCATE(Track,Track^.Size);
  186.       @END
  187.       Track:=next;
  188.    END;
  189. END DISPOSEALL;
  190.  
  191. (************************************************************************)
  192. (*  MAIN INITIALIZATION CODE                                            *)
  193. (************************************************************************)
  194.  
  195. BEGIN
  196.    Track:=NIL;
  197.    FOR i:= 1 TO NumFreeLists DO
  198.       WITH FreeList[i] DO
  199.          Free    := NIL;
  200.          HowMany := BlockSize DIV (i*Grain);
  201.       END;
  202.    END;
  203. END FStorage.
  204.