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 ptr_t
GC_alloc_large(lw
, k
, flags
)
45 word n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
48 if (!GC_is_initialized
) GC_init_inner();
49 /* Do our share of marking work */
50 if(GC_incremental
&& !GC_dont_gc
)
51 GC_collect_a_little_inner((int)n_blocks
);
52 h
= GC_allochblk(lw
, k
, flags
);
56 h
= GC_allochblk(lw
, k
, flags
);
59 while (0 == h
&& GC_collect_or_expand(n_blocks
, (flags
!= 0))) {
60 h
= GC_allochblk(lw
, k
, flags
);
65 int total_bytes
= BYTES_TO_WORDS(n_blocks
* HBLKSIZE
);
67 GC_large_allocd_bytes
+= n_blocks
* HBLKSIZE
;
68 if (GC_large_allocd_bytes
> GC_max_large_allocd_bytes
)
69 GC_max_large_allocd_bytes
= GC_large_allocd_bytes
;
71 result
= (ptr_t
) (h
-> hb_body
);
72 GC_words_wasted
+= total_bytes
- lw
;
78 /* Allocate a large block of size lb bytes. Clear if appropriate. */
79 ptr_t
GC_alloc_large_and_clear(lw
, k
, flags
)
84 ptr_t result
= GC_alloc_large(lw
, k
, flags
);
85 word n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
87 if (0 == result
) return 0;
88 if (GC_debugging_started
|| GC_obj_kinds
[k
].ok_init
) {
89 /* Clear the whole block, in case of GC_realloc call. */
90 BZERO(result
, n_blocks
* HBLKSIZE
);
95 /* allocate lb bytes for an object of kind k. */
96 /* Should not be used to directly to allocate */
97 /* objects such as STUBBORN objects that */
98 /* require special handling on allocation. */
99 /* First a version that assumes we already */
101 ptr_t
GC_generic_malloc_inner(lb
, k
)
109 if( SMALL_OBJ(lb
) ) {
110 register struct obj_kind
* kind
= GC_obj_kinds
+ k
;
112 lw
= GC_size_map
[lb
];
114 lw
= ALIGNED_WORDS(lb
);
115 if (lw
== 0) lw
= MIN_WORDS
;
117 opp
= &(kind
-> ok_freelist
[lw
]);
118 if( (op
= *opp
) == 0 ) {
120 if (GC_size_map
[lb
] == 0) {
121 if (!GC_is_initialized
) GC_init_inner();
122 if (GC_size_map
[lb
] == 0) GC_extend_size_map(lb
);
123 return(GC_generic_malloc_inner(lb
, k
));
126 if (!GC_is_initialized
) {
128 return(GC_generic_malloc_inner(lb
, k
));
131 if (kind
-> ok_reclaim_list
== 0) {
132 if (!GC_alloc_reclaim_list(kind
)) goto out
;
134 op
= GC_allocobj(lw
, k
);
135 if (op
== 0) goto out
;
137 /* Here everything is in a consistent state. */
138 /* We assume the following assignment is */
139 /* atomic. If we get aborted */
140 /* after the assignment, we lose an object, */
141 /* but that's benign. */
142 /* Volatile declarations may need to be added */
143 /* to prevent the compiler from breaking things.*/
144 /* If we only execute the second of the */
145 /* following assignments, we lose the free */
146 /* list, but that should still be OK, at least */
147 /* for garbage collected memory. */
151 lw
= ROUNDED_UP_WORDS(lb
);
152 op
= (ptr_t
)GC_alloc_large_and_clear(lw
, k
, 0);
154 GC_words_allocd
+= lw
;
160 /* Allocate a composite object of size n bytes. The caller guarantees */
161 /* that pointers past the first page are not relevant. Caller holds */
162 /* allocation lock. */
163 ptr_t
GC_generic_malloc_inner_ignore_off_page(lb
, k
)
171 return(GC_generic_malloc_inner((word
)lb
, k
));
172 lw
= ROUNDED_UP_WORDS(lb
);
173 op
= (ptr_t
)GC_alloc_large_and_clear(lw
, k
, IGNORE_OFF_PAGE
);
174 GC_words_allocd
+= lw
;
178 ptr_t
GC_generic_malloc(lb
, k
)
185 if (GC_have_errors
) GC_print_all_errors();
186 GC_INVOKE_FINALIZERS();
190 result
= GC_generic_malloc_inner((word
)lb
, k
);
197 lw
= ROUNDED_UP_WORDS(lb
);
198 n_blocks
= OBJ_SZ_TO_BLOCKS(lw
);
199 init
= GC_obj_kinds
[k
].ok_init
;
202 result
= (ptr_t
)GC_alloc_large(lw
, k
, 0);
204 if (GC_debugging_started
) {
205 BZERO(result
, n_blocks
* HBLKSIZE
);
208 /* Clear any memory that might be used for GC descriptors */
209 /* before we release the lock. */
210 ((word
*)result
)[0] = 0;
211 ((word
*)result
)[1] = 0;
212 ((word
*)result
)[lw
-1] = 0;
213 ((word
*)result
)[lw
-2] = 0;
217 GC_words_allocd
+= lw
;
220 if (init
&& !GC_debugging_started
&& 0 != result
) {
221 BZERO(result
, n_blocks
* HBLKSIZE
);
225 return((*GC_oom_fn
)(lb
));
232 #define GENERAL_MALLOC(lb,k) \
233 (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
234 /* We make the GC_clear_stack_call a tail call, hoping to get more of */
237 /* Allocate lb bytes of atomic (pointerfree) data */
239 GC_PTR
GC_malloc_atomic(size_t lb
)
241 GC_PTR
GC_malloc_atomic(lb
)
246 register ptr_t
* opp
;
250 if( EXPECT(SMALL_OBJ(lb
), 1) ) {
252 lw
= GC_size_map
[lb
];
254 lw
= ALIGNED_WORDS(lb
);
256 opp
= &(GC_aobjfreelist
[lw
]);
258 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op
= *opp
) == 0, 0) ) {
260 return(GENERAL_MALLOC((word
)lb
, PTRFREE
));
262 /* See above comment on signals. */
264 GC_words_allocd
+= lw
;
268 return(GENERAL_MALLOC((word
)lb
, PTRFREE
));
272 /* Allocate lb bytes of composite (pointerful) data */
274 GC_PTR
GC_malloc(size_t lb
)
285 if( EXPECT(SMALL_OBJ(lb
), 1) ) {
287 lw
= GC_size_map
[lb
];
289 lw
= ALIGNED_WORDS(lb
);
291 opp
= &(GC_objfreelist
[lw
]);
293 if( EXPECT(!FASTLOCK_SUCCEEDED() || (op
= *opp
) == 0, 0) ) {
295 return(GENERAL_MALLOC((word
)lb
, NORMAL
));
297 /* See above comment on signals. */
298 GC_ASSERT(0 == obj_link(op
)
299 || (word
)obj_link(op
)
300 <= (word
)GC_greatest_plausible_heap_addr
301 && (word
)obj_link(op
)
302 >= (word
)GC_least_plausible_heap_addr
);
305 GC_words_allocd
+= lw
;
309 return(GENERAL_MALLOC((word
)lb
, NORMAL
));
313 # ifdef REDIRECT_MALLOC
315 GC_PTR
malloc(size_t lb
)
321 /* It might help to manually inline the GC_malloc call here. */
322 /* But any decent compiler should reduce the extra procedure call */
323 /* to at most a jump instruction in this case. */
324 # if defined(I386) && defined(GC_SOLARIS_THREADS)
326 * Thread initialisation can call malloc before
327 * we're ready for it.
328 * It's not clear that this is enough to help matters.
329 * The thread implementation may well call malloc at other
332 if (!GC_is_initialized
) return sbrk(lb
);
333 # endif /* I386 && GC_SOLARIS_THREADS */
334 return((GC_PTR
)REDIRECT_MALLOC(lb
));
338 GC_PTR
calloc(size_t n
, size_t lb
)
344 return((GC_PTR
)REDIRECT_MALLOC(n
*lb
));
350 char *strdup(const char *s
)
356 size_t len
= strlen(s
) + 1;
357 char * result
= ((char *)REDIRECT_MALLOC(len
+1));
358 BCOPY(s
, result
, len
+1);
361 #endif /* !defined(strdup) */
362 /* If strdup is macro defined, we assume that it actually calls malloc, */
363 /* and thus the right thing will happen even without overriding it. */
364 /* This seems to be true on most Linux systems. */
366 # endif /* REDIRECT_MALLOC */
368 /* Explicitly deallocate an object p. */
370 void GC_free(GC_PTR p
)
376 register struct hblk
*h
;
378 register signed_word sz
;
379 register ptr_t
* flh
;
381 register struct obj_kind
* ok
;
385 /* Required by ANSI. It's not my fault ... */
388 GC_ASSERT(GC_base(p
) == p
);
389 # if defined(REDIRECT_MALLOC) && \
390 (defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
391 || defined(__MINGW32__)) /* Should this be MSWIN32 in general? */
392 /* For Solaris, we have to redirect malloc calls during */
393 /* initialization. For the others, this seems to happen */
395 /* Don't try to deallocate that memory. */
396 if (0 == hhdr
) return;
398 knd
= hhdr
-> hb_obj_kind
;
400 ok
= &GC_obj_kinds
[knd
];
401 if (EXPECT((sz
<= MAXOBJSZ
), 1)) {
407 /* A signal here can make GC_mem_freed and GC_non_gc_bytes */
408 /* inconsistent. We claim this is benign. */
409 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
410 /* Its unnecessary to clear the mark bit. If the */
411 /* object is reallocated, it doesn't matter. O.w. the */
412 /* collector will do it, since it's on a free list. */
414 BZERO((word
*)p
+ 1, WORDS_TO_BYTES(sz
-1));
416 flh
= &(ok
-> ok_freelist
[sz
]);
427 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
434 /* Explicitly deallocate an object p when we already hold lock. */
435 /* Only used for internally allocated objects, so we can take some */
438 void GC_free_inner(GC_PTR p
)
440 register struct hblk
*h
;
442 register signed_word sz
;
443 register ptr_t
* flh
;
445 register struct obj_kind
* ok
;
450 knd
= hhdr
-> hb_obj_kind
;
452 ok
= &GC_obj_kinds
[knd
];
453 if (sz
<= MAXOBJSZ
) {
455 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
457 BZERO((word
*)p
+ 1, WORDS_TO_BYTES(sz
-1));
459 flh
= &(ok
-> ok_freelist
[sz
]);
464 if (IS_UNCOLLECTABLE(knd
)) GC_non_gc_bytes
-= WORDS_TO_BYTES(sz
);
470 # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
471 # define REDIRECT_FREE GC_free
473 # ifdef REDIRECT_FREE
485 # endif /* REDIRECT_MALLOC */