home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / IOLib / misc / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  1.9 KB  |  117 lines

  1. /*
  2.  * malloc.c: Amiga version is quite a bit simpler than gem version.
  3.  *          15May89 - Created by Jeff Lydiatt
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <stddef.h>
  8. #include <exec/memory.h>
  9.  
  10. typedef struct memchunk {
  11.     struct memchunk    *next;
  12.     struct memchunk    *prev;
  13.     long         size;
  14. }
  15.     MEMCHUNK;
  16.  
  17. extern char *AllocMem();
  18. extern void FreeMem();
  19. extern void  (*_freeall)();
  20. void freeall(); /* forward reference */
  21.  
  22. static MEMCHUNK sentinel;
  23.  
  24.     char *
  25. lalloc( size )
  26. unsigned long size;
  27. {
  28.     register MEMCHUNK *mp;
  29.     register unsigned long chunksize;    
  30.  
  31.     if (sentinel.prev == NULL) { /* 'Sozobon-C won't allow static def*/
  32.         sentinel.prev = &sentinel;
  33.         sentinel.next = &sentinel;
  34.     }
  35.  
  36.     chunksize = size + sizeof(MEMCHUNK);
  37.     mp = (MEMCHUNK *)AllocMem( chunksize, MEMF_CLEAR);
  38.     if ( mp == NULL )
  39.         return NULL;
  40.  
  41.     /*
  42.      * Keep the forward and backward links.
  43.      */
  44.  
  45.     sentinel.prev->next = mp;
  46.     mp->prev = sentinel.prev;
  47.     sentinel.prev = mp;
  48.  
  49.     mp->next = &sentinel;
  50.     mp->size = chunksize;
  51.     _freeall = freeall;
  52.     return ++mp;
  53. }
  54.  
  55. char *malloc(size)
  56. unsigned int size;
  57. {
  58.     return(lalloc((long)size));
  59. }
  60.  
  61.  
  62.     void
  63. free( p )
  64. char *p;
  65. {
  66.     register MEMCHUNK *mp, *prevmp, *nextmp;
  67.     long Output();
  68.  
  69.     mp = (MEMCHUNK *)(p - sizeof(MEMCHUNK));
  70.  
  71.     /*
  72.      * Sanity check: the prev link should point to us. Do nothing if bad.
  73.      */
  74.  
  75.     prevmp = mp->prev;
  76.     nextmp = mp->next;
  77.     if ( prevmp->next != mp ) {
  78.         return;
  79.     }
  80.  
  81.     FreeMem( mp, mp->size );
  82.     prevmp->next = nextmp;
  83.     nextmp->prev = prevmp;
  84.  
  85.     return;
  86. }
  87.  
  88. char *calloc(n, size)
  89. unsigned int n;
  90. size_t size;
  91. {
  92.     register long total;
  93.     register char *p, *q;
  94.  
  95.     total = (((long) n) * ((long) size));
  96.     if(p = lalloc(total))
  97.         for(q=p; total--; *q++ = 0)
  98.             ;
  99.     return(p);
  100. }
  101.  
  102. /*
  103.  * Called by exit() to free any allocated memory.
  104.  */
  105.  
  106.   static void
  107. freeall()
  108. {
  109.     register MEMCHUNK    *mp, *mp1;
  110.  
  111.     for ( mp = sentinel.prev; mp != &sentinel; ) {
  112.         mp1 = mp->prev;
  113.         FreeMem( mp, mp->size );
  114.         mp = mp1;
  115.     }
  116. }
  117.