home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / iolib / alloc.c next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  1.1 KB  |  76 lines

  1. #
  2. /*
  3.  * C library -- alloc/free
  4.  */
  5.  
  6. #define    logical    char *
  7.  
  8. struct fb {
  9.     logical    size;
  10.     char    *next;
  11. };
  12.  
  13. int    freelist[] {
  14.     0,
  15.     -1,
  16. };
  17. logical    slop    2;
  18.  
  19. alloc(asize)
  20. logical asize;
  21. {
  22.     register logical size;
  23.     register logical np;
  24.     register logical cp;
  25.  
  26.     if ((size = asize) == 0)
  27.         return(0);
  28.     size =+ 3;
  29.     size =& ~01;
  30.     for (;;) {
  31.         for (cp=freelist; (np= cp->next) != -1; cp=np) {
  32.             if (np->size>=size) {
  33.                 if (size+slop >= np->size) {
  34.                     cp->next = np->next;
  35.                     return(&np->next);
  36.                 }
  37.                 cp = cp->next = np+size;
  38.                 cp->size = np->size - size;
  39.                 cp->next = np->next;
  40.                 np->size = size;
  41.                 return(&np->next);
  42.             }
  43.         }
  44.         asize = size<1024? 1024: size;
  45.         if ((cp = sbrk(asize)) == -1) {
  46.             return (-1);
  47.         }
  48.         cp->size = asize;
  49.         free(&cp->next);
  50.     }
  51. }
  52.  
  53. free(aptr)
  54. char *aptr;
  55. {
  56.     register logical ptr;
  57.     register logical cp;
  58.     register logical np;
  59.  
  60.     ptr = aptr-2;
  61.     cp = freelist;
  62.     while ((np = cp->next) < ptr)
  63.         cp = np;
  64.     if (ptr+ptr->size == np) {
  65.         ptr->size =+ np->size;
  66.         ptr->next = np->next;
  67.         np = ptr;
  68.     } else
  69.         ptr->next = np;
  70.     if (cp+cp->size == ptr) {
  71.         cp->size =+ ptr->size;
  72.         cp->next = ptr->next;
  73.     } else
  74.         cp->next = ptr;
  75. }
  76.