Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 611 lines (562 sloc) 20.917 kB
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
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
ivmai authored
5 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
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
17 #include "private/gc_priv.h"
18
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
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
27 #include <string.h>
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
28
bfda2d0 2011-03-27 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
39 /* Some externally visible but unadvertised variables to allow access to */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
ivmai authored
45 # ifdef ATOMIC_UNCOLLECTABLE
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
46 void ** const GC_auobjfreelist_ptr = GC_auobjfreelist;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
47 # endif
48
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
49
68b9f27 2008-07-25 Hans Boehm <Hans.Boehm@hp.com> (Really mostly Ivan Maidan…
hboehm authored
50 STATIC void * GC_generic_or_special_malloc(size_t lb, int knd)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
51 {
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
52 switch(knd) {
53 # ifdef STUBBORN_ALLOC
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
54 case STUBBORN:
55 return(GC_malloc_stubborn((size_t)lb));
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
56 # endif
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
63 # ifdef ATOMIC_UNCOLLECTABLE
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
74 /* The kind (e.g. atomic) is the same as that of the old. */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
75 /* Shrinking of large blocks is not implemented well. */
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored
76 GC_API void * GC_CALL GC_realloc(void * p, size_t lb)
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
77 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
78 struct hblk * h;
79 hdr * hhdr;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
80 size_t sz; /* Current size in bytes */
81 size_t orig_sz; /* Original sz in bytes */
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
82 int obj_kind;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
83
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
84 if (p == 0) return(GC_malloc(lb)); /* Required by ANSI */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
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
92 /* Round it up to the next whole heap block */
0dbf78a 2009-09-25 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
93 word descr;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
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
ivmai authored
108 }
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
109 if (ADD_SLOP(lb) <= sz) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
135 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
146 }
147 }
148
f363243 @ivmai gc6.1 tarball import
ivmai authored
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
ivmai authored
154
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
155 /* As with malloc, avoid two levels of extra calls here. */
1b159c5 @ivmai gc6.3alpha4 tarball import
ivmai authored
156 # define GC_debug_realloc_replacement(p, lb) \
52ac0a4 @ivmai Replace GC_DBG_RA with GC_DBG_EXTRAS macro
ivmai authored
157 GC_debug_realloc(p, lb, GC_DBG_EXTRAS)
1b159c5 @ivmai gc6.3alpha4 tarball import
ivmai authored
158
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
159 void * realloc(void * p, size_t lb)
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
160 {
f363243 @ivmai gc6.1 tarball import
ivmai authored
161 return(REDIRECT_REALLOC(p, lb));
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
162 }
1b159c5 @ivmai gc6.3alpha4 tarball import
ivmai authored
163
164 # undef GC_debug_realloc_replacement
f363243 @ivmai gc6.1 tarball import
ivmai authored
165 # endif /* REDIRECT_REALLOC */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
166
6404004 @ivmai gc7.0alpha2 tarball import
ivmai authored
167 /* Allocate memory such that only pointers to near the */
168 /* beginning of the object are considered. */
7bd92c9 @ivmai Make GC_generic_malloc_ignore_off_page() public
ivmai authored
169 /* We avoid holding allocation lock while we clear the memory. */
170 GC_API void * GC_CALL GC_generic_malloc_ignore_off_page(size_t lb, int k)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
171 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
172 void *result;
f1a9c69 2009-02-28 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidansky)
hboehm authored
173 size_t lg;
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
174 size_t lb_rounded;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
175 word n_blocks;
176 GC_bool init;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
177 DCL_LOCK_STATE;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
178
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
179 if (SMALL_OBJ(lb))
180 return(GC_generic_malloc((word)lb, k));
e637f6a 2008-10-27 Hans Boehm <Hans.Boehm@hp.com>
hboehm authored
181 lg = ROUNDED_UP_GRANULES(lb);
182 lb_rounded = GRANULES_TO_BYTES(lg);
be9df82 @xiw Fix allocation size overflows due to rounding.
xiw authored
183 if (lb_rounded < lb)
184 return((*GC_get_oom_fn())(lb));
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
185 n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
186 init = GC_obj_kinds[k].ok_init;
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
ivmai authored
187 if (EXPECT(GC_have_errors, FALSE))
188 GC_print_all_errors();
c521d2c @ivmai gc4.13 tarball import
ivmai authored
189 GC_INVOKE_FINALIZERS();
9dbe549 @ivmai Enable 'Force GC at every GC_malloc' debug-related functionality
ivmai authored
190 GC_DBG_COLLECT_AT_MALLOC(lb);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
191 LOCK();
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
192 result = (ptr_t)GC_alloc_large(ADD_SLOP(lb), k, IGNORE_OFF_PAGE);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
193 if (0 != result) {
194 if (GC_debugging_started) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
195 BZERO(result, n_blocks * HBLKSIZE);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
196 } else {
197 # ifdef THREADS
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
198 /* Clear any memory that might be used for GC descriptors */
199 /* before we release the lock. */
200 ((word *)result)[0] = 0;
201 ((word *)result)[1] = 0;
202 ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0;
203 ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0;
204 # endif
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
205 }
206 }
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
207 GC_bytes_allocd += lb_rounded;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
208 if (0 == result) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
209 GC_oom_func oom_fn = GC_oom_fn;
210 UNLOCK();
211 return((*oom_fn)(lb));
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
212 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
213 UNLOCK();
214 if (init && !GC_debugging_started) {
215 BZERO(result, n_blocks * HBLKSIZE);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
216 }
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
217 return(result);
218 }
219 }
220
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored
221 GC_API void * GC_CALL GC_malloc_ignore_off_page(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
222 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
223 return((void *)GC_generic_malloc_ignore_off_page(lb, NORMAL));
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
224 }
225
559c07b 2008-10-24 Hans Boehm <Hans.Boehm@hp.com> (Really Ivan Maidanski)
hboehm authored
226 GC_API void * GC_CALL GC_malloc_atomic_ignore_off_page(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
227 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
228 return((void *)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
229 }
230
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
231 /* Increment GC_bytes_allocd from code that doesn't have direct access */
232 /* to GC_arrays. */
f9aaf5b 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
233 GC_API void GC_CALL GC_incr_bytes_allocd(size_t n)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
234 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
235 GC_bytes_allocd += n;
29be14a @ivmai gc5.3 tarball import
ivmai authored
236 }
237
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
238 /* The same for GC_bytes_freed. */
f9aaf5b 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
239 GC_API void GC_CALL GC_incr_bytes_freed(size_t n)
29be14a @ivmai gc5.3 tarball import
ivmai authored
240 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
241 GC_bytes_freed += n;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
242 }
243
7098159 2009-10-18 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
244 # ifdef PARALLEL_MARK
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
ivmai authored
245 STATIC volatile AO_t GC_bytes_allocd_tmp = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
246 /* Number of bytes of memory allocated since */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
247 /* we released the GC lock. Instead of */
248 /* reacquiring the GC lock just to add this in, */
249 /* we add it in the next time we reacquire */
250 /* the lock. (Atomically adding it doesn't */
251 /* work, since we would have to atomically */
252 /* update it in GC_malloc, which is too */
7098159 2009-10-18 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
253 /* expensive.) */
254 # endif /* PARALLEL_MARK */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
255
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
256 /* Return a list of 1 or more objects of the indicated size, linked */
257 /* through the first word in the object. This has the advantage that */
258 /* it acquires the allocation lock only once, and may greatly reduce */
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
259 /* time wasted contending for the allocation lock. Typical usage would */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
260 /* be in a thread that requires many items of the same size. It would */
261 /* keep its own free list in thread-local storage, and call */
262 /* GC_malloc_many or friends to replenish it. (We do not round up */
263 /* object sizes, since a call indicates the intention to consume many */
264 /* objects of exactly this size.) */
265 /* We assume that the size is a multiple of GRANULE_BYTES. */
266 /* We return the free-list by assigning it to *result, since it is */
267 /* not safe to return, e.g. a linked list of pointer-free objects, */
268 /* since the collector would not retain the entire list if it were */
269 /* invoked just as we were returning. */
270 /* Note that the client should usually clear the link field. */
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
271 GC_API void GC_CALL GC_generic_malloc_many(size_t lb, int k, void **result)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
272 {
a9bb70c 2009-09-20 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
273 void *op;
274 void *p;
275 void **opp;
276 size_t lw; /* Length in words. */
277 size_t lg; /* Length in granules. */
278 signed_word my_bytes_allocd = 0;
279 struct obj_kind * ok = &(GC_obj_kinds[k]);
280 DCL_LOCK_STATE;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
281
68b9f27 2008-07-25 Hans Boehm <Hans.Boehm@hp.com> (Really mostly Ivan Maidan…
hboehm authored
282 GC_ASSERT(lb != 0 && (lb & (GRANULE_BYTES-1)) == 0);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
283 if (!SMALL_OBJ(lb)) {
284 op = GC_generic_malloc(lb, k);
33dc2de @ivmai Use EXPECT in malloc result testing for NULL
ivmai authored
285 if (EXPECT(0 != op, TRUE))
286 obj_link(op) = 0;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
287 *result = op;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
288 return;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
289 }
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
290 lw = BYTES_TO_WORDS(lb);
291 lg = BYTES_TO_GRANULES(lb);
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
ivmai authored
292 if (EXPECT(GC_have_errors, FALSE))
293 GC_print_all_errors();
c521d2c @ivmai gc4.13 tarball import
ivmai authored
294 GC_INVOKE_FINALIZERS();
9dbe549 @ivmai Enable 'Force GC at every GC_malloc' debug-related functionality
ivmai authored
295 GC_DBG_COLLECT_AT_MALLOC(lb);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
296 LOCK();
089667c @ivmai Use EXPECT for checking various 'initialized' boolean variables
ivmai authored
297 if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
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
301 GC_collect_a_little_inner(1);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
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
305 /* reclaimed. */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
306 {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
307 struct hblk ** rlh = ok -> ok_reclaim_list;
308 struct hblk * hbp;
309 hdr * hhdr;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
310
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
311 rlh += lg;
312 while ((hbp = *rlh) != 0) {
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
313 hhdr = HDR(hbp);
314 *rlh = hhdr -> hb_next;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
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
ivmai authored
325
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
326 if (my_bytes_allocd_tmp != 0) {
8e44959 @ivmai Add missing 'volatile' for AO_t variable definitions
ivmai authored
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
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
ivmai authored
339 if (op != 0) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
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 */
d4f36a0 @neleai Fix more typos in comments
neleai authored
369 /* must decrement fl_builder_count before reacquiring */
370 /* the lock. Hopefully this path is rare. */
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
371 }
372 # endif
373 }
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
374 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
378 opp = &(GC_obj_kinds[k].ok_freelist[lg]);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
379 if ( (op = *opp) != 0 ) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
380 *opp = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
381 my_bytes_allocd = 0;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
382 for (p = op; p != 0; p = obj_link(p)) {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
383 my_bytes_allocd += lb;
2c23fbe @ivmai Resolve "comparison of signed and unsigned values" compiler warnings.
ivmai authored
384 if ((word)my_bytes_allocd >= HBLKSIZE) {
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
385 *opp = obj_link(p);
386 obj_link(p) = 0;
387 break;
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
388 }
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
389 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
390 GC_bytes_allocd += my_bytes_allocd;
391 goto out;
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
392 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
393 /* Next try to allocate a new block worth of objects of this size. */
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
394 {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
421 }
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
427
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
428 out:
429 *result = op;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
430 UNLOCK();
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
431 (void) GC_clear_stack(0);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
432 }
433
03fa669 2010-02-04 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
436 GC_API void * GC_CALL GC_malloc_many(size_t lb)
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
437 {
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
438 void *result;
03fa669 2010-02-04 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
441 NORMAL, &result);
9f27ae7 @ivmai gc6.0 tarball import
ivmai authored
442 return result;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
443 }
444
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
445 /* Not well tested nor integrated. */
446 /* Debug version is tricky and currently missing. */
f363243 @ivmai gc6.1 tarball import
ivmai authored
447 #include <limits.h>
448
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
449 GC_API void * GC_CALL GC_memalign(size_t align, size_t lb)
450 {
f363243 @ivmai gc6.1 tarball import
ivmai authored
451 size_t new_lb;
452 size_t offset;
453 ptr_t result;
454
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
455 if (align <= GRANULE_BYTES) return GC_malloc(lb);
f363243 @ivmai gc6.1 tarball import
ivmai authored
456 if (align >= HBLKSIZE/2 || lb >= HBLKSIZE/2) {
20031fb 2009-09-10 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
457 if (align > HBLKSIZE) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
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
464 /* is a multiple of align. That would be correct up to HBLKSIZE. */
f363243 @ivmai gc6.1 tarball import
ivmai authored
465 new_lb = lb + align - 1;
466 result = GC_malloc(new_lb);
d4f36a0 @neleai Fix more typos in comments
neleai authored
467 /* It is OK not to check result for NULL as in that case */
ceeb33a @ivmai Add comment in GC_memalign
ivmai authored
468 /* GC_memalign returns NULL too since (0 + 0 % align) is 0. */
f363243 @ivmai gc6.1 tarball import
ivmai authored
469 offset = (word)result % align;
470 if (offset != 0) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
471 offset = align - offset;
f363243 @ivmai gc6.1 tarball import
ivmai authored
472 if (!GC_all_interior_pointers) {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
473 if (offset >= VALID_OFFSET_SZ) return GC_malloc(HBLKSIZE);
474 GC_register_displacement(offset);
475 }
f363243 @ivmai gc6.1 tarball import
ivmai authored
476 }
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
477 result = (void *) ((ptr_t)result + offset);
f363243 @ivmai gc6.1 tarball import
ivmai authored
478 GC_ASSERT((word)result % align == 0);
479 return result;
480 }
481
af45302 @neleai Fix typos in comments
neleai authored
482 /* This one exists largely to redirect posix_memalign for leaks finding. */
bfda2d0 2011-03-27 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
504 #ifdef ATOMIC_UNCOLLECTABLE
af45302 @neleai Fix typos in comments
neleai authored
505 /* Allocate lb bytes of pointer-free, untraced, uncollectible data */
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
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
ivmai authored
510 void *op;
511 void **opp;
512 size_t lg;
513 DCL_LOCK_STATE;
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
514
515 if( SMALL_OBJ(lb) ) {
9dbe549 @ivmai Enable 'Force GC at every GC_malloc' debug-related functionality
ivmai authored
516 GC_DBG_COLLECT_AT_MALLOC(lb);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
517 if (EXTRA_BYTES != 0 && lb != 0) lb--;
518 /* We don't need the extra byte, since this won't be */
519 /* collected anyway. */
520 lg = GC_size_map[lb];
521 opp = &(GC_auobjfreelist[lg]);
522 LOCK();
33dc2de @ivmai Use EXPECT in malloc result testing for NULL
ivmai authored
523 op = *opp;
524 if (EXPECT(0 != op, TRUE)) {
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
525 *opp = obj_link(op);
526 obj_link(op) = 0;
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
527 GC_bytes_allocd += GRANULES_TO_BYTES(lg);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
528 /* Mark bit was already set while object was on free list. */
e35a417 @ivmai gc7.0alpha1 tarball import
ivmai authored
529 GC_non_gc_bytes += GRANULES_TO_BYTES(lg);
3c50a68 @ivmai gc7.0alpha5 tarball import
ivmai authored
530 UNLOCK();
531 } else {
532 UNLOCK();
533 op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
534 }
535 GC_ASSERT(0 == op || GC_is_marked(op));
3c50a68 @ivmai gc7.0alpha5 tarball import
ivmai authored
536 return((void *) op);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
537 } else {
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
538 hdr * hhdr;
539
540 op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
3c50a68 @ivmai gc7.0alpha5 tarball import
ivmai authored
541 if (0 == op) return(0);
542
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
543 GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0);
544 hhdr = HDR(op);
545
546 LOCK();
547 set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
5b25be3 @ivmai Fix assertion in GC_malloc_[atomic_]uncollectable (THREADS case only)
ivmai authored
548 # ifndef THREADS
549 GC_ASSERT(hhdr -> hb_n_marks == 0);
550 # endif
db25651 2009-09-16 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
551 hhdr -> hb_n_marks = 1;
552 UNLOCK();
553 return((void *) op);
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
554 }
c06769b 2009-10-22 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
555 }
f9b1aa2 @ivmai gc4.11 tarball import
ivmai authored
556 #endif /* ATOMIC_UNCOLLECTABLE */
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
557
558 /* provide a version of strdup() that uses the collector to allocate the
559 copy of the string */
560 GC_API char * GC_CALL GC_strdup(const char *s)
561 {
562 char *copy;
563 size_t lb;
564 if (s == NULL) return NULL;
565 lb = strlen(s) + 1;
566 if ((copy = GC_malloc_atomic(lb)) == NULL) {
567 # ifndef MSWINCE
568 errno = ENOMEM;
569 # endif
570 return NULL;
571 }
943c0eb @ivmai Use memcpy (BCOPY) instead of strcpy (to suppress GCC warning)
ivmai authored
572 BCOPY(s, copy, lb);
00ee2f7 2011-04-09 Ivan Maidanski <ivmai@mail.ru>
ivmai authored
573 return copy;
574 }
575
576 GC_API char * GC_CALL GC_strndup(const char *str, size_t size)
577 {
578 char *copy;
579 size_t len = strlen(str); /* str is expected to be non-NULL */
580 if (len > size)
581 len = size;
582 copy = GC_malloc_atomic(len + 1);
583 if (copy == NULL) {
584 # ifndef MSWINCE
585 errno = ENOMEM;
586 # endif
587 return NULL;
588 }
589 BCOPY(str, copy, len);
590 copy[len] = '\0';
591 return copy;
592 }
593
594 #ifdef GC_REQUIRE_WCSDUP
595 # include <wchar.h> /* for wcslen() */
596
597 GC_API wchar_t * GC_CALL GC_wcsdup(const wchar_t *str)
598 {
599 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
600 wchar_t *copy = GC_malloc_atomic(lb);
601 if (copy == NULL) {
602 # ifndef MSWINCE
603 errno = ENOMEM;
604 # endif
605 return NULL;
606 }
607 BCOPY(str, copy, lb);
608 return copy;
609 }
610 #endif /* GC_REQUIRE_WCSDUP */
Something went wrong with that request. Please try again.