2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996 by Silicon Graphics. 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.
17 * These are extra allocation routines which are likely to be less
18 * frequently used than those in malloc.c. They are separate in the
19 * hope that the .o file will be excluded from statically linked
20 * executables. We should probably break this up further.
26 extern ptr_t GC_clear_stack(); /* in misc.c, behaves like identity */
27 void GC_extend_size_map(); /* in misc.c. */
28 GC_bool GC_alloc_reclaim_list(); /* in malloc.c */
30 /* Some externally visible but unadvertised variables to allow access to */
31 /* free lists from inlined allocators without including gc_priv.h */
32 /* or introducing dependencies on internal data structure layouts. */
33 ptr_t * CONST GC_objfreelist_ptr = GC_objfreelist;
34 ptr_t * CONST GC_aobjfreelist_ptr = GC_aobjfreelist;
35 ptr_t * CONST GC_uobjfreelist_ptr = GC_uobjfreelist;
36 # ifdef ATOMIC_UNCOLLECTABLE
37 ptr_t * CONST GC_auobjfreelist_ptr = GC_auobjfreelist;
40 /* Allocate a composite object of size n bytes. The caller guarantees */
41 /* that pointers past the first page are not relevant. Caller holds */
42 /* allocation lock. */
43 ptr_t GC_generic_malloc_inner_ignore_off_page(lb, k)
47 register struct hblk * h;
48 register word n_blocks;
53 return(GC_generic_malloc_inner((word)lb, k));
54 n_blocks = divHBLKSZ(ADD_SLOP(lb) + HDR_BYTES + HBLKSIZE-1);
55 if (!GC_is_initialized) GC_init_inner();
56 /* Do our share of marking work */
57 if(GC_incremental && !GC_dont_gc)
58 GC_collect_a_little_inner((int)n_blocks);
59 lw = ROUNDED_UP_WORDS(lb);
60 while ((h = GC_allochblk(lw, k, IGNORE_OFF_PAGE)) == 0
61 && GC_collect_or_expand(n_blocks, TRUE));
65 op = (ptr_t) (h -> hb_body);
66 GC_words_wasted += BYTES_TO_WORDS(n_blocks * HBLKSIZE) - lw;
68 GC_words_allocd += lw;
72 ptr_t GC_generic_malloc_ignore_off_page(lb, k)
76 register ptr_t result;
79 GC_INVOKE_FINALIZERS();
82 result = GC_generic_malloc_inner_ignore_off_page(lb,k);
86 return((*GC_oom_fn)(lb));
92 # if defined(__STDC__) || defined(__cplusplus)
93 void * GC_malloc_ignore_off_page(size_t lb)
95 char * GC_malloc_ignore_off_page(lb)
99 return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, NORMAL));
102 # if defined(__STDC__) || defined(__cplusplus)
103 void * GC_malloc_atomic_ignore_off_page(size_t lb)
105 char * GC_malloc_atomic_ignore_off_page(lb)
109 return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
112 /* Increment GC_words_allocd from code that doesn't have direct access */
115 void GC_incr_words_allocd(size_t n)
117 GC_words_allocd += n;
120 /* The same for GC_mem_freed. */
121 void GC_incr_mem_freed(size_t n)
125 # endif /* __STDC__ */
127 /* Analogous to the above, but assumes a small object size, and */
128 /* bypasses MERGE_SIZES mechanism. Used by gc_inline.h. */
130 ptr_t GC_generic_malloc_words_small(size_t lw, int k)
132 ptr_t GC_generic_malloc_words_small(lw, k)
139 register struct obj_kind * kind = GC_obj_kinds + k;
142 GC_INVOKE_FINALIZERS();
145 opp = &(kind -> ok_freelist[lw]);
146 if( (op = *opp) == 0 ) {
147 if (!GC_is_initialized) {
150 if (kind -> ok_reclaim_list != 0 || GC_alloc_reclaim_list(kind)) {
151 op = GC_clear_stack(GC_allocobj(lw, k));
156 return ((*GC_oom_fn)(WORDS_TO_BYTES(lw)));
161 GC_words_allocd += lw;
167 #if defined(THREADS) && !defined(SRC_M3)
168 /* Return a list of 1 or more objects of the indicated size, linked */
169 /* through the first word in the object. This has the advantage that */
170 /* it acquires the allocation lock only once, and may greatly reduce */
171 /* time wasted contending for the allocation lock. Typical usage would */
172 /* be in a thread that requires many items of the same size. It would */
173 /* keep its own free list in thread-local storage, and call */
174 /* GC_malloc_many or friends to replenish it. (We do not round up */
175 /* object sizes, since a call indicates the intention to consume many */
176 /* objects of exactly this size.) */
177 /* Note that the client should usually clear the link field. */
178 ptr_t GC_generic_malloc_many(lb, k)
186 register word my_words_allocd;
189 if (!SMALL_OBJ(lb)) {
190 op = GC_generic_malloc(lb, k);
191 if(0 != op) obj_link(op) = 0;
194 lw = ALIGNED_WORDS(lb);
195 GC_INVOKE_FINALIZERS();
198 opp = &(GC_obj_kinds[k].ok_freelist[lw]);
199 if( (op = *opp) == 0 ) {
200 if (!GC_is_initialized) {
203 op = GC_clear_stack(GC_allocobj(lw, k));
207 op = (*GC_oom_fn)(lb);
208 if(0 != op) obj_link(op) = 0;
214 for (p = op; p != 0; p = obj_link(p)) {
215 my_words_allocd += lw;
216 if (my_words_allocd >= BODY_SZ) {
222 GC_words_allocd += my_words_allocd;
231 void * GC_malloc_many(size_t lb)
233 return(GC_generic_malloc_many(lb, NORMAL));
236 /* Note that the "atomic" version of this would be unsafe, since the */
237 /* links would not be seen by the collector. */
240 /* Allocate lb bytes of pointerful, traced, but not collectable data */
242 GC_PTR GC_malloc_uncollectable(size_t lb)
244 GC_PTR GC_malloc_uncollectable(lb)
253 if( SMALL_OBJ(lb) ) {
255 # ifdef ADD_BYTE_AT_END
257 /* We don't need the extra byte, since this won't be */
258 /* collected anyway. */
260 lw = GC_size_map[lb];
262 lw = ALIGNED_WORDS(lb);
264 opp = &(GC_uobjfreelist[lw]);
266 if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
267 /* See above comment on signals. */
270 GC_words_allocd += lw;
271 /* Mark bit ws already set on free list. It will be */
272 /* cleared only temporarily during a collection, as a */
273 /* result of the normal free list mark bit clearing. */
274 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
279 op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
281 op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
283 if (0 == op) return(0);
284 /* We don't need the lock here, since we have an undisguised */
285 /* pointer. We do need to hold the lock while we adjust */
288 register struct hblk * h;
291 lw = HDR(h) -> hb_sz;
296 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
303 # ifdef ATOMIC_UNCOLLECTABLE
304 /* Allocate lb bytes of pointerfree, untraced, uncollectable data */
305 /* This is normally roughly equivalent to the system malloc. */
306 /* But it may be useful if malloc is redefined. */
308 GC_PTR GC_malloc_atomic_uncollectable(size_t lb)
310 GC_PTR GC_malloc_atomic_uncollectable(lb)
319 if( SMALL_OBJ(lb) ) {
321 # ifdef ADD_BYTE_AT_END
323 /* We don't need the extra byte, since this won't be */
324 /* collected anyway. */
326 lw = GC_size_map[lb];
328 lw = ALIGNED_WORDS(lb);
330 opp = &(GC_auobjfreelist[lw]);
332 if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
333 /* See above comment on signals. */
336 GC_words_allocd += lw;
337 /* Mark bit was already set while object was on free list. */
338 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
343 op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
345 op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
347 if (0 == op) return(0);
348 /* We don't need the lock here, since we have an undisguised */
349 /* pointer. We do need to hold the lock while we adjust */
352 register struct hblk * h;
355 lw = HDR(h) -> hb_sz;
360 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
367 #endif /* ATOMIC_UNCOLLECTABLE */