home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / E / TFF-A32R.LZX / AmigaE3.2a / RkrmSrc / Exec_Library / Memory / allocate.e next >
Encoding:
Text File  |  1996-08-29  |  1015 b   |  41 lines

  1. -> allocate.e - Example of allocating and using a private memory pool.
  2.  
  3. MODULE 'exec/memory',
  4.        'exec/nodes'
  5.  
  6. CONST BLOCKSIZE=4000  -> Or whatever you need
  7.  
  8. PROC main() HANDLE
  9.   DEF mh=NIL:PTR TO mh, mc=NIL:PTR TO mc, block1, block2
  10.  
  11.   -> Get the MemHeader needed to keep track of our new block.
  12.   NEW mh
  13.  
  14.   -> Get the actual block the above MemHeader will manage.
  15.   mc:=NewR(BLOCKSIZE)
  16.  
  17.   mh.ln.type:=NT_MEMORY
  18.   mh.first:=mc
  19.   mh.lower:=mc
  20.   mh.upper:=mc+BLOCKSIZE
  21.   mh.free:=BLOCKSIZE
  22.  
  23.   mc.next:=NIL  -> Set up first chunk in the freelist
  24.   mc.bytes:=BLOCKSIZE
  25.  
  26.   block1:=Allocate(mh, 20)
  27.   block2:=Allocate(mh, 314)
  28.  
  29.   WriteF('Our mh object at $\h.  Our block of memory at $\h\n', mh, mc)
  30.   WriteF('Allocated from our pool: block1 at $\h, block2 at $\h\n',
  31.          block1, block2)
  32.  
  33. EXCEPT DO
  34.   -> E-Note: the freeing is not necessary, since the program is terminating
  35.   IF mc THEN Dispose(mc)
  36.   IF mh THEN END mh
  37.   SELECT exception
  38.   CASE "MEM";  WriteF('Error: Ran out of memory\n')
  39.   ENDSELECT
  40. ENDPROC
  41.