/vendor/gc/allchblk.c

http://github.com/feyeleanor/RubyGoLightly · C · 887 lines · 639 code · 76 blank · 172 comment · 200 complexity · 24384706407d730d7473bfdc00a3752e MD5 · raw file

  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) 1998-1999 by Silicon Graphics. All rights reserved.
  5. * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
  6. *
  7. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  8. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  9. *
  10. * Permission is hereby granted to use or copy this program
  11. * for any purpose, provided the above notices are retained on all copies.
  12. * Permission to modify the code and to distribute modified code is granted,
  13. * provided the above notices are retained, and a notice that the code was
  14. * modified is included with the above copyright notice.
  15. */
  16. /* #define DEBUG */
  17. #include <stdio.h>
  18. #include "private/gc_priv.h"
  19. GC_bool GC_use_entire_heap = 0;
  20. /*
  21. * Free heap blocks are kept on one of several free lists,
  22. * depending on the size of the block. Each free list is doubly linked.
  23. * Adjacent free blocks are coalesced.
  24. */
  25. # define MAX_BLACK_LIST_ALLOC (2*HBLKSIZE)
  26. /* largest block we will allocate starting on a black */
  27. /* listed block. Must be >= HBLKSIZE. */
  28. # define UNIQUE_THRESHOLD 32
  29. /* Sizes up to this many HBLKs each have their own free list */
  30. # define HUGE_THRESHOLD 256
  31. /* Sizes of at least this many heap blocks are mapped to a */
  32. /* single free list. */
  33. # define FL_COMPRESSION 8
  34. /* In between sizes map this many distinct sizes to a single */
  35. /* bin. */
  36. # define N_HBLK_FLS (HUGE_THRESHOLD - UNIQUE_THRESHOLD)/FL_COMPRESSION \
  37. + UNIQUE_THRESHOLD
  38. struct hblk * GC_hblkfreelist[N_HBLK_FLS+1] = { 0 };
  39. #ifndef USE_MUNMAP
  40. word GC_free_bytes[N_HBLK_FLS+1] = { 0 };
  41. /* Number of free bytes on each list. */
  42. /* Return the largest n such that */
  43. /* Is GC_large_allocd_bytes + the number of free bytes on lists */
  44. /* n .. N_HBLK_FLS > GC_max_large_allocd_bytes. */
  45. /* If there is no such n, return 0. */
  46. # ifdef __GNUC__
  47. __inline__
  48. # endif
  49. static int GC_enough_large_bytes_left(void)
  50. {
  51. int n;
  52. word bytes = GC_large_allocd_bytes;
  53. GC_ASSERT(GC_max_large_allocd_bytes <= GC_heapsize);
  54. for (n = N_HBLK_FLS; n >= 0; --n) {
  55. bytes += GC_free_bytes[n];
  56. if (bytes >= GC_max_large_allocd_bytes) return n;
  57. }
  58. return 0;
  59. }
  60. # define INCR_FREE_BYTES(n, b) GC_free_bytes[n] += (b);
  61. # define FREE_ASSERT(e) GC_ASSERT(e)
  62. #else /* USE_MUNMAP */
  63. # define INCR_FREE_BYTES(n, b)
  64. # define FREE_ASSERT(e)
  65. #endif /* USE_MUNMAP */
  66. /* Map a number of blocks to the appropriate large block free list index. */
  67. int GC_hblk_fl_from_blocks(word blocks_needed)
  68. {
  69. if (blocks_needed <= UNIQUE_THRESHOLD) return (int)blocks_needed;
  70. if (blocks_needed >= HUGE_THRESHOLD) return N_HBLK_FLS;
  71. return (int)(blocks_needed - UNIQUE_THRESHOLD)/FL_COMPRESSION
  72. + UNIQUE_THRESHOLD;
  73. }
  74. # define PHDR(hhdr) HDR(hhdr -> hb_prev)
  75. # define NHDR(hhdr) HDR(hhdr -> hb_next)
  76. # ifdef USE_MUNMAP
  77. # define IS_MAPPED(hhdr) (((hhdr) -> hb_flags & WAS_UNMAPPED) == 0)
  78. # else /* !USE_MMAP */
  79. # define IS_MAPPED(hhdr) 1
  80. # endif /* USE_MUNMAP */
  81. # if !defined(NO_DEBUGGING)
  82. void GC_print_hblkfreelist()
  83. {
  84. struct hblk * h;
  85. word total_free = 0;
  86. hdr * hhdr;
  87. word sz;
  88. unsigned i;
  89. for (i = 0; i <= N_HBLK_FLS; ++i) {
  90. h = GC_hblkfreelist[i];
  91. # ifdef USE_MUNMAP
  92. if (0 != h) GC_printf("Free list %ld:\n",
  93. (unsigned long)i);
  94. # else
  95. if (0 != h) GC_printf("Free list %lu (Total size %lu):\n",
  96. i, (unsigned long)GC_free_bytes[i]);
  97. # endif
  98. while (h != 0) {
  99. hhdr = HDR(h);
  100. sz = hhdr -> hb_sz;
  101. GC_printf("\t%p size %lu ", h, (unsigned long)sz);
  102. total_free += sz;
  103. if (GC_is_black_listed(h, HBLKSIZE) != 0) {
  104. GC_printf("start black listed\n");
  105. } else if (GC_is_black_listed(h, hhdr -> hb_sz) != 0) {
  106. GC_printf("partially black listed\n");
  107. } else {
  108. GC_printf("not black listed\n");
  109. }
  110. h = hhdr -> hb_next;
  111. }
  112. }
  113. # ifndef USE_MUNMAP
  114. if (total_free != GC_large_free_bytes) {
  115. GC_printf("GC_large_free_bytes = %lu (INCONSISTENT!!)\n",
  116. (unsigned long) GC_large_free_bytes);
  117. }
  118. # endif
  119. GC_printf("Total of %lu bytes on free list\n", (unsigned long)total_free);
  120. }
  121. /* Return the free list index on which the block described by the header */
  122. /* appears, or -1 if it appears nowhere. */
  123. int free_list_index_of(hdr *wanted)
  124. {
  125. struct hblk * h;
  126. hdr * hhdr;
  127. int i;
  128. for (i = 0; i <= N_HBLK_FLS; ++i) {
  129. h = GC_hblkfreelist[i];
  130. while (h != 0) {
  131. hhdr = HDR(h);
  132. if (hhdr == wanted) return i;
  133. h = hhdr -> hb_next;
  134. }
  135. }
  136. return -1;
  137. }
  138. void GC_dump_regions()
  139. {
  140. unsigned i;
  141. ptr_t start, end;
  142. ptr_t p;
  143. size_t bytes;
  144. hdr *hhdr;
  145. for (i = 0; i < GC_n_heap_sects; ++i) {
  146. start = GC_heap_sects[i].hs_start;
  147. bytes = GC_heap_sects[i].hs_bytes;
  148. end = start + bytes;
  149. /* Merge in contiguous sections. */
  150. while (i+1 < GC_n_heap_sects && GC_heap_sects[i+1].hs_start == end) {
  151. ++i;
  152. end = GC_heap_sects[i].hs_start + GC_heap_sects[i].hs_bytes;
  153. }
  154. GC_printf("***Section from %p to %p\n", start, end);
  155. for (p = start; p < end;) {
  156. hhdr = HDR(p);
  157. GC_printf("\t%p ", p);
  158. if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  159. GC_printf("Missing header!!(%d)\n", hhdr);
  160. p += HBLKSIZE;
  161. continue;
  162. }
  163. if (HBLK_IS_FREE(hhdr)) {
  164. int correct_index = GC_hblk_fl_from_blocks(
  165. divHBLKSZ(hhdr -> hb_sz));
  166. int actual_index;
  167. GC_printf("\tfree block of size 0x%lx bytes",
  168. (unsigned long)(hhdr -> hb_sz));
  169. if (IS_MAPPED(hhdr)) {
  170. GC_printf("\n");
  171. } else {
  172. GC_printf("(unmapped)\n");
  173. }
  174. actual_index = free_list_index_of(hhdr);
  175. if (-1 == actual_index) {
  176. GC_printf("\t\tBlock not on free list %d!!\n",
  177. correct_index);
  178. } else if (correct_index != actual_index) {
  179. GC_printf("\t\tBlock on list %d, should be on %d!!\n",
  180. actual_index, correct_index);
  181. }
  182. p += hhdr -> hb_sz;
  183. } else {
  184. GC_printf("\tused for blocks of size 0x%lx bytes\n",
  185. (unsigned long)(hhdr -> hb_sz));
  186. p += HBLKSIZE * OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
  187. }
  188. }
  189. }
  190. }
  191. # endif /* NO_DEBUGGING */
  192. /* Initialize hdr for a block containing the indicated size and */
  193. /* kind of objects. */
  194. /* Return FALSE on failure. */
  195. static GC_bool setup_header(hdr * hhdr, struct hblk *block, size_t byte_sz,
  196. int kind, unsigned flags)
  197. {
  198. word descr;
  199. size_t granules;
  200. /* Set size, kind and mark proc fields */
  201. hhdr -> hb_sz = byte_sz;
  202. hhdr -> hb_obj_kind = (unsigned char)kind;
  203. hhdr -> hb_flags = (unsigned char)flags;
  204. hhdr -> hb_block = block;
  205. descr = GC_obj_kinds[kind].ok_descriptor;
  206. if (GC_obj_kinds[kind].ok_relocate_descr) descr += byte_sz;
  207. hhdr -> hb_descr = descr;
  208. # ifdef MARK_BIT_PER_OBJ
  209. /* Set hb_inv_sz as portably as possible. */
  210. /* We set it to the smallest value such that sz * inv_sz > 2**32 */
  211. /* This may be more precision than necessary. */
  212. if (byte_sz > MAXOBJBYTES) {
  213. hhdr -> hb_inv_sz = LARGE_INV_SZ;
  214. } else {
  215. word inv_sz;
  216. # if CPP_WORDSZ == 64
  217. inv_sz = ((word)1 << 32)/byte_sz;
  218. if (((inv_sz*byte_sz) >> 32) == 0) ++inv_sz;
  219. # else /* 32 bit words */
  220. GC_ASSERT(byte_sz >= 4);
  221. inv_sz = ((unsigned)1 << 31)/byte_sz;
  222. inv_sz *= 2;
  223. while (inv_sz*byte_sz > byte_sz) ++inv_sz;
  224. # endif
  225. hhdr -> hb_inv_sz = inv_sz;
  226. }
  227. # else /* MARK_BIT_PER_GRANULE */
  228. hhdr -> hb_large_block = (unsigned char)(byte_sz > MAXOBJBYTES);
  229. granules = BYTES_TO_GRANULES(byte_sz);
  230. if (EXPECT(!GC_add_map_entry(granules), FALSE)) {
  231. /* Make it look like a valid block. */
  232. hhdr -> hb_sz = HBLKSIZE;
  233. hhdr -> hb_descr = 0;
  234. hhdr -> hb_large_block = TRUE;
  235. hhdr -> hb_map = 0;
  236. return FALSE;
  237. } else {
  238. size_t index = (hhdr -> hb_large_block? 0 : granules);
  239. hhdr -> hb_map = GC_obj_map[index];
  240. }
  241. # endif /* MARK_BIT_PER_GRANULE */
  242. /* Clear mark bits */
  243. GC_clear_hdr_marks(hhdr);
  244. hhdr -> hb_last_reclaimed = (unsigned short)GC_gc_no;
  245. return(TRUE);
  246. }
  247. #define FL_UNKNOWN -1
  248. /*
  249. * Remove hhdr from the appropriate free list.
  250. * We assume it is on the nth free list, or on the size
  251. * appropriate free list if n is FL_UNKNOWN.
  252. */
  253. void GC_remove_from_fl(hdr *hhdr, int n)
  254. {
  255. int index;
  256. GC_ASSERT(((hhdr -> hb_sz) & (HBLKSIZE-1)) == 0);
  257. # ifndef USE_MUNMAP
  258. /* We always need index to mainatin free counts. */
  259. if (FL_UNKNOWN == n) {
  260. index = GC_hblk_fl_from_blocks(divHBLKSZ(hhdr -> hb_sz));
  261. } else {
  262. index = n;
  263. }
  264. # endif
  265. if (hhdr -> hb_prev == 0) {
  266. # ifdef USE_MUNMAP
  267. if (FL_UNKNOWN == n) {
  268. index = GC_hblk_fl_from_blocks(divHBLKSZ(hhdr -> hb_sz));
  269. } else {
  270. index = n;
  271. }
  272. # endif
  273. GC_ASSERT(HDR(GC_hblkfreelist[index]) == hhdr);
  274. GC_hblkfreelist[index] = hhdr -> hb_next;
  275. } else {
  276. hdr *phdr;
  277. GET_HDR(hhdr -> hb_prev, phdr);
  278. phdr -> hb_next = hhdr -> hb_next;
  279. }
  280. FREE_ASSERT(GC_free_bytes[index] >= hhdr -> hb_sz);
  281. INCR_FREE_BYTES(index, - (signed_word)(hhdr -> hb_sz));
  282. if (0 != hhdr -> hb_next) {
  283. hdr * nhdr;
  284. GC_ASSERT(!IS_FORWARDING_ADDR_OR_NIL(NHDR(hhdr)));
  285. GET_HDR(hhdr -> hb_next, nhdr);
  286. nhdr -> hb_prev = hhdr -> hb_prev;
  287. }
  288. }
  289. /*
  290. * Return a pointer to the free block ending just before h, if any.
  291. */
  292. struct hblk * GC_free_block_ending_at(struct hblk *h)
  293. {
  294. struct hblk * p = h - 1;
  295. hdr * phdr;
  296. GET_HDR(p, phdr);
  297. while (0 != phdr && IS_FORWARDING_ADDR_OR_NIL(phdr)) {
  298. p = FORWARDED_ADDR(p,phdr);
  299. phdr = HDR(p);
  300. }
  301. if (0 != phdr) {
  302. if(HBLK_IS_FREE(phdr)) {
  303. return p;
  304. } else {
  305. return 0;
  306. }
  307. }
  308. p = GC_prev_block(h - 1);
  309. if (0 != p) {
  310. phdr = HDR(p);
  311. if (HBLK_IS_FREE(phdr) && (ptr_t)p + phdr -> hb_sz == (ptr_t)h) {
  312. return p;
  313. }
  314. }
  315. return 0;
  316. }
  317. /*
  318. * Add hhdr to the appropriate free list.
  319. * We maintain individual free lists sorted by address.
  320. */
  321. void GC_add_to_fl(struct hblk *h, hdr *hhdr)
  322. {
  323. int index = GC_hblk_fl_from_blocks(divHBLKSZ(hhdr -> hb_sz));
  324. struct hblk *second = GC_hblkfreelist[index];
  325. hdr * second_hdr;
  326. # if defined(GC_ASSERTIONS) && !defined(USE_MUNMAP)
  327. struct hblk *next = (struct hblk *)((word)h + hhdr -> hb_sz);
  328. hdr * nexthdr = HDR(next);
  329. struct hblk *prev = GC_free_block_ending_at(h);
  330. hdr * prevhdr = HDR(prev);
  331. GC_ASSERT(nexthdr == 0 || !HBLK_IS_FREE(nexthdr)
  332. || (signed_word)GC_heapsize < 0);
  333. /* In the last case, blocks may be too large to merge. */
  334. GC_ASSERT(prev == 0 || !HBLK_IS_FREE(prevhdr)
  335. || (signed_word)GC_heapsize < 0);
  336. # endif
  337. GC_ASSERT(((hhdr -> hb_sz) & (HBLKSIZE-1)) == 0);
  338. GC_hblkfreelist[index] = h;
  339. INCR_FREE_BYTES(index, hhdr -> hb_sz);
  340. FREE_ASSERT(GC_free_bytes[index] <= GC_large_free_bytes)
  341. hhdr -> hb_next = second;
  342. hhdr -> hb_prev = 0;
  343. if (0 != second) {
  344. GET_HDR(second, second_hdr);
  345. second_hdr -> hb_prev = h;
  346. }
  347. hhdr -> hb_flags |= FREE_BLK;
  348. }
  349. #ifdef USE_MUNMAP
  350. /* Unmap blocks that haven't been recently touched. This is the only way */
  351. /* way blocks are ever unmapped. */
  352. void GC_unmap_old(void)
  353. {
  354. struct hblk * h;
  355. hdr * hhdr;
  356. word sz;
  357. unsigned short last_rec, threshold;
  358. int i;
  359. # ifndef MUNMAP_THRESHOLD
  360. # define MUNMAP_THRESHOLD 6
  361. # endif
  362. for (i = 0; i <= N_HBLK_FLS; ++i) {
  363. for (h = GC_hblkfreelist[i]; 0 != h; h = hhdr -> hb_next) {
  364. hhdr = HDR(h);
  365. if (!IS_MAPPED(hhdr)) continue;
  366. threshold = (unsigned short)(GC_gc_no - MUNMAP_THRESHOLD);
  367. last_rec = hhdr -> hb_last_reclaimed;
  368. if ((last_rec > GC_gc_no || last_rec < threshold)
  369. && threshold < GC_gc_no /* not recently wrapped */) {
  370. sz = hhdr -> hb_sz;
  371. GC_unmap((ptr_t)h, sz);
  372. hhdr -> hb_flags |= WAS_UNMAPPED;
  373. }
  374. }
  375. }
  376. }
  377. /* Merge all unmapped blocks that are adjacent to other free */
  378. /* blocks. This may involve remapping, since all blocks are either */
  379. /* fully mapped or fully unmapped. */
  380. void GC_merge_unmapped(void)
  381. {
  382. struct hblk * h, *next;
  383. hdr * hhdr, *nexthdr;
  384. word size, nextsize;
  385. int i;
  386. for (i = 0; i <= N_HBLK_FLS; ++i) {
  387. h = GC_hblkfreelist[i];
  388. while (h != 0) {
  389. GET_HDR(h, hhdr);
  390. size = hhdr->hb_sz;
  391. next = (struct hblk *)((word)h + size);
  392. GET_HDR(next, nexthdr);
  393. /* Coalesce with successor, if possible */
  394. if (0 != nexthdr && HBLK_IS_FREE(nexthdr)
  395. && (signed_word) (size + (nextsize = nexthdr->hb_sz)) > 0
  396. /* no pot. overflow */) {
  397. if (IS_MAPPED(hhdr)) {
  398. GC_ASSERT(!IS_MAPPED(nexthdr));
  399. /* make both consistent, so that we can merge */
  400. if (size > nextsize) {
  401. GC_remap((ptr_t)next, nextsize);
  402. } else {
  403. GC_unmap((ptr_t)h, size);
  404. hhdr -> hb_flags |= WAS_UNMAPPED;
  405. }
  406. } else if (IS_MAPPED(nexthdr)) {
  407. GC_ASSERT(!IS_MAPPED(hhdr));
  408. if (size > nextsize) {
  409. GC_unmap((ptr_t)next, nextsize);
  410. } else {
  411. GC_remap((ptr_t)h, size);
  412. hhdr -> hb_flags &= ~WAS_UNMAPPED;
  413. hhdr -> hb_last_reclaimed = nexthdr -> hb_last_reclaimed;
  414. }
  415. } else {
  416. /* Unmap any gap in the middle */
  417. GC_unmap_gap((ptr_t)h, size, (ptr_t)next, nexthdr -> hb_sz);
  418. }
  419. /* If they are both unmapped, we merge, but leave unmapped. */
  420. GC_remove_from_fl(hhdr, i);
  421. GC_remove_from_fl(nexthdr, FL_UNKNOWN);
  422. hhdr -> hb_sz += nexthdr -> hb_sz;
  423. GC_remove_header(next);
  424. GC_add_to_fl(h, hhdr);
  425. /* Start over at beginning of list */
  426. h = GC_hblkfreelist[i];
  427. } else /* not mergable with successor */ {
  428. h = hhdr -> hb_next;
  429. }
  430. } /* while (h != 0) ... */
  431. } /* for ... */
  432. }
  433. #endif /* USE_MUNMAP */
  434. /*
  435. * Return a pointer to a block starting at h of length bytes.
  436. * Memory for the block is mapped.
  437. * Remove the block from its free list, and return the remainder (if any)
  438. * to its appropriate free list.
  439. * May fail by returning 0.
  440. * The header for the returned block must be set up by the caller.
  441. * If the return value is not 0, then hhdr is the header for it.
  442. */
  443. struct hblk * GC_get_first_part(struct hblk *h, hdr *hhdr,
  444. size_t bytes, int index)
  445. {
  446. word total_size = hhdr -> hb_sz;
  447. struct hblk * rest;
  448. hdr * rest_hdr;
  449. GC_ASSERT((total_size & (HBLKSIZE-1)) == 0);
  450. GC_remove_from_fl(hhdr, index);
  451. if (total_size == bytes) return h;
  452. rest = (struct hblk *)((word)h + bytes);
  453. rest_hdr = GC_install_header(rest);
  454. if (0 == rest_hdr) {
  455. /* FIXME: This is likely to be very bad news ... */
  456. WARN("Header allocation failed: Dropping block.\n", 0);
  457. return(0);
  458. }
  459. rest_hdr -> hb_sz = total_size - bytes;
  460. rest_hdr -> hb_flags = 0;
  461. # ifdef GC_ASSERTIONS
  462. /* Mark h not free, to avoid assertion about adjacent free blocks. */
  463. hhdr -> hb_flags &= ~FREE_BLK;
  464. # endif
  465. GC_add_to_fl(rest, rest_hdr);
  466. return h;
  467. }
  468. /*
  469. * H is a free block. N points at an address inside it.
  470. * A new header for n has already been set up. Fix up h's header
  471. * to reflect the fact that it is being split, move it to the
  472. * appropriate free list.
  473. * N replaces h in the original free list.
  474. *
  475. * Nhdr is not completely filled in, since it is about to allocated.
  476. * It may in fact end up on the wrong free list for its size.
  477. * (Hence adding it to a free list is silly. But this path is hopefully
  478. * rare enough that it doesn't matter. The code is cleaner this way.)
  479. */
  480. void GC_split_block(struct hblk *h, hdr *hhdr, struct hblk *n,
  481. hdr *nhdr, int index /* Index of free list */)
  482. {
  483. word total_size = hhdr -> hb_sz;
  484. word h_size = (word)n - (word)h;
  485. struct hblk *prev = hhdr -> hb_prev;
  486. struct hblk *next = hhdr -> hb_next;
  487. /* Replace h with n on its freelist */
  488. nhdr -> hb_prev = prev;
  489. nhdr -> hb_next = next;
  490. nhdr -> hb_sz = total_size - h_size;
  491. nhdr -> hb_flags = 0;
  492. if (0 != prev) {
  493. HDR(prev) -> hb_next = n;
  494. } else {
  495. GC_hblkfreelist[index] = n;
  496. }
  497. if (0 != next) {
  498. HDR(next) -> hb_prev = n;
  499. }
  500. INCR_FREE_BYTES(index, -(signed_word)h_size);
  501. FREE_ASSERT(GC_free_bytes[index] > 0);
  502. # ifdef GC_ASSERTIONS
  503. nhdr -> hb_flags &= ~FREE_BLK;
  504. /* Don't fail test for consecutive */
  505. /* free blocks in GC_add_to_fl. */
  506. # endif
  507. # ifdef USE_MUNMAP
  508. hhdr -> hb_last_reclaimed = (unsigned short)GC_gc_no;
  509. # endif
  510. hhdr -> hb_sz = h_size;
  511. GC_add_to_fl(h, hhdr);
  512. nhdr -> hb_flags |= FREE_BLK;
  513. }
  514. struct hblk *
  515. GC_allochblk_nth(size_t sz/* bytes */, int kind, unsigned flags, int n,
  516. GC_bool may_split);
  517. /*
  518. * Allocate (and return pointer to) a heap block
  519. * for objects of size sz bytes, searching the nth free list.
  520. *
  521. * NOTE: We set obj_map field in header correctly.
  522. * Caller is responsible for building an object freelist in block.
  523. *
  524. * The client is responsible for clearing the block, if necessary.
  525. */
  526. struct hblk *
  527. GC_allochblk(size_t sz, int kind, unsigned flags/* IGNORE_OFF_PAGE or 0 */)
  528. {
  529. word blocks;
  530. int start_list;
  531. int i;
  532. struct hblk *result;
  533. int split_limit; /* Highest index of free list whose blocks we */
  534. /* split. */
  535. GC_ASSERT((sz & (GRANULE_BYTES - 1)) == 0);
  536. blocks = OBJ_SZ_TO_BLOCKS(sz);
  537. if ((signed_word)(blocks * HBLKSIZE) < 0) {
  538. return 0;
  539. }
  540. start_list = GC_hblk_fl_from_blocks(blocks);
  541. /* Try for an exact match first. */
  542. result = GC_allochblk_nth(sz, kind, flags, start_list, FALSE);
  543. if (0 != result) return result;
  544. if (GC_use_entire_heap || GC_dont_gc
  545. || USED_HEAP_SIZE < GC_requested_heapsize
  546. || TRUE_INCREMENTAL || !GC_should_collect()) {
  547. /* Should use more of the heap, even if it requires splitting. */
  548. split_limit = N_HBLK_FLS;
  549. } else {
  550. # ifdef USE_MUNMAP
  551. /* avoid splitting, since that might require remapping */
  552. split_limit = 0;
  553. # else
  554. if (GC_finalizer_bytes_freed > (GC_heapsize >> 4)) {
  555. /* If we are deallocating lots of memory from */
  556. /* finalizers, fail and collect sooner rather */
  557. /* than later. */
  558. split_limit = 0;
  559. } else {
  560. /* If we have enough large blocks left to cover any */
  561. /* previous request for large blocks, we go ahead */
  562. /* and split. Assuming a steady state, that should */
  563. /* be safe. It means that we can use the full */
  564. /* heap if we allocate only small objects. */
  565. split_limit = GC_enough_large_bytes_left();
  566. }
  567. # endif
  568. }
  569. if (start_list < UNIQUE_THRESHOLD) {
  570. /* No reason to try start_list again, since all blocks are exact */
  571. /* matches. */
  572. ++start_list;
  573. }
  574. for (i = start_list; i <= split_limit; ++i) {
  575. struct hblk * result = GC_allochblk_nth(sz, kind, flags, i, TRUE);
  576. if (0 != result) return result;
  577. }
  578. return 0;
  579. }
  580. /*
  581. * The same, but with search restricted to nth free list.
  582. * Flags is IGNORE_OFF_PAGE or zero.
  583. * Unlike the above, sz is in bytes.
  584. * The may_split flag indicates whether it's OK to split larger blocks.
  585. */
  586. struct hblk *
  587. GC_allochblk_nth(size_t sz, int kind, unsigned flags, int n, GC_bool may_split)
  588. {
  589. struct hblk *hbp;
  590. hdr * hhdr; /* Header corr. to hbp */
  591. /* Initialized after loop if hbp !=0 */
  592. /* Gcc uninitialized use warning is bogus. */
  593. struct hblk *thishbp;
  594. hdr * thishdr; /* Header corr. to hbp */
  595. signed_word size_needed; /* number of bytes in requested objects */
  596. signed_word size_avail; /* bytes available in this block */
  597. size_needed = HBLKSIZE * OBJ_SZ_TO_BLOCKS(sz);
  598. /* search for a big enough block in free list */
  599. hbp = GC_hblkfreelist[n];
  600. for(; 0 != hbp; hbp = hhdr -> hb_next) {
  601. GET_HDR(hbp, hhdr);
  602. size_avail = hhdr->hb_sz;
  603. if (size_avail < size_needed) continue;
  604. if (size_avail != size_needed) {
  605. signed_word next_size;
  606. if (!may_split) continue;
  607. /* If the next heap block is obviously better, go on. */
  608. /* This prevents us from disassembling a single large block */
  609. /* to get tiny blocks. */
  610. thishbp = hhdr -> hb_next;
  611. if (thishbp != 0) {
  612. GET_HDR(thishbp, thishdr);
  613. next_size = (signed_word)(thishdr -> hb_sz);
  614. if (next_size < size_avail
  615. && next_size >= size_needed
  616. && !GC_is_black_listed(thishbp, (word)size_needed)) {
  617. continue;
  618. }
  619. }
  620. }
  621. if ( !IS_UNCOLLECTABLE(kind) &&
  622. (kind != PTRFREE || size_needed > MAX_BLACK_LIST_ALLOC)) {
  623. struct hblk * lasthbp = hbp;
  624. ptr_t search_end = (ptr_t)hbp + size_avail - size_needed;
  625. signed_word orig_avail = size_avail;
  626. signed_word eff_size_needed = ((flags & IGNORE_OFF_PAGE)?
  627. HBLKSIZE
  628. : size_needed);
  629. while ((ptr_t)lasthbp <= search_end
  630. && (thishbp = GC_is_black_listed(lasthbp,
  631. (word)eff_size_needed))
  632. != 0) {
  633. lasthbp = thishbp;
  634. }
  635. size_avail -= (ptr_t)lasthbp - (ptr_t)hbp;
  636. thishbp = lasthbp;
  637. if (size_avail >= size_needed) {
  638. if (thishbp != hbp &&
  639. 0 != (thishdr = GC_install_header(thishbp))) {
  640. /* Make sure it's mapped before we mangle it. */
  641. # ifdef USE_MUNMAP
  642. if (!IS_MAPPED(hhdr)) {
  643. GC_remap((ptr_t)hbp, hhdr -> hb_sz);
  644. hhdr -> hb_flags &= ~WAS_UNMAPPED;
  645. }
  646. # endif
  647. /* Split the block at thishbp */
  648. GC_split_block(hbp, hhdr, thishbp, thishdr, n);
  649. /* Advance to thishbp */
  650. hbp = thishbp;
  651. hhdr = thishdr;
  652. /* We must now allocate thishbp, since it may */
  653. /* be on the wrong free list. */
  654. }
  655. } else if (size_needed > (signed_word)BL_LIMIT
  656. && orig_avail - size_needed
  657. > (signed_word)BL_LIMIT) {
  658. /* Punt, since anything else risks unreasonable heap growth. */
  659. if (++GC_large_alloc_warn_suppressed
  660. >= GC_large_alloc_warn_interval) {
  661. WARN("Repeated allocation of very large block "
  662. "(appr. size %ld):\n"
  663. "\tMay lead to memory leak and poor performance.\n",
  664. size_needed);
  665. GC_large_alloc_warn_suppressed = 0;
  666. }
  667. size_avail = orig_avail;
  668. } else if (size_avail == 0 && size_needed == HBLKSIZE
  669. && IS_MAPPED(hhdr)) {
  670. if (!GC_find_leak) {
  671. static unsigned count = 0;
  672. /* The block is completely blacklisted. We need */
  673. /* to drop some such blocks, since otherwise we spend */
  674. /* all our time traversing them if pointerfree */
  675. /* blocks are unpopular. */
  676. /* A dropped block will be reconsidered at next GC. */
  677. if ((++count & 3) == 0) {
  678. /* Allocate and drop the block in small chunks, to */
  679. /* maximize the chance that we will recover some */
  680. /* later. */
  681. word total_size = hhdr -> hb_sz;
  682. struct hblk * limit = hbp + divHBLKSZ(total_size);
  683. struct hblk * h;
  684. struct hblk * prev = hhdr -> hb_prev;
  685. GC_large_free_bytes -= total_size;
  686. GC_bytes_dropped += total_size;
  687. GC_remove_from_fl(hhdr, n);
  688. for (h = hbp; h < limit; h++) {
  689. if (h == hbp || 0 != (hhdr = GC_install_header(h))) {
  690. (void) setup_header(
  691. hhdr, h,
  692. HBLKSIZE,
  693. PTRFREE, 0); /* Cant fail */
  694. if (GC_debugging_started) {
  695. BZERO(h, HBLKSIZE);
  696. }
  697. }
  698. }
  699. /* Restore hbp to point at free block */
  700. hbp = prev;
  701. if (0 == hbp) {
  702. return GC_allochblk_nth(sz, kind, flags, n, may_split);
  703. }
  704. hhdr = HDR(hbp);
  705. }
  706. }
  707. }
  708. }
  709. if( size_avail >= size_needed ) {
  710. # ifdef USE_MUNMAP
  711. if (!IS_MAPPED(hhdr)) {
  712. GC_remap((ptr_t)hbp, hhdr -> hb_sz);
  713. hhdr -> hb_flags &= ~WAS_UNMAPPED;
  714. /* Note: This may leave adjacent, mapped free blocks. */
  715. }
  716. # endif
  717. /* hbp may be on the wrong freelist; the parameter n */
  718. /* is important. */
  719. hbp = GC_get_first_part(hbp, hhdr, size_needed, n);
  720. break;
  721. }
  722. }
  723. if (0 == hbp) return 0;
  724. /* Add it to map of valid blocks */
  725. if (!GC_install_counts(hbp, (word)size_needed)) return(0);
  726. /* This leaks memory under very rare conditions. */
  727. /* Set up header */
  728. if (!setup_header(hhdr, hbp, sz, kind, flags)) {
  729. GC_remove_counts(hbp, (word)size_needed);
  730. return(0); /* ditto */
  731. }
  732. /* Notify virtual dirty bit implementation that we are about to write. */
  733. /* Ensure that pointerfree objects are not protected if it's avoidable. */
  734. GC_remove_protection(hbp, divHBLKSZ(size_needed),
  735. (hhdr -> hb_descr == 0) /* pointer-free */);
  736. /* We just successfully allocated a block. Restart count of */
  737. /* consecutive failures. */
  738. {
  739. extern unsigned GC_fail_count;
  740. GC_fail_count = 0;
  741. }
  742. GC_large_free_bytes -= size_needed;
  743. GC_ASSERT(IS_MAPPED(hhdr));
  744. return( hbp );
  745. }
  746. struct hblk * GC_freehblk_ptr = 0; /* Search position hint for GC_freehblk */
  747. /*
  748. * Free a heap block.
  749. *
  750. * Coalesce the block with its neighbors if possible.
  751. *
  752. * All mark words are assumed to be cleared.
  753. */
  754. void
  755. GC_freehblk(struct hblk *hbp)
  756. {
  757. struct hblk *next, *prev;
  758. hdr *hhdr, *prevhdr, *nexthdr;
  759. signed_word size;
  760. GET_HDR(hbp, hhdr);
  761. size = hhdr->hb_sz;
  762. size = HBLKSIZE * OBJ_SZ_TO_BLOCKS(size);
  763. if (size <= 0)
  764. ABORT("Deallocating excessively large block. Too large an allocation?");
  765. /* Probably possible if we try to allocate more than half the address */
  766. /* space at once. If we dont catch it here, strange things happen */
  767. /* later. */
  768. GC_remove_counts(hbp, (word)size);
  769. hhdr->hb_sz = size;
  770. # ifdef USE_MUNMAP
  771. hhdr -> hb_last_reclaimed = (unsigned short)GC_gc_no;
  772. # endif
  773. /* Check for duplicate deallocation in the easy case */
  774. if (HBLK_IS_FREE(hhdr)) {
  775. GC_printf("Duplicate large block deallocation of %p\n", hbp);
  776. ABORT("Duplicate large block deallocation");
  777. }
  778. GC_ASSERT(IS_MAPPED(hhdr));
  779. hhdr -> hb_flags |= FREE_BLK;
  780. next = (struct hblk *)((word)hbp + size);
  781. GET_HDR(next, nexthdr);
  782. prev = GC_free_block_ending_at(hbp);
  783. /* Coalesce with successor, if possible */
  784. if(0 != nexthdr && HBLK_IS_FREE(nexthdr) && IS_MAPPED(nexthdr)
  785. && (signed_word)(hhdr -> hb_sz + nexthdr -> hb_sz) > 0
  786. /* no overflow */) {
  787. GC_remove_from_fl(nexthdr, FL_UNKNOWN);
  788. hhdr -> hb_sz += nexthdr -> hb_sz;
  789. GC_remove_header(next);
  790. }
  791. /* Coalesce with predecessor, if possible. */
  792. if (0 != prev) {
  793. prevhdr = HDR(prev);
  794. if (IS_MAPPED(prevhdr)
  795. && (signed_word)(hhdr -> hb_sz + prevhdr -> hb_sz) > 0) {
  796. GC_remove_from_fl(prevhdr, FL_UNKNOWN);
  797. prevhdr -> hb_sz += hhdr -> hb_sz;
  798. # ifdef USE_MUNMAP
  799. prevhdr -> hb_last_reclaimed = (unsigned short)GC_gc_no;
  800. # endif
  801. GC_remove_header(hbp);
  802. hbp = prev;
  803. hhdr = prevhdr;
  804. }
  805. }
  806. /* FIXME: It is not clear we really always want to do these merges */
  807. /* with -DUSE_MUNMAP, since it updates ages and hence prevents */
  808. /* unmapping. */
  809. GC_large_free_bytes += size;
  810. GC_add_to_fl(hbp, hhdr);
  811. }