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

  1. ;/* allocentry.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 allocentry.c
  3. Blink FROM LIB:c.o,allocentry.o TO allocentry LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. allocentry.c - example of allocating several memory areas.
  7. */
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <clib/exec_protos.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #ifdef LATTICE
  15. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  16. void chkabort(void) { return; }  /* really */
  17. #endif
  18.  
  19. #define ALLOCERROR 0x80000000
  20.  
  21. struct MemList *memlist;             /* pointer to a MemList structure        */
  22.  
  23. struct MemBlocks /* define a new structure because C cannot initialize unions */
  24. {
  25.     struct MemList  mn_head;         /* one entry in the header               */
  26.     struct MemEntry mn_body[3];      /* additional entries follow directly as */
  27. } memblocks;                         /* part of the same data structure       */
  28.  
  29. VOID main(VOID)
  30. {
  31.     memblocks.mn_head.ml_NumEntries = 4; /* 4! Since the MemEntry starts at 1! */
  32.  
  33.     /* Describe the first piece of memory we want.  Because of our MemBlocks structure */
  34.     /* setup, we reference the first MemEntry differently when initializing it.        */
  35.     memblocks.mn_head.ml_ME[0].me_Reqs = MEMF_CLEAR;
  36.     memblocks.mn_head.ml_ME[0].me_Length = 4000;
  37.  
  38.     memblocks.mn_body[0].me_Reqs   = MEMF_CHIP | MEMF_CLEAR;   /* Describe the other pieces of    */
  39.     memblocks.mn_body[0].me_Length = 100000;                   /* memory we want. Additional      */
  40.     memblocks.mn_body[1].me_Reqs   = MEMF_PUBLIC | MEMF_CLEAR; /* MemEntries are initialized this */
  41.     memblocks.mn_body[1].me_Length = 200000;                   /* way. If we wanted even more en- */
  42.     memblocks.mn_body[2].me_Reqs   = MEMF_PUBLIC;              /* tries, we would need to declare */
  43.     memblocks.mn_body[2].me_Length = 25000;                    /* a larger MemEntry array in our  */
  44.                                                                /* MemBlocks structure.            */
  45.  
  46.     memlist = (struct MemList *)AllocEntry((struct MemList *)&memblocks);
  47.  
  48.     if ((ULONG)memlist & ALLOCERROR)          /* 'error' bit 31 is set (see below). */
  49.     {
  50.        printf("AllocEntry FAILED\n");
  51.        exit(200);
  52.     }
  53.     /* We got all memory we wanted.  Use it and call FreeEntry() to free it */
  54.     printf("AllocEntry succeeded - now freeing all allocated blocks\n");
  55.     FreeEntry(memlist);
  56. }
  57.