home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / HEADERS.C < prev    next >
C/C++ Source or Header  |  1994-10-07  |  7KB  |  272 lines

  1. /* 
  2.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3.  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
  4.  *
  5.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  7.  *
  8.  * Permission is hereby granted to use or copy this program
  9.  * for any purpose,  provided the above notices are retained on all copies.
  10.  * Permission to modify the code and to distribute modified code is granted,
  11.  * provided the above notices are retained, and a notice that the code was
  12.  * modified is included with the above copyright notice.
  13.  */
  14. /* Boehm, October 7, 1994 9:54 pm PDT */
  15.  
  16. /*
  17.  * This implements:
  18.  * 1. allocation of heap block headers
  19.  * 2. A map from addresses to heap block addresses to heap block headers
  20.  *
  21.  * Access speed is crucial.  We implement an index structure based on a 2
  22.  * level tree.
  23.  */
  24.  
  25. # include "gc_priv.h"
  26.  
  27. bottom_index * GC_all_bottom_indices = 0;
  28.  
  29. /* Non-macro version of header location routine */
  30. hdr * GC_find_header(h)
  31. ptr_t h;
  32. {
  33. #   ifdef HASH_TL
  34.     register hdr * result;
  35.     GET_HDR(h, result);
  36.     return(result);
  37. #   else
  38.     return(HDR_INNER(h));
  39. #   endif
  40. }
  41.  
  42. /* Routines to dynamically allocate collector data structures that will */
  43. /* never be freed.                             */
  44.  
  45. static ptr_t scratch_free_ptr = 0;
  46.  
  47. ptr_t GC_scratch_end_ptr = 0;
  48.  
  49. ptr_t GC_scratch_alloc(bytes)
  50. register word bytes;
  51. {
  52.     register ptr_t result = scratch_free_ptr;
  53.     scratch_free_ptr += bytes;
  54.     if (scratch_free_ptr <= GC_scratch_end_ptr) {
  55.         return(result);
  56.     }
  57.     {
  58.         word bytes_to_get = MINHINCR * HBLKSIZE;
  59.          
  60.         if (bytes_to_get <= bytes) {
  61.           /* Undo the damage, and get memory directly */
  62.             scratch_free_ptr -= bytes;
  63.             return((ptr_t)GET_MEM(bytes));
  64.         }
  65.         result = (ptr_t)GET_MEM(bytes_to_get);
  66.         if (result == 0) {
  67. #        ifdef PRINTSTATS
  68.                 GC_printf0("Out of memory - trying to allocate less\n");
  69. #        endif
  70.             scratch_free_ptr -= bytes;
  71.             return((ptr_t)GET_MEM(bytes));
  72.         }
  73.         scratch_free_ptr = result;
  74.         GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
  75.         return(GC_scratch_alloc(bytes));
  76.     }
  77. }
  78.  
  79. static hdr * hdr_free_list = 0;
  80.  
  81. /* Return an uninitialized header */
  82. static hdr * alloc_hdr()
  83. {
  84.     register hdr * result;
  85.     
  86.     if (hdr_free_list == 0) {
  87.         result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
  88.     } else {
  89.         result = hdr_free_list;
  90.         hdr_free_list = (hdr *) (result -> hb_next);
  91.     }
  92.     return(result);
  93. }
  94.  
  95. static void free_hdr(hhdr)
  96. hdr * hhdr;
  97. {
  98.     hhdr -> hb_next = (struct hblk *) hdr_free_list;
  99.     hdr_free_list = hhdr;
  100. }
  101.  
  102. void GC_init_headers()
  103. {
  104.     register int i;
  105.     
  106.     GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
  107.     BZERO(GC_all_nils, sizeof(bottom_index));
  108.     for (i = 0; i < TOP_SZ; i++) {
  109.         GC_top_index[i] = GC_all_nils;
  110.     }
  111. }
  112.  
  113. /* Make sure that there is a bottom level index block for address addr  */
  114. /* Return FALSE on failure.                        */
  115. static bool get_index(addr)
  116. register word addr;
  117. {
  118.     register word hi =
  119.             (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
  120.     register bottom_index * r;
  121.     register bottom_index * p;
  122.     register bottom_index ** prev;
  123. #   ifdef HASH_TL
  124.       register i = TL_HASH(hi);
  125.       register bottom_index * old;
  126.       
  127.       old = p = GC_top_index[i];
  128.       while(p != GC_all_nils) {
  129.           if (p -> key == hi) return(TRUE);
  130.           p = p -> hash_link;
  131.       }
  132.       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
  133.       if (r == 0) return(FALSE);
  134.       BZERO(r, sizeof (bottom_index));
  135.       r -> hash_link = old;
  136.       GC_top_index[i] = r;
  137. #   else
  138.       if (GC_top_index[hi] != GC_all_nils) return(TRUE);
  139.       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
  140.       if (r == 0) return(FALSE);
  141.       GC_top_index[hi] = r;
  142.       BZERO(r, sizeof (bottom_index));
  143. # endif
  144.     r -> key = hi;
  145.     /* Add it to the list of bottom indices */
  146.       prev = &GC_all_bottom_indices;
  147.       while ((p = *prev) != 0 && p -> key < hi) prev = &(p -> asc_link);
  148.       r -> asc_link = p;
  149.       *prev = r;
  150.     return(TRUE);
  151. }
  152.  
  153. /* Install a header for block h.  */
  154. /* The header is uninitialized.      */
  155. /* Returns FALSE on failure.      */
  156. bool GC_install_header(h)
  157. register struct hblk * h;
  158. {
  159.     hdr * result;
  160.     
  161.     if (!get_index((word) h)) return(FALSE);
  162.     result = alloc_hdr();
  163.     SET_HDR(h, result);
  164.     return(result != 0);
  165. }
  166.  
  167. /* Set up forwarding counts for block h of size sz */
  168. bool GC_install_counts(h, sz)
  169. register struct hblk * h;
  170. register word sz; /* bytes */
  171. {
  172.     register struct hblk * hbp;
  173.     register int i;
  174.     
  175.     for (hbp = h; (char *)hbp < (char *)h + sz; hbp += BOTTOM_SZ) {
  176.         if (!get_index((word) hbp)) return(FALSE);
  177.     }
  178.     if (!get_index((word)h + sz - 1)) return(FALSE);
  179.     for (hbp = h + 1; (char *)hbp < (char *)h + sz; hbp += 1) {
  180.         i = HBLK_PTR_DIFF(hbp, h);
  181.         SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
  182.     }
  183.     return(TRUE);
  184. }
  185.  
  186. /* Remove the header for block h */
  187. void GC_remove_header(h)
  188. register struct hblk * h;
  189. {
  190.     hdr ** ha;
  191.     
  192.     GET_HDR_ADDR(h, ha);
  193.     free_hdr(*ha);
  194.     *ha = 0;
  195. }
  196.  
  197. /* Remove forwarding counts for h */
  198. void GC_remove_counts(h, sz)
  199. register struct hblk * h;
  200. register word sz; /* bytes */
  201. {
  202.     register struct hblk * hbp;
  203.     
  204.     for (hbp = h+1; (char *)hbp < (char *)h + sz; hbp += 1) {
  205.         SET_HDR(hbp, 0);
  206.     }
  207. }
  208.  
  209. /* Apply fn to all allocated blocks */
  210. /*VARARGS1*/
  211. void GC_apply_to_all_blocks(fn, client_data)
  212. void (*fn)(/* struct hblk *h, word client_data */);
  213. word client_data;
  214. {
  215.     register int j;
  216.     register bottom_index * index_p;
  217.     
  218.     for (index_p = GC_all_bottom_indices; index_p != 0;
  219.          index_p = index_p -> asc_link) {
  220.         for (j = BOTTOM_SZ-1; j >= 0;) {
  221.             if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
  222.                 if (index_p->index[j]->hb_map != GC_invalid_map) {
  223.                     (*fn)(((struct hblk *)
  224.                             (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
  225.                              << LOG_HBLKSIZE)),
  226.                           client_data);
  227.                 }
  228.                 j--;
  229.              } else if (index_p->index[j] == 0) {
  230.                 j--;
  231.              } else {
  232.                 j -= (word)(index_p->index[j]);
  233.              }
  234.          }
  235.      }
  236. }
  237.  
  238. /* Get the next valid block whose address is at least h    */
  239. /* Return 0 if there is none.                */
  240. struct hblk * GC_next_block(h)
  241. struct hblk * h;
  242. {
  243.     register bottom_index * bi;
  244.     register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
  245.     
  246.     GET_BI(h, bi);
  247.     if (bi == GC_all_nils) {
  248.         register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
  249.         bi = GC_all_bottom_indices;
  250.         while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
  251.         j = 0;
  252.     }
  253.     while(bi != 0) {
  254.         while (j < BOTTOM_SZ) {
  255.             if (IS_FORWARDING_ADDR_OR_NIL(bi -> index[j])) {
  256.                 j++;
  257.             } else {
  258.                 if (bi->index[j]->hb_map != GC_invalid_map) {
  259.                     return((struct hblk *)
  260.                             (((bi -> key << LOG_BOTTOM_SZ) + j)
  261.                              << LOG_HBLKSIZE));
  262.                 } else {
  263.                     j += divHBLKSZ(bi->index[j] -> hb_sz);
  264.                 }
  265.             }
  266.         }
  267.         j = 0;
  268.         bi = bi -> asc_link;
  269.     }
  270.     return(0);
  271. }
  272.