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 */
18 #include "private/gc_priv.h"
20 extern ptr_t
GC_clear_stack(); /* in misc.c, behaves like identity */
21 void GC_extend_size_map(); /* in misc.c. */
23 /* Allocate reclaim list for kind: */
24 /* Return TRUE on success */
25 GC_bool
GC_alloc_reclaim_list(kind
)
26 register struct obj_kind
* kind
;
28 struct hblk
** result
= (struct hblk
**)
29 GC_scratch_alloc((MAXOBJSZ
+1) * sizeof(struct hblk
*));
30 if (result
== 0) return(FALSE
);
31 BZERO(result
, (MAXOBJSZ
+1)*sizeof(struct hblk
*));
32 kind
-> ok_reclaim_list
= result
;
36 /* Allocate a large block of size lw words. */
37 /* The block is not cleared. */
38 /* Flags is 0 or IGNORE_OFF_PAGE. */
39 /* We hold the allocation lock. */
40 ptr_t
GC_alloc_large(lw
, k
, flags
)
46 word n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
49 if (!GC_is_initialized
) GC_init_inner();
50 /* Do our share of marking work */
51 if(GC_incremental
&& !GC_dont_gc
)
52 GC_collect_a_little_inner((int)n_blocks
);
53 h
= GC_allochblk(lw
, k
, flags
);
57 h
= GC_allochblk(lw
, k
, flags
);
60 while (0 == h
&& GC_collect_or_expand(n_blocks
, (flags
!= 0))) {
61 h
= GC_allochblk(lw
, k
, flags
);
66 int total_bytes
= n_blocks
* HBLKSIZE
;
68 GC_large_allocd_bytes
+= total_bytes
;
69 if (GC_large_allocd_bytes
> GC_max_large_allocd_bytes
)
70 GC_max_large_allocd_bytes
= GC_large_allocd_bytes
;
72 result
= (ptr_t
) (h
-> hb_body
);
73 GC_words_wasted
+= BYTES_TO_WORDS(total_bytes
) - lw
;
79 /* Allocate a large block of size lb bytes. Clear if appropriate. */
80 /* We hold the allocation lock. */
81 ptr_t
GC_alloc_large_and_clear(lw
, k
, flags
)
86 ptr_t result
= GC_alloc_large(lw
, k
, flags
);
87 word n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
89 if (0 == result
) return 0;
90 if (GC_debugging_started
|| GC_obj_kinds
[k
].ok_init
) {
91 /* Clear the whole block, in case of GC_realloc call. */
92 BZERO(result
, n_blocks
* HBLKSIZE
);
97 /* allocate lb bytes for an object of kind k. */
98 /* Should not be used to directly to allocate */
99 /* objects such as STUBBORN objects that */
100 /* require special handling on allocation. */
101 /* First a version that assumes we already */
103 ptr_t
GC_generic_malloc_inner(lb
, k
)
111 if( SMALL_OBJ(lb
) ) {
112 register struct obj_kind
* kind
= GC_obj_kinds
+ k
;
114 lw
= GC_size_map
[lb
];
116 lw
= ALIGNED_WORDS(lb
);
117 if (lw
== 0) lw
= MIN_WORDS
;
119 opp
= &(kind
-> ok_freelist
[lw
]);
120 if( (op
= *opp
) == 0 ) {
122 if (GC_size_map
[lb
] == 0) {
123 if (!GC_is_initialized
) GC_init_inner();
124 if (GC_size_map
[lb
] == 0) GC_extend_size_map(lb
);
125 return(GC_generic_malloc_inner(lb
, k
));
128 if (!GC_is_initialized
) {
130 return(GC_generic_malloc_inner(lb
, k
));
133 if (kind
-> ok_reclaim_list
== 0) {
134 if (!GC_alloc_reclaim_list(kind
)) goto out
;
136 op
= GC_allocobj(lw
, k
);
137 if (op
== 0) goto out
;
139 /* Here everything is in a consistent state. */
140 /* We assume the following assignment is */
141 /* atomic. If we get aborted */
142 /* after the assignment, we lose an object, */
143 /* but that's benign. */
144 /* Volatile declarations may need to be added */
145 /* to prevent the compiler from breaking things.*/
146 /* If we only execute the second of the */
147 /* following assignments, we lose the free */
148 /* list, but that should still be OK, at least */
149 /* for garbage collected memory. */
153 lw
= ROUNDED_UP_WORDS(lb
);
154 op
= (ptr_t
)GC_alloc_large_and_clear(lw
, k
, 0);
156 GC_words_allocd
+= lw
;
162 /* Allocate a composite object of size n bytes. The caller guarantees */
163 /* that pointers past the first page are not relevant. Caller holds */
164 /* allocation lock. */
165 ptr_t
GC_generic_malloc_inner_ignore_off_page(lb
, k
)
173 return(GC_generic_malloc_inner((word
)lb
, k
));
174 lw
= ROUNDED_UP_WORDS(lb
);
175 op
= (ptr_t
)GC_alloc_large_and_clear(lw
, k
, IGNORE_OFF_PAGE
);
176 GC_words_allocd
+= lw
;
180 ptr_t
GC_generic_malloc(lb
, k
)
187 if (GC_have_errors
) GC_print_all_errors();
188 GC_INVOKE_FINALIZERS();
192 result
= GC_generic_malloc_inner((word
)lb
, k
);
199 lw
= ROUNDED_UP_WORDS(lb
);
200 n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
201 init
= GC_obj_kinds
[k
].ok_init
;
204 result
= (ptr_t
)GC_alloc_large(lw
, k
, 0);
206 if (GC_debugging_started
) {
207 BZERO(result
, n_blocks
* HBLKSIZE
);
210 /* Clear any memory that might be used for GC descriptors */
211 /* before we release the lock. */
212 ((word
*)result
)[0] = 0;
213 ((word
*)result
)[1] = 0;
214 ((word
*)result
)[lw
-1] = 0;
215 ((word
*)result
)[lw
-2] = 0;
219 GC_words_allocd
+= lw
;
222 if (init
&& !GC_debugging_started
&& 0 != result
) {
223 BZERO(result
, n_blocks
* HBLKSIZE
);
227 return((*GC_oom_fn
)(lb
));
234 #define GENERAL_MALLOC(lb,k) \
235 (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
236 /* We make the GC_clear_stack_call a tail call, hoping to get more of */
239 /* Allocate lb bytes of atomic (pointerfree) data */
241 GC_PTR
GC_malloc_atomic(size_t lb
)
243 GC_PTR
GC_malloc_atomic(lb
)
248 register ptr_t
* opp
;
252 if( EXPECT(SMALL_OBJ(lb
), 1) ) {
254 lw
= GC_size_map
[lb
];
256 lw
= ALIGNED_WORDS(lb
);
258 opp
= &(GC_aobjfreelist
[lw
]);
260 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op
= *opp
) == 0, 0) ) {
262 return(GENERAL_MALLOC((word
)lb
, PTRFREE
));
264 /* See above comment on signals. */
266 GC_words_allocd
+= lw
;
270 return(GENERAL_MALLOC((word
)lb
, PTRFREE
));
274 /* Allocate lb bytes of composite (pointerful) data */
276 GC_PTR
GC_malloc(size_t lb
)
287 if( EXPECT(SMALL_OBJ(lb
), 1) ) {
289 lw
= GC_size_map
[lb
];
291 lw
= ALIGNED_WORDS(lb
);
293 opp
= &(GC_objfreelist
[lw
]);
295 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op
= *opp
) == 0, 0) ) {
297 return(GENERAL_MALLOC((word
)lb
, NORMAL
));
299 /* See above comment on signals. */
300 GC_ASSERT(0 == obj_link(op
)
301 || (word
)obj_link(op
)
302 <= (word
)GC_greatest_plausible_heap_addr
303 && (word
)obj_link(op
)
304 >= (word
)GC_least_plausible_heap_addr
);
307 GC_words_allocd
+= lw
;
311 return(GENERAL_MALLOC((word
)lb
, NORMAL
));
315 # ifdef REDIRECT_MALLOC
317 /* Avoid unnecessary nested procedure calls here, by #defining some */
318 /* malloc replacements. Otherwise we end up saving a */
319 /* meaningless return address in the object. It also speeds things up, */
320 /* but it is admittedly quite ugly. */
321 # ifdef GC_ADD_CALLER
322 # define RA GC_RETURN_ADDR,
326 # define GC_debug_malloc_replacement(lb) \
327 GC_debug_malloc(lb, RA "unknown", 0)
330 GC_PTR
malloc(size_t lb
)
336 /* It might help to manually inline the GC_malloc call here. */
337 /* But any decent compiler should reduce the extra procedure call */
338 /* to at most a jump instruction in this case. */
339 # if defined(I386) && defined(GC_SOLARIS_THREADS)
341 * Thread initialisation can call malloc before
342 * we're ready for it.
343 * It's not clear that this is enough to help matters.
344 * The thread implementation may well call malloc at other
347 if (!GC_is_initialized
) return sbrk(lb
);
348 # endif /* I386 && GC_SOLARIS_THREADS */
349 return((GC_PTR
)REDIRECT_MALLOC(lb
));
353 GC_PTR
calloc(size_t n
, size_t lb
)
359 return((GC_PTR
)REDIRECT_MALLOC(n
*lb
));
365 char *strdup(const char *s
)
371 size_t len
= strlen(s
) + 1;
372 char * result
= ((char *)REDIRECT_MALLOC(len
+1));
373 BCOPY(s
, result
, len
+1);
376 #endif /* !defined(strdup) */
377 /* If strdup is macro defined, we assume that it actually calls malloc, */
378 /* and thus the right thing will happen even without overriding it. */
379 /* This seems to be true on most Linux systems. */
381 #undef GC_debug_malloc_replacement
383 # endif /* REDIRECT_MALLOC */
385 /* Explicitly deallocate an object p. */
387 void GC_free(GC_PTR p
)
393 register struct hblk
*h
;
395 register signed_word sz
;
396 register ptr_t
* flh
;
398 register struct obj_kind
* ok
;
402 /* Required by ANSI. It's not my fault ... */
405 GC_ASSERT(GC_base(p
) == p
);
406 # if defined(REDIRECT_MALLOC) && \
407 (defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
408 || defined(__MINGW32__)) /* Should this be MSWIN32 in general? */
409 /* For Solaris, we have to redirect malloc calls during */
410 /* initialization. For the others, this seems to happen */
412 /* Don't try to deallocate that memory. */
413 if (0 == hhdr
) return;
415 knd
= hhdr
-> hb_obj_kind
;
417 ok
= &GC_obj_kinds
[knd
];
418 if (EXPECT((sz
<= MAXOBJSZ
), 1)) {
424 /* A signal here can make GC_mem_freed and GC_non_gc_bytes */
425 /* inconsistent. We claim this is benign. */
426 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
427 /* Its unnecessary to clear the mark bit. If the */
428 /* object is reallocated, it doesn't matter. O.w. the */
429 /* collector will do it, since it's on a free list. */
431 BZERO((word
*)p
+ 1, WORDS_TO_BYTES(sz
-1));
433 flh
= &(ok
-> ok_freelist
[sz
]);
444 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
451 /* Explicitly deallocate an object p when we already hold lock. */
452 /* Only used for internally allocated objects, so we can take some */
455 void GC_free_inner(GC_PTR p
)
457 register struct hblk
*h
;
459 register signed_word sz
;
460 register ptr_t
* flh
;
462 register struct obj_kind
* ok
;
467 knd
= hhdr
-> hb_obj_kind
;
469 ok
= &GC_obj_kinds
[knd
];
470 if (sz
<= MAXOBJSZ
) {
472 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
474 BZERO((word
*)p
+ 1, WORDS_TO_BYTES(sz
-1));
476 flh
= &(ok
-> ok_freelist
[sz
]);
481 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
487 # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
488 # define REDIRECT_FREE GC_free
490 # ifdef REDIRECT_FREE
502 # endif /* REDIRECT_MALLOC */