| 1 |
/* |
| 2 |
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved. |
| 3 |
* Copyright (c) 1999-2000 by Hewlett-Packard Company. All rights reserved. |
| 4 |
* |
| 5 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 6 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 7 |
* |
| 8 |
* Permission is hereby granted to use or copy this program |
| 9 |
* for any purpose, provided the above notices are retained on all copies. |
| 10 |
* Permission to modify the code and to distribute modified code is granted, |
| 11 |
* provided the above notices are retained, and a notice that the code was |
| 12 |
* modified is included with the above copyright notice. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "private/gc_pmark.h" |
| 17 |
#include "gc_inline.h" /* for GC_malloc_kind */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Some simple primitives for allocation with explicit type information. |
| 21 |
* Simple objects are allocated such that they contain a GC_descr at the |
| 22 |
* end (in the last allocated word). This descriptor may be a procedure |
| 23 |
* which then examines an extended descriptor passed as its environment. |
| 24 |
* |
| 25 |
* Arrays are treated as simple objects if they have sufficiently simple |
| 26 |
* structure. Otherwise they are allocated from an array kind that supplies |
| 27 |
* a special mark procedure. These arrays contain a pointer to a |
| 28 |
* complex_descriptor as their last word. |
| 29 |
* This is done because the environment field is too small, and the collector |
| 30 |
* must trace the complex_descriptor. |
| 31 |
* |
| 32 |
* Note that descriptors inside objects may appear cleared, if we encounter a |
| 33 |
* false reference to an object on a free list. In the GC_descr case, this |
| 34 |
* is OK, since a 0 descriptor corresponds to examining no fields. |
| 35 |
* In the complex_descriptor case, we explicitly check for that case. |
| 36 |
* |
| 37 |
* MAJOR PARTS OF THIS CODE HAVE NOT BEEN TESTED AT ALL and are not testable, |
| 38 |
* since they are not accessible through the current interface. |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "gc_typed.h" |
| 42 |
|
| 43 |
#define TYPD_EXTRA_BYTES (sizeof(word) - EXTRA_BYTES) |
| 44 |
|
| 45 |
STATIC int GC_explicit_kind = 0; |
| 46 |
/* Object kind for objects with indirect */ |
| 47 |
/* (possibly extended) descriptors. */ |
| 48 |
|
| 49 |
STATIC int GC_array_kind = 0; |
| 50 |
/* Object kind for objects with complex */ |
| 51 |
/* descriptors and GC_array_mark_proc. */ |
| 52 |
|
| 53 |
/* Array descriptors. GC_array_mark_proc understands these. */ |
| 54 |
/* We may eventually need to add provisions for headers and */ |
| 55 |
/* trailers. Hence we provide for tree structured descriptors, */ |
| 56 |
/* though we don't really use them currently. */ |
| 57 |
|
| 58 |
struct LeafDescriptor { /* Describes simple array. */ |
| 59 |
word ld_tag; |
| 60 |
# define LEAF_TAG 1 |
| 61 |
size_t ld_size; /* bytes per element */ |
| 62 |
/* multiple of ALIGNMENT. */ |
| 63 |
size_t ld_nelements; /* Number of elements. */ |
| 64 |
GC_descr ld_descriptor; /* A simple length, bitmap, */ |
| 65 |
/* or procedure descriptor. */ |
| 66 |
}; |
| 67 |
|
| 68 |
struct ComplexArrayDescriptor { |
| 69 |
word ad_tag; |
| 70 |
# define ARRAY_TAG 2 |
| 71 |
size_t ad_nelements; |
| 72 |
union ComplexDescriptor * ad_element_descr; |
| 73 |
}; |
| 74 |
|
| 75 |
struct SequenceDescriptor { |
| 76 |
word sd_tag; |
| 77 |
# define SEQUENCE_TAG 3 |
| 78 |
union ComplexDescriptor * sd_first; |
| 79 |
union ComplexDescriptor * sd_second; |
| 80 |
}; |
| 81 |
|
| 82 |
typedef union ComplexDescriptor { |
| 83 |
struct LeafDescriptor ld; |
| 84 |
struct ComplexArrayDescriptor ad; |
| 85 |
struct SequenceDescriptor sd; |
| 86 |
} complex_descriptor; |
| 87 |
#define TAG ad.ad_tag |
| 88 |
|
| 89 |
#define ED_INITIAL_SIZE 100 |
| 90 |
|
| 91 |
STATIC int GC_typed_mark_proc_index = 0; /* Indices of my mark */ |
| 92 |
STATIC int GC_array_mark_proc_index = 0; /* procedures. */ |
| 93 |
|
| 94 |
STATIC void GC_push_typed_structures_proc(void) |
| 95 |
{ |
| 96 |
GC_PUSH_ALL_SYM(GC_ext_descriptors); |
| 97 |
} |
| 98 |
|
| 99 |
/* Add a multiword bitmap to GC_ext_descriptors arrays. Return */ |
| 100 |
/* starting index. */ |
| 101 |
/* Returns -1 on failure. */ |
| 102 |
/* Caller does not hold allocation lock. */ |
| 103 |
STATIC signed_word GC_add_ext_descriptor(const word * bm, word nbits) |
| 104 |
{ |
| 105 |
size_t nwords = divWORDSZ(nbits + WORDSZ-1); |
| 106 |
signed_word result; |
| 107 |
size_t i; |
| 108 |
word last_part; |
| 109 |
size_t extra_bits; |
| 110 |
DCL_LOCK_STATE; |
| 111 |
|
| 112 |
LOCK(); |
| 113 |
while (GC_avail_descr + nwords >= GC_ed_size) { |
| 114 |
typed_ext_descr_t *newExtD; |
| 115 |
size_t new_size; |
| 116 |
word ed_size = GC_ed_size; |
| 117 |
|
| 118 |
if (ed_size == 0) { |
| 119 |
GC_ASSERT((word)(&GC_ext_descriptors) % sizeof(word) == 0); |
| 120 |
GC_push_typed_structures = GC_push_typed_structures_proc; |
| 121 |
UNLOCK(); |
| 122 |
new_size = ED_INITIAL_SIZE; |
| 123 |
} else { |
| 124 |
UNLOCK(); |
| 125 |
new_size = 2 * ed_size; |
| 126 |
if (new_size > MAX_ENV) return(-1); |
| 127 |
} |
| 128 |
newExtD = (typed_ext_descr_t*)GC_malloc_atomic(new_size |
| 129 |
* sizeof(typed_ext_descr_t)); |
| 130 |
if (NULL == newExtD) |
| 131 |
return -1; |
| 132 |
LOCK(); |
| 133 |
if (ed_size == GC_ed_size) { |
| 134 |
if (GC_avail_descr != 0) { |
| 135 |
BCOPY(GC_ext_descriptors, newExtD, |
| 136 |
GC_avail_descr * sizeof(typed_ext_descr_t)); |
| 137 |
} |
| 138 |
GC_ed_size = new_size; |
| 139 |
GC_ext_descriptors = newExtD; |
| 140 |
} /* else another thread already resized it in the meantime */ |
| 141 |
} |
| 142 |
result = GC_avail_descr; |
| 143 |
for (i = 0; i < nwords-1; i++) { |
| 144 |
GC_ext_descriptors[result + i].ed_bitmap = bm[i]; |
| 145 |
GC_ext_descriptors[result + i].ed_continued = TRUE; |
| 146 |
} |
| 147 |
last_part = bm[i]; |
| 148 |
/* Clear irrelevant bits. */ |
| 149 |
extra_bits = nwords * WORDSZ - nbits; |
| 150 |
last_part <<= extra_bits; |
| 151 |
last_part >>= extra_bits; |
| 152 |
GC_ext_descriptors[result + i].ed_bitmap = last_part; |
| 153 |
GC_ext_descriptors[result + i].ed_continued = FALSE; |
| 154 |
GC_avail_descr += nwords; |
| 155 |
UNLOCK(); |
| 156 |
return(result); |
| 157 |
} |
| 158 |
|
| 159 |
/* Table of bitmap descriptors for n word long all pointer objects. */ |
| 160 |
STATIC GC_descr GC_bm_table[WORDSZ/2]; |
| 161 |
|
| 162 |
/* Return a descriptor for the concatenation of 2 nwords long objects, */ |
| 163 |
/* each of which is described by descriptor. */ |
| 164 |
/* The result is known to be short enough to fit into a bitmap */ |
| 165 |
/* descriptor. */ |
| 166 |
/* Descriptor is a GC_DS_LENGTH or GC_DS_BITMAP descriptor. */ |
| 167 |
STATIC GC_descr GC_double_descr(GC_descr descriptor, word nwords) |
| 168 |
{ |
| 169 |
if ((descriptor & GC_DS_TAGS) == GC_DS_LENGTH) { |
| 170 |
descriptor = GC_bm_table[BYTES_TO_WORDS((word)descriptor)]; |
| 171 |
} |
| 172 |
descriptor |= (descriptor & ~(GC_descr)GC_DS_TAGS) >> nwords; |
| 173 |
return(descriptor); |
| 174 |
} |
| 175 |
|
| 176 |
STATIC complex_descriptor * |
| 177 |
GC_make_sequence_descriptor(complex_descriptor *first, |
| 178 |
complex_descriptor *second); |
| 179 |
|
| 180 |
/* Build a descriptor for an array with nelements elements, */ |
| 181 |
/* each of which can be described by a simple descriptor. */ |
| 182 |
/* We try to optimize some common cases. */ |
| 183 |
/* If the result is COMPLEX, then a complex_descr* is returned */ |
| 184 |
/* in *complex_d. */ |
| 185 |
/* If the result is LEAF, then we built a LeafDescriptor in */ |
| 186 |
/* the structure pointed to by leaf. */ |
| 187 |
/* The tag in the leaf structure is not set. */ |
| 188 |
/* If the result is SIMPLE, then a GC_descr */ |
| 189 |
/* is returned in *simple_d. */ |
| 190 |
/* If the result is NO_MEM, then */ |
| 191 |
/* we failed to allocate the descriptor. */ |
| 192 |
/* The implementation knows that GC_DS_LENGTH is 0. */ |
| 193 |
/* *leaf, *complex_d, and *simple_d may be used as temporaries */ |
| 194 |
/* during the construction. */ |
| 195 |
#define COMPLEX 2 |
| 196 |
#define LEAF 1 |
| 197 |
#define SIMPLE 0 |
| 198 |
#define NO_MEM (-1) |
| 199 |
STATIC int GC_make_array_descriptor(size_t nelements, size_t size, |
| 200 |
GC_descr descriptor, GC_descr *simple_d, |
| 201 |
complex_descriptor **complex_d, |
| 202 |
struct LeafDescriptor * leaf) |
| 203 |
{ |
| 204 |
# define OPT_THRESHOLD 50 |
| 205 |
/* For larger arrays, we try to combine descriptors of adjacent */ |
| 206 |
/* descriptors to speed up marking, and to reduce the amount */ |
| 207 |
/* of space needed on the mark stack. */ |
| 208 |
if ((descriptor & GC_DS_TAGS) == GC_DS_LENGTH) { |
| 209 |
if (descriptor == (GC_descr)size) { |
| 210 |
*simple_d = nelements * descriptor; /* no overflow */ |
| 211 |
return(SIMPLE); |
| 212 |
} else if ((word)descriptor == 0) { |
| 213 |
*simple_d = (GC_descr)0; |
| 214 |
return(SIMPLE); |
| 215 |
} |
| 216 |
} |
| 217 |
if (nelements <= OPT_THRESHOLD) { |
| 218 |
if (nelements <= 1) { |
| 219 |
if (nelements == 1) { |
| 220 |
*simple_d = descriptor; |
| 221 |
return(SIMPLE); |
| 222 |
} else { |
| 223 |
*simple_d = (GC_descr)0; |
| 224 |
return(SIMPLE); |
| 225 |
} |
| 226 |
} |
| 227 |
} else if (size <= BITMAP_BITS/2 |
| 228 |
&& (descriptor & GC_DS_TAGS) != GC_DS_PROC |
| 229 |
&& (size & (sizeof(word)-1)) == 0) { |
| 230 |
int result = |
| 231 |
GC_make_array_descriptor(nelements/2, 2*size, |
| 232 |
GC_double_descr(descriptor, |
| 233 |
BYTES_TO_WORDS(size)), |
| 234 |
simple_d, complex_d, leaf); |
| 235 |
if ((nelements & 1) == 0) { |
| 236 |
return(result); |
| 237 |
} else { |
| 238 |
struct LeafDescriptor * one_element = |
| 239 |
(struct LeafDescriptor *) |
| 240 |
GC_malloc_atomic(sizeof(struct LeafDescriptor)); |
| 241 |
|
| 242 |
if (result == NO_MEM || one_element == 0) return(NO_MEM); |
| 243 |
one_element -> ld_tag = LEAF_TAG; |
| 244 |
one_element -> ld_size = size; |
| 245 |
one_element -> ld_nelements = 1; |
| 246 |
one_element -> ld_descriptor = descriptor; |
| 247 |
switch(result) { |
| 248 |
case SIMPLE: |
| 249 |
{ |
| 250 |
struct LeafDescriptor * beginning = |
| 251 |
(struct LeafDescriptor *) |
| 252 |
GC_malloc_atomic(sizeof(struct LeafDescriptor)); |
| 253 |
if (beginning == 0) return(NO_MEM); |
| 254 |
beginning -> ld_tag = LEAF_TAG; |
| 255 |
beginning -> ld_size = size; |
| 256 |
beginning -> ld_nelements = 1; |
| 257 |
beginning -> ld_descriptor = *simple_d; |
| 258 |
*complex_d = GC_make_sequence_descriptor( |
| 259 |
(complex_descriptor *)beginning, |
| 260 |
(complex_descriptor *)one_element); |
| 261 |
break; |
| 262 |
} |
| 263 |
case LEAF: |
| 264 |
{ |
| 265 |
struct LeafDescriptor * beginning = |
| 266 |
(struct LeafDescriptor *) |
| 267 |
GC_malloc_atomic(sizeof(struct LeafDescriptor)); |
| 268 |
if (beginning == 0) return(NO_MEM); |
| 269 |
beginning -> ld_tag = LEAF_TAG; |
| 270 |
beginning -> ld_size = leaf -> ld_size; |
| 271 |
beginning -> ld_nelements = leaf -> ld_nelements; |
| 272 |
beginning -> ld_descriptor = leaf -> ld_descriptor; |
| 273 |
*complex_d = GC_make_sequence_descriptor( |
| 274 |
(complex_descriptor *)beginning, |
| 275 |
(complex_descriptor *)one_element); |
| 276 |
break; |
| 277 |
} |
| 278 |
case COMPLEX: |
| 279 |
*complex_d = GC_make_sequence_descriptor( |
| 280 |
*complex_d, |
| 281 |
(complex_descriptor *)one_element); |
| 282 |
break; |
| 283 |
} |
| 284 |
if (EXPECT(NULL == *complex_d, FALSE)) return NO_MEM; |
| 285 |
|
| 286 |
return(COMPLEX); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
leaf -> ld_size = size; |
| 291 |
leaf -> ld_nelements = nelements; |
| 292 |
leaf -> ld_descriptor = descriptor; |
| 293 |
return(LEAF); |
| 294 |
} |
| 295 |
|
| 296 |
STATIC complex_descriptor * |
| 297 |
GC_make_sequence_descriptor(complex_descriptor *first, |
| 298 |
complex_descriptor *second) |
| 299 |
{ |
| 300 |
struct SequenceDescriptor * result = |
| 301 |
(struct SequenceDescriptor *) |
| 302 |
GC_malloc(sizeof(struct SequenceDescriptor)); |
| 303 |
/* Can't result in overly conservative marking, since tags are */ |
| 304 |
/* very small integers. Probably faster than maintaining type */ |
| 305 |
/* info. */ |
| 306 |
if (result != 0) { |
| 307 |
result -> sd_tag = SEQUENCE_TAG; |
| 308 |
result -> sd_first = first; |
| 309 |
result -> sd_second = second; |
| 310 |
GC_dirty(result); |
| 311 |
REACHABLE_AFTER_DIRTY(first); |
| 312 |
REACHABLE_AFTER_DIRTY(second); |
| 313 |
} |
| 314 |
return((complex_descriptor *)result); |
| 315 |
} |
| 316 |
|
| 317 |
STATIC mse * GC_typed_mark_proc(word * addr, mse * mark_stack_ptr, |
| 318 |
mse * mark_stack_limit, word env); |
| 319 |
|
| 320 |
STATIC mse * GC_array_mark_proc(word * addr, mse * mark_stack_ptr, |
| 321 |
mse * mark_stack_limit, word env); |
| 322 |
|
| 323 |
STATIC void GC_init_explicit_typing(void) |
| 324 |
{ |
| 325 |
unsigned i; |
| 326 |
|
| 327 |
GC_STATIC_ASSERT(sizeof(struct LeafDescriptor) % sizeof(word) == 0); |
| 328 |
/* Set up object kind with simple indirect descriptor. */ |
| 329 |
GC_explicit_kind = GC_new_kind_inner(GC_new_free_list_inner(), |
| 330 |
(WORDS_TO_BYTES((word)-1) | GC_DS_PER_OBJECT), |
| 331 |
TRUE, TRUE); |
| 332 |
/* Descriptors are in the last word of the object. */ |
| 333 |
GC_typed_mark_proc_index = GC_new_proc_inner(GC_typed_mark_proc); |
| 334 |
/* Set up object kind with array descriptor. */ |
| 335 |
GC_array_mark_proc_index = GC_new_proc_inner(GC_array_mark_proc); |
| 336 |
GC_array_kind = GC_new_kind_inner(GC_new_free_list_inner(), |
| 337 |
GC_MAKE_PROC(GC_array_mark_proc_index, 0), |
| 338 |
FALSE, TRUE); |
| 339 |
GC_bm_table[0] = GC_DS_BITMAP; |
| 340 |
for (i = 1; i < WORDSZ/2; i++) { |
| 341 |
GC_bm_table[i] = (((word)-1) << (WORDSZ - i)) | GC_DS_BITMAP; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
STATIC mse * GC_typed_mark_proc(word * addr, mse * mark_stack_ptr, |
| 346 |
mse * mark_stack_limit, word env) |
| 347 |
{ |
| 348 |
word bm = GC_ext_descriptors[env].ed_bitmap; |
| 349 |
word * current_p = addr; |
| 350 |
word current; |
| 351 |
ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr; |
| 352 |
ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr; |
| 353 |
DECLARE_HDR_CACHE; |
| 354 |
|
| 355 |
INIT_HDR_CACHE; |
| 356 |
for (; bm != 0; bm >>= 1, current_p++) { |
| 357 |
if (bm & 1) { |
| 358 |
current = *current_p; |
| 359 |
FIXUP_POINTER(current); |
| 360 |
if (current >= (word)least_ha && current <= (word)greatest_ha) { |
| 361 |
PUSH_CONTENTS((ptr_t)current, mark_stack_ptr, |
| 362 |
mark_stack_limit, (ptr_t)current_p); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
if (GC_ext_descriptors[env].ed_continued) { |
| 367 |
/* Push an entry with the rest of the descriptor back onto the */ |
| 368 |
/* stack. Thus we never do too much work at once. Note that */ |
| 369 |
/* we also can't overflow the mark stack unless we actually */ |
| 370 |
/* mark something. */ |
| 371 |
mark_stack_ptr++; |
| 372 |
if ((word)mark_stack_ptr >= (word)mark_stack_limit) { |
| 373 |
mark_stack_ptr = GC_signal_mark_stack_overflow(mark_stack_ptr); |
| 374 |
} |
| 375 |
mark_stack_ptr -> mse_start = (ptr_t)(addr + WORDSZ); |
| 376 |
mark_stack_ptr -> mse_descr.w = |
| 377 |
GC_MAKE_PROC(GC_typed_mark_proc_index, env + 1); |
| 378 |
} |
| 379 |
return(mark_stack_ptr); |
| 380 |
} |
| 381 |
|
| 382 |
/* Return the size of the object described by d. It would be faster to */ |
| 383 |
/* store this directly, or to compute it as part of */ |
| 384 |
/* GC_push_complex_descriptor, but hopefully it doesn't matter. */ |
| 385 |
STATIC word GC_descr_obj_size(complex_descriptor *d) |
| 386 |
{ |
| 387 |
switch(d -> TAG) { |
| 388 |
case LEAF_TAG: |
| 389 |
return(d -> ld.ld_nelements * d -> ld.ld_size); |
| 390 |
case ARRAY_TAG: |
| 391 |
return(d -> ad.ad_nelements |
| 392 |
* GC_descr_obj_size(d -> ad.ad_element_descr)); |
| 393 |
case SEQUENCE_TAG: |
| 394 |
return(GC_descr_obj_size(d -> sd.sd_first) |
| 395 |
+ GC_descr_obj_size(d -> sd.sd_second)); |
| 396 |
default: |
| 397 |
ABORT_RET("Bad complex descriptor"); |
| 398 |
return 0; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/* Push descriptors for the object at addr with complex descriptor d */ |
| 403 |
/* onto the mark stack. Return 0 if the mark stack overflowed. */ |
| 404 |
STATIC mse * GC_push_complex_descriptor(word *addr, complex_descriptor *d, |
| 405 |
mse *msp, mse *msl) |
| 406 |
{ |
| 407 |
ptr_t current = (ptr_t)addr; |
| 408 |
word nelements; |
| 409 |
word sz; |
| 410 |
word i; |
| 411 |
|
| 412 |
switch(d -> TAG) { |
| 413 |
case LEAF_TAG: |
| 414 |
{ |
| 415 |
GC_descr descr = d -> ld.ld_descriptor; |
| 416 |
|
| 417 |
nelements = d -> ld.ld_nelements; |
| 418 |
if (msl - msp <= (ptrdiff_t)nelements) return(0); |
| 419 |
sz = d -> ld.ld_size; |
| 420 |
for (i = 0; i < nelements; i++) { |
| 421 |
msp++; |
| 422 |
msp -> mse_start = current; |
| 423 |
msp -> mse_descr.w = descr; |
| 424 |
current += sz; |
| 425 |
} |
| 426 |
return(msp); |
| 427 |
} |
| 428 |
case ARRAY_TAG: |
| 429 |
{ |
| 430 |
complex_descriptor *descr = d -> ad.ad_element_descr; |
| 431 |
|
| 432 |
nelements = d -> ad.ad_nelements; |
| 433 |
sz = GC_descr_obj_size(descr); |
| 434 |
for (i = 0; i < nelements; i++) { |
| 435 |
msp = GC_push_complex_descriptor((word *)current, descr, |
| 436 |
msp, msl); |
| 437 |
if (msp == 0) return(0); |
| 438 |
current += sz; |
| 439 |
} |
| 440 |
return(msp); |
| 441 |
} |
| 442 |
case SEQUENCE_TAG: |
| 443 |
{ |
| 444 |
sz = GC_descr_obj_size(d -> sd.sd_first); |
| 445 |
msp = GC_push_complex_descriptor((word *)current, d -> sd.sd_first, |
| 446 |
msp, msl); |
| 447 |
if (msp == 0) return(0); |
| 448 |
current += sz; |
| 449 |
msp = GC_push_complex_descriptor((word *)current, d -> sd.sd_second, |
| 450 |
msp, msl); |
| 451 |
return(msp); |
| 452 |
} |
| 453 |
default: |
| 454 |
ABORT_RET("Bad complex descriptor"); |
| 455 |
return 0; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
GC_ATTR_NO_SANITIZE_THREAD |
| 460 |
static complex_descriptor *get_complex_descr(word *addr, word nwords) |
| 461 |
{ |
| 462 |
return (complex_descriptor *)addr[nwords - 1]; |
| 463 |
} |
| 464 |
|
| 465 |
#ifdef AO_HAVE_store_release |
| 466 |
# define set_obj_descr(op, nwords, d) \ |
| 467 |
AO_store_release((volatile AO_t *)(op) + (nwords) - 1, (AO_t)(d)) |
| 468 |
#else |
| 469 |
# define set_obj_descr(op, nwords, d) \ |
| 470 |
(void)(((word *)(op))[(nwords) - 1] = (word)(d)) |
| 471 |
#endif |
| 472 |
|
| 473 |
STATIC mse * GC_array_mark_proc(word * addr, mse * mark_stack_ptr, |
| 474 |
mse * mark_stack_limit, |
| 475 |
word env GC_ATTR_UNUSED) |
| 476 |
{ |
| 477 |
hdr * hhdr = HDR(addr); |
| 478 |
word sz = hhdr -> hb_sz; |
| 479 |
word nwords = BYTES_TO_WORDS(sz); |
| 480 |
complex_descriptor *descr = get_complex_descr(addr, nwords); |
| 481 |
mse * orig_mark_stack_ptr = mark_stack_ptr; |
| 482 |
mse * new_mark_stack_ptr; |
| 483 |
|
| 484 |
if (descr == 0) { |
| 485 |
/* Found a reference to a free list entry. Ignore it. */ |
| 486 |
return(orig_mark_stack_ptr); |
| 487 |
} |
| 488 |
/* In use counts were already updated when array descriptor was */ |
| 489 |
/* pushed. Here we only replace it by subobject descriptors, so */ |
| 490 |
/* no update is necessary. */ |
| 491 |
new_mark_stack_ptr = GC_push_complex_descriptor(addr, descr, |
| 492 |
mark_stack_ptr, |
| 493 |
mark_stack_limit-1); |
| 494 |
if (new_mark_stack_ptr == 0) { |
| 495 |
/* Explicitly instruct Clang Static Analyzer that ptr is non-null. */ |
| 496 |
if (NULL == mark_stack_ptr) ABORT("Bad mark_stack_ptr"); |
| 497 |
|
| 498 |
/* Doesn't fit. Conservatively push the whole array as a unit */ |
| 499 |
/* and request a mark stack expansion. */ |
| 500 |
/* This cannot cause a mark stack overflow, since it replaces */ |
| 501 |
/* the original array entry. */ |
| 502 |
# ifdef PARALLEL_MARK |
| 503 |
/* We might be using a local_mark_stack in parallel mode. */ |
| 504 |
if (GC_mark_stack + GC_mark_stack_size == mark_stack_limit) |
| 505 |
# endif |
| 506 |
{ |
| 507 |
GC_mark_stack_too_small = TRUE; |
| 508 |
} |
| 509 |
new_mark_stack_ptr = orig_mark_stack_ptr + 1; |
| 510 |
new_mark_stack_ptr -> mse_start = (ptr_t)addr; |
| 511 |
new_mark_stack_ptr -> mse_descr.w = sz | GC_DS_LENGTH; |
| 512 |
} else { |
| 513 |
/* Push descriptor itself */ |
| 514 |
new_mark_stack_ptr++; |
| 515 |
new_mark_stack_ptr -> mse_start = (ptr_t)(addr + nwords - 1); |
| 516 |
new_mark_stack_ptr -> mse_descr.w = sizeof(word) | GC_DS_LENGTH; |
| 517 |
} |
| 518 |
return new_mark_stack_ptr; |
| 519 |
} |
| 520 |
|
| 521 |
GC_API GC_descr GC_CALL GC_make_descriptor(const GC_word * bm, size_t len) |
| 522 |
{ |
| 523 |
signed_word last_set_bit = (signed_word)len - 1; |
| 524 |
GC_descr result; |
| 525 |
DCL_LOCK_STATE; |
| 526 |
|
| 527 |
# if defined(AO_HAVE_load_acquire) && defined(AO_HAVE_store_release) |
| 528 |
if (!EXPECT(AO_load_acquire(&GC_explicit_typing_initialized), TRUE)) { |
| 529 |
LOCK(); |
| 530 |
if (!GC_explicit_typing_initialized) { |
| 531 |
GC_init_explicit_typing(); |
| 532 |
AO_store_release(&GC_explicit_typing_initialized, TRUE); |
| 533 |
} |
| 534 |
UNLOCK(); |
| 535 |
} |
| 536 |
# else |
| 537 |
LOCK(); |
| 538 |
if (!EXPECT(GC_explicit_typing_initialized, TRUE)) { |
| 539 |
GC_init_explicit_typing(); |
| 540 |
GC_explicit_typing_initialized = TRUE; |
| 541 |
} |
| 542 |
UNLOCK(); |
| 543 |
# endif |
| 544 |
|
| 545 |
while (last_set_bit >= 0 && !GC_get_bit(bm, last_set_bit)) |
| 546 |
last_set_bit--; |
| 547 |
if (last_set_bit < 0) return(0 /* no pointers */); |
| 548 |
|
| 549 |
# if ALIGNMENT == CPP_WORDSZ/8 |
| 550 |
{ |
| 551 |
signed_word i; |
| 552 |
|
| 553 |
for (i = 0; i < last_set_bit; i++) { |
| 554 |
if (!GC_get_bit(bm, i)) { |
| 555 |
break; |
| 556 |
} |
| 557 |
} |
| 558 |
if (i == last_set_bit) { |
| 559 |
/* An initial section contains all pointers. Use length descriptor. */ |
| 560 |
return (WORDS_TO_BYTES(last_set_bit+1) | GC_DS_LENGTH); |
| 561 |
} |
| 562 |
} |
| 563 |
# endif |
| 564 |
if ((word)last_set_bit < BITMAP_BITS) { |
| 565 |
signed_word i; |
| 566 |
|
| 567 |
/* Hopefully the common case. */ |
| 568 |
/* Build bitmap descriptor (with bits reversed) */ |
| 569 |
result = SIGNB; |
| 570 |
for (i = last_set_bit - 1; i >= 0; i--) { |
| 571 |
result >>= 1; |
| 572 |
if (GC_get_bit(bm, i)) result |= SIGNB; |
| 573 |
} |
| 574 |
result |= GC_DS_BITMAP; |
| 575 |
} else { |
| 576 |
signed_word index = GC_add_ext_descriptor(bm, (word)last_set_bit + 1); |
| 577 |
if (index == -1) return(WORDS_TO_BYTES(last_set_bit+1) | GC_DS_LENGTH); |
| 578 |
/* Out of memory: use conservative */ |
| 579 |
/* approximation. */ |
| 580 |
result = GC_MAKE_PROC(GC_typed_mark_proc_index, index); |
| 581 |
} |
| 582 |
return result; |
| 583 |
} |
| 584 |
|
| 585 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_explicitly_typed(size_t lb, |
| 586 |
GC_descr d) |
| 587 |
{ |
| 588 |
word *op; |
| 589 |
size_t lg; |
| 590 |
|
| 591 |
GC_ASSERT(GC_explicit_typing_initialized); |
| 592 |
if (EXPECT(0 == lb, FALSE)) lb = 1; /* ensure nwords > 1 */ |
| 593 |
lb = SIZET_SAT_ADD(lb, TYPD_EXTRA_BYTES); |
| 594 |
op = (word *)GC_malloc_kind(lb, GC_explicit_kind); |
| 595 |
if (EXPECT(NULL == op, FALSE)) |
| 596 |
return NULL; |
| 597 |
/* It is not safe to use GC_size_map[lb] to compute lg here as */ |
| 598 |
/* the former might be updated asynchronously. */ |
| 599 |
lg = BYTES_TO_GRANULES(GC_size(op)); |
| 600 |
set_obj_descr(op, GRANULES_TO_WORDS(lg), d); |
| 601 |
return op; |
| 602 |
} |
| 603 |
|
| 604 |
/* We make the GC_clear_stack() call a tail one, hoping to get more of */ |
| 605 |
/* the stack. */ |
| 606 |
#define GENERAL_MALLOC_IOP(lb, k) \ |
| 607 |
GC_clear_stack(GC_generic_malloc_ignore_off_page(lb, k)) |
| 608 |
|
| 609 |
GC_API GC_ATTR_MALLOC void * GC_CALL |
| 610 |
GC_malloc_explicitly_typed_ignore_off_page(size_t lb, GC_descr d) |
| 611 |
{ |
| 612 |
ptr_t op; |
| 613 |
size_t lg; |
| 614 |
DCL_LOCK_STATE; |
| 615 |
|
| 616 |
GC_ASSERT(GC_explicit_typing_initialized); |
| 617 |
if (EXPECT(0 == lb, FALSE)) lb = 1; |
| 618 |
lb = SIZET_SAT_ADD(lb, TYPD_EXTRA_BYTES); |
| 619 |
if (SMALL_OBJ(lb)) { |
| 620 |
void **opp; |
| 621 |
|
| 622 |
GC_DBG_COLLECT_AT_MALLOC(lb); |
| 623 |
LOCK(); |
| 624 |
lg = GC_size_map[lb]; |
| 625 |
opp = &GC_obj_kinds[GC_explicit_kind].ok_freelist[lg]; |
| 626 |
op = (ptr_t)(*opp); |
| 627 |
if (EXPECT(0 == op, FALSE)) { |
| 628 |
UNLOCK(); |
| 629 |
op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_explicit_kind); |
| 630 |
if (0 == op) return 0; |
| 631 |
/* See the comment in GC_malloc_explicitly_typed. */ |
| 632 |
lg = BYTES_TO_GRANULES(GC_size(op)); |
| 633 |
} else { |
| 634 |
*opp = obj_link(op); |
| 635 |
obj_link(op) = 0; |
| 636 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg); |
| 637 |
UNLOCK(); |
| 638 |
} |
| 639 |
} else { |
| 640 |
op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_explicit_kind); |
| 641 |
if (NULL == op) return NULL; |
| 642 |
lg = BYTES_TO_GRANULES(GC_size(op)); |
| 643 |
} |
| 644 |
set_obj_descr(op, GRANULES_TO_WORDS(lg), d); |
| 645 |
return op; |
| 646 |
} |
| 647 |
|
| 648 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_calloc_explicitly_typed(size_t n, |
| 649 |
size_t lb, GC_descr d) |
| 650 |
{ |
| 651 |
word *op; |
| 652 |
size_t lg; |
| 653 |
GC_descr simple_descr; |
| 654 |
complex_descriptor *complex_descr; |
| 655 |
int descr_type; |
| 656 |
struct LeafDescriptor leaf; |
| 657 |
DCL_LOCK_STATE; |
| 658 |
|
| 659 |
GC_ASSERT(GC_explicit_typing_initialized); |
| 660 |
if (EXPECT(0 == lb || 0 == n, FALSE)) lb = n = 1; |
| 661 |
if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial check */ |
| 662 |
&& n > GC_SIZE_MAX / lb) |
| 663 |
return (*GC_get_oom_fn())(GC_SIZE_MAX); /* n*lb overflow */ |
| 664 |
|
| 665 |
descr_type = GC_make_array_descriptor((word)n, (word)lb, d, &simple_descr, |
| 666 |
&complex_descr, &leaf); |
| 667 |
lb *= n; |
| 668 |
switch(descr_type) { |
| 669 |
case NO_MEM: |
| 670 |
return (*GC_get_oom_fn())(lb); |
| 671 |
case SIMPLE: |
| 672 |
return GC_malloc_explicitly_typed(lb, simple_descr); |
| 673 |
case LEAF: |
| 674 |
lb = SIZET_SAT_ADD(lb, |
| 675 |
sizeof(struct LeafDescriptor) + TYPD_EXTRA_BYTES); |
| 676 |
break; |
| 677 |
case COMPLEX: |
| 678 |
lb = SIZET_SAT_ADD(lb, TYPD_EXTRA_BYTES); |
| 679 |
break; |
| 680 |
} |
| 681 |
op = (word *)GC_malloc_kind(lb, GC_array_kind); |
| 682 |
if (EXPECT(NULL == op, FALSE)) |
| 683 |
return NULL; |
| 684 |
lg = BYTES_TO_GRANULES(GC_size(op)); |
| 685 |
if (descr_type == LEAF) { |
| 686 |
/* Set up the descriptor inside the object itself. */ |
| 687 |
struct LeafDescriptor * lp = |
| 688 |
(struct LeafDescriptor *) |
| 689 |
(op + GRANULES_TO_WORDS(lg) |
| 690 |
- (BYTES_TO_WORDS(sizeof(struct LeafDescriptor)) + 1)); |
| 691 |
|
| 692 |
lp -> ld_tag = LEAF_TAG; |
| 693 |
lp -> ld_size = leaf.ld_size; |
| 694 |
lp -> ld_nelements = leaf.ld_nelements; |
| 695 |
lp -> ld_descriptor = leaf.ld_descriptor; |
| 696 |
/* Hold the allocation lock while writing the descriptor word */ |
| 697 |
/* to the object to ensure that the descriptor contents are */ |
| 698 |
/* seen by GC_array_mark_proc as expected. */ |
| 699 |
/* TODO: It should be possible to replace locking with the */ |
| 700 |
/* atomic operations (with the release barrier here) but, in */ |
| 701 |
/* this case, avoiding the acquire barrier in */ |
| 702 |
/* GC_array_mark_proc seems to be tricky as GC_mark_some might */ |
| 703 |
/* be invoked with the world running. */ |
| 704 |
LOCK(); |
| 705 |
op[GRANULES_TO_WORDS(lg) - 1] = (word)lp; |
| 706 |
UNLOCK(); |
| 707 |
} else { |
| 708 |
# ifndef GC_NO_FINALIZATION |
| 709 |
size_t lw = GRANULES_TO_WORDS(lg); |
| 710 |
|
| 711 |
LOCK(); |
| 712 |
op[lw - 1] = (word)complex_descr; |
| 713 |
UNLOCK(); |
| 714 |
|
| 715 |
GC_dirty(op + lw - 1); |
| 716 |
REACHABLE_AFTER_DIRTY(complex_descr); |
| 717 |
|
| 718 |
/* Make sure the descriptor is cleared once there is any danger */ |
| 719 |
/* it may have been collected. */ |
| 720 |
if (EXPECT(GC_general_register_disappearing_link( |
| 721 |
(void **)(op + lw - 1), op) |
| 722 |
== GC_NO_MEMORY, FALSE)) |
| 723 |
# endif |
| 724 |
{ |
| 725 |
/* Couldn't register it due to lack of memory. Punt. */ |
| 726 |
return (*GC_get_oom_fn())(lb); |
| 727 |
} |
| 728 |
} |
| 729 |
return op; |
| 730 |
} |