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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
15 /* Boehm, February 7, 1996 4:32 pm PST */
22 #include "private/gc_priv.h"
24 extern ptr_t GC_clear_stack(); /* in misc.c, behaves like identity */
25 void GC_extend_size_map(); /* in misc.c. */
27 /* Allocate reclaim list for kind: */
28 /* Return TRUE on success */
29 GC_bool GC_alloc_reclaim_list(kind)
30 register struct obj_kind * kind;
32 struct hblk ** result = (struct hblk **)
33 GC_scratch_alloc((MAXOBJSZ+1) * sizeof(struct hblk *));
34 if (result == 0) return(FALSE);
35 BZERO(result, (MAXOBJSZ+1)*sizeof(struct hblk *));
36 kind -> ok_reclaim_list = result;
40 /* Allocate a large block of size lw words. */
41 /* The block is not cleared. */
42 /* Flags is 0 or IGNORE_OFF_PAGE. */
43 /* We hold the allocation lock. */
44 ptr_t GC_alloc_large(lw, k, flags)
50 word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
53 if (!GC_is_initialized) GC_init_inner();
54 /* Do our share of marking work */
55 if(GC_incremental && !GC_dont_gc)
56 GC_collect_a_little_inner((int)n_blocks);
57 h = GC_allochblk(lw, k, flags);
61 h = GC_allochblk(lw, k, flags);
64 while (0 == h && GC_collect_or_expand(n_blocks, (flags != 0))) {
65 h = GC_allochblk(lw, k, flags);
70 int total_bytes = n_blocks * HBLKSIZE;
72 GC_large_allocd_bytes += total_bytes;
73 if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
74 GC_max_large_allocd_bytes = GC_large_allocd_bytes;
76 result = (ptr_t) (h -> hb_body);
77 GC_words_wasted += BYTES_TO_WORDS(total_bytes) - lw;
83 /* Allocate a large block of size lb bytes. Clear if appropriate. */
84 /* We hold the allocation lock. */
85 ptr_t GC_alloc_large_and_clear(lw, k, flags)
90 ptr_t result = GC_alloc_large(lw, k, flags);
91 word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
93 if (0 == result) return 0;
94 if (GC_debugging_started || GC_obj_kinds[k].ok_init) {
95 /* Clear the whole block, in case of GC_realloc call. */
96 BZERO(result, n_blocks * HBLKSIZE);
101 /* allocate lb bytes for an object of kind k. */
102 /* Should not be used to directly to allocate */
103 /* objects such as STUBBORN objects that */
104 /* require special handling on allocation. */
105 /* First a version that assumes we already */
107 ptr_t GC_generic_malloc_inner(lb, k)
115 if( SMALL_OBJ(lb) ) {
116 register struct obj_kind * kind = GC_obj_kinds + k;
118 lw = GC_size_map[lb];
120 lw = ALIGNED_WORDS(lb);
121 if (lw == 0) lw = MIN_WORDS;
123 opp = &(kind -> ok_freelist[lw]);
124 if( (op = *opp) == 0 ) {
126 if (GC_size_map[lb] == 0) {
127 if (!GC_is_initialized) GC_init_inner();
128 if (GC_size_map[lb] == 0) GC_extend_size_map(lb);
129 return(GC_generic_malloc_inner(lb, k));
132 if (!GC_is_initialized) {
134 return(GC_generic_malloc_inner(lb, k));
137 if (kind -> ok_reclaim_list == 0) {
138 if (!GC_alloc_reclaim_list(kind)) goto out;
140 op = GC_allocobj(lw, k);
141 if (op == 0) goto out;
143 /* Here everything is in a consistent state. */
144 /* We assume the following assignment is */
145 /* atomic. If we get aborted */
146 /* after the assignment, we lose an object, */
147 /* but that's benign. */
148 /* Volatile declarations may need to be added */
149 /* to prevent the compiler from breaking things.*/
150 /* If we only execute the second of the */
151 /* following assignments, we lose the free */
152 /* list, but that should still be OK, at least */
153 /* for garbage collected memory. */
157 lw = ROUNDED_UP_WORDS(lb);
158 op = (ptr_t)GC_alloc_large_and_clear(lw, k, 0);
160 GC_words_allocd += lw;
166 /* Allocate a composite object of size n bytes. The caller guarantees */
167 /* that pointers past the first page are not relevant. Caller holds */
168 /* allocation lock. */
169 ptr_t GC_generic_malloc_inner_ignore_off_page(lb, k)
177 return(GC_generic_malloc_inner((word)lb, k));
178 lw = ROUNDED_UP_WORDS(lb);
179 op = (ptr_t)GC_alloc_large_and_clear(lw, k, IGNORE_OFF_PAGE);
180 GC_words_allocd += lw;
184 ptr_t GC_generic_malloc(lb, k)
191 if (GC_have_errors) GC_print_all_errors();
192 GC_INVOKE_FINALIZERS();
196 result = GC_generic_malloc_inner((word)lb, k);
203 lw = ROUNDED_UP_WORDS(lb);
204 n_blocks = OBJ_SZ_TO_BLOCKS(lw);
205 init = GC_obj_kinds[k].ok_init;
208 result = (ptr_t)GC_alloc_large(lw, k, 0);
210 if (GC_debugging_started) {
211 BZERO(result, n_blocks * HBLKSIZE);
214 /* Clear any memory that might be used for GC descriptors */
215 /* before we release the lock. */
216 ((word *)result)[0] = 0;
217 ((word *)result)[1] = 0;
218 ((word *)result)[lw-1] = 0;
219 ((word *)result)[lw-2] = 0;
223 GC_words_allocd += lw;
226 if (init && !GC_debugging_started && 0 != result) {
227 BZERO(result, n_blocks * HBLKSIZE);
231 return((*GC_oom_fn)(lb));
238 #define GENERAL_MALLOC(lb,k) \
239 (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
240 /* We make the GC_clear_stack_call a tail call, hoping to get more of */
243 /* Allocate lb bytes of atomic (pointerfree) data */
245 GC_PTR GC_malloc_atomic(size_t lb)
247 GC_PTR GC_malloc_atomic(lb)
252 register ptr_t * opp;
256 if( EXPECT(SMALL_OBJ(lb), 1) ) {
258 lw = GC_size_map[lb];
260 lw = ALIGNED_WORDS(lb);
262 opp = &(GC_aobjfreelist[lw]);
264 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
266 return(GENERAL_MALLOC((word)lb, PTRFREE));
268 /* See above comment on signals. */
270 GC_words_allocd += lw;
274 return(GENERAL_MALLOC((word)lb, PTRFREE));
278 /* provide a version of strdup() that uses the collector to allocate the
279 copy of the string */
281 char *GC_strdup(const char *s)
289 if (s == NULL) return NULL;
290 if ((copy = GC_malloc_atomic(strlen(s) + 1)) == NULL) {
298 /* Allocate lb bytes of composite (pointerful) data */
300 GC_PTR GC_malloc(size_t lb)
311 if( EXPECT(SMALL_OBJ(lb), 1) ) {
313 lw = GC_size_map[lb];
315 lw = ALIGNED_WORDS(lb);
317 opp = &(GC_objfreelist[lw]);
319 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
321 return(GENERAL_MALLOC((word)lb, NORMAL));
323 /* See above comment on signals. */
324 GC_ASSERT(0 == obj_link(op)
325 || (word)obj_link(op)
326 <= (word)GC_greatest_plausible_heap_addr
327 && (word)obj_link(op)
328 >= (word)GC_least_plausible_heap_addr);
331 GC_words_allocd += lw;
335 return(GENERAL_MALLOC((word)lb, NORMAL));
339 # ifdef REDIRECT_MALLOC
341 /* Avoid unnecessary nested procedure calls here, by #defining some */
342 /* malloc replacements. Otherwise we end up saving a */
343 /* meaningless return address in the object. It also speeds things up, */
344 /* but it is admittedly quite ugly. */
345 # ifdef GC_ADD_CALLER
346 # define RA GC_RETURN_ADDR,
350 # define GC_debug_malloc_replacement(lb) \
351 GC_debug_malloc(lb, RA "unknown", 0)
354 GC_PTR malloc(size_t lb)
360 # if defined(GC_WIN32_THREADS) && defined(__GNUC__)
361 /* According to Gerard Allen, this helps with MINGW. */
362 /* When using threads need to initalised before use, but GCC uses a malloc
363 in __w32_sharedptr_initialize (w32-shared-ptr.c) */
364 if (!GC_is_initialized) GC_init();
366 /* It might help to manually inline the GC_malloc call here. */
367 /* But any decent compiler should reduce the extra procedure call */
368 /* to at most a jump instruction in this case. */
369 # if defined(I386) && defined(GC_SOLARIS_THREADS)
371 * Thread initialisation can call malloc before
372 * we're ready for it.
373 * It's not clear that this is enough to help matters.
374 * The thread implementation may well call malloc at other
377 if (!GC_is_initialized) return sbrk(lb);
378 # endif /* I386 && GC_SOLARIS_THREADS */
379 return((GC_PTR)REDIRECT_MALLOC(lb));
383 GC_PTR calloc(size_t n, size_t lb)
389 return((GC_PTR)REDIRECT_MALLOC(n*lb));
395 char *strdup(const char *s)
401 size_t len = strlen(s) + 1;
402 char * result = ((char *)REDIRECT_MALLOC(len+1));
407 BCOPY(s, result, len+1);
410 #endif /* !defined(strdup) */
411 /* If strdup is macro defined, we assume that it actually calls malloc, */
412 /* and thus the right thing will happen even without overriding it. */
413 /* This seems to be true on most Linux systems. */
415 #undef GC_debug_malloc_replacement
417 # endif /* REDIRECT_MALLOC */
419 /* Explicitly deallocate an object p. */
421 void GC_free(GC_PTR p)
427 register struct hblk *h;
429 register signed_word sz;
430 register ptr_t * flh;
432 register struct obj_kind * ok;
436 /* Required by ANSI. It's not my fault ... */
439 GC_ASSERT(GC_base(p) == p);
440 # if defined(REDIRECT_MALLOC) && \
441 (defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
442 || defined(__MINGW32__)) /* Should this be MSWIN32 in general? */
443 /* For Solaris, we have to redirect malloc calls during */
444 /* initialization. For the others, this seems to happen */
446 /* Don't try to deallocate that memory. */
447 if (0 == hhdr) return;
449 knd = hhdr -> hb_obj_kind;
451 ok = &GC_obj_kinds[knd];
452 if (EXPECT((sz <= MAXOBJSZ), 1)) {
458 /* A signal here can make GC_mem_freed and GC_non_gc_bytes */
459 /* inconsistent. We claim this is benign. */
460 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
461 /* Its unnecessary to clear the mark bit. If the */
462 /* object is reallocated, it doesn't matter. O.w. the */
463 /* collector will do it, since it's on a free list. */
465 BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
467 flh = &(ok -> ok_freelist[sz]);
478 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
485 /* Explicitly deallocate an object p when we already hold lock. */
486 /* Only used for internally allocated objects, so we can take some */
489 void GC_free_inner(GC_PTR p)
491 register struct hblk *h;
493 register signed_word sz;
494 register ptr_t * flh;
496 register struct obj_kind * ok;
501 knd = hhdr -> hb_obj_kind;
503 ok = &GC_obj_kinds[knd];
504 if (sz <= MAXOBJSZ) {
506 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
508 BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
510 flh = &(ok -> ok_freelist[sz]);
515 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
521 # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
522 # define REDIRECT_FREE GC_free
524 # ifdef REDIRECT_FREE
536 # endif /* REDIRECT_MALLOC */