allchblk.c 34 KB

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