malloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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) 2000 by Hewlett-Packard Company. 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. /* Boehm, February 7, 1996 4:32 pm PST */
  16. #include <stdio.h>
  17. #include "private/gc_priv.h"
  18. extern ptr_t GC_clear_stack(); /* in misc.c, behaves like identity */
  19. void GC_extend_size_map(); /* in misc.c. */
  20. /* Allocate reclaim list for kind: */
  21. /* Return TRUE on success */
  22. GC_bool GC_alloc_reclaim_list(kind)
  23. register struct obj_kind * kind;
  24. {
  25. struct hblk ** result = (struct hblk **)
  26. GC_scratch_alloc((MAXOBJSZ+1) * sizeof(struct hblk *));
  27. if (result == 0) return(FALSE);
  28. BZERO(result, (MAXOBJSZ+1)*sizeof(struct hblk *));
  29. kind -> ok_reclaim_list = result;
  30. return(TRUE);
  31. }
  32. /* Allocate a large block of size lw words. */
  33. /* The block is not cleared. */
  34. /* Flags is 0 or IGNORE_OFF_PAGE. */
  35. /* We hold the allocation lock. */
  36. ptr_t GC_alloc_large(lw, k, flags)
  37. word lw;
  38. int k;
  39. unsigned flags;
  40. {
  41. struct hblk * h;
  42. word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
  43. ptr_t result;
  44. if (!GC_is_initialized) GC_init_inner();
  45. /* Do our share of marking work */
  46. if(GC_incremental && !GC_dont_gc)
  47. GC_collect_a_little_inner((int)n_blocks);
  48. h = GC_allochblk(lw, k, flags);
  49. # ifdef USE_MUNMAP
  50. if (0 == h) {
  51. GC_merge_unmapped();
  52. h = GC_allochblk(lw, k, flags);
  53. }
  54. # endif
  55. while (0 == h && GC_collect_or_expand(n_blocks, (flags != 0))) {
  56. h = GC_allochblk(lw, k, flags);
  57. }
  58. if (h == 0) {
  59. result = 0;
  60. } else {
  61. int total_bytes = n_blocks * HBLKSIZE;
  62. if (n_blocks > 1) {
  63. GC_large_allocd_bytes += total_bytes;
  64. if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
  65. GC_max_large_allocd_bytes = GC_large_allocd_bytes;
  66. }
  67. result = (ptr_t) (h -> hb_body);
  68. GC_words_wasted += BYTES_TO_WORDS(total_bytes) - lw;
  69. }
  70. return result;
  71. }
  72. /* Allocate a large block of size lb bytes. Clear if appropriate. */
  73. /* We hold the allocation lock. */
  74. ptr_t GC_alloc_large_and_clear(lw, k, flags)
  75. word lw;
  76. int k;
  77. unsigned flags;
  78. {
  79. ptr_t result = GC_alloc_large(lw, k, flags);
  80. word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
  81. if (0 == result) return 0;
  82. if (GC_debugging_started || GC_obj_kinds[k].ok_init) {
  83. /* Clear the whole block, in case of GC_realloc call. */
  84. BZERO(result, n_blocks * HBLKSIZE);
  85. }
  86. return result;
  87. }
  88. /* allocate lb bytes for an object of kind k. */
  89. /* Should not be used to directly to allocate */
  90. /* objects such as STUBBORN objects that */
  91. /* require special handling on allocation. */
  92. /* First a version that assumes we already */
  93. /* hold lock: */
  94. ptr_t GC_generic_malloc_inner(lb, k)
  95. register word lb;
  96. register int k;
  97. {
  98. register word lw;
  99. register ptr_t op;
  100. register ptr_t *opp;
  101. if( SMALL_OBJ(lb) ) {
  102. register struct obj_kind * kind = GC_obj_kinds + k;
  103. # ifdef MERGE_SIZES
  104. lw = GC_size_map[lb];
  105. # else
  106. lw = ALIGNED_WORDS(lb);
  107. if (lw == 0) lw = MIN_WORDS;
  108. # endif
  109. opp = &(kind -> ok_freelist[lw]);
  110. if( (op = *opp) == 0 ) {
  111. # ifdef MERGE_SIZES
  112. if (GC_size_map[lb] == 0) {
  113. if (!GC_is_initialized) GC_init_inner();
  114. if (GC_size_map[lb] == 0) GC_extend_size_map(lb);
  115. return(GC_generic_malloc_inner(lb, k));
  116. }
  117. # else
  118. if (!GC_is_initialized) {
  119. GC_init_inner();
  120. return(GC_generic_malloc_inner(lb, k));
  121. }
  122. # endif
  123. if (kind -> ok_reclaim_list == 0) {
  124. if (!GC_alloc_reclaim_list(kind)) goto out;
  125. }
  126. op = GC_allocobj(lw, k);
  127. if (op == 0) goto out;
  128. }
  129. /* Here everything is in a consistent state. */
  130. /* We assume the following assignment is */
  131. /* atomic. If we get aborted */
  132. /* after the assignment, we lose an object, */
  133. /* but that's benign. */
  134. /* Volatile declarations may need to be added */
  135. /* to prevent the compiler from breaking things.*/
  136. /* If we only execute the second of the */
  137. /* following assignments, we lose the free */
  138. /* list, but that should still be OK, at least */
  139. /* for garbage collected memory. */
  140. *opp = obj_link(op);
  141. obj_link(op) = 0;
  142. } else {
  143. lw = ROUNDED_UP_WORDS(lb);
  144. op = (ptr_t)GC_alloc_large_and_clear(lw, k, 0);
  145. }
  146. GC_words_allocd += lw;
  147. out:
  148. return op;
  149. }
  150. /* Allocate a composite object of size n bytes. The caller guarantees */
  151. /* that pointers past the first page are not relevant. Caller holds */
  152. /* allocation lock. */
  153. ptr_t GC_generic_malloc_inner_ignore_off_page(lb, k)
  154. register size_t lb;
  155. register int k;
  156. {
  157. register word lw;
  158. ptr_t op;
  159. if (lb <= HBLKSIZE)
  160. return(GC_generic_malloc_inner((word)lb, k));
  161. lw = ROUNDED_UP_WORDS(lb);
  162. op = (ptr_t)GC_alloc_large_and_clear(lw, k, IGNORE_OFF_PAGE);
  163. GC_words_allocd += lw;
  164. return op;
  165. }
  166. ptr_t GC_generic_malloc(lb, k)
  167. register word lb;
  168. register int k;
  169. {
  170. ptr_t result;
  171. DCL_LOCK_STATE;
  172. if (GC_have_errors) GC_print_all_errors();
  173. GC_INVOKE_FINALIZERS();
  174. if (SMALL_OBJ(lb)) {
  175. DISABLE_SIGNALS();
  176. LOCK();
  177. result = GC_generic_malloc_inner((word)lb, k);
  178. UNLOCK();
  179. ENABLE_SIGNALS();
  180. } else {
  181. word lw;
  182. word n_blocks;
  183. GC_bool init;
  184. lw = ROUNDED_UP_WORDS(lb);
  185. n_blocks = OBJ_SZ_TO_BLOCKS(lw);
  186. init = GC_obj_kinds[k].ok_init;
  187. DISABLE_SIGNALS();
  188. LOCK();
  189. result = (ptr_t)GC_alloc_large(lw, k, 0);
  190. if (0 != result) {
  191. if (GC_debugging_started) {
  192. BZERO(result, n_blocks * HBLKSIZE);
  193. } else {
  194. # ifdef THREADS
  195. /* Clear any memory that might be used for GC descriptors */
  196. /* before we release the lock. */
  197. ((word *)result)[0] = 0;
  198. ((word *)result)[1] = 0;
  199. ((word *)result)[lw-1] = 0;
  200. ((word *)result)[lw-2] = 0;
  201. # endif
  202. }
  203. }
  204. GC_words_allocd += lw;
  205. UNLOCK();
  206. ENABLE_SIGNALS();
  207. if (init && !GC_debugging_started && 0 != result) {
  208. BZERO(result, n_blocks * HBLKSIZE);
  209. }
  210. }
  211. if (0 == result) {
  212. return((*GC_oom_fn)(lb));
  213. } else {
  214. return(result);
  215. }
  216. }
  217. #define GENERAL_MALLOC(lb,k) \
  218. (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
  219. /* We make the GC_clear_stack_call a tail call, hoping to get more of */
  220. /* the stack. */
  221. /* Allocate lb bytes of atomic (pointerfree) data */
  222. # ifdef __STDC__
  223. GC_PTR GC_malloc_atomic(size_t lb)
  224. # else
  225. GC_PTR GC_malloc_atomic(lb)
  226. size_t lb;
  227. # endif
  228. {
  229. register ptr_t op;
  230. register ptr_t * opp;
  231. register word lw;
  232. DCL_LOCK_STATE;
  233. if( EXPECT(SMALL_OBJ(lb), 1) ) {
  234. # ifdef MERGE_SIZES
  235. lw = GC_size_map[lb];
  236. # else
  237. lw = ALIGNED_WORDS(lb);
  238. # endif
  239. opp = &(GC_aobjfreelist[lw]);
  240. FASTLOCK();
  241. if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
  242. FASTUNLOCK();
  243. return(GENERAL_MALLOC((word)lb, PTRFREE));
  244. }
  245. /* See above comment on signals. */
  246. *opp = obj_link(op);
  247. GC_words_allocd += lw;
  248. FASTUNLOCK();
  249. return((GC_PTR) op);
  250. } else {
  251. return(GENERAL_MALLOC((word)lb, PTRFREE));
  252. }
  253. }
  254. /* Allocate lb bytes of composite (pointerful) data */
  255. # ifdef __STDC__
  256. GC_PTR GC_malloc(size_t lb)
  257. # else
  258. GC_PTR GC_malloc(lb)
  259. size_t lb;
  260. # endif
  261. {
  262. register ptr_t op;
  263. register ptr_t *opp;
  264. register word lw;
  265. DCL_LOCK_STATE;
  266. if( EXPECT(SMALL_OBJ(lb), 1) ) {
  267. # ifdef MERGE_SIZES
  268. lw = GC_size_map[lb];
  269. # else
  270. lw = ALIGNED_WORDS(lb);
  271. # endif
  272. opp = &(GC_objfreelist[lw]);
  273. FASTLOCK();
  274. if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
  275. FASTUNLOCK();
  276. return(GENERAL_MALLOC((word)lb, NORMAL));
  277. }
  278. /* See above comment on signals. */
  279. GC_ASSERT(0 == obj_link(op)
  280. || (word)obj_link(op)
  281. <= (word)GC_greatest_plausible_heap_addr
  282. && (word)obj_link(op)
  283. >= (word)GC_least_plausible_heap_addr);
  284. *opp = obj_link(op);
  285. obj_link(op) = 0;
  286. GC_words_allocd += lw;
  287. FASTUNLOCK();
  288. return((GC_PTR) op);
  289. } else {
  290. return(GENERAL_MALLOC((word)lb, NORMAL));
  291. }
  292. }
  293. # ifdef REDIRECT_MALLOC
  294. /* Avoid unnecessary nested procedure calls here, by #defining some */
  295. /* malloc replacements. Otherwise we end up saving a */
  296. /* meaningless return address in the object. It also speeds things up, */
  297. /* but it is admittedly quite ugly. */
  298. # ifdef GC_ADD_CALLER
  299. # define RA GC_RETURN_ADDR,
  300. # else
  301. # define RA
  302. # endif
  303. # define GC_debug_malloc_replacement(lb) \
  304. GC_debug_malloc(lb, RA "unknown", 0)
  305. # ifdef __STDC__
  306. GC_PTR malloc(size_t lb)
  307. # else
  308. GC_PTR malloc(lb)
  309. size_t lb;
  310. # endif
  311. {
  312. /* It might help to manually inline the GC_malloc call here. */
  313. /* But any decent compiler should reduce the extra procedure call */
  314. /* to at most a jump instruction in this case. */
  315. # if defined(I386) && defined(GC_SOLARIS_THREADS)
  316. /*
  317. * Thread initialisation can call malloc before
  318. * we're ready for it.
  319. * It's not clear that this is enough to help matters.
  320. * The thread implementation may well call malloc at other
  321. * inopportune times.
  322. */
  323. if (!GC_is_initialized) return sbrk(lb);
  324. # endif /* I386 && GC_SOLARIS_THREADS */
  325. return((GC_PTR)REDIRECT_MALLOC(lb));
  326. }
  327. # ifdef __STDC__
  328. GC_PTR calloc(size_t n, size_t lb)
  329. # else
  330. GC_PTR calloc(n, lb)
  331. size_t n, lb;
  332. # endif
  333. {
  334. return((GC_PTR)REDIRECT_MALLOC(n*lb));
  335. }
  336. #ifndef strdup
  337. # include <string.h>
  338. # ifdef __STDC__
  339. char *strdup(const char *s)
  340. # else
  341. char *strdup(s)
  342. char *s;
  343. # endif
  344. {
  345. size_t len = strlen(s) + 1;
  346. char * result = ((char *)REDIRECT_MALLOC(len+1));
  347. BCOPY(s, result, len+1);
  348. return result;
  349. }
  350. #endif /* !defined(strdup) */
  351. /* If strdup is macro defined, we assume that it actually calls malloc, */
  352. /* and thus the right thing will happen even without overriding it. */
  353. /* This seems to be true on most Linux systems. */
  354. #undef GC_debug_malloc_replacement
  355. # endif /* REDIRECT_MALLOC */
  356. /* Explicitly deallocate an object p. */
  357. # ifdef __STDC__
  358. void GC_free(GC_PTR p)
  359. # else
  360. void GC_free(p)
  361. GC_PTR p;
  362. # endif
  363. {
  364. register struct hblk *h;
  365. register hdr *hhdr;
  366. register signed_word sz;
  367. register ptr_t * flh;
  368. register int knd;
  369. register struct obj_kind * ok;
  370. DCL_LOCK_STATE;
  371. if (p == 0) return;
  372. /* Required by ANSI. It's not my fault ... */
  373. h = HBLKPTR(p);
  374. hhdr = HDR(h);
  375. GC_ASSERT(GC_base(p) == p);
  376. # if defined(REDIRECT_MALLOC) && \
  377. (defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
  378. || defined(__MINGW32__)) /* Should this be MSWIN32 in general? */
  379. /* For Solaris, we have to redirect malloc calls during */
  380. /* initialization. For the others, this seems to happen */
  381. /* implicitly. */
  382. /* Don't try to deallocate that memory. */
  383. if (0 == hhdr) return;
  384. # endif
  385. knd = hhdr -> hb_obj_kind;
  386. sz = hhdr -> hb_sz;
  387. ok = &GC_obj_kinds[knd];
  388. if (EXPECT((sz <= MAXOBJSZ), 1)) {
  389. # ifdef THREADS
  390. DISABLE_SIGNALS();
  391. LOCK();
  392. # endif
  393. GC_mem_freed += sz;
  394. /* A signal here can make GC_mem_freed and GC_non_gc_bytes */
  395. /* inconsistent. We claim this is benign. */
  396. if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
  397. /* Its unnecessary to clear the mark bit. If the */
  398. /* object is reallocated, it doesn't matter. O.w. the */
  399. /* collector will do it, since it's on a free list. */
  400. if (ok -> ok_init) {
  401. BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
  402. }
  403. flh = &(ok -> ok_freelist[sz]);
  404. obj_link(p) = *flh;
  405. *flh = (ptr_t)p;
  406. # ifdef THREADS
  407. UNLOCK();
  408. ENABLE_SIGNALS();
  409. # endif
  410. } else {
  411. DISABLE_SIGNALS();
  412. LOCK();
  413. GC_mem_freed += sz;
  414. if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
  415. GC_freehblk(h);
  416. UNLOCK();
  417. ENABLE_SIGNALS();
  418. }
  419. }
  420. /* Explicitly deallocate an object p when we already hold lock. */
  421. /* Only used for internally allocated objects, so we can take some */
  422. /* shortcuts. */
  423. #ifdef THREADS
  424. void GC_free_inner(GC_PTR p)
  425. {
  426. register struct hblk *h;
  427. register hdr *hhdr;
  428. register signed_word sz;
  429. register ptr_t * flh;
  430. register int knd;
  431. register struct obj_kind * ok;
  432. DCL_LOCK_STATE;
  433. h = HBLKPTR(p);
  434. hhdr = HDR(h);
  435. knd = hhdr -> hb_obj_kind;
  436. sz = hhdr -> hb_sz;
  437. ok = &GC_obj_kinds[knd];
  438. if (sz <= MAXOBJSZ) {
  439. GC_mem_freed += sz;
  440. if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
  441. if (ok -> ok_init) {
  442. BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
  443. }
  444. flh = &(ok -> ok_freelist[sz]);
  445. obj_link(p) = *flh;
  446. *flh = (ptr_t)p;
  447. } else {
  448. GC_mem_freed += sz;
  449. if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
  450. GC_freehblk(h);
  451. }
  452. }
  453. #endif /* THREADS */
  454. # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
  455. # define REDIRECT_FREE GC_free
  456. # endif
  457. # ifdef REDIRECT_FREE
  458. # ifdef __STDC__
  459. void free(GC_PTR p)
  460. # else
  461. void free(p)
  462. GC_PTR p;
  463. # endif
  464. {
  465. # ifndef IGNORE_FREE
  466. REDIRECT_FREE(p);
  467. # endif
  468. }
  469. # endif /* REDIRECT_MALLOC */