| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory, llarena, rffi |
| 2 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 3 |
from rpython.rlib.debug import ll_assert |
| 4 |
from rpython.rlib.objectmodel import specialize |
| 5 |
from rpython.memory.gcheader import GCHeaderBuilder |
| 6 |
from rpython.memory.support import DEFAULT_CHUNK_SIZE |
| 7 |
from rpython.memory.support import get_address_stack, get_address_deque |
| 8 |
from rpython.memory.support import AddressDict, null_address_dict |
| 9 |
from rpython.memory.gc.hook import GcHooks |
| 10 |
from rpython.rtyper.lltypesystem.llmemory import NULL, raw_malloc_usage |
| 11 |
from rpython.rtyper.annlowlevel import cast_adr_to_nongc_instance |
| 12 |
|
| 13 |
TYPEID_MAP = lltype.GcStruct('TYPEID_MAP', ('count', lltype.Signed), |
| 14 |
('size', lltype.Signed), |
| 15 |
('links', lltype.Array(lltype.Signed))) |
| 16 |
ARRAY_TYPEID_MAP = lltype.GcArray(lltype.Ptr(TYPEID_MAP)) |
| 17 |
|
| 18 |
|
| 19 |
class GCBase(object): |
| 20 |
_alloc_flavor_ = "raw" |
| 21 |
moving_gc = False |
| 22 |
needs_write_barrier = False |
| 23 |
malloc_zero_filled = False |
| 24 |
prebuilt_gc_objects_are_static_roots = True |
| 25 |
can_usually_pin_objects = False |
| 26 |
object_minimal_size = 0 |
| 27 |
gcflag_extra = 0 |
| 28 |
gcflag_dummy = 0 |
| 29 |
_totalroots_rpy = 0 |
| 30 |
|
| 31 |
def __init__(self, config, chunk_size=DEFAULT_CHUNK_SIZE, |
| 32 |
translated_to_c=True, hooks=None): |
| 33 |
self.gcheaderbuilder = GCHeaderBuilder(self.HDR) |
| 34 |
self.AddressStack = get_address_stack(chunk_size) |
| 35 |
self.AddressDeque = get_address_deque(chunk_size) |
| 36 |
self.AddressDict = AddressDict |
| 37 |
self.null_address_dict = null_address_dict |
| 38 |
self.config = config |
| 39 |
assert isinstance(translated_to_c, bool) |
| 40 |
self.translated_to_c = translated_to_c |
| 41 |
if hooks is None: |
| 42 |
hooks = GcHooks() |
| 43 |
self.hooks = hooks |
| 44 |
|
| 45 |
def setup(self): |
| 46 |
|
| 47 |
|
| 48 |
self.finalizer_lock = False |
| 49 |
self.run_old_style_finalizers = self.AddressDeque() |
| 50 |
|
| 51 |
def mark_finalizer_to_run(self, fq_index, obj): |
| 52 |
if fq_index == -1: |
| 53 |
self.run_old_style_finalizers.append(obj) |
| 54 |
return |
| 55 |
handlers = self.finalizer_handlers() |
| 56 |
self._adr2deque(handlers[fq_index].deque).append(obj) |
| 57 |
|
| 58 |
def post_setup(self): |
| 59 |
|
| 60 |
|
| 61 |
from rpython.memory.gc import env |
| 62 |
self.DEBUG = env.read_from_env('PYPY_GC_DEBUG') |
| 63 |
|
| 64 |
def _teardown(self): |
| 65 |
pass |
| 66 |
|
| 67 |
def can_optimize_clean_setarrayitems(self): |
| 68 |
return True |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
DEBUG = False |
| 76 |
|
| 77 |
def set_query_functions(self, is_varsize, has_gcptr_in_varsize, |
| 78 |
is_gcarrayofgcptr, |
| 79 |
finalizer_handlers, |
| 80 |
destructor_or_custom_trace, |
| 81 |
is_old_style_finalizer, |
| 82 |
offsets_to_gc_pointers, |
| 83 |
fixed_size, varsize_item_sizes, |
| 84 |
varsize_offset_to_variable_part, |
| 85 |
varsize_offset_to_length, |
| 86 |
varsize_offsets_to_gcpointers_in_var_part, |
| 87 |
weakpointer_offset, |
| 88 |
member_index, |
| 89 |
is_rpython_class, |
| 90 |
has_custom_trace, |
| 91 |
fast_path_tracing, |
| 92 |
has_gcptr, |
| 93 |
cannot_pin, |
| 94 |
has_memory_pressure, |
| 95 |
get_memory_pressure_ofs): |
| 96 |
self.finalizer_handlers = finalizer_handlers |
| 97 |
self.destructor_or_custom_trace = destructor_or_custom_trace |
| 98 |
self.is_old_style_finalizer = is_old_style_finalizer |
| 99 |
self.is_varsize = is_varsize |
| 100 |
self.has_gcptr_in_varsize = has_gcptr_in_varsize |
| 101 |
self.is_gcarrayofgcptr = is_gcarrayofgcptr |
| 102 |
self.offsets_to_gc_pointers = offsets_to_gc_pointers |
| 103 |
self.fixed_size = fixed_size |
| 104 |
self.varsize_item_sizes = varsize_item_sizes |
| 105 |
self.varsize_offset_to_variable_part = varsize_offset_to_variable_part |
| 106 |
self.varsize_offset_to_length = varsize_offset_to_length |
| 107 |
self.varsize_offsets_to_gcpointers_in_var_part = varsize_offsets_to_gcpointers_in_var_part |
| 108 |
self.weakpointer_offset = weakpointer_offset |
| 109 |
self.member_index = member_index |
| 110 |
self.is_rpython_class = is_rpython_class |
| 111 |
self.has_custom_trace = has_custom_trace |
| 112 |
self.fast_path_tracing = fast_path_tracing |
| 113 |
self.has_gcptr = has_gcptr |
| 114 |
self.cannot_pin = cannot_pin |
| 115 |
self.has_memory_pressure = has_memory_pressure |
| 116 |
self.get_memory_pressure_ofs = get_memory_pressure_ofs |
| 117 |
|
| 118 |
def get_member_index(self, type_id): |
| 119 |
return self.member_index(type_id) |
| 120 |
|
| 121 |
def set_root_walker(self, root_walker): |
| 122 |
self.root_walker = root_walker |
| 123 |
|
| 124 |
def write_barrier(self, addr_struct): |
| 125 |
pass |
| 126 |
|
| 127 |
def size_gc_header(self, typeid=0): |
| 128 |
return self.gcheaderbuilder.size_gc_header |
| 129 |
|
| 130 |
def header(self, addr): |
| 131 |
addr -= self.gcheaderbuilder.size_gc_header |
| 132 |
return llmemory.cast_adr_to_ptr(addr, lltype.Ptr(self.HDR)) |
| 133 |
|
| 134 |
def _get_size_for_typeid(self, obj, typeid): |
| 135 |
size = self.fixed_size(typeid) |
| 136 |
if self.is_varsize(typeid): |
| 137 |
lenaddr = obj + self.varsize_offset_to_length(typeid) |
| 138 |
length = lenaddr.signed[0] |
| 139 |
size += length * self.varsize_item_sizes(typeid) |
| 140 |
size = llarena.round_up_for_allocation(size) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
return size |
| 145 |
|
| 146 |
def get_size(self, obj): |
| 147 |
return self._get_size_for_typeid(obj, self.get_type_id(obj)) |
| 148 |
|
| 149 |
def get_type_id_cast(self, obj): |
| 150 |
return rffi.cast(lltype.Signed, self.get_type_id(obj)) |
| 151 |
|
| 152 |
def get_size_incl_hash(self, obj): |
| 153 |
return self.get_size(obj) |
| 154 |
|
| 155 |
|
| 156 |
def enable(self): |
| 157 |
pass |
| 158 |
|
| 159 |
def disable(self): |
| 160 |
pass |
| 161 |
|
| 162 |
def isenabled(self): |
| 163 |
return True |
| 164 |
|
| 165 |
def collect_step(self): |
| 166 |
self.collect() |
| 167 |
return True |
| 168 |
|
| 169 |
def malloc(self, typeid, length=0, zero=False): |
| 170 |
"""NOT_RPYTHON |
| 171 |
For testing. The interface used by the gctransformer is |
| 172 |
the four malloc_[fixed,var]size[_clear]() functions. |
| 173 |
""" |
| 174 |
size = self.fixed_size(typeid) |
| 175 |
needs_finalizer = (bool(self.destructor_or_custom_trace(typeid)) |
| 176 |
and not self.has_custom_trace(typeid)) |
| 177 |
finalizer_is_light = (needs_finalizer and |
| 178 |
not self.is_old_style_finalizer(typeid)) |
| 179 |
contains_weakptr = self.weakpointer_offset(typeid) >= 0 |
| 180 |
assert not (needs_finalizer and contains_weakptr) |
| 181 |
if self.is_varsize(typeid): |
| 182 |
assert not contains_weakptr |
| 183 |
assert not needs_finalizer |
| 184 |
itemsize = self.varsize_item_sizes(typeid) |
| 185 |
offset_to_length = self.varsize_offset_to_length(typeid) |
| 186 |
if self.malloc_zero_filled: |
| 187 |
malloc_varsize = self.malloc_varsize_clear |
| 188 |
else: |
| 189 |
malloc_varsize = self.malloc_varsize |
| 190 |
ref = malloc_varsize(typeid, length, size, itemsize, |
| 191 |
offset_to_length) |
| 192 |
size += itemsize * length |
| 193 |
else: |
| 194 |
if self.malloc_zero_filled: |
| 195 |
malloc_fixedsize = self.malloc_fixedsize_clear |
| 196 |
else: |
| 197 |
malloc_fixedsize = self.malloc_fixedsize |
| 198 |
ref = malloc_fixedsize(typeid, size, needs_finalizer, |
| 199 |
finalizer_is_light, |
| 200 |
contains_weakptr) |
| 201 |
|
| 202 |
ref = llmemory.cast_ptr_to_adr(ref) |
| 203 |
if zero and not self.malloc_zero_filled: |
| 204 |
llmemory.raw_memclear(ref, size) |
| 205 |
return ref |
| 206 |
|
| 207 |
def id(self, ptr): |
| 208 |
return lltype.cast_ptr_to_int(ptr) |
| 209 |
|
| 210 |
def can_move(self, addr): |
| 211 |
return False |
| 212 |
|
| 213 |
def malloc_fixed_or_varsize_nonmovable(self, typeid, length): |
| 214 |
raise MemoryError |
| 215 |
|
| 216 |
def pin(self, addr): |
| 217 |
return False |
| 218 |
|
| 219 |
def unpin(self, addr): |
| 220 |
pass |
| 221 |
|
| 222 |
def _is_pinned(self, addr): |
| 223 |
return False |
| 224 |
|
| 225 |
def set_max_heap_size(self, size): |
| 226 |
raise NotImplementedError |
| 227 |
|
| 228 |
@staticmethod |
| 229 |
@specialize.memo() |
| 230 |
def assert_callback_is_a_function(callback): |
| 231 |
""" |
| 232 |
The specialize:arg(2) in gc.trace() allows callbacks to be either PBC |
| 233 |
functions or bound methods. This function ensures that bound methods are |
| 234 |
NOT allowed because we want to be able to @specialize.memo() on it in |
| 235 |
other parts of the code (in particular, for hpy_customtrace). |
| 236 |
""" |
| 237 |
import types |
| 238 |
assert type(callback) is types.FunctionType |
| 239 |
|
| 240 |
@staticmethod |
| 241 |
@specialize.memo() |
| 242 |
def make_callback(meth_name): |
| 243 |
""" |
| 244 |
Create a callback suitable for gc.trace which calls the given meth_name on |
| 245 |
the passed argument. |
| 246 |
|
| 247 |
The intended usage is: |
| 248 |
self.trace(obj, self.make_callback('foo'), self, None) |
| 249 |
or, equivalently: |
| 250 |
gc.trace(obj, gc.make_callback('foo'), gc, None) |
| 251 |
""" |
| 252 |
def callback(pointer, arg1, arg2): |
| 253 |
meth = getattr(arg1, meth_name) |
| 254 |
meth(pointer, arg2) |
| 255 |
callback.__name__ = 'gc_callback_%s' % meth_name |
| 256 |
callback._always_inline_ = True |
| 257 |
return callback |
| 258 |
|
| 259 |
def trace(self, obj, callback, arg1, arg2): |
| 260 |
"""Enumerate the locations inside the given obj that can contain |
| 261 |
GC pointers. For each such location, callback(pointer, arg1, arg2) is |
| 262 |
called, where 'pointer' is an address inside the object. |
| 263 |
'callback' must be a function, it cannot be a bound method. |
| 264 |
""" |
| 265 |
self.assert_callback_is_a_function(callback) |
| 266 |
typeid = self.get_type_id(obj) |
| 267 |
|
| 268 |
|
| 269 |
if not self.fast_path_tracing(typeid): |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if self.is_gcarrayofgcptr(typeid): |
| 275 |
length = (obj + llmemory.gcarrayofptr_lengthoffset).signed[0] |
| 276 |
item = obj + llmemory.gcarrayofptr_itemsoffset |
| 277 |
while length > 0: |
| 278 |
if self.points_to_valid_gc_object(item): |
| 279 |
callback(item, arg1, arg2) |
| 280 |
item += llmemory.gcarrayofptr_singleitemoffset |
| 281 |
length -= 1 |
| 282 |
return |
| 283 |
self._trace_slow_path(obj, callback, arg1, arg2) |
| 284 |
|
| 285 |
|
| 286 |
offsets = self.offsets_to_gc_pointers(typeid) |
| 287 |
i = 0 |
| 288 |
while i < len(offsets): |
| 289 |
item = obj + offsets[i] |
| 290 |
if self.points_to_valid_gc_object(item): |
| 291 |
callback(item, arg1, arg2) |
| 292 |
i += 1 |
| 293 |
trace._annspecialcase_ = 'specialize:arg(2)' |
| 294 |
|
| 295 |
def _trace_slow_path(self, obj, callback, arg1, arg2): |
| 296 |
typeid = self.get_type_id(obj) |
| 297 |
if self.has_gcptr_in_varsize(typeid): |
| 298 |
length = (obj + self.varsize_offset_to_length(typeid)).signed[0] |
| 299 |
if length > 0: |
| 300 |
item = obj + self.varsize_offset_to_variable_part(typeid) |
| 301 |
offsets = self.varsize_offsets_to_gcpointers_in_var_part(typeid) |
| 302 |
itemlength = self.varsize_item_sizes(typeid) |
| 303 |
len_offsets = len(offsets) |
| 304 |
if len_offsets == 1: |
| 305 |
offsets0 = offsets[0] |
| 306 |
while length > 0: |
| 307 |
itemobj0 = item + offsets0 |
| 308 |
if self.points_to_valid_gc_object(itemobj0): |
| 309 |
callback(itemobj0, arg1, arg2) |
| 310 |
item += itemlength |
| 311 |
length -= 1 |
| 312 |
elif len_offsets == 2: |
| 313 |
offsets0 = offsets[0] |
| 314 |
offsets1 = offsets[1] |
| 315 |
while length > 0: |
| 316 |
itemobj0 = item + offsets0 |
| 317 |
if self.points_to_valid_gc_object(itemobj0): |
| 318 |
callback(itemobj0, arg1, arg2) |
| 319 |
itemobj1 = item + offsets1 |
| 320 |
if self.points_to_valid_gc_object(itemobj1): |
| 321 |
callback(itemobj1, arg1, arg2) |
| 322 |
item += itemlength |
| 323 |
length -= 1 |
| 324 |
else: |
| 325 |
while length > 0: |
| 326 |
j = 0 |
| 327 |
while j < len_offsets: |
| 328 |
itemobj = item + offsets[j] |
| 329 |
if self.points_to_valid_gc_object(itemobj): |
| 330 |
callback(itemobj, arg1, arg2) |
| 331 |
j += 1 |
| 332 |
item += itemlength |
| 333 |
length -= 1 |
| 334 |
if self.has_custom_trace(typeid): |
| 335 |
self.custom_trace_dispatcher(obj, typeid, callback, arg1, arg2) |
| 336 |
_trace_slow_path._annspecialcase_ = 'specialize:arg(2)' |
| 337 |
|
| 338 |
def _trace_callback(self, callback, arg1, arg2, addr): |
| 339 |
if self.is_valid_gc_object(addr.address[0]): |
| 340 |
callback(addr, arg1, arg2) |
| 341 |
_trace_callback._annspecialcase_ = 'specialize:arg(1)' |
| 342 |
|
| 343 |
def trace_partial(self, obj, start, stop, callback, arg1, arg2): |
| 344 |
"""Like trace(), but only walk the array part, for indices in |
| 345 |
range(start, stop). Must only be called if has_gcptr_in_varsize(). |
| 346 |
""" |
| 347 |
self.assert_callback_is_a_function(callback) |
| 348 |
length = stop - start |
| 349 |
typeid = self.get_type_id(obj) |
| 350 |
if self.is_gcarrayofgcptr(typeid): |
| 351 |
|
| 352 |
item = obj + llmemory.gcarrayofptr_itemsoffset |
| 353 |
item += llmemory.gcarrayofptr_singleitemoffset * start |
| 354 |
while length > 0: |
| 355 |
if self.points_to_valid_gc_object(item): |
| 356 |
callback(item, arg1, arg2) |
| 357 |
item += llmemory.gcarrayofptr_singleitemoffset |
| 358 |
length -= 1 |
| 359 |
return |
| 360 |
ll_assert(self.has_gcptr_in_varsize(typeid), |
| 361 |
"trace_partial() on object without has_gcptr_in_varsize()") |
| 362 |
item = obj + self.varsize_offset_to_variable_part(typeid) |
| 363 |
offsets = self.varsize_offsets_to_gcpointers_in_var_part(typeid) |
| 364 |
itemlength = self.varsize_item_sizes(typeid) |
| 365 |
item += itemlength * start |
| 366 |
while length > 0: |
| 367 |
j = 0 |
| 368 |
while j < len(offsets): |
| 369 |
itemobj = item + offsets[j] |
| 370 |
if self.points_to_valid_gc_object(itemobj): |
| 371 |
callback(itemobj, arg1, arg2) |
| 372 |
j += 1 |
| 373 |
item += itemlength |
| 374 |
length -= 1 |
| 375 |
trace_partial._annspecialcase_ = 'specialize:arg(4)' |
| 376 |
|
| 377 |
def points_to_valid_gc_object(self, addr): |
| 378 |
return self.is_valid_gc_object(addr.address[0]) |
| 379 |
|
| 380 |
def is_valid_gc_object(self, addr): |
| 381 |
return (addr != NULL and |
| 382 |
(not self.config.taggedpointers or |
| 383 |
llmemory.cast_adr_to_int(addr) & 1 == 0)) |
| 384 |
|
| 385 |
def enumerate_all_roots(self, callback, arg): |
| 386 |
"""For each root object, invoke callback(obj, arg). |
| 387 |
'callback' should not be a bound method. |
| 388 |
Note that this method is not suitable for actually doing the |
| 389 |
collection in a moving GC, because you cannot write back a |
| 390 |
modified address. It is there only for inspection. |
| 391 |
""" |
| 392 |
|
| 393 |
|
| 394 |
callback2, attrname = _convert_callback_formats(callback) |
| 395 |
setattr(self, attrname, arg) |
| 396 |
self.root_walker.walk_roots(callback2, callback2, callback2) |
| 397 |
self.enum_live_with_finalizers(callback, arg) |
| 398 |
self.enum_pending_finalizers(callback, arg) |
| 399 |
enumerate_all_roots._annspecialcase_ = 'specialize:arg(1)' |
| 400 |
|
| 401 |
def enum_pending_finalizers(self, callback, arg): |
| 402 |
self.run_old_style_finalizers.foreach(callback, arg) |
| 403 |
handlers = self.finalizer_handlers() |
| 404 |
i = 0 |
| 405 |
while i < len(handlers): |
| 406 |
self._adr2deque(handlers[i].deque).foreach(callback, arg) |
| 407 |
i += 1 |
| 408 |
enum_pending_finalizers._annspecialcase_ = 'specialize:arg(1)' |
| 409 |
|
| 410 |
def enum_live_with_finalizers(self, callback, arg): |
| 411 |
|
| 412 |
|
| 413 |
pass |
| 414 |
enum_live_with_finalizers._annspecialcase_ = 'specialize:arg(1)' |
| 415 |
|
| 416 |
def _copy_pending_finalizers_deque(self, deque, copy_fn): |
| 417 |
tmp = self.AddressDeque() |
| 418 |
while deque.non_empty(): |
| 419 |
obj = deque.popleft() |
| 420 |
tmp.append(copy_fn(obj)) |
| 421 |
while tmp.non_empty(): |
| 422 |
deque.append(tmp.popleft()) |
| 423 |
tmp.delete() |
| 424 |
|
| 425 |
def copy_pending_finalizers(self, copy_fn): |
| 426 |
"NOTE: not very efficient, but only for SemiSpaceGC and subclasses" |
| 427 |
self._copy_pending_finalizers_deque( |
| 428 |
self.run_old_style_finalizers, copy_fn) |
| 429 |
handlers = self.finalizer_handlers() |
| 430 |
i = 0 |
| 431 |
while i < len(handlers): |
| 432 |
h = handlers[i] |
| 433 |
self._copy_pending_finalizers_deque( |
| 434 |
self._adr2deque(h.deque), copy_fn) |
| 435 |
i += 1 |
| 436 |
|
| 437 |
def call_destructor(self, obj): |
| 438 |
destructor = self.destructor_or_custom_trace(self.get_type_id(obj)) |
| 439 |
ll_assert(bool(destructor), "no destructor found") |
| 440 |
destructor(obj) |
| 441 |
|
| 442 |
def debug_check_consistency(self): |
| 443 |
"""To use after a collection. If self.DEBUG is set, this |
| 444 |
enumerates all roots and traces all objects to check if we didn't |
| 445 |
accidentally free a reachable object or forgot to update a pointer |
| 446 |
to an object that moved. |
| 447 |
""" |
| 448 |
if self.DEBUG: |
| 449 |
from rpython.rlib.objectmodel import we_are_translated |
| 450 |
from rpython.memory.support import AddressDict |
| 451 |
self._debug_seen = AddressDict() |
| 452 |
self._debug_pending = self.AddressStack() |
| 453 |
if not we_are_translated(): |
| 454 |
self.root_walker._walk_prebuilt_gc(self._debug_record) |
| 455 |
self.enumerate_all_roots(GCBase._debug_callback, self) |
| 456 |
pending = self._debug_pending |
| 457 |
while pending.non_empty(): |
| 458 |
obj = pending.pop() |
| 459 |
self.trace(obj, self.make_callback('_debug_callback2'), self, None) |
| 460 |
self._debug_seen.delete() |
| 461 |
self._debug_pending.delete() |
| 462 |
|
| 463 |
def _debug_record(self, obj): |
| 464 |
seen = self._debug_seen |
| 465 |
if not seen.contains(obj): |
| 466 |
seen.add(obj) |
| 467 |
self.debug_check_object(obj) |
| 468 |
self._debug_pending.append(obj) |
| 469 |
|
| 470 |
@staticmethod |
| 471 |
def _debug_callback(obj, self): |
| 472 |
self._debug_record(obj) |
| 473 |
|
| 474 |
def _debug_callback2(self, pointer, ignored): |
| 475 |
obj = pointer.address[0] |
| 476 |
ll_assert(bool(obj), "NULL address from self.trace()") |
| 477 |
self._debug_record(obj) |
| 478 |
|
| 479 |
def debug_check_object(self, obj): |
| 480 |
pass |
| 481 |
|
| 482 |
def _adr2deque(self, adr): |
| 483 |
return cast_adr_to_nongc_instance(self.AddressDeque, adr) |
| 484 |
|
| 485 |
def execute_finalizers(self): |
| 486 |
if self.finalizer_lock: |
| 487 |
return |
| 488 |
self.finalizer_lock = True |
| 489 |
try: |
| 490 |
handlers = self.finalizer_handlers() |
| 491 |
i = 0 |
| 492 |
while i < len(handlers): |
| 493 |
if self._adr2deque(handlers[i].deque).non_empty(): |
| 494 |
handlers[i].trigger() |
| 495 |
i += 1 |
| 496 |
while self.run_old_style_finalizers.non_empty(): |
| 497 |
obj = self.run_old_style_finalizers.popleft() |
| 498 |
self.call_destructor(obj) |
| 499 |
finally: |
| 500 |
self.finalizer_lock = False |
| 501 |
|
| 502 |
|
| 503 |
class MovingGCBase(GCBase): |
| 504 |
moving_gc = True |
| 505 |
|
| 506 |
def setup(self): |
| 507 |
GCBase.setup(self) |
| 508 |
self.objects_with_id = self.AddressDict() |
| 509 |
self.id_free_list = self.AddressStack() |
| 510 |
self.next_free_id = 1 |
| 511 |
|
| 512 |
def can_move(self, addr): |
| 513 |
return True |
| 514 |
|
| 515 |
def id(self, ptr): |
| 516 |
|
| 517 |
|
| 518 |
obj = llmemory.cast_ptr_to_adr(ptr) |
| 519 |
|
| 520 |
|
| 521 |
if not self.is_valid_gc_object(obj) or self._is_external(obj): |
| 522 |
return llmemory.cast_adr_to_int(obj) |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
return llmemory.cast_adr_to_int(self._compute_id(obj)) * 2 |
| 530 |
|
| 531 |
def _next_id(self): |
| 532 |
|
| 533 |
if self.id_free_list.non_empty(): |
| 534 |
result = self.id_free_list.pop() |
| 535 |
else: |
| 536 |
|
| 537 |
result = llmemory.cast_int_to_adr(self.next_free_id) |
| 538 |
self.next_free_id += 2 |
| 539 |
|
| 540 |
|
| 541 |
return result |
| 542 |
|
| 543 |
def _compute_id(self, obj): |
| 544 |
|
| 545 |
result = self.objects_with_id.get(obj) |
| 546 |
if not result: |
| 547 |
result = self._next_id() |
| 548 |
self.objects_with_id.setitem(obj, result) |
| 549 |
return result |
| 550 |
|
| 551 |
def update_objects_with_id(self): |
| 552 |
old = self.objects_with_id |
| 553 |
new_objects_with_id = self.AddressDict(old.length()) |
| 554 |
old.foreach(self._update_object_id_FAST, new_objects_with_id) |
| 555 |
old.delete() |
| 556 |
self.objects_with_id = new_objects_with_id |
| 557 |
|
| 558 |
def _update_object_id(self, obj, id, new_objects_with_id): |
| 559 |
|
| 560 |
if self.surviving(obj): |
| 561 |
newobj = self.get_forwarding_address(obj) |
| 562 |
new_objects_with_id.setitem(newobj, id) |
| 563 |
else: |
| 564 |
self.id_free_list.append(id) |
| 565 |
|
| 566 |
def _update_object_id_FAST(self, obj, id, new_objects_with_id): |
| 567 |
|
| 568 |
if self.surviving(obj): |
| 569 |
newobj = self.get_forwarding_address(obj) |
| 570 |
new_objects_with_id.insertclean(newobj, id) |
| 571 |
else: |
| 572 |
self.id_free_list.append(id) |
| 573 |
|
| 574 |
|
| 575 |
def choose_gc_from_config(config): |
| 576 |
"""Return a (GCClass, GC_PARAMS) from the given config object. |
| 577 |
""" |
| 578 |
if config.translation.gctransformer != "framework": |
| 579 |
raise AssertionError("fix this test") |
| 580 |
|
| 581 |
classes = {"semispace": "semispace.SemiSpaceGC", |
| 582 |
"generation": "generation.GenerationGC", |
| 583 |
"hybrid": "hybrid.HybridGC", |
| 584 |
"minimark" : "minimark.MiniMarkGC", |
| 585 |
"incminimark" : "incminimark.IncrementalMiniMarkGC", |
| 586 |
} |
| 587 |
try: |
| 588 |
modulename, classname = classes[config.translation.gc].split('.') |
| 589 |
except KeyError: |
| 590 |
raise ValueError("unknown value for translation.gc: %r" % ( |
| 591 |
config.translation.gc,)) |
| 592 |
module = __import__("rpython.memory.gc." + modulename, |
| 593 |
globals(), locals(), [classname]) |
| 594 |
GCClass = getattr(module, classname) |
| 595 |
return GCClass, GCClass.TRANSLATION_PARAMS |
| 596 |
|
| 597 |
def _convert_callback_formats(callback): |
| 598 |
callback = getattr(callback, 'im_func', callback) |
| 599 |
if callback not in _converted_callback_formats: |
| 600 |
def callback2(gc, root): |
| 601 |
obj = root.address[0] |
| 602 |
ll_assert(bool(obj), "NULL address from walk_roots()") |
| 603 |
callback(obj, getattr(gc, attrname)) |
| 604 |
attrname = '_callback2_arg%d' % len(_converted_callback_formats) |
| 605 |
_converted_callback_formats[callback] = callback2, attrname |
| 606 |
return _converted_callback_formats[callback] |
| 607 |
|
| 608 |
_convert_callback_formats._annspecialcase_ = 'specialize:memo' |
| 609 |
_converted_callback_formats = {} |