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 h = GC_allochblk(lw, k, IGNORE_OFF_PAGE);
64 h = GC_allochblk(lw, k, IGNORE_OFF_PAGE);
67 while (0 == h && GC_collect_or_expand(n_blocks, TRUE)) {
68 h = GC_allochblk(lw, k, IGNORE_OFF_PAGE);
73 op = (ptr_t) (h -> hb_body);
74 GC_words_wasted += BYTES_TO_WORDS(n_blocks * HBLKSIZE) - lw;
76 GC_words_allocd += lw;
80 ptr_t GC_generic_malloc_ignore_off_page(lb, k)
84 register ptr_t result;
87 GC_INVOKE_FINALIZERS();
90 result = GC_generic_malloc_inner_ignore_off_page(lb,k);
94 return((*GC_oom_fn)(lb));
100 # if defined(__STDC__) || defined(__cplusplus)
101 void * GC_malloc_ignore_off_page(size_t lb)
103 char * GC_malloc_ignore_off_page(lb)
107 return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, NORMAL));
110 # if defined(__STDC__) || defined(__cplusplus)
111 void * GC_malloc_atomic_ignore_off_page(size_t lb)
113 char * GC_malloc_atomic_ignore_off_page(lb)
117 return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
120 /* Increment GC_words_allocd from code that doesn't have direct access */
123 void GC_incr_words_allocd(size_t n)
125 GC_words_allocd += n;
128 /* The same for GC_mem_freed. */
129 void GC_incr_mem_freed(size_t n)
133 # endif /* __STDC__ */
135 /* Analogous to the above, but assumes a small object size, and */
136 /* bypasses MERGE_SIZES mechanism. Used by gc_inline.h. */
138 ptr_t GC_generic_malloc_words_small(size_t lw, int k)
140 ptr_t GC_generic_malloc_words_small(lw, k)
147 register struct obj_kind * kind = GC_obj_kinds + k;
150 GC_INVOKE_FINALIZERS();
153 opp = &(kind -> ok_freelist[lw]);
154 if( (op = *opp) == 0 ) {
155 if (!GC_is_initialized) {
158 if (kind -> ok_reclaim_list != 0 || GC_alloc_reclaim_list(kind)) {
159 op = GC_clear_stack(GC_allocobj((word)lw, k));
164 return ((*GC_oom_fn)(WORDS_TO_BYTES(lw)));
169 GC_words_allocd += lw;
175 #if defined(THREADS) && !defined(SRC_M3)
176 /* Return a list of 1 or more objects of the indicated size, linked */
177 /* through the first word in the object. This has the advantage that */
178 /* it acquires the allocation lock only once, and may greatly reduce */
179 /* time wasted contending for the allocation lock. Typical usage would */
180 /* be in a thread that requires many items of the same size. It would */
181 /* keep its own free list in thread-local storage, and call */
182 /* GC_malloc_many or friends to replenish it. (We do not round up */
183 /* object sizes, since a call indicates the intention to consume many */
184 /* objects of exactly this size.) */
185 /* Note that the client should usually clear the link field. */
186 ptr_t GC_generic_malloc_many(lb, k)
194 register word my_words_allocd;
197 if (!SMALL_OBJ(lb)) {
198 op = GC_generic_malloc(lb, k);
199 if(0 != op) obj_link(op) = 0;
202 lw = ALIGNED_WORDS(lb);
203 GC_INVOKE_FINALIZERS();
206 opp = &(GC_obj_kinds[k].ok_freelist[lw]);
207 if( (op = *opp) == 0 ) {
208 if (!GC_is_initialized) {
211 op = GC_clear_stack(GC_allocobj(lw, k));
215 op = (*GC_oom_fn)(lb);
216 if(0 != op) obj_link(op) = 0;
222 for (p = op; p != 0; p = obj_link(p)) {
223 my_words_allocd += lw;
224 if (my_words_allocd >= BODY_SZ) {
230 GC_words_allocd += my_words_allocd;
239 void * GC_malloc_many(size_t lb)
241 return(GC_generic_malloc_many(lb, NORMAL));
244 /* Note that the "atomic" version of this would be unsafe, since the */
245 /* links would not be seen by the collector. */
248 /* Allocate lb bytes of pointerful, traced, but not collectable data */
250 GC_PTR GC_malloc_uncollectable(size_t lb)
252 GC_PTR GC_malloc_uncollectable(lb)
261 if( SMALL_OBJ(lb) ) {
263 # ifdef ADD_BYTE_AT_END
265 /* We don't need the extra byte, since this won't be */
266 /* collected anyway. */
268 lw = GC_size_map[lb];
270 lw = ALIGNED_WORDS(lb);
272 opp = &(GC_uobjfreelist[lw]);
274 if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
275 /* See above comment on signals. */
278 GC_words_allocd += lw;
279 /* Mark bit ws already set on free list. It will be */
280 /* cleared only temporarily during a collection, as a */
281 /* result of the normal free list mark bit clearing. */
282 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
287 op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
289 op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
291 if (0 == op) return(0);
292 /* We don't need the lock here, since we have an undisguised */
293 /* pointer. We do need to hold the lock while we adjust */
296 register struct hblk * h;
299 lw = HDR(h) -> hb_sz;
304 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
311 # ifdef ATOMIC_UNCOLLECTABLE
312 /* Allocate lb bytes of pointerfree, untraced, uncollectable data */
313 /* This is normally roughly equivalent to the system malloc. */
314 /* But it may be useful if malloc is redefined. */
316 GC_PTR GC_malloc_atomic_uncollectable(size_t lb)
318 GC_PTR GC_malloc_atomic_uncollectable(lb)
327 if( SMALL_OBJ(lb) ) {
329 # ifdef ADD_BYTE_AT_END
331 /* We don't need the extra byte, since this won't be */
332 /* collected anyway. */
334 lw = GC_size_map[lb];
336 lw = ALIGNED_WORDS(lb);
338 opp = &(GC_auobjfreelist[lw]);
340 if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
341 /* See above comment on signals. */
344 GC_words_allocd += lw;
345 /* Mark bit was already set while object was on free list. */
346 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
351 op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
353 op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
355 if (0 == op) return(0);
356 /* We don't need the lock here, since we have an undisguised */
357 /* pointer. We do need to hold the lock while we adjust */
360 register struct hblk * h;
363 lw = HDR(h) -> hb_sz;
368 GC_non_gc_bytes += WORDS_TO_BYTES(lw);
375 #endif /* ATOMIC_UNCOLLECTABLE */