home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / SOURCE / MEMMGT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  3.8 KB  |  133 lines

  1. /*
  2.  * 68K/386 32-bit C compiler.
  3.  *
  4.  * copyright (c) 1996, David Lindauer
  5.  * 
  6.  * This compiler is intended for educational use.  It may not be used
  7.  * for profit without the express written consent of the author.
  8.  *
  9.  * It may be freely redistributed, as long as this notice remains intact
  10.  * and sources are distributed along with any executables derived from them.
  11.  *
  12.  * The author is not responsible for damages, either direct or consequential,
  13.  * that may arise from use of this software.
  14.  *
  15.  * v1.5 August 1996
  16.  * David Lindauer, gclind01@starbase.spd.louisville.edu
  17.  *
  18.  * Credits to Mathew Brandt for original K&R C compiler
  19.  *
  20.  */
  21. #include        <stdio.h>
  22. #include                <malloc.h>
  23. #include        "expr.h"
  24. #include        "c.h"
  25. #include        "gen.h"
  26. #include        "cglbdec.h"
  27.  
  28. extern TABLE oldlsym;
  29.  
  30. typedef struct _blk_ {
  31.         struct _blk_      *next;
  32.         char            m[1];           /* memory area */
  33.         } BLK;
  34.  
  35. static int      glbsize = 0,    /* size left in current global block */
  36.                 locsize = 0,    /* size left in current local block */
  37.                 glbindx = 0,    /* global index */
  38.                 locindx = 0;    /* local index */
  39.  
  40. static BLK       *locblk = 0,    /* pointer to local block */
  41.                         *glbblk = 0;    /* pointer to global block */
  42.  
  43. void memini(void)
  44. {
  45.     glbsize = 0;
  46.     locsize = 0;
  47.     glbindx = 0;
  48.     locindx = 0;
  49.     locblk = 0;
  50.     glbblk = 0;
  51. }
  52. char    *xalloc(int siz)
  53. {       BLK      *bp;
  54.         char            *rv;
  55.     if( siz & 1 )        /* if odd size */
  56.         siz += 1;    /* make it even */
  57.         if( global_flag ) {
  58.                 if( glbsize >= siz ) {
  59.                         rv = &(glbblk->m[glbindx]);
  60.                         glbsize -= siz;
  61.                         glbindx += siz;
  62.                         return rv;
  63.                         }
  64.                 else    {
  65.                         bp = calloc(1,sizeof(BLK) + 2047);
  66.             if( bp == NULL )
  67.             {
  68.                 printf(" not enough memory.\n");
  69.                 exit(1);
  70.             }
  71.                         bp->next = glbblk;
  72.                         glbblk = bp;
  73.                         glbsize = 2048 - siz;
  74.                         glbindx = siz;
  75.                         return glbblk->m;
  76.                         }
  77.                 }
  78.         else    {
  79.                 if( locsize >= siz ) {
  80.                         rv = &(locblk->m[locindx]);
  81.                         locsize -= siz;
  82.                         locindx += siz;
  83.                         return rv;
  84.                         }
  85.                 else    {
  86.                         bp = calloc(1,sizeof(BLK) + 2047);
  87.             if( bp == NULL )
  88.             {
  89.                 printf(" not enough local memory.\n");
  90.                 exit(1);
  91.             }
  92.                         bp->next = locblk;
  93.                         locblk = bp;
  94.                         locsize = 2048 - siz;
  95.                         locindx = siz;
  96.                         return locblk->m;
  97.                         }
  98.                 }
  99. }
  100.  
  101. void release_local(void )
  102. {       BLK      *bp1, *bp2;
  103.         int             blkcnt;
  104.         blkcnt = 0;
  105.         bp1 = locblk;
  106.         while( bp1 != 0 ) {
  107.                 bp2 = bp1->next;
  108.                 free( bp1 );
  109.                 ++blkcnt;
  110.                 bp1 = bp2;
  111.                 }
  112.         locblk = 0;
  113.         locsize = 0;
  114.         lsyms.head = 0;
  115.                 oldlsym.head = oldlsym.tail = 0;
  116. }
  117.  
  118. void release_global(void)
  119. {       BLK      *bp1, *bp2;
  120.         int             blkcnt;
  121.         bp1 = glbblk;
  122.         blkcnt = 0;
  123.         while( bp1 != 0 ) {
  124.                 bp2 = bp1->next;
  125.                 free(bp1);
  126.                 ++blkcnt;
  127.                 bp1 = bp2;
  128.                 }
  129.         glbblk = 0;
  130.         glbsize = 0;
  131.         gsyms.head = 0;         /* clear global symbol table */
  132.         strtab = 0;             /* clear literal table */
  133. }