Debian Codegraph @

rpython/memory/gc/generation.py

raw symbols SCIP
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 # The following flag is never set on young objects, i.e. the ones living
17 # in the nursery. It is initially set on all prebuilt and old objects,
18 # and gets cleared by the write_barrier() when we write in them a
19 # pointer to a young object.
20 GCFLAG_NO_YOUNG_PTRS = SemiSpaceGC.first_unused_gcflag << 0
21
22 # The following flag is set on some last-generation objects (== prebuilt
23 # objects for GenerationGC, but see also HybridGC). The flag is set
24 # unless the object is already listed in 'last_generation_root_objects'.
25 # When a pointer is written inside an object with GCFLAG_NO_HEAP_PTRS
26 # set, the write_barrier clears the flag and adds the object to
27 # 'last_generation_root_objects'.
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 # the following values override the default arguments of __init__ when
43 # translating to a real backend.
44 TRANSLATION_PARAMS = {'space_size': 8*1024*1024, # 8 MB
45 'nursery_size': 3*1024*1024, # 3 MB
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 # define nursery fields
68 self.reset_nursery()
69 self._setup_wb()
70
71 # compute the constant lower bounds for the attributes
72 # largest_young_fixedsize and largest_young_var_basesize.
73 # It is expected that most (or all) objects have a fixedsize
74 # that is much lower anyway.
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 # ^^^ a list of addresses inside the old objects space; it
83 # may contain static prebuilt objects as well. More precisely,
84 # it lists exactly the old and static objects whose
85 # GCFLAG_NO_YOUNG_PTRS bit is not set.
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 # the GC is fully setup now. The rest can make use of it.
93 if self.auto_nursery_size:
94 newsize = nursery_size_from_env()
95 #if newsize <= 0:
96 # ---disabled--- just use the default value.
97 # newsize = env.estimate_best_nursery_size()
98 if newsize > 0:
99 self.set_nursery_size(newsize)
100
101 self.reset_nursery()
102
103 def _teardown(self):
104 self.collect() # should restore last gen objects flags
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 # Compute the new bounds for how large young objects can be
120 # (larger objects are allocated directly old). XXX adjust
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 # we get the following invariant:
135 assert self.nursery_size >= (self.min_nursery_size << scale)
136
137 # Force a full collect to remove the current nursery whose size
138 # no longer matches the bounds that we just computed. This must
139 # be done after changing the bounds, because it might re-create
140 # a new nursery (e.g. if it invokes finalizers).
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 # same as is_in_nursery(), but may return True accidentally if
164 # 'addr' is a tagged pointer with just the wrong value.
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 # ^^^ we do two size comparisons; the first one appears redundant,
178 # but it can be constant-folded if 'size' is a constant; then
179 # it almost always folds down to False, which kills the
180 # second comparison as well.
181 ll_assert(not contains_weakptr, "wrong case for mallocing weakref")
182 # "non-simple" case or object too big: don't use the nursery
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 # GCFLAG_NO_YOUNG_PTRS is never set on young objs
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 # Only use the nursery if there are not too many items.
203 if not raw_malloc_usage(itemsize):
204 too_many_items = False
205 else:
206 # The following line is usually constant-folded because both
207 # min_nursery_size and itemsize are constants (the latter
208 # due to inlining).
209 maxlength_for_minimal_nursery = (self.min_nursery_size // 4 //
210 raw_malloc_usage(itemsize))
211
212 # The actual maximum length for our nursery depends on how
213 # many times our nursery is bigger than the minimal size.
214 # The computation is done in this roundabout way so that
215 # only the only remaining computation is the following
216 # shift.
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 # ^^^ we do two size comparisons; the first one appears redundant,
224 # but it can be constant-folded if 'size' is a constant; then
225 # it almost always folds down to False, which kills the
226 # second comparison as well.
227 return SemiSpaceGC.malloc_varsize_clear(self, typeid, length, size,
228 itemsize, offset_to_length)
229 # with the above checks we know now that totalsize cannot be more
230 # than about half of the nursery size; in particular, the + and *
231 # cannot overflow
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 # GCFLAG_NO_YOUNG_PTRS is never set on young objs
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 # override the init_gc_object methods to change the default value of 'flags',
245 # used by objects that are directly created outside the nursery by the SemiSpaceGC.
246 # These objects must have the GCFLAG_NO_YOUNG_PTRS flag set immediately.
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 # flags exposed for the HybridGC subclass
255 GCFLAGS_FOR_NEW_YOUNG_OBJECTS = 0 # NO_YOUNG_PTRS never set on young objs
256 GCFLAGS_FOR_NEW_EXTERNAL_OBJECTS = (GCFLAG_EXTERNAL | GCFLAG_FORWARDED |
257 GCFLAG_NO_YOUNG_PTRS |
258 GC_HASH_TAKEN_ADDR)
259
260 # ____________________________________________________________
261 # Support code for full collections
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() # we are doing a full collection anyway
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 # During a full collect, all copied objects might implicitly come
279 # from the nursery. In case they do, we must add this flag:
280 tid |= GCFLAG_NO_YOUNG_PTRS
281 return self._make_a_copy_with_tid(obj, objsize, tid)
282 # history: this was missing and caused an object to become old but without the
283 # flag set. Such an object is bogus in the sense that the write_barrier doesn't
284 # work on it. So it can eventually contain a ptr to a young object but we didn't
285 # know about it. That ptr was not updated in the next minor collect... boom at
286 # the next usage.
287
288 def reset_young_gcflags(self):
289 # This empties self.old_objects_pointing_to_young, and puts the
290 # GCFLAG_NO_YOUNG_PTRS back on all these objects. We could put
291 # the flag back more lazily but we expect this list to be short
292 # anyway, and it's much saner to stick to the invariant:
293 # non-young objects all have GCFLAG_NO_YOUNG_PTRS set unless
294 # they are listed in old_objects_pointing_to_young.
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 # Warning! References from static (and possibly gen3) objects
311 # are found by collect_last_generation_roots(), which must be
312 # called *first*! If it is called after walk_roots(), then the
313 # HybridGC explodes if one of the _collect_root causes an object
314 # to be added to self.last_generation_root_objects. Indeed, in
315 # this case, the newly added object is traced twice: once by
316 # collect_last_generation_roots() and once because it was added
317 # in self.rawmalloced_objects_to_trace.
318 self.collect_last_generation_roots()
319 self.root_walker.walk_roots(
320 SemiSpaceGC._collect_root, # stack roots
321 SemiSpaceGC._collect_root, # static in prebuilt non-gc structures
322 None) # we don't need the static in prebuilt gc objects
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 # ^^^ the flag we just added will be removed immediately if
331 # the object still contains pointers to younger objects
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 # Implementation of nursery-only collections
343
344 def collect_nursery(self):
345 if self.nursery_size > self.top_of_space - self.free:
346 # the semispace is running out, do a full collect
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 # a nursery-only collection
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 # at this point, all static and old objects have got their
361 # GCFLAG_NO_YOUNG_PTRS set again by trace_and_drag_out_of_nursery
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 # mark the nursery as free and fill it with zeroes again
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 #self.debug_check_consistency() # -- quite expensive
372 else:
373 # no nursery - this occurs after a full collect, triggered either
374 # just above or by some previous non-nursery-based allocation.
375 # Grab a piece of the current space for the nursery.
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 # at this point we know that the nursery is empty
381 self.change_nursery_hash_base()
382 return self.nursery_free
383
384 def change_nursery_hash_base(self):
385 # The following should be enough to ensure that young objects
386 # tend to always get a different hash. It also makes sure that
387 # nursery_hash_base is not a multiple of 4, to avoid collisions
388 # with the hash of non-young objects.
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 # NB. we can use self.copy() to move objects out of the nursery,
396 # but only if the object was really in the nursery.
397
398 def collect_oldrefs_to_nursery(self):
399 # Follow the old_objects_pointing_to_young list and move the
400 # young objects they point to out of the nursery.
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 # we don't need to trace prebuilt GcStructs during a minor collect:
413 # if a prebuilt GcStruct contains a pointer to a young object,
414 # then the write_barrier must have ensured that the prebuilt
415 # GcStruct is in the list self.old_objects_pointing_to_young.
416 self.root_walker.walk_roots(
417 GenerationGC._collect_root_in_nursery, # stack roots
418 GenerationGC._collect_root_in_nursery, # static in prebuilt non-gc
419 None) # static in prebuilt gc
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 # XXX always walk the whole 'objects_with_finalizers' list here
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 # The code relies on the fact that no weakref can be an old object
457 # weakly pointing to a young object. Indeed, weakrefs are immutable
458 # so they cannot point to an object that was created after it.
459 def invalidate_young_weakrefs(self):
460 # walk over the list of objects that contain weakrefs and are in the
461 # nursery. if the object it references survives then update the
462 # weakref; otherwise invalidate the weakref
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 # weakref itself dies
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 # no need to remember this weakref any longer
477 self.objects_with_weakrefs.append(obj)
478
479 # for the JIT: a minimal description of the write_barrier() method
480 # (the JIT assumes it is of the shape
481 # "if addr_struct.int0 & JIT_WB_IF_FLAG: remember_young_pointer()")
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 # The purpose of attaching remember_young_pointer to the instance
491 # instead of keeping it as a regular method is to help the JIT call it.
492 # Additionally, it makes the code in write_barrier() marginally smaller
493 # (which is important because it is inlined *everywhere*).
494 # For x86, there is also an extra requirement: when the JIT calls
495 # remember_young_pointer(), it assumes that it will not touch the SSE
496 # registers, so it does not save and restore them (that's a *hack*!).
497 def remember_young_pointer(addr_struct):
498 #llop.debug_print(lltype.Void, "\tremember_young_pointer",
499 # addr_struct)
500 if DEBUG:
501 ll_assert(not self.is_in_nursery(addr_struct),
502 "nursery object with GCFLAG_NO_YOUNG_PTRS")
503 #
504 # What is important in this function is that it *must*
505 # clear the flag GCFLAG_NO_YOUNG_PTRS from 'addr_struct'
506 # if the newly written value is in the nursery. It is ok
507 # if it also clears the flag in some more cases --- it is
508 # a win to not actually pass the 'newvalue' pointer here.
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 # ^^^ a fast path of write-barrier
534 if source_hdr.tid & GCFLAG_NO_YOUNG_PTRS == 0:
535 # there might be an object in source that is in nursery
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 # ^^^ equivalend of addr from source not being in last
541 # gen
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 # nothing to do
548
549 def is_last_generation(self, obj):
550 # overridden by HybridGC
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 # NB. the clear() also makes the dictionary shrink back to its
568 # minimal size, which is actually a good idea: a large, mostly-empty
569 # table is bad for the next call to 'foreach'.
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 # it's ok to copy an object out of the nursery
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')