home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / allocate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  1.7 KB  |  54 lines

  1. ;/* allocate.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 allocate.c
  3. Blink FROM LIB:c.o,allocate.o TO allocate LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5. allocate.c - example of allocating and using a private memory pool.
  6. */
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <clib/exec_protos.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #ifdef LATTICE
  14. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  15. void chkabort(void) { return; }  /* really */
  16. #endif
  17.  
  18. #define BLOCKSIZE 4000     /* or whatever you need */
  19.  
  20. VOID main(VOID)
  21. {
  22.     struct MemHeader *mh;
  23.     struct MemChunk  *mc;
  24.     APTR   block1, block2;
  25.  
  26.     /* Get the MemHeader needed to keep track of our new block. */
  27.     mh = (struct MemHeader *)AllocMem((LONG)sizeof(struct MemHeader), MEMF_CLEAR);
  28.     if (!mh) exit(10);
  29.  
  30.     /* Get the actual block the above MemHeader will manage. */
  31.     if ( !(mc = (struct MemChunk *)AllocMem(BLOCKSIZE, 0)) );
  32.     {
  33.         FreeMem(mh, (LONG)sizeof(struct MemHeader));
  34.         exit(10);
  35.     }
  36.     mh->mh_Node.ln_Type = NT_MEMORY;
  37.     mh->mh_First        = mc;
  38.     mh->mh_Lower        = (APTR)mc;
  39.     mh->mh_Upper        = (APTR)(BLOCKSIZE + (ULONG)mc);
  40.     mh->mh_Free         = BLOCKSIZE;
  41.  
  42.     mc->mc_Next  = NULL;                     /* Set up first chunk in the freelist */
  43.     mc->mc_Bytes = BLOCKSIZE;
  44.  
  45.     block1 = (APTR)Allocate(mh,20);
  46.     block2 = (APTR)Allocate(mh, 314);
  47.  
  48.     printf("Our MemHeader struct at $%lx. Our block of memory at $%lx\n", mh, mc);
  49.     printf("Allocated from our pool: block1 at $%lx, block2 at $%lx\n", block1, block2);
  50.  
  51.     FreeMem(mh, (LONG)sizeof(struct MemHeader));
  52.     FreeMem(mc, (LONG)BLOCKSIZE);
  53. }
  54.