Skip to content
Newer
Older
100644 615 lines (564 sloc) 20.4 KB
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
1 /*
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.
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
5 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
6 *
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 *
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
15 */
16
bad4c7a 2009-10-21 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 21, 2009
17 #include "private/gc_priv.h"
18
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
19 /*
20 * These are extra allocation routines which are likely to be less
21 * frequently used than those in malloc.c. They are separate in the
22 * hope that the .o file will be excluded from statically linked
23 * executables. We should probably break this up further.
24 */
25
26 #include <stdio.h>
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Apr 9, 2011
27 #include <string.h>
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
28
bfda2d0 2011-03-27 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Mar 27, 2011
29 #ifdef MSWINCE
30 # ifndef WIN32_LEAN_AND_MEAN
31 # define WIN32_LEAN_AND_MEAN 1
32 # endif
33 # define NOSERVICE
34 # include <windows.h>
35 #else
36 # include <errno.h>
37 #endif
38
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
39 /* Some externally visible but unadvertised variables to allow access to */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
40 /* free lists from inlined allocators without including gc_priv.h */
41 /* or introducing dependencies on internal data structure layouts. */
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
42 void ** const GC_objfreelist_ptr = GC_objfreelist;
43 void ** const GC_aobjfreelist_ptr = GC_aobjfreelist;
44 void ** const GC_uobjfreelist_ptr = GC_uobjfreelist;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
45 # ifdef ATOMIC_UNCOLLECTABLE
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
46 void ** const GC_auobjfreelist_ptr = GC_auobjfreelist;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
47 # endif
48
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
49
68b9f27 2008-07-25 Hans Boehm <Hans.Boehm@hp.com> (Really mostly Ivan Maidanski)
hboehm authored Jul 26, 2008
50 STATIC void * GC_generic_or_special_malloc(size_t lb, int knd)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
51 {
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
52 switch(knd) {
53 # ifdef STUBBORN_ALLOC
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
54 case STUBBORN:
55 return(GC_malloc_stubborn((size_t)lb));
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
56 # endif
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
57 case PTRFREE:
58 return(GC_malloc_atomic((size_t)lb));
59 case NORMAL:
60 return(GC_malloc((size_t)lb));
61 case UNCOLLECTABLE:
62 return(GC_malloc_uncollectable((size_t)lb));
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
63 # ifdef ATOMIC_UNCOLLECTABLE
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
64 case AUNCOLLECTABLE:
65 return(GC_malloc_atomic_uncollectable((size_t)lb));
66 # endif /* ATOMIC_UNCOLLECTABLE */
67 default:
68 return(GC_generic_malloc(lb,knd));
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
69 }
70 }
71
72 /* Change the size of the block pointed to by p to contain at least */
73 /* lb bytes. The object may be (and quite likely will be) moved. */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
74 /* The kind (e.g. atomic) is the same as that of the old. */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
75 /* Shrinking of large blocks is not implemented well. */
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored Oct 25, 2008
76 GC_API void * GC_CALL GC_realloc(void * p, size_t lb)
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
77 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
78 struct hblk * h;
79 hdr * hhdr;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
80 size_t sz; /* Current size in bytes */
81 size_t orig_sz; /* Original sz in bytes */
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
82 int obj_kind;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
83
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
84 if (p == 0) return(GC_malloc(lb)); /* Required by ANSI */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
85 h = HBLKPTR(p);
86 hhdr = HDR(h);
87 sz = hhdr -> hb_sz;
88 obj_kind = hhdr -> hb_obj_kind;
89 orig_sz = sz;
90
91 if (sz > MAXOBJBYTES) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
92 /* Round it up to the next whole heap block */
0dbf78a 2009-09-25 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 25, 2009
93 word descr;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
94
95 sz = (sz+HBLKSIZE-1) & (~HBLKMASK);
96 hhdr -> hb_sz = sz;
97 descr = GC_obj_kinds[obj_kind].ok_descriptor;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
98 if (GC_obj_kinds[obj_kind].ok_relocate_descr) descr += sz;
99 hhdr -> hb_descr = descr;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
100 # ifdef MARK_BIT_PER_OBJ
101 GC_ASSERT(hhdr -> hb_inv_sz == LARGE_INV_SZ);
102 # else
103 GC_ASSERT(hhdr -> hb_large_block &&
104 hhdr -> hb_map[ANY_INDEX] == 1);
105 # endif
106 if (IS_UNCOLLECTABLE(obj_kind)) GC_non_gc_bytes += (sz - orig_sz);
107 /* Extra area is already cleared by GC_alloc_large_and_clear. */
e955362 @ivmai gc5.0alpha3 tarball import
authored Jul 26, 2011
108 }
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
109 if (ADD_SLOP(lb) <= sz) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
110 if (lb >= (sz >> 1)) {
111 # ifdef STUBBORN_ALLOC
112 if (obj_kind == STUBBORN) GC_change_stubborn(p);
113 # endif
114 if (orig_sz > lb) {
115 /* Clear unneeded part of object to avoid bogus pointer */
116 /* tracing. */
117 /* Safe for stubborn objects. */
118 BZERO(((ptr_t)p) + lb, orig_sz - lb);
119 }
120 return(p);
121 } else {
122 /* shrink */
123 void * result =
124 GC_generic_or_special_malloc((word)lb, obj_kind);
125
126 if (result == 0) return(0);
127 /* Could also return original object. But this */
128 /* gives the client warning of imminent disaster. */
129 BCOPY(p, result, lb);
130 # ifndef IGNORE_FREE
131 GC_free(p);
132 # endif
133 return(result);
134 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
135 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
136 /* grow */
137 void * result =
138 GC_generic_or_special_malloc((word)lb, obj_kind);
139
140 if (result == 0) return(0);
141 BCOPY(p, result, sz);
142 # ifndef IGNORE_FREE
143 GC_free(p);
144 # endif
145 return(result);
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
146 }
147 }
148
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
149 # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_REALLOC)
150 # define REDIRECT_REALLOC GC_realloc
151 # endif
152
153 # ifdef REDIRECT_REALLOC
1b159c5 @ivmai gc6.3alpha4 tarball import
authored Jul 26, 2011
154
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
155 /* As with malloc, avoid two levels of extra calls here. */
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Apr 9, 2011
156
1b159c5 @ivmai gc6.3alpha4 tarball import
authored Jul 26, 2011
157 # define GC_debug_realloc_replacement(p, lb) \
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Apr 9, 2011
158 GC_debug_realloc(p, lb, GC_DBG_RA "unknown", 0)
1b159c5 @ivmai gc6.3alpha4 tarball import
authored Jul 26, 2011
159
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
160 void * realloc(void * p, size_t lb)
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
161 {
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
162 return(REDIRECT_REALLOC(p, lb));
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
163 }
1b159c5 @ivmai gc6.3alpha4 tarball import
authored Jul 26, 2011
164
165 # undef GC_debug_realloc_replacement
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
166 # endif /* REDIRECT_REALLOC */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
167
168
6404004 @ivmai gc7.0alpha2 tarball import
authored Jul 26, 2011
169 /* Allocate memory such that only pointers to near the */
170 /* beginning of the object are considered. */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
171 /* We avoid holding allocation lock while we clear memory. */
b2345fb 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 22, 2009
172 GC_INNER void * GC_generic_malloc_ignore_off_page(size_t lb, int k)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
173 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
174 void *result;
f1a9c69 2009-02-28 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidansky)
hboehm authored Mar 1, 2009
175 size_t lg;
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
176 size_t lb_rounded;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
177 word n_blocks;
178 GC_bool init;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
179 DCL_LOCK_STATE;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
180
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
181 if (SMALL_OBJ(lb))
182 return(GC_generic_malloc((word)lb, k));
e637f6a 2008-10-27 Hans Boehm <Hans.Boehm@hp.com>
hboehm authored Oct 28, 2008
183 lg = ROUNDED_UP_GRANULES(lb);
184 lb_rounded = GRANULES_TO_BYTES(lg);
be9df82 @xiw Fix allocation size overflows due to rounding.
xiw authored Mar 15, 2012
185 if (lb_rounded < lb)
186 return((*GC_get_oom_fn())(lb));
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
187 n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
188 init = GC_obj_kinds[k].ok_init;
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
authored Dec 29, 2011
189 if (EXPECT(GC_have_errors, FALSE))
190 GC_print_all_errors();
c521d2c @ivmai gc4.13 tarball import
authored Jul 26, 2011
191 GC_INVOKE_FINALIZERS();
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
192 LOCK();
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
193 result = (ptr_t)GC_alloc_large(ADD_SLOP(lb), k, IGNORE_OFF_PAGE);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
194 if (0 != result) {
195 if (GC_debugging_started) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
196 BZERO(result, n_blocks * HBLKSIZE);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
197 } else {
198 # ifdef THREADS
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
199 /* Clear any memory that might be used for GC descriptors */
200 /* before we release the lock. */
201 ((word *)result)[0] = 0;
202 ((word *)result)[1] = 0;
203 ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0;
204 ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0;
205 # endif
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
206 }
207 }
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
208 GC_bytes_allocd += lb_rounded;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
209 if (0 == result) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
210 GC_oom_func oom_fn = GC_oom_fn;
211 UNLOCK();
212 return((*oom_fn)(lb));
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
213 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
214 UNLOCK();
215 if (init && !GC_debugging_started) {
216 BZERO(result, n_blocks * HBLKSIZE);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
217 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
218 return(result);
219 }
220 }
221
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored Oct 25, 2008
222 GC_API void * GC_CALL GC_malloc_ignore_off_page(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
223 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
224 return((void *)GC_generic_malloc_ignore_off_page(lb, NORMAL));
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
225 }
226
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored Oct 25, 2008
227 GC_API void * GC_CALL GC_malloc_atomic_ignore_off_page(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
228 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
229 return((void *)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
230 }
231
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
232 /* Increment GC_bytes_allocd from code that doesn't have direct access */
233 /* to GC_arrays. */
f9aaf5b 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 10, 2009
234 GC_API void GC_CALL GC_incr_bytes_allocd(size_t n)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
235 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
236 GC_bytes_allocd += n;
29be14a @ivmai gc5.3 tarball import
authored Jul 26, 2011
237 }
238
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
239 /* The same for GC_bytes_freed. */
f9aaf5b 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 10, 2009
240 GC_API void GC_CALL GC_incr_bytes_freed(size_t n)
29be14a @ivmai gc5.3 tarball import
authored Jul 26, 2011
241 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
242 GC_bytes_freed += n;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
243 }
244
7098159 2009-10-18 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 18, 2009
245 # ifdef PARALLEL_MARK
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
authored Jan 29, 2012
246 STATIC volatile AO_t GC_bytes_allocd_tmp = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
247 /* Number of bytes of memory allocated since */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
248 /* we released the GC lock. Instead of */
249 /* reacquiring the GC lock just to add this in, */
250 /* we add it in the next time we reacquire */
251 /* the lock. (Atomically adding it doesn't */
252 /* work, since we would have to atomically */
253 /* update it in GC_malloc, which is too */
7098159 2009-10-18 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 18, 2009
254 /* expensive.) */
255 # endif /* PARALLEL_MARK */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
256
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
257 /* Return a list of 1 or more objects of the indicated size, linked */
258 /* through the first word in the object. This has the advantage that */
259 /* it acquires the allocation lock only once, and may greatly reduce */
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
260 /* time wasted contending for the allocation lock. Typical usage would */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
261 /* be in a thread that requires many items of the same size. It would */
262 /* keep its own free list in thread-local storage, and call */
263 /* GC_malloc_many or friends to replenish it. (We do not round up */
264 /* object sizes, since a call indicates the intention to consume many */
265 /* objects of exactly this size.) */
266 /* We assume that the size is a multiple of GRANULE_BYTES. */
267 /* We return the free-list by assigning it to *result, since it is */
268 /* not safe to return, e.g. a linked list of pointer-free objects, */
269 /* since the collector would not retain the entire list if it were */
270 /* invoked just as we were returning. */
271 /* Note that the client should usually clear the link field. */
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 22, 2009
272 GC_API void GC_CALL GC_generic_malloc_many(size_t lb, int k, void **result)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
273 {
a9bb70c 2009-09-20 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 20, 2009
274 void *op;
275 void *p;
276 void **opp;
277 size_t lw; /* Length in words. */
278 size_t lg; /* Length in granules. */
279 signed_word my_bytes_allocd = 0;
280 struct obj_kind * ok = &(GC_obj_kinds[k]);
281 DCL_LOCK_STATE;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
282
68b9f27 2008-07-25 Hans Boehm <Hans.Boehm@hp.com> (Really mostly Ivan Maidanski)
hboehm authored Jul 26, 2008
283 GC_ASSERT(lb != 0 && (lb & (GRANULE_BYTES-1)) == 0);
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
284 if (!SMALL_OBJ(lb)) {
285 op = GC_generic_malloc(lb, k);
33dc2de @ivmai Use EXPECT in malloc result testing for NULL
authored Jan 18, 2012
286 if (EXPECT(0 != op, TRUE))
287 obj_link(op) = 0;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
288 *result = op;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
289 return;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
290 }
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
291 lw = BYTES_TO_WORDS(lb);
292 lg = BYTES_TO_GRANULES(lb);
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
authored Dec 29, 2011
293 if (EXPECT(GC_have_errors, FALSE))
294 GC_print_all_errors();
c521d2c @ivmai gc4.13 tarball import
authored Jul 26, 2011
295 GC_INVOKE_FINALIZERS();
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
296 LOCK();
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
authored Dec 29, 2011
297 if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
298 /* Do our share of marking work */
299 if (GC_incremental && !GC_dont_gc) {
300 ENTER_GC();
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
301 GC_collect_a_little_inner(1);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
302 EXIT_GC();
303 }
304 /* First see if we can reclaim a page of objects waiting to be */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
305 /* reclaimed. */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
306 {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
307 struct hblk ** rlh = ok -> ok_reclaim_list;
308 struct hblk * hbp;
309 hdr * hhdr;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
310
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
311 rlh += lg;
312 while ((hbp = *rlh) != 0) {
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
313 hhdr = HDR(hbp);
314 *rlh = hhdr -> hb_next;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
315 GC_ASSERT(hhdr -> hb_sz == lb);
316 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
317 # ifdef PARALLEL_MARK
318 if (GC_parallel) {
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
authored Jan 29, 2012
319 signed_word my_bytes_allocd_tmp =
320 (signed_word)AO_load(&GC_bytes_allocd_tmp);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
321 GC_ASSERT(my_bytes_allocd_tmp >= 0);
322 /* We only decrement it while holding the GC lock. */
323 /* Thus we can't accidentally adjust it down in more */
324 /* than one thread simultaneously. */
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
authored Jan 29, 2012
325
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
326 if (my_bytes_allocd_tmp != 0) {
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
authored Jan 29, 2012
327 (void)AO_fetch_and_add(&GC_bytes_allocd_tmp,
328 (AO_t)(-my_bytes_allocd_tmp));
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
329 GC_bytes_allocd += my_bytes_allocd_tmp;
330 }
331 GC_acquire_mark_lock();
332 ++ GC_fl_builder_count;
333 UNLOCK();
334 GC_release_mark_lock();
335 }
336 # endif
337 op = GC_reclaim_generic(hbp, hhdr, lb,
338 ok -> ok_init, 0, &my_bytes_allocd);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
339 if (op != 0) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
340 /* We also reclaimed memory, so we need to adjust */
341 /* that count. */
342 /* This should be atomic, so the results may be */
343 /* inaccurate. */
344 GC_bytes_found += my_bytes_allocd;
345 # ifdef PARALLEL_MARK
346 if (GC_parallel) {
347 *result = op;
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
authored Jan 29, 2012
348 (void)AO_fetch_and_add(&GC_bytes_allocd_tmp,
349 (AO_t)my_bytes_allocd);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
350 GC_acquire_mark_lock();
351 -- GC_fl_builder_count;
352 if (GC_fl_builder_count == 0) GC_notify_all_builder();
353 GC_release_mark_lock();
354 (void) GC_clear_stack(0);
355 return;
356 }
357 # endif
358 GC_bytes_allocd += my_bytes_allocd;
359 goto out;
360 }
361 # ifdef PARALLEL_MARK
362 if (GC_parallel) {
363 GC_acquire_mark_lock();
364 -- GC_fl_builder_count;
365 if (GC_fl_builder_count == 0) GC_notify_all_builder();
366 GC_release_mark_lock();
367 LOCK();
368 /* GC lock is needed for reclaim list access. We */
369 /* must decrement fl_builder_count before reaquiring GC */
370 /* lock. Hopefully this path is rare. */
371 }
372 # endif
373 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
374 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
375 /* Next try to use prefix of global free list if there is one. */
376 /* We don't refill it, but we need to use it up before allocating */
377 /* a new block ourselves. */
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
378 opp = &(GC_obj_kinds[k].ok_freelist[lg]);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
379 if ( (op = *opp) != 0 ) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
380 *opp = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
381 my_bytes_allocd = 0;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
382 for (p = op; p != 0; p = obj_link(p)) {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
383 my_bytes_allocd += lb;
2c23fbe @ivmai Resolve "comparison of signed and unsigned values" compiler warnings.
authored Aug 21, 2011
384 if ((word)my_bytes_allocd >= HBLKSIZE) {
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
385 *opp = obj_link(p);
386 obj_link(p) = 0;
387 break;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
388 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
389 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
390 GC_bytes_allocd += my_bytes_allocd;
391 goto out;
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
392 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
393 /* Next try to allocate a new block worth of objects of this size. */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
394 {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
395 struct hblk *h = GC_allochblk(lb, k, 0);
396 if (h != 0) {
397 if (IS_UNCOLLECTABLE(k)) GC_set_hdr_marks(HDR(h));
398 GC_bytes_allocd += HBLKSIZE - HBLKSIZE % lb;
399 # ifdef PARALLEL_MARK
400 if (GC_parallel) {
401 GC_acquire_mark_lock();
402 ++ GC_fl_builder_count;
403 UNLOCK();
404 GC_release_mark_lock();
405
406 op = GC_build_fl(h, lw,
407 (ok -> ok_init || GC_debugging_started), 0);
408
409 *result = op;
410 GC_acquire_mark_lock();
411 -- GC_fl_builder_count;
412 if (GC_fl_builder_count == 0) GC_notify_all_builder();
413 GC_release_mark_lock();
414 (void) GC_clear_stack(0);
415 return;
416 }
417 # endif
418 op = GC_build_fl(h, lw, (ok -> ok_init || GC_debugging_started), 0);
419 goto out;
420 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
421 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
422
423 /* As a last attempt, try allocating a single object. Note that */
424 /* this may trigger a collection or expand the heap. */
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
425 op = GC_generic_malloc_inner(lb, k);
426 if (0 != op) obj_link(op) = 0;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
427
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
428 out:
429 *result = op;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
430 UNLOCK();
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
431 (void) GC_clear_stack(0);
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
432 }
433
03fa669 2010-02-04 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Feb 4, 2010
434 /* Note that the "atomic" version of this would be unsafe, since the */
435 /* links would not be seen by the collector. */
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored Oct 25, 2008
436 GC_API void * GC_CALL GC_malloc_many(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
437 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
438 void *result;
03fa669 2010-02-04 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Feb 4, 2010
439 GC_generic_malloc_many((lb + EXTRA_BYTES + GRANULE_BYTES-1)
440 & ~(GRANULE_BYTES-1),
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
441 NORMAL, &result);
9f27ae7 @ivmai gc6.0 tarball import
authored Jul 26, 2011
442 return result;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
443 }
444
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
445 /* Not well tested nor integrated. */
446 /* Debug version is tricky and currently missing. */
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
447 #include <limits.h>
448
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
449 GC_API void * GC_CALL GC_memalign(size_t align, size_t lb)
450 {
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
451 size_t new_lb;
452 size_t offset;
453 ptr_t result;
454
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
455 if (align <= GRANULE_BYTES) return GC_malloc(lb);
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
456 if (align >= HBLKSIZE/2 || lb >= HBLKSIZE/2) {
20031fb 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 10, 2009
457 if (align > HBLKSIZE) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
458 return (*GC_get_oom_fn())(LONG_MAX-1024); /* Fail */
459 }
460 return GC_malloc(lb <= HBLKSIZE? HBLKSIZE : lb);
461 /* Will be HBLKSIZE aligned. */
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
462 }
463 /* We could also try to make sure that the real rounded-up object size */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
464 /* is a multiple of align. That would be correct up to HBLKSIZE. */
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
465 new_lb = lb + align - 1;
466 result = GC_malloc(new_lb);
ceeb33a @ivmai Add comment in GC_memalign
authored Jun 8, 2012
467 /* It is ok not to check result for NULL as in that case */
468 /* GC_memalign returns NULL too since (0 + 0 % align) is 0. */
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
469 offset = (word)result % align;
470 if (offset != 0) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
471 offset = align - offset;
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
472 if (!GC_all_interior_pointers) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
473 if (offset >= VALID_OFFSET_SZ) return GC_malloc(HBLKSIZE);
474 GC_register_displacement(offset);
475 }
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
476 }
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
477 result = (void *) ((ptr_t)result + offset);
f363243 @ivmai gc6.1 tarball import
authored Jul 26, 2011
478 GC_ASSERT((word)result % align == 0);
479 return result;
480 }
481
bfda2d0 2011-03-27 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Mar 27, 2011
482 /* This one exists largerly to redirect posix_memalign for leaks finding. */
483 GC_API int GC_CALL GC_posix_memalign(void **memptr, size_t align, size_t lb)
484 {
485 /* Check alignment properly. */
486 if (((align - 1) & align) != 0 || align < sizeof(void *)) {
487 # ifdef MSWINCE
488 return ERROR_INVALID_PARAMETER;
489 # else
490 return EINVAL;
491 # endif
492 }
493
494 if ((*memptr = GC_memalign(align, lb)) == NULL) {
495 # ifdef MSWINCE
496 return ERROR_NOT_ENOUGH_MEMORY;
497 # else
498 return ENOMEM;
499 # endif
500 }
501 return 0;
502 }
503
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 22, 2009
504 #ifdef ATOMIC_UNCOLLECTABLE
505 /* Allocate lb bytes of pointerfree, untraced, uncollectable data */
506 /* This is normally roughly equivalent to the system malloc. */
507 /* But it may be useful if malloc is redefined. */
508 GC_API void * GC_CALL GC_malloc_atomic_uncollectable(size_t lb)
509 {
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
510 void *op;
511 void **opp;
512 size_t lg;
513 DCL_LOCK_STATE;
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
514
515 if( SMALL_OBJ(lb) ) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
516 if (EXTRA_BYTES != 0 && lb != 0) lb--;
517 /* We don't need the extra byte, since this won't be */
518 /* collected anyway. */
519 lg = GC_size_map[lb];
520 opp = &(GC_auobjfreelist[lg]);
521 LOCK();
33dc2de @ivmai Use EXPECT in malloc result testing for NULL
authored Jan 18, 2012
522 op = *opp;
523 if (EXPECT(0 != op, TRUE)) {
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
524 *opp = obj_link(op);
525 obj_link(op) = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
526 GC_bytes_allocd += GRANULES_TO_BYTES(lg);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
527 /* Mark bit was already set while object was on free list. */
e35a417 @ivmai gc7.0alpha1 tarball import
authored Jul 26, 2011
528 GC_non_gc_bytes += GRANULES_TO_BYTES(lg);
3c50a68 @ivmai gc7.0alpha5 tarball import
authored Jul 26, 2011
529 UNLOCK();
530 } else {
531 UNLOCK();
532 op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
533 }
534 GC_ASSERT(0 == op || GC_is_marked(op));
3c50a68 @ivmai gc7.0alpha5 tarball import
authored Jul 26, 2011
535 return((void *) op);
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
536 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
537 hdr * hhdr;
538
539 op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
3c50a68 @ivmai gc7.0alpha5 tarball import
authored Jul 26, 2011
540 if (0 == op) return(0);
541
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
542 GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0);
543 hhdr = HDR(op);
544
545 LOCK();
546 set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
5b25be3 @ivmai Fix assertion in GC_malloc_[atomic_]uncollectable (THREADS case only)
authored May 16, 2012
547 # ifndef THREADS
548 GC_ASSERT(hhdr -> hb_n_marks == 0);
549 # endif
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Sep 16, 2009
550 hhdr -> hb_n_marks = 1;
551 UNLOCK();
552 return((void *) op);
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
553 }
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Oct 22, 2009
554 }
f9b1aa2 @ivmai gc4.11 tarball import
authored Jul 26, 2011
555 #endif /* ATOMIC_UNCOLLECTABLE */
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored Apr 9, 2011
556
557 /* provide a version of strdup() that uses the collector to allocate the
558 copy of the string */
559 GC_API char * GC_CALL GC_strdup(const char *s)
560 {
561 char *copy;
562 size_t lb;
563 if (s == NULL) return NULL;
564 lb = strlen(s) + 1;
565 if ((copy = GC_malloc_atomic(lb)) == NULL) {
566 # ifndef MSWINCE
567 errno = ENOMEM;
568 # endif
569 return NULL;
570 }
571 # ifndef MSWINCE
572 strcpy(copy, s);
573 # else
574 /* strcpy() is deprecated in WinCE */
575 memcpy(copy, s, lb);
576 # endif
577 return copy;
578 }
579
580 GC_API char * GC_CALL GC_strndup(const char *str, size_t size)
581 {
582 char *copy;
583 size_t len = strlen(str); /* str is expected to be non-NULL */
584 if (len > size)
585 len = size;
586 copy = GC_malloc_atomic(len + 1);
587 if (copy == NULL) {
588 # ifndef MSWINCE
589 errno = ENOMEM;
590 # endif
591 return NULL;
592 }
593 BCOPY(str, copy, len);
594 copy[len] = '\0';
595 return copy;
596 }
597
598 #ifdef GC_REQUIRE_WCSDUP
599 # include <wchar.h> /* for wcslen() */
600
601 GC_API wchar_t * GC_CALL GC_wcsdup(const wchar_t *str)
602 {
603 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
604 wchar_t *copy = GC_malloc_atomic(lb);
605 if (copy == NULL) {
606 # ifndef MSWINCE
607 errno = ENOMEM;
608 # endif
609 return NULL;
610 }
611 BCOPY(str, copy, lb);
612 return copy;
613 }
614 #endif /* GC_REQUIRE_WCSDUP */
Something went wrong with that request. Please try again.