home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / struct / 0.alloc.c next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  2.4 KB  |  154 lines

  1. #include <stdio.h>
  2. #include "def.h"
  3. int routbeg;
  4.  
  5. extern int debug;
  6. struct coreblk    {struct coreblk *nxtblk;
  7.             int blksize;
  8.             int nxtfree;
  9.             int *blk;
  10.             };
  11.  
  12. long space;
  13. challoc(n)
  14. int n;
  15.     {
  16.     int i;
  17.     i = malloc(n);
  18.     if(i) { space += n; return(i); }
  19.     fprintf(stderr,"alloc out of space\n");
  20.     fprintf(stderr,"total space alloc'ed = %D\n",space);
  21.     fprintf(stderr,"%d more bytes requested\n",n);
  22.     exit(1);
  23.     }
  24.  
  25.  
  26. chfree(p,n)
  27. int *p,n;
  28.     {
  29.     ASSERT(p,chfree);
  30.     space -= n;
  31.     free(p);
  32.     }
  33.  
  34.  
  35. struct coreblk *tcore, *gcore;
  36. int tblksize=12, gblksize=300;
  37.  
  38.  
  39. balloc(n,p,size)        /* allocate n bytes from coreblk *p */
  40. int n,size;        /* use specifies where called */
  41. struct coreblk **p;
  42.     {
  43.     int i;
  44.     struct coreblk *q;
  45.     n = (n+sizeof(i)-1)/sizeof(i);    /* convert bytes to wds to ensure ints always at wd boundaries */
  46.     for (q = *p; ; q = q->nxtblk)
  47.         {
  48.         if (!q)
  49.             {
  50.             q = morespace(n,p,size);
  51.             break;
  52.             }
  53.         if (q-> blksize - q->nxtfree >= n)  break;
  54.         }
  55.     i = q->nxtfree;
  56.     q ->nxtfree += n;
  57.     return( &(q->blk)[i]);
  58.     }
  59.  
  60. talloc(n)        /* allocate from line-by-line storage area */
  61. int n;
  62.     {return(balloc(n,&tcore,tblksize)); }
  63.  
  64. galloc(n)        /* allocate from graph storage area */
  65. int n;
  66.     {
  67.     return(balloc(n,&gcore,gblksize));
  68.     }
  69.  
  70. reuse(p)        /* set nxtfree so coreblk can be reused */
  71. struct coreblk *p;
  72.     {
  73.     for (; p; p=p->nxtblk)  p->nxtfree = 0;  
  74.     }
  75.  
  76. bfree(p)        /* free coreblk p */
  77. struct coreblk *p;
  78.     {
  79.     if (!p) return;
  80.     bfree(p->nxtblk);
  81.     p->nxtblk = 0;
  82.     free(p);
  83.     }
  84.  
  85.  
  86. morespace(n,p,size)        /* get at least n more wds for coreblk *p */
  87. int n,size;
  88. struct coreblk **p;
  89.     {struct coreblk *q;
  90.     int t,i;
  91.  
  92.     t = n<size?size:n;
  93.     q = malloc(i=t*sizeof(*(q->blk))+sizeof(*q));
  94.     if(!q){
  95.         error(": alloc out of space","","");
  96.         fprintf(stderr,"space = %D\n",space);
  97.         fprintf(stderr,"%d more bytes requested\n",n);
  98.         exit(1);
  99.         }
  100.     space += i;
  101.     q->nxtblk = *p;
  102.     *p = q;
  103.     q -> blksize = t;
  104.     q-> nxtfree = 0;
  105.     q->blk = q + 1;
  106.     return(q);
  107.     }
  108.  
  109.  
  110.  
  111.  
  112. freegraf()
  113.     {
  114.     bfree(gcore);
  115.     gcore = 0;
  116.  
  117.  
  118.     }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. error(mess1, mess2, mess3)
  129. char *mess1, *mess2, *mess3;
  130.     {
  131.     static lastbeg;
  132.     if (lastbeg != routbeg)
  133.         {
  134.         fprintf(stderr,"routine beginning on line %d:\n",routbeg);
  135.         lastbeg = routbeg;
  136.         }
  137.     fprintf(stderr,"error %s %s %s\n",mess1, mess2, mess3);
  138.     }
  139.  
  140.  
  141. faterr(mess1, mess2, mess3)
  142. char *mess1, *mess2, *mess3;
  143.     {
  144.     error(mess1, mess2, mess3);
  145.     exit(1);
  146.     }
  147.  
  148.  
  149. strerr(mess1, mess2, mess3)
  150. char *mess1, *mess2, *mess3;
  151.     {
  152.     error("struct error: ",mess1, mess2);
  153.     }
  154.