home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d110 / pdc.lha / Pdc / src / Memmgt.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  4KB  |  125 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 questions
  18.  *to:
  19.  *
  20.  *Matthew Brandt
  21.  *Box 920337
  22.  *Norcross, Ga 30092
  23.  */
  24.  
  25. struct blk {
  26.         struct blk      *next;
  27.         char            m[1];           /* memory area */
  28.         };
  29.  
  30. static long     glbsize = 0,    /* size left in current global block */
  31.                 locsize = 0,    /* size left in current local block */
  32.                 glbindx = 0,    /* global index */
  33.                 locindx = 0;    /* local index */
  34.  
  35. static struct blk       *locblk = 0,    /* pointer to local block */
  36.                         *glbblk = 0;    /* pointer to global block */
  37.  
  38. void    *xalloc(siz)
  39. int     siz;
  40. {       struct blk      *bp;
  41.         char            *rv;
  42.     extern void    *calloc();
  43.  
  44.     if( siz & 1 )/* if odd size */
  45.        siz += 1; /* make it even */
  46.         if( global_flag ) {
  47.                 if( glbsize >= siz ) {
  48.                         rv = &(glbblk->m[glbindx]);
  49.                         glbsize -= siz;
  50.                         glbindx += siz;
  51.                         return rv;
  52.                         }
  53.                 else    {
  54.                         bp = calloc(1,sizeof(struct blk) + 2047);
  55.             if( bp == NULL )
  56.                {
  57.                 printf(" not enough memory.\n");
  58.                 exit(1);
  59.                }
  60.                         bp->next = glbblk;
  61.                         glbblk = bp;
  62.                         glbsize = 2048 - siz;
  63.                         glbindx = siz;
  64.                         return glbblk->m;
  65.                         }
  66.                 }
  67.         else    {
  68.                 if( locsize >= siz ) {
  69.                         rv = &(locblk->m[locindx]);
  70.                         locsize -= siz;
  71.                         locindx += siz;
  72.                         return rv;
  73.                         }
  74.                 else    {
  75.                         bp = calloc(1,sizeof(struct blk) + 2047);
  76.             if( bp == NULL )
  77.                  {
  78.                 printf(" not enough local memory.\n");
  79.                 exit(1);
  80.                  }
  81.                         bp->next = locblk;
  82.                         locblk = bp;
  83.                         locsize = 2048 - siz;
  84.                         locindx = siz;
  85.                         return locblk->m;
  86.                         }
  87.                 }
  88. }
  89.  
  90. release_local()
  91. {       struct blk      *bp1, *bp2;
  92.         int             blkcnt;
  93.         blkcnt = 0;
  94.         bp1 = locblk;
  95.         while( bp1 != 0 ) {
  96.                 bp2 = bp1->next;
  97.                 free( bp1 );
  98.                 ++blkcnt;
  99.                 bp1 = bp2;
  100.                 }
  101.         locblk = 0;
  102.         locsize = 0;
  103.         lsyms.head = 0;
  104.         printf(" releasing %ld bytes local tables.\n",(long)blkcnt * 2048);
  105. }
  106.  
  107. release_global()
  108. {       struct blk      *bp1, *bp2;
  109.         int             blkcnt;
  110.         bp1 = glbblk;
  111.         blkcnt = 0;
  112.         while( bp1 != 0 ) {
  113.                 bp2 = bp1->next;
  114.                 free(bp1);
  115.                 ++blkcnt;
  116.                 bp1 = bp2;
  117.                 }
  118.         glbblk = 0;
  119.         glbsize = 0;
  120.         gsyms.head = 0;         /* clear global symbol table */
  121.         printf(" releasing %ld bytes global tables.\n",(long)blkcnt * 2048);
  122.         strtab = 0;             /* clear literal table */
  123. }
  124.  
  125.