| 1 |
import sys |
| 2 |
from rpython.memory.gc.semispace import SemiSpaceGC |
| 3 |
from rpython.memory.gc.semispace import GCFLAG_EXTERNAL, GCFLAG_FORWARDED |
| 4 |
from rpython.memory.gc.semispace import GC_HASH_TAKEN_ADDR |
| 5 |
from rpython.memory.gc import env |
| 6 |
from rpython.rtyper.lltypesystem.llmemory import NULL, raw_malloc_usage |
| 7 |
from rpython.rtyper.lltypesystem import lltype, llmemory, llarena |
| 8 |
from rpython.rlib.objectmodel import free_non_gc_object |
| 9 |
from rpython.rlib.debug import ll_assert |
| 10 |
from rpython.rlib.debug import debug_print, debug_start, debug_stop |
| 11 |
from rpython.rlib.rarithmetic import intmask, LONG_BIT |
| 12 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 13 |
|
| 14 |
WORD = LONG_BIT // 8 |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
GCFLAG_NO_YOUNG_PTRS = SemiSpaceGC.first_unused_gcflag << 0 |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
GCFLAG_NO_HEAP_PTRS = SemiSpaceGC.first_unused_gcflag << 1 |
| 29 |
|
| 30 |
class GenerationGC(SemiSpaceGC): |
| 31 |
"""A basic generational GC: it's a SemiSpaceGC with an additional |
| 32 |
nursery for young objects. A write barrier is used to ensure that |
| 33 |
old objects that contain pointers to young objects are recorded in |
| 34 |
a list. |
| 35 |
""" |
| 36 |
inline_simple_malloc = True |
| 37 |
inline_simple_malloc_varsize = True |
| 38 |
needs_write_barrier = True |
| 39 |
prebuilt_gc_objects_are_static_roots = False |
| 40 |
first_unused_gcflag = SemiSpaceGC.first_unused_gcflag << 2 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
TRANSLATION_PARAMS = {'space_size': 8*1024*1024, |
| 45 |
'nursery_size': 3*1024*1024, |
| 46 |
'min_nursery_size': 48*1024, |
| 47 |
'auto_nursery_size': True} |
| 48 |
|
| 49 |
nursery_hash_base = -1 |
| 50 |
|
| 51 |
def __init__(self, config, |
| 52 |
nursery_size=32*WORD, |
| 53 |
min_nursery_size=32*WORD, |
| 54 |
auto_nursery_size=False, |
| 55 |
space_size=1024*WORD, |
| 56 |
max_space_size=sys.maxint//2+1, |
| 57 |
**kwds): |
| 58 |
SemiSpaceGC.__init__(self, config, |
| 59 |
space_size = space_size, |
| 60 |
max_space_size = max_space_size, |
| 61 |
**kwds) |
| 62 |
assert min_nursery_size <= nursery_size <= space_size // 2 |
| 63 |
self.initial_nursery_size = nursery_size |
| 64 |
self.auto_nursery_size = auto_nursery_size |
| 65 |
self.min_nursery_size = min_nursery_size |
| 66 |
|
| 67 |
|
| 68 |
self.reset_nursery() |
| 69 |
self._setup_wb() |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
sz = self.get_young_fixedsize(self.min_nursery_size) |
| 76 |
self.lb_young_fixedsize = sz |
| 77 |
sz = self.get_young_var_basesize(self.min_nursery_size) |
| 78 |
self.lb_young_var_basesize = sz |
| 79 |
|
| 80 |
def setup(self): |
| 81 |
self.old_objects_pointing_to_young = self.AddressStack() |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
self.young_objects_with_weakrefs = self.AddressStack() |
| 87 |
|
| 88 |
self.last_generation_root_objects = self.AddressStack() |
| 89 |
self.young_objects_with_id = self.AddressDict() |
| 90 |
SemiSpaceGC.setup(self) |
| 91 |
self.set_nursery_size(self.initial_nursery_size) |
| 92 |
|
| 93 |
if self.auto_nursery_size: |
| 94 |
newsize = nursery_size_from_env() |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if newsize > 0: |
| 99 |
self.set_nursery_size(newsize) |
| 100 |
|
| 101 |
self.reset_nursery() |
| 102 |
|
| 103 |
def _teardown(self): |
| 104 |
self.collect() |
| 105 |
SemiSpaceGC._teardown(self) |
| 106 |
|
| 107 |
def reset_nursery(self): |
| 108 |
self.nursery = NULL |
| 109 |
self.nursery_top = NULL |
| 110 |
self.nursery_free = NULL |
| 111 |
|
| 112 |
def set_nursery_size(self, newsize): |
| 113 |
debug_start("gc-set-nursery-size") |
| 114 |
if newsize < self.min_nursery_size: |
| 115 |
newsize = self.min_nursery_size |
| 116 |
if newsize > self.space_size // 2: |
| 117 |
newsize = self.space_size // 2 |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
self.nursery_size = newsize |
| 122 |
self.largest_young_fixedsize = self.get_young_fixedsize(newsize) |
| 123 |
self.largest_young_var_basesize = self.get_young_var_basesize(newsize) |
| 124 |
scale = 0 |
| 125 |
while (self.min_nursery_size << (scale+1)) <= newsize: |
| 126 |
scale += 1 |
| 127 |
self.nursery_scale = scale |
| 128 |
debug_print("nursery_size =", newsize) |
| 129 |
debug_print("largest_young_fixedsize =", |
| 130 |
self.largest_young_fixedsize) |
| 131 |
debug_print("largest_young_var_basesize =", |
| 132 |
self.largest_young_var_basesize) |
| 133 |
debug_print("nursery_scale =", scale) |
| 134 |
|
| 135 |
assert self.nursery_size >= (self.min_nursery_size << scale) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
self.semispace_collect() |
| 142 |
debug_stop("gc-set-nursery-size") |
| 143 |
|
| 144 |
@staticmethod |
| 145 |
def get_young_fixedsize(nursery_size): |
| 146 |
return nursery_size // 2 - 1 |
| 147 |
|
| 148 |
@staticmethod |
| 149 |
def get_young_var_basesize(nursery_size): |
| 150 |
return nursery_size // 4 - 1 |
| 151 |
|
| 152 |
@classmethod |
| 153 |
def JIT_max_size_of_young_obj(cls): |
| 154 |
min_nurs_size = cls.TRANSLATION_PARAMS['min_nursery_size'] |
| 155 |
return cls.get_young_fixedsize(min_nurs_size) |
| 156 |
|
| 157 |
def is_in_nursery(self, addr): |
| 158 |
ll_assert(llmemory.cast_adr_to_int(addr) & 1 == 0, |
| 159 |
"odd-valued (i.e. tagged) pointer unexpected here") |
| 160 |
return self.nursery <= addr < self.nursery_top |
| 161 |
|
| 162 |
def appears_to_be_in_nursery(self, addr): |
| 163 |
|
| 164 |
|
| 165 |
if not self.translated_to_c: |
| 166 |
if not self.is_valid_gc_object(addr): |
| 167 |
return False |
| 168 |
return self.nursery <= addr < self.nursery_top |
| 169 |
|
| 170 |
def malloc_fixedsize_clear(self, typeid, size, |
| 171 |
has_finalizer=False, |
| 172 |
is_finalizer_light=False, |
| 173 |
contains_weakptr=False): |
| 174 |
if (has_finalizer or |
| 175 |
(raw_malloc_usage(size) > self.lb_young_fixedsize and |
| 176 |
raw_malloc_usage(size) > self.largest_young_fixedsize)): |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ll_assert(not contains_weakptr, "wrong case for mallocing weakref") |
| 182 |
|
| 183 |
return SemiSpaceGC.malloc_fixedsize_clear(self, typeid, size, |
| 184 |
has_finalizer, |
| 185 |
is_finalizer_light, |
| 186 |
contains_weakptr) |
| 187 |
size_gc_header = self.gcheaderbuilder.size_gc_header |
| 188 |
totalsize = size_gc_header + size |
| 189 |
result = self.nursery_free |
| 190 |
if raw_malloc_usage(totalsize) > self.nursery_top - result: |
| 191 |
result = self.collect_nursery() |
| 192 |
llarena.arena_reserve(result, totalsize) |
| 193 |
|
| 194 |
self.init_gc_object(result, typeid, flags=0) |
| 195 |
self.nursery_free = result + totalsize |
| 196 |
if contains_weakptr: |
| 197 |
self.young_objects_with_weakrefs.append(result + size_gc_header) |
| 198 |
return llmemory.cast_adr_to_ptr(result+size_gc_header, llmemory.GCREF) |
| 199 |
|
| 200 |
def malloc_varsize_clear(self, typeid, length, size, itemsize, |
| 201 |
offset_to_length): |
| 202 |
|
| 203 |
if not raw_malloc_usage(itemsize): |
| 204 |
too_many_items = False |
| 205 |
else: |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
maxlength_for_minimal_nursery = (self.min_nursery_size // 4 // |
| 210 |
raw_malloc_usage(itemsize)) |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
maxlength = maxlength_for_minimal_nursery << self.nursery_scale |
| 218 |
too_many_items = length > maxlength |
| 219 |
|
| 220 |
if (too_many_items or |
| 221 |
(raw_malloc_usage(size) > self.lb_young_var_basesize and |
| 222 |
raw_malloc_usage(size) > self.largest_young_var_basesize)): |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
return SemiSpaceGC.malloc_varsize_clear(self, typeid, length, size, |
| 228 |
itemsize, offset_to_length) |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
size_gc_header = self.gcheaderbuilder.size_gc_header |
| 233 |
totalsize = size_gc_header + size + itemsize * length |
| 234 |
result = self.nursery_free |
| 235 |
if raw_malloc_usage(totalsize) > self.nursery_top - result: |
| 236 |
result = self.collect_nursery() |
| 237 |
llarena.arena_reserve(result, totalsize) |
| 238 |
|
| 239 |
self.init_gc_object(result, typeid, flags=0) |
| 240 |
(result + size_gc_header + offset_to_length).signed[0] = length |
| 241 |
self.nursery_free = result + llarena.round_up_for_allocation(totalsize) |
| 242 |
return llmemory.cast_adr_to_ptr(result+size_gc_header, llmemory.GCREF) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
def init_gc_object(self, addr, typeid, flags=GCFLAG_NO_YOUNG_PTRS): |
| 248 |
SemiSpaceGC.init_gc_object(self, addr, typeid, flags) |
| 249 |
|
| 250 |
def init_gc_object_immortal(self, addr, typeid, |
| 251 |
flags=GCFLAG_NO_YOUNG_PTRS|GCFLAG_NO_HEAP_PTRS): |
| 252 |
SemiSpaceGC.init_gc_object_immortal(self, addr, typeid, flags) |
| 253 |
|
| 254 |
|
| 255 |
GCFLAGS_FOR_NEW_YOUNG_OBJECTS = 0 |
| 256 |
GCFLAGS_FOR_NEW_EXTERNAL_OBJECTS = (GCFLAG_EXTERNAL | GCFLAG_FORWARDED | |
| 257 |
GCFLAG_NO_YOUNG_PTRS | |
| 258 |
GC_HASH_TAKEN_ADDR) |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
def collect(self, gen=1): |
| 264 |
if gen == 0: |
| 265 |
self.collect_nursery() |
| 266 |
else: |
| 267 |
SemiSpaceGC.collect(self) |
| 268 |
|
| 269 |
def semispace_collect(self, size_changing=False): |
| 270 |
self.reset_young_gcflags() |
| 271 |
self.weakrefs_grow_older() |
| 272 |
self.ids_grow_older() |
| 273 |
self.reset_nursery() |
| 274 |
SemiSpaceGC.semispace_collect(self, size_changing) |
| 275 |
|
| 276 |
def make_a_copy(self, obj, objsize): |
| 277 |
tid = self.header(obj).tid |
| 278 |
|
| 279 |
|
| 280 |
tid |= GCFLAG_NO_YOUNG_PTRS |
| 281 |
return self._make_a_copy_with_tid(obj, objsize, tid) |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
def reset_young_gcflags(self): |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
oldlist = self.old_objects_pointing_to_young |
| 296 |
while oldlist.non_empty(): |
| 297 |
obj = oldlist.pop() |
| 298 |
hdr = self.header(obj) |
| 299 |
hdr.tid |= GCFLAG_NO_YOUNG_PTRS |
| 300 |
|
| 301 |
def weakrefs_grow_older(self): |
| 302 |
while self.young_objects_with_weakrefs.non_empty(): |
| 303 |
obj = self.young_objects_with_weakrefs.pop() |
| 304 |
self.objects_with_weakrefs.append(obj) |
| 305 |
|
| 306 |
def collect_roots(self): |
| 307 |
"""GenerationGC: collects all roots. |
| 308 |
HybridGC: collects all roots, excluding the generation 3 ones. |
| 309 |
""" |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
self.collect_last_generation_roots() |
| 319 |
self.root_walker.walk_roots( |
| 320 |
SemiSpaceGC._collect_root, |
| 321 |
SemiSpaceGC._collect_root, |
| 322 |
None) |
| 323 |
|
| 324 |
def collect_last_generation_roots(self): |
| 325 |
stack = self.last_generation_root_objects |
| 326 |
self.last_generation_root_objects = self.AddressStack() |
| 327 |
while stack.non_empty(): |
| 328 |
obj = stack.pop() |
| 329 |
self.header(obj).tid |= GCFLAG_NO_HEAP_PTRS |
| 330 |
|
| 331 |
|
| 332 |
self.trace(obj, self.make_callback('_trace_external_obj'), self, obj) |
| 333 |
stack.delete() |
| 334 |
|
| 335 |
def _trace_external_obj(self, pointer, obj): |
| 336 |
addr = pointer.address[0] |
| 337 |
newaddr = self.copy(addr) |
| 338 |
pointer.address[0] = newaddr |
| 339 |
self.write_into_last_generation_obj(obj) |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
def collect_nursery(self): |
| 345 |
if self.nursery_size > self.top_of_space - self.free: |
| 346 |
|
| 347 |
self.obtain_free_space(self.nursery_size) |
| 348 |
ll_assert(self.nursery_size <= self.top_of_space - self.free, |
| 349 |
"obtain_free_space failed to do its job") |
| 350 |
if self.nursery: |
| 351 |
debug_start("gc-minor") |
| 352 |
debug_print("--- minor collect ---") |
| 353 |
debug_print("nursery:", self.nursery, "to", self.nursery_top) |
| 354 |
|
| 355 |
scan = beginning = self.free |
| 356 |
self.collect_oldrefs_to_nursery() |
| 357 |
self.collect_roots_in_nursery() |
| 358 |
self.collect_young_objects_with_finalizers() |
| 359 |
scan = self.scan_objects_just_copied_out_of_nursery(scan) |
| 360 |
|
| 361 |
|
| 362 |
if self.young_objects_with_weakrefs.non_empty(): |
| 363 |
self.invalidate_young_weakrefs() |
| 364 |
if self.young_objects_with_id.length() > 0: |
| 365 |
self.update_young_objects_with_id() |
| 366 |
|
| 367 |
llarena.arena_reset(self.nursery, self.nursery_size, 2) |
| 368 |
debug_print("survived (fraction of the size):", |
| 369 |
float(scan - beginning) / self.nursery_size) |
| 370 |
debug_stop("gc-minor") |
| 371 |
|
| 372 |
else: |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
self.nursery = self.free |
| 377 |
self.nursery_top = self.nursery + self.nursery_size |
| 378 |
self.free = self.nursery_top |
| 379 |
self.nursery_free = self.nursery |
| 380 |
|
| 381 |
self.change_nursery_hash_base() |
| 382 |
return self.nursery_free |
| 383 |
|
| 384 |
def change_nursery_hash_base(self): |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
hash_base = self.nursery_hash_base |
| 390 |
hash_base += self.nursery_size - 1 |
| 391 |
if (hash_base & 3) == 0: |
| 392 |
hash_base -= 1 |
| 393 |
self.nursery_hash_base = intmask(hash_base) |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
def collect_oldrefs_to_nursery(self): |
| 399 |
|
| 400 |
|
| 401 |
count = 0 |
| 402 |
oldlist = self.old_objects_pointing_to_young |
| 403 |
while oldlist.non_empty(): |
| 404 |
count += 1 |
| 405 |
obj = oldlist.pop() |
| 406 |
hdr = self.header(obj) |
| 407 |
hdr.tid |= GCFLAG_NO_YOUNG_PTRS |
| 408 |
self.trace_and_drag_out_of_nursery(obj) |
| 409 |
debug_print("collect_oldrefs_to_nursery", count) |
| 410 |
|
| 411 |
def collect_roots_in_nursery(self): |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
self.root_walker.walk_roots( |
| 417 |
GenerationGC._collect_root_in_nursery, |
| 418 |
GenerationGC._collect_root_in_nursery, |
| 419 |
None) |
| 420 |
|
| 421 |
def _collect_root_in_nursery(self, root): |
| 422 |
obj = root.address[0] |
| 423 |
if self.is_in_nursery(obj): |
| 424 |
root.address[0] = self.copy(obj) |
| 425 |
|
| 426 |
def collect_young_objects_with_finalizers(self): |
| 427 |
|
| 428 |
new = self.AddressDeque() |
| 429 |
while self.objects_with_finalizers.non_empty(): |
| 430 |
obj = self.objects_with_finalizers.popleft() |
| 431 |
fq_nr = self.objects_with_finalizers.popleft() |
| 432 |
if self.is_in_nursery(obj): |
| 433 |
obj = self.copy(obj) |
| 434 |
new.append(obj) |
| 435 |
new.append(fq_nr) |
| 436 |
self.objects_with_finalizers.delete() |
| 437 |
self.objects_with_finalizers = new |
| 438 |
|
| 439 |
def scan_objects_just_copied_out_of_nursery(self, scan): |
| 440 |
while scan < self.free: |
| 441 |
curr = scan + self.size_gc_header() |
| 442 |
self.trace_and_drag_out_of_nursery(curr) |
| 443 |
scan += self.size_gc_header() + self.get_size_incl_hash(curr) |
| 444 |
return scan |
| 445 |
|
| 446 |
def trace_and_drag_out_of_nursery(self, obj): |
| 447 |
"""obj must not be in the nursery. This copies all the |
| 448 |
young objects it references out of the nursery. |
| 449 |
""" |
| 450 |
self.trace(obj, self.make_callback('_trace_drag_out'), self, None) |
| 451 |
|
| 452 |
def _trace_drag_out(self, pointer, ignored): |
| 453 |
if self.is_in_nursery(pointer.address[0]): |
| 454 |
pointer.address[0] = self.copy(pointer.address[0]) |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
def invalidate_young_weakrefs(self): |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
while self.young_objects_with_weakrefs.non_empty(): |
| 464 |
obj = self.young_objects_with_weakrefs.pop() |
| 465 |
if not self.surviving(obj): |
| 466 |
continue |
| 467 |
obj = self.get_forwarding_address(obj) |
| 468 |
offset = self.weakpointer_offset(self.get_type_id(obj)) |
| 469 |
pointing_to = (obj + offset).address[0] |
| 470 |
if self.is_in_nursery(pointing_to): |
| 471 |
if self.surviving(pointing_to): |
| 472 |
(obj + offset).address[0] = self.get_forwarding_address( |
| 473 |
pointing_to) |
| 474 |
else: |
| 475 |
(obj + offset).address[0] = NULL |
| 476 |
continue |
| 477 |
self.objects_with_weakrefs.append(obj) |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
JIT_WB_IF_FLAG = GCFLAG_NO_YOUNG_PTRS |
| 483 |
|
| 484 |
def write_barrier(self, addr_struct): |
| 485 |
if self.header(addr_struct).tid & GCFLAG_NO_YOUNG_PTRS: |
| 486 |
self.remember_young_pointer(addr_struct) |
| 487 |
|
| 488 |
def _setup_wb(self): |
| 489 |
DEBUG = self.DEBUG |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
def remember_young_pointer(addr_struct): |
| 498 |
|
| 499 |
|
| 500 |
if DEBUG: |
| 501 |
ll_assert(not self.is_in_nursery(addr_struct), |
| 502 |
"nursery object with GCFLAG_NO_YOUNG_PTRS") |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
self.old_objects_pointing_to_young.append(addr_struct) |
| 510 |
self.header(addr_struct).tid &= ~GCFLAG_NO_YOUNG_PTRS |
| 511 |
self.write_into_last_generation_obj(addr_struct) |
| 512 |
remember_young_pointer._dont_inline_ = True |
| 513 |
self.remember_young_pointer = remember_young_pointer |
| 514 |
|
| 515 |
def write_into_last_generation_obj(self, addr_struct): |
| 516 |
objhdr = self.header(addr_struct) |
| 517 |
if objhdr.tid & GCFLAG_NO_HEAP_PTRS: |
| 518 |
objhdr.tid &= ~GCFLAG_NO_HEAP_PTRS |
| 519 |
self.last_generation_root_objects.append(addr_struct) |
| 520 |
write_into_last_generation_obj._always_inline_ = True |
| 521 |
|
| 522 |
def writebarrier_before_copy(self, source_addr, dest_addr, |
| 523 |
source_start, dest_start, length): |
| 524 |
""" This has the same effect as calling writebarrier over |
| 525 |
each element in dest copied from source, except it might reset |
| 526 |
one of the following flags a bit too eagerly, which means we'll have |
| 527 |
a bit more objects to track, but being on the safe side. |
| 528 |
""" |
| 529 |
source_hdr = self.header(source_addr) |
| 530 |
dest_hdr = self.header(dest_addr) |
| 531 |
if dest_hdr.tid & GCFLAG_NO_YOUNG_PTRS == 0: |
| 532 |
return True |
| 533 |
|
| 534 |
if source_hdr.tid & GCFLAG_NO_YOUNG_PTRS == 0: |
| 535 |
|
| 536 |
self.old_objects_pointing_to_young.append(dest_addr) |
| 537 |
dest_hdr.tid &= ~GCFLAG_NO_YOUNG_PTRS |
| 538 |
if dest_hdr.tid & GCFLAG_NO_HEAP_PTRS: |
| 539 |
if source_hdr.tid & GCFLAG_NO_HEAP_PTRS == 0: |
| 540 |
|
| 541 |
|
| 542 |
dest_hdr.tid &= ~GCFLAG_NO_HEAP_PTRS |
| 543 |
self.last_generation_root_objects.append(dest_addr) |
| 544 |
return True |
| 545 |
|
| 546 |
def writebarrier_before_move(self, array_addr): |
| 547 |
pass |
| 548 |
|
| 549 |
def is_last_generation(self, obj): |
| 550 |
|
| 551 |
return (self.header(obj).tid & GCFLAG_EXTERNAL) != 0 |
| 552 |
|
| 553 |
def _compute_id(self, obj): |
| 554 |
if self.is_in_nursery(obj): |
| 555 |
result = self.young_objects_with_id.get(obj) |
| 556 |
if not result: |
| 557 |
result = self._next_id() |
| 558 |
self.young_objects_with_id.setitem(obj, result) |
| 559 |
return result |
| 560 |
else: |
| 561 |
return SemiSpaceGC._compute_id(self, obj) |
| 562 |
|
| 563 |
def update_young_objects_with_id(self): |
| 564 |
self.young_objects_with_id.foreach(self._update_object_id, |
| 565 |
self.objects_with_id) |
| 566 |
self.young_objects_with_id.clear() |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
def ids_grow_older(self): |
| 572 |
self.young_objects_with_id.foreach(self._id_grow_older, None) |
| 573 |
self.young_objects_with_id.clear() |
| 574 |
|
| 575 |
def _id_grow_older(self, obj, id, ignored): |
| 576 |
self.objects_with_id.setitem(obj, id) |
| 577 |
|
| 578 |
def _compute_current_nursery_hash(self, obj): |
| 579 |
return intmask(llmemory.cast_adr_to_int(obj) + self.nursery_hash_base) |
| 580 |
|
| 581 |
def enumerate_all_roots(self, callback, arg): |
| 582 |
self.last_generation_root_objects.foreach(callback, arg) |
| 583 |
SemiSpaceGC.enumerate_all_roots(self, callback, arg) |
| 584 |
enumerate_all_roots._annspecialcase_ = 'specialize:arg(1)' |
| 585 |
|
| 586 |
def debug_check_object(self, obj): |
| 587 |
"""Check the invariants about 'obj' that should be true |
| 588 |
between collections.""" |
| 589 |
SemiSpaceGC.debug_check_object(self, obj) |
| 590 |
tid = self.header(obj).tid |
| 591 |
if tid & GCFLAG_NO_YOUNG_PTRS: |
| 592 |
ll_assert(not self.is_in_nursery(obj), |
| 593 |
"nursery object with GCFLAG_NO_YOUNG_PTRS") |
| 594 |
self.trace(obj, self.make_callback('_debug_no_nursery_pointer'), self, None) |
| 595 |
elif not self.is_in_nursery(obj): |
| 596 |
ll_assert(self._d_oopty.contains(obj), |
| 597 |
"missing from old_objects_pointing_to_young") |
| 598 |
if tid & GCFLAG_NO_HEAP_PTRS: |
| 599 |
ll_assert(self.is_last_generation(obj), |
| 600 |
"GCFLAG_NO_HEAP_PTRS on non-3rd-generation object") |
| 601 |
self.trace(obj, self.make_callback('_debug_no_gen1or2_pointer'), self, None) |
| 602 |
elif self.is_last_generation(obj): |
| 603 |
ll_assert(self._d_lgro.contains(obj), |
| 604 |
"missing from last_generation_root_objects") |
| 605 |
|
| 606 |
def _debug_no_nursery_pointer(self, root, ignored): |
| 607 |
ll_assert(not self.is_in_nursery(root.address[0]), |
| 608 |
"GCFLAG_NO_YOUNG_PTRS but found a young pointer") |
| 609 |
def _debug_no_gen1or2_pointer(self, root, ignored): |
| 610 |
target = root.address[0] |
| 611 |
ll_assert(not target or self.is_last_generation(target), |
| 612 |
"GCFLAG_NO_HEAP_PTRS but found a pointer to gen1or2") |
| 613 |
|
| 614 |
def debug_check_consistency(self): |
| 615 |
if self.DEBUG: |
| 616 |
self._d_oopty = self.old_objects_pointing_to_young.stack2dict() |
| 617 |
self._d_lgro = self.last_generation_root_objects.stack2dict() |
| 618 |
SemiSpaceGC.debug_check_consistency(self) |
| 619 |
self._d_oopty.delete() |
| 620 |
self._d_lgro.delete() |
| 621 |
self.old_objects_pointing_to_young.foreach( |
| 622 |
self._debug_check_flag_1, None) |
| 623 |
self.last_generation_root_objects.foreach( |
| 624 |
self._debug_check_flag_2, None) |
| 625 |
|
| 626 |
def _debug_check_flag_1(self, obj, ignored): |
| 627 |
ll_assert(not (self.header(obj).tid & GCFLAG_NO_YOUNG_PTRS), |
| 628 |
"unexpected GCFLAG_NO_YOUNG_PTRS") |
| 629 |
def _debug_check_flag_2(self, obj, ignored): |
| 630 |
ll_assert(not (self.header(obj).tid & GCFLAG_NO_HEAP_PTRS), |
| 631 |
"unexpected GCFLAG_NO_HEAP_PTRS") |
| 632 |
|
| 633 |
def debug_check_can_copy(self, obj): |
| 634 |
if self.is_in_nursery(obj): |
| 635 |
pass |
| 636 |
else: |
| 637 |
SemiSpaceGC.debug_check_can_copy(self, obj) |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
def nursery_size_from_env(): |
| 643 |
return env.read_from_env('PYPY_GENERATIONGC_NURSERY') |