blacklst.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, August 9, 1995 6:09 pm PDT */
  15. # include "private/gc_priv.h"
  16. /*
  17. * We maintain several hash tables of hblks that have had false hits.
  18. * Each contains one bit per hash bucket; If any page in the bucket
  19. * has had a false hit, we assume that all of them have.
  20. * See the definition of page_hash_table in gc_private.h.
  21. * False hits from the stack(s) are much more dangerous than false hits
  22. * from elsewhere, since the former can pin a large object that spans the
  23. * block, eventhough it does not start on the dangerous block.
  24. */
  25. /*
  26. * Externally callable routines are:
  27. * GC_add_to_black_list_normal
  28. * GC_add_to_black_list_stack
  29. * GC_promote_black_lists
  30. * GC_is_black_listed
  31. *
  32. * All require that the allocator lock is held.
  33. */
  34. /* Pointers to individual tables. We replace one table by another by */
  35. /* switching these pointers. */
  36. word * GC_old_normal_bl;
  37. /* Nonstack false references seen at last full */
  38. /* collection. */
  39. word * GC_incomplete_normal_bl;
  40. /* Nonstack false references seen since last */
  41. /* full collection. */
  42. word * GC_old_stack_bl;
  43. word * GC_incomplete_stack_bl;
  44. word GC_total_stack_black_listed;
  45. word GC_black_list_spacing = MINHINCR*HBLKSIZE; /* Initial rough guess */
  46. void GC_clear_bl();
  47. # if defined(__STDC__) || defined(__cplusplus)
  48. void GC_default_print_heap_obj_proc(ptr_t p)
  49. # else
  50. void GC_default_print_heap_obj_proc(p)
  51. ptr_t p;
  52. # endif
  53. {
  54. ptr_t base = GC_base(p);
  55. GC_err_printf2("start: 0x%lx, appr. length: %ld", base, GC_size(base));
  56. }
  57. void (*GC_print_heap_obj) GC_PROTO((ptr_t p)) =
  58. GC_default_print_heap_obj_proc;
  59. void GC_print_source_ptr(p)
  60. ptr_t p;
  61. {
  62. ptr_t base = GC_base(p);
  63. if (0 == base) {
  64. if (0 == p) {
  65. GC_err_printf0("in register");
  66. } else {
  67. GC_err_printf0("in root set");
  68. }
  69. } else {
  70. GC_err_printf0("in object at ");
  71. (*GC_print_heap_obj)(base);
  72. }
  73. }
  74. void GC_bl_init()
  75. {
  76. if (!GC_all_interior_pointers) {
  77. GC_old_normal_bl = (word *)
  78. GC_scratch_alloc((word)(sizeof (page_hash_table)));
  79. GC_incomplete_normal_bl = (word *)GC_scratch_alloc
  80. ((word)(sizeof(page_hash_table)));
  81. if (GC_old_normal_bl == 0 || GC_incomplete_normal_bl == 0) {
  82. GC_err_printf0("Insufficient memory for black list\n");
  83. EXIT();
  84. }
  85. GC_clear_bl(GC_old_normal_bl);
  86. GC_clear_bl(GC_incomplete_normal_bl);
  87. }
  88. GC_old_stack_bl = (word *)GC_scratch_alloc((word)(sizeof(page_hash_table)));
  89. GC_incomplete_stack_bl = (word *)GC_scratch_alloc
  90. ((word)(sizeof(page_hash_table)));
  91. if (GC_old_stack_bl == 0 || GC_incomplete_stack_bl == 0) {
  92. GC_err_printf0("Insufficient memory for black list\n");
  93. EXIT();
  94. }
  95. GC_clear_bl(GC_old_stack_bl);
  96. GC_clear_bl(GC_incomplete_stack_bl);
  97. }
  98. void GC_clear_bl(doomed)
  99. word *doomed;
  100. {
  101. BZERO(doomed, sizeof(page_hash_table));
  102. }
  103. void GC_copy_bl(old, new)
  104. word *new, *old;
  105. {
  106. BCOPY(old, new, sizeof(page_hash_table));
  107. }
  108. static word total_stack_black_listed();
  109. /* Signal the completion of a collection. Turn the incomplete black */
  110. /* lists into new black lists, etc. */
  111. void GC_promote_black_lists()
  112. {
  113. word * very_old_normal_bl = GC_old_normal_bl;
  114. word * very_old_stack_bl = GC_old_stack_bl;
  115. GC_old_normal_bl = GC_incomplete_normal_bl;
  116. GC_old_stack_bl = GC_incomplete_stack_bl;
  117. if (!GC_all_interior_pointers) {
  118. GC_clear_bl(very_old_normal_bl);
  119. }
  120. GC_clear_bl(very_old_stack_bl);
  121. GC_incomplete_normal_bl = very_old_normal_bl;
  122. GC_incomplete_stack_bl = very_old_stack_bl;
  123. GC_total_stack_black_listed = total_stack_black_listed();
  124. # ifdef PRINTSTATS
  125. GC_printf1("%ld bytes in heap blacklisted for interior pointers\n",
  126. (unsigned long)GC_total_stack_black_listed);
  127. # endif
  128. if (GC_total_stack_black_listed != 0) {
  129. GC_black_list_spacing =
  130. HBLKSIZE*(GC_heapsize/GC_total_stack_black_listed);
  131. }
  132. if (GC_black_list_spacing < 3 * HBLKSIZE) {
  133. GC_black_list_spacing = 3 * HBLKSIZE;
  134. }
  135. if (GC_black_list_spacing > MAXHINCR * HBLKSIZE) {
  136. GC_black_list_spacing = MAXHINCR * HBLKSIZE;
  137. /* Makes it easier to allocate really huge blocks, which otherwise */
  138. /* may have problems with nonuniform blacklist distributions. */
  139. /* This way we should always succeed immediately after growing the */
  140. /* heap. */
  141. }
  142. }
  143. void GC_unpromote_black_lists()
  144. {
  145. if (!GC_all_interior_pointers) {
  146. GC_copy_bl(GC_old_normal_bl, GC_incomplete_normal_bl);
  147. }
  148. GC_copy_bl(GC_old_stack_bl, GC_incomplete_stack_bl);
  149. }
  150. /* P is not a valid pointer reference, but it falls inside */
  151. /* the plausible heap bounds. */
  152. /* Add it to the normal incomplete black list if appropriate. */
  153. #ifdef PRINT_BLACK_LIST
  154. void GC_add_to_black_list_normal(p, source)
  155. ptr_t source;
  156. #else
  157. void GC_add_to_black_list_normal(p)
  158. #endif
  159. word p;
  160. {
  161. if (!(GC_modws_valid_offsets[p & (sizeof(word)-1)])) return;
  162. {
  163. register int index = PHT_HASH(p);
  164. if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_normal_bl, index)) {
  165. # ifdef PRINT_BLACK_LIST
  166. if (!get_pht_entry_from_index(GC_incomplete_normal_bl, index)) {
  167. GC_err_printf2(
  168. "Black listing (normal) 0x%lx referenced from 0x%lx ",
  169. (unsigned long) p, (unsigned long) source);
  170. GC_print_source_ptr(source);
  171. GC_err_puts("\n");
  172. }
  173. # endif
  174. set_pht_entry_from_index(GC_incomplete_normal_bl, index);
  175. } /* else this is probably just an interior pointer to an allocated */
  176. /* object, and isn't worth black listing. */
  177. }
  178. }
  179. /* And the same for false pointers from the stack. */
  180. #ifdef PRINT_BLACK_LIST
  181. void GC_add_to_black_list_stack(p, source)
  182. ptr_t source;
  183. #else
  184. void GC_add_to_black_list_stack(p)
  185. #endif
  186. word p;
  187. {
  188. register int index = PHT_HASH(p);
  189. if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_stack_bl, index)) {
  190. # ifdef PRINT_BLACK_LIST
  191. if (!get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
  192. GC_err_printf2(
  193. "Black listing (stack) 0x%lx referenced from 0x%lx ",
  194. (unsigned long)p, (unsigned long)source);
  195. GC_print_source_ptr(source);
  196. GC_err_puts("\n");
  197. }
  198. # endif
  199. set_pht_entry_from_index(GC_incomplete_stack_bl, index);
  200. }
  201. }
  202. /*
  203. * Is the block starting at h of size len bytes black listed? If so,
  204. * return the address of the next plausible r such that (r, len) might not
  205. * be black listed. (R may not actually be in the heap. We guarantee only
  206. * that every smaller value of r after h is also black listed.)
  207. * If (h,len) is not black listed, return 0.
  208. * Knows about the structure of the black list hash tables.
  209. */
  210. struct hblk * GC_is_black_listed(h, len)
  211. struct hblk * h;
  212. word len;
  213. {
  214. register int index = PHT_HASH((word)h);
  215. register word i;
  216. word nblocks = divHBLKSZ(len);
  217. if (!GC_all_interior_pointers) {
  218. if (get_pht_entry_from_index(GC_old_normal_bl, index)
  219. || get_pht_entry_from_index(GC_incomplete_normal_bl, index)) {
  220. return(h+1);
  221. }
  222. }
  223. for (i = 0; ; ) {
  224. if (GC_old_stack_bl[divWORDSZ(index)] == 0
  225. && GC_incomplete_stack_bl[divWORDSZ(index)] == 0) {
  226. /* An easy case */
  227. i += WORDSZ - modWORDSZ(index);
  228. } else {
  229. if (get_pht_entry_from_index(GC_old_stack_bl, index)
  230. || get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
  231. return(h+i+1);
  232. }
  233. i++;
  234. }
  235. if (i >= nblocks) break;
  236. index = PHT_HASH((word)(h+i));
  237. }
  238. return(0);
  239. }
  240. /* Return the number of blacklisted blocks in a given range. */
  241. /* Used only for statistical purposes. */
  242. /* Looks only at the GC_incomplete_stack_bl. */
  243. word GC_number_stack_black_listed(start, endp1)
  244. struct hblk *start, *endp1;
  245. {
  246. register struct hblk * h;
  247. word result = 0;
  248. for (h = start; h < endp1; h++) {
  249. register int index = PHT_HASH((word)h);
  250. if (get_pht_entry_from_index(GC_old_stack_bl, index)) result++;
  251. }
  252. return(result);
  253. }
  254. /* Return the total number of (stack) black-listed bytes. */
  255. static word total_stack_black_listed()
  256. {
  257. register unsigned i;
  258. word total = 0;
  259. for (i = 0; i < GC_n_heap_sects; i++) {
  260. struct hblk * start = (struct hblk *) GC_heap_sects[i].hs_start;
  261. word len = (word) GC_heap_sects[i].hs_bytes;
  262. struct hblk * endp1 = start + len/HBLKSIZE;
  263. total += GC_number_stack_black_listed(start, endp1);
  264. }
  265. return(total * HBLKSIZE);
  266. }