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 */
20 #include "private/gc_priv.h"
22 extern ptr_t GC_clear_stack(); /* in misc.c, behaves like identity */
23 void GC_extend_size_map(); /* in misc.c. */
25 /* Allocate reclaim list for kind: */
26 /* Return TRUE on success */
27 GC_bool GC_alloc_reclaim_list(kind)
28 register struct obj_kind * kind;
30 struct hblk ** result = (struct hblk **)
31 GC_scratch_alloc((MAXOBJSZ+1) * sizeof(struct hblk *));
32 if (result == 0) return(FALSE);
33 BZERO(result, (MAXOBJSZ+1)*sizeof(struct hblk *));
34 kind -> ok_reclaim_list = result;
38 /* Allocate a large block of size lw words. */
39 /* The block is not cleared. */
40 /* Flags is 0 or IGNORE_OFF_PAGE. */
41 /* We hold the allocation lock. */
42 ptr_t GC_alloc_large(lw, k, flags)
48 word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
51 if (!GC_is_initialized) GC_init_inner();
52 /* Do our share of marking work */
53 if(GC_incremental && !GC_dont_gc)
54 GC_collect_a_little_inner((int)n_blocks);
55 h = GC_allochblk(lw, k, flags);
59 h = GC_allochblk(lw, k, flags);
62 while (0 == h && GC_collect_or_expand(n_blocks, (flags != 0))) {
63 h = GC_allochblk(lw, k, flags);
68 int total_bytes = n_blocks * HBLKSIZE;
70 GC_large_allocd_bytes += total_bytes;
71 if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
72 GC_max_large_allocd_bytes = GC_large_allocd_bytes;
74 result = (ptr_t) (h -> hb_body);
75 GC_words_wasted += BYTES_TO_WORDS(total_bytes) - lw;
81 /* Allocate a large block of size lb bytes. Clear if appropriate. */
82 /* We hold the allocation lock. */
83 ptr_t GC_alloc_large_and_clear(lw, k, flags)
88 ptr_t result = GC_alloc_large(lw, k, flags);
89 word n_blocks = OBJ_SZ_TO_BLOCKS(lw);
91 if (0 == result) return 0;
92 if (GC_debugging_started || GC_obj_kinds[k].ok_init) {
93 /* Clear the whole block, in case of GC_realloc call. */
94 BZERO(result, n_blocks * HBLKSIZE);
99 /* allocate lb bytes for an object of kind k. */
100 /* Should not be used to directly to allocate */
101 /* objects such as STUBBORN objects that */
102 /* require special handling on allocation. */
103 /* First a version that assumes we already */
105 ptr_t GC_generic_malloc_inner(lb, k)
113 if( SMALL_OBJ(lb) ) {
114 register struct obj_kind * kind = GC_obj_kinds + k;
116 lw = GC_size_map[lb];
118 lw = ALIGNED_WORDS(lb);
119 if (lw == 0) lw = MIN_WORDS;
121 opp = &(kind -> ok_freelist[lw]);
122 if( (op = *opp) == 0 ) {
124 if (GC_size_map[lb] == 0) {
125 if (!GC_is_initialized) GC_init_inner();
126 if (GC_size_map[lb] == 0) GC_extend_size_map(lb);
127 return(GC_generic_malloc_inner(lb, k));
130 if (!GC_is_initialized) {
132 return(GC_generic_malloc_inner(lb, k));
135 if (kind -> ok_reclaim_list == 0) {
136 if (!GC_alloc_reclaim_list(kind)) goto out;
138 op = GC_allocobj(lw, k);
139 if (op == 0) goto out;
141 /* Here everything is in a consistent state. */
142 /* We assume the following assignment is */
143 /* atomic. If we get aborted */
144 /* after the assignment, we lose an object, */
145 /* but that's benign. */
146 /* Volatile declarations may need to be added */
147 /* to prevent the compiler from breaking things.*/
148 /* If we only execute the second of the */
149 /* following assignments, we lose the free */
150 /* list, but that should still be OK, at least */
151 /* for garbage collected memory. */
155 lw = ROUNDED_UP_WORDS(lb);
156 op = (ptr_t)GC_alloc_large_and_clear(lw, k, 0);
158 GC_words_allocd += lw;
164 /* Allocate a composite object of size n bytes. The caller guarantees */
165 /* that pointers past the first page are not relevant. Caller holds */
166 /* allocation lock. */
167 ptr_t GC_generic_malloc_inner_ignore_off_page(lb, k)
175 return(GC_generic_malloc_inner((word)lb, k));
176 lw = ROUNDED_UP_WORDS(lb);
177 op = (ptr_t)GC_alloc_large_and_clear(lw, k, IGNORE_OFF_PAGE);
178 GC_words_allocd += lw;
182 ptr_t GC_generic_malloc(lb, k)
189 if (GC_have_errors) GC_print_all_errors();
190 GC_INVOKE_FINALIZERS();
194 result = GC_generic_malloc_inner((word)lb, k);
201 lw = ROUNDED_UP_WORDS(lb);
202 n_blocks = OBJ_SZ_TO_BLOCKS(lw);
203 init = GC_obj_kinds[k].ok_init;
206 result = (ptr_t)GC_alloc_large(lw, k, 0);
208 if (GC_debugging_started) {
209 BZERO(result, n_blocks * HBLKSIZE);
212 /* Clear any memory that might be used for GC descriptors */
213 /* before we release the lock. */
214 ((word *)result)[0] = 0;
215 ((word *)result)[1] = 0;
216 ((word *)result)[lw-1] = 0;
217 ((word *)result)[lw-2] = 0;
221 GC_words_allocd += lw;
224 if (init && !GC_debugging_started && 0 != result) {
225 BZERO(result, n_blocks * HBLKSIZE);
229 return((*GC_oom_fn)(lb));
236 #define GENERAL_MALLOC(lb,k) \
237 (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
238 /* We make the GC_clear_stack_call a tail call, hoping to get more of */
241 /* Allocate lb bytes of atomic (pointerfree) data */
243 GC_PTR GC_malloc_atomic(size_t lb)
245 GC_PTR GC_malloc_atomic(lb)
250 register ptr_t * opp;
254 if( EXPECT(SMALL_OBJ(lb), 1) ) {
256 lw = GC_size_map[lb];
258 lw = ALIGNED_WORDS(lb);
260 opp = &(GC_aobjfreelist[lw]);
262 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
264 return(GENERAL_MALLOC((word)lb, PTRFREE));
266 /* See above comment on signals. */
268 GC_words_allocd += lw;
272 return(GENERAL_MALLOC((word)lb, PTRFREE));
276 /* Allocate lb bytes of composite (pointerful) data */
278 GC_PTR GC_malloc(size_t lb)
289 if( EXPECT(SMALL_OBJ(lb), 1) ) {
291 lw = GC_size_map[lb];
293 lw = ALIGNED_WORDS(lb);
295 opp = &(GC_objfreelist[lw]);
297 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op = *opp) == 0, 0) ) {
299 return(GENERAL_MALLOC((word)lb, NORMAL));
301 /* See above comment on signals. */
302 GC_ASSERT(0 == obj_link(op)
303 || (word)obj_link(op)
304 <= (word)GC_greatest_plausible_heap_addr
305 && (word)obj_link(op)
306 >= (word)GC_least_plausible_heap_addr);
309 GC_words_allocd += lw;
313 return(GENERAL_MALLOC((word)lb, NORMAL));
317 # ifdef REDIRECT_MALLOC
319 /* Avoid unnecessary nested procedure calls here, by #defining some */
320 /* malloc replacements. Otherwise we end up saving a */
321 /* meaningless return address in the object. It also speeds things up, */
322 /* but it is admittedly quite ugly. */
323 # ifdef GC_ADD_CALLER
324 # define RA GC_RETURN_ADDR,
328 # define GC_debug_malloc_replacement(lb) \
329 GC_debug_malloc(lb, RA "unknown", 0)
332 GC_PTR malloc(size_t lb)
338 /* It might help to manually inline the GC_malloc call here. */
339 /* But any decent compiler should reduce the extra procedure call */
340 /* to at most a jump instruction in this case. */
341 # if defined(I386) && defined(GC_SOLARIS_THREADS)
343 * Thread initialisation can call malloc before
344 * we're ready for it.
345 * It's not clear that this is enough to help matters.
346 * The thread implementation may well call malloc at other
349 if (!GC_is_initialized) return sbrk(lb);
350 # endif /* I386 && GC_SOLARIS_THREADS */
351 return((GC_PTR)REDIRECT_MALLOC(lb));
355 GC_PTR calloc(size_t n, size_t lb)
361 return((GC_PTR)REDIRECT_MALLOC(n*lb));
367 char *strdup(const char *s)
373 size_t len = strlen(s) + 1;
374 char * result = ((char *)REDIRECT_MALLOC(len+1));
375 BCOPY(s, result, len+1);
378 #endif /* !defined(strdup) */
379 /* If strdup is macro defined, we assume that it actually calls malloc, */
380 /* and thus the right thing will happen even without overriding it. */
381 /* This seems to be true on most Linux systems. */
383 #undef GC_debug_malloc_replacement
385 # endif /* REDIRECT_MALLOC */
387 /* Explicitly deallocate an object p. */
389 void GC_free(GC_PTR p)
395 register struct hblk *h;
397 register signed_word sz;
398 register ptr_t * flh;
400 register struct obj_kind * ok;
404 /* Required by ANSI. It's not my fault ... */
407 GC_ASSERT(GC_base(p) == p);
408 # if defined(REDIRECT_MALLOC) && \
409 (defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
410 || defined(__MINGW32__)) /* Should this be MSWIN32 in general? */
411 /* For Solaris, we have to redirect malloc calls during */
412 /* initialization. For the others, this seems to happen */
414 /* Don't try to deallocate that memory. */
415 if (0 == hhdr) return;
417 knd = hhdr -> hb_obj_kind;
419 ok = &GC_obj_kinds[knd];
420 if (EXPECT((sz <= MAXOBJSZ), 1)) {
426 /* A signal here can make GC_mem_freed and GC_non_gc_bytes */
427 /* inconsistent. We claim this is benign. */
428 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
429 /* Its unnecessary to clear the mark bit. If the */
430 /* object is reallocated, it doesn't matter. O.w. the */
431 /* collector will do it, since it's on a free list. */
433 BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
435 flh = &(ok -> ok_freelist[sz]);
446 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
453 /* Explicitly deallocate an object p when we already hold lock. */
454 /* Only used for internally allocated objects, so we can take some */
457 void GC_free_inner(GC_PTR p)
459 register struct hblk *h;
461 register signed_word sz;
462 register ptr_t * flh;
464 register struct obj_kind * ok;
469 knd = hhdr -> hb_obj_kind;
471 ok = &GC_obj_kinds[knd];
472 if (sz <= MAXOBJSZ) {
474 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
476 BZERO((word *)p + 1, WORDS_TO_BYTES(sz-1));
478 flh = &(ok -> ok_freelist[sz]);
483 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= WORDS_TO_BYTES(sz);
489 # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
490 # define REDIRECT_FREE GC_free
492 # ifdef REDIRECT_FREE
504 # endif /* REDIRECT_MALLOC */