home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / c68k_src / memmgt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-10  |  3.6 KB  |  124 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or question
  18. s
  19.  *    to:
  20.  *
  21.  *        Matthew Brandt
  22.  *        Box 920337
  23.  *        Norcross, Ga 30092
  24.  */
  25.  
  26. struct blk {
  27.         struct blk      *next;
  28.         char            m[1];           /* memory area */
  29.         };
  30.  
  31. static int      glbsize = 0,    /* size left in current global block */
  32.                 locsize = 0,    /* size left in current local block */
  33.                 glbindx = 0,    /* global index */
  34.                 locindx = 0;    /* local index */
  35.  
  36. static struct blk       *locblk = 0,    /* pointer to local block */
  37.                         *glbblk = 0;    /* pointer to global block */
  38.  
  39. char    *xalloc(siz)
  40. int     siz;
  41. {       struct blk      *bp;
  42.         char            *rv;
  43.     if( siz & 1 )        /* if odd size */
  44.         siz += 1;    /* make it even */
  45.         if( global_flag ) {
  46.                 if( glbsize >= siz ) {
  47.                         rv = &(glbblk->m[glbindx]);
  48.                         glbsize -= siz;
  49.                         glbindx += siz;
  50.                         return rv;
  51.                         }
  52.                 else    {
  53.                         bp = calloc(1,sizeof(struct blk) + 2047);
  54.             if( bp == NULL )
  55.             {
  56.                 printf(" not enough memory.\n");
  57.                 exit(1);
  58.             }
  59.                         bp->next = glbblk;
  60.                         glbblk = bp;
  61.                         glbsize = 2048 - siz;
  62.                         glbindx = siz;
  63.                         return glbblk->m;
  64.                         }
  65.                 }
  66.         else    {
  67.                 if( locsize >= siz ) {
  68.                         rv = &(locblk->m[locindx]);
  69.                         locsize -= siz;
  70.                         locindx += siz;
  71.                         return rv;
  72.                         }
  73.                 else    {
  74.                         bp = calloc(1,sizeof(struct blk) + 2047);
  75.             if( bp == NULL )
  76.             {
  77.                 printf(" not enough local memory.\n");
  78.                 exit(1);
  79.             }
  80.                         bp->next = locblk;
  81.                         locblk = bp;
  82.                         locsize = 2048 - siz;
  83.                         locindx = siz;
  84.                         return locblk->m;
  85.                         }
  86.                 }
  87. }
  88.  
  89. release_local()
  90. {       struct blk      *bp1, *bp2;
  91.         int             blkcnt;
  92.         blkcnt = 0;
  93.         bp1 = locblk;
  94.         while( bp1 != 0 ) {
  95.                 bp2 = bp1->next;
  96.                 free( bp1 );
  97.                 ++blkcnt;
  98.                 bp1 = bp2;
  99.                 }
  100.         locblk = 0;
  101.         locsize = 0;
  102.         lsyms.head = 0;
  103.         printf(" releasing %d bytes local tables.\n",blkcnt * 2048);
  104. }
  105.  
  106. release_global()
  107. {       struct blk      *bp1, *bp2;
  108.         int             blkcnt;
  109.         bp1 = glbblk;
  110.         blkcnt = 0;
  111.         while( bp1 != 0 ) {
  112.                 bp2 = bp1->next;
  113.                 free(bp1);
  114.                 ++blkcnt;
  115.                 bp1 = bp2;
  116.                 }
  117.         glbblk = 0;
  118.         glbsize = 0;
  119.         gsyms.head = 0;         /* clear global symbol table */
  120.         printf(" releasing %d bytes global tables.\n",blkcnt * 2048);
  121.         strtab = 0;             /* clear literal table */
  122. }
  123.  
  124.