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