headers.c 9.7 KB

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