home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / sys / ken / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-07-17  |  1.6 KB  |  88 lines

  1. #
  2. /*
  3.  */
  4.  
  5. /*
  6.  * Structure of the coremap and swapmap
  7.  * arrays. Consists of non-zero count
  8.  * and base address of that many
  9.  * contiguous units.
  10.  * (The coremap unit is 64 bytes,
  11.  * the swapmap unit is 512 bytes)
  12.  * The addresses are increasing and
  13.  * the list is terminated with the
  14.  * first zero count.
  15.  */
  16. struct map
  17. {
  18.     char *m_size;
  19.     char *m_addr;
  20. };
  21.  
  22. /*
  23.  * Allocate size units from the given
  24.  * map. Return the base of the allocated
  25.  * space.
  26.  * Algorithm is first fit.
  27.  */
  28. malloc(mp, size)
  29. struct map *mp;
  30. {
  31.     register int a;
  32.     register struct map *bp;
  33.  
  34.     for (bp = mp; bp->m_size; bp++) {
  35.         if (bp->m_size >= size) {
  36.             a = bp->m_addr;
  37.             bp->m_addr =+ size;
  38.             if ((bp->m_size =- size) == 0)
  39.                 do {
  40.                     bp++;
  41.                     (bp-1)->m_addr = bp->m_addr;
  42.                 } while ((bp-1)->m_size = bp->m_size);
  43.             return(a);
  44.         }
  45.     }
  46.     return(0);
  47. }
  48.  
  49. /*
  50.  * Free the previously allocated space aa
  51.  * of size units into the specified map.
  52.  * Sort aa into map and combine on
  53.  * one or both ends if possible.
  54.  */
  55. mfree(mp, size, aa)
  56. struct map *mp;
  57. {
  58.     register struct map *bp;
  59.     register int t;
  60.     register int a;
  61.  
  62.     a = aa;
  63.     for (bp = mp; bp->m_addr<=a && bp->m_size!=0; bp++);
  64.     if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) {
  65.         (bp-1)->m_size =+ size;
  66.         if (a+size == bp->m_addr) {
  67.             (bp-1)->m_size =+ bp->m_size;
  68.             while (bp->m_size) {
  69.                 bp++;
  70.                 (bp-1)->m_addr = bp->m_addr;
  71.                 (bp-1)->m_size = bp->m_size;
  72.             }
  73.         }
  74.     } else {
  75.         if (a+size == bp->m_addr && bp->m_size) {
  76.             bp->m_addr =- size;
  77.             bp->m_size =+ size;
  78.         } else if (size) do {
  79.             t = bp->m_addr;
  80.             bp->m_addr = a;
  81.             a = t;
  82.             t = bp->m_size;
  83.             bp->m_size = size;
  84.             bp++;
  85.         } while (size = t);
  86.     }
  87. }
  88.