| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory, llarena, llgroup, rffi |
| 2 |
from rpython.rtyper import rclass |
| 3 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 4 |
from rpython.rlib.debug import ll_assert |
| 5 |
from rpython.rlib.rarithmetic import intmask |
| 6 |
from rpython.tool.identity_dict import identity_dict |
| 7 |
|
| 8 |
|
| 9 |
class GCData(object): |
| 10 |
"""The GC information tables, and the query functions that the GC |
| 11 |
calls to decode their content. The encoding of this information |
| 12 |
is done by encode_type_shape(). These two places should be in sync, |
| 13 |
obviously, but in principle no other code should depend on the |
| 14 |
details of the encoding in TYPE_INFO. |
| 15 |
""" |
| 16 |
_alloc_flavor_ = 'raw' |
| 17 |
|
| 18 |
OFFSETS_TO_GC_PTR = lltype.Array(lltype.Signed) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
CUSTOM_FUNC = lltype.FuncType([llmemory.Address], lltype.Void) |
| 27 |
CUSTOM_FUNC_PTR = lltype.Ptr(CUSTOM_FUNC) |
| 28 |
CUSTOM_DATA_STRUCT = lltype.Struct('custom_data', |
| 29 |
('customfunc', CUSTOM_FUNC_PTR), |
| 30 |
('memory_pressure_offset', lltype.Signed), |
| 31 |
|
| 32 |
) |
| 33 |
CUSTOM_DATA_STRUCT_PTR = lltype.Ptr(CUSTOM_DATA_STRUCT) |
| 34 |
|
| 35 |
|
| 36 |
TYPE_INFO = lltype.Struct("type_info", |
| 37 |
("infobits", lltype.Signed), |
| 38 |
("customdata", CUSTOM_DATA_STRUCT_PTR), |
| 39 |
("fixedsize", lltype.Signed), |
| 40 |
("ofstoptrs", lltype.Ptr(OFFSETS_TO_GC_PTR)), |
| 41 |
hints={'immutable': True}, |
| 42 |
) |
| 43 |
VARSIZE_TYPE_INFO = lltype.Struct("varsize_type_info", |
| 44 |
("header", TYPE_INFO), |
| 45 |
("varitemsize", lltype.Signed), |
| 46 |
("ofstovar", lltype.Signed), |
| 47 |
("ofstolength", lltype.Signed), |
| 48 |
("varofstoptrs", lltype.Ptr(OFFSETS_TO_GC_PTR)), |
| 49 |
hints={'immutable': True}, |
| 50 |
) |
| 51 |
TYPE_INFO_PTR = lltype.Ptr(TYPE_INFO) |
| 52 |
VARSIZE_TYPE_INFO_PTR = lltype.Ptr(VARSIZE_TYPE_INFO) |
| 53 |
|
| 54 |
def __init__(self, type_info_group): |
| 55 |
assert isinstance(type_info_group, llgroup.group) |
| 56 |
self.type_info_group = type_info_group |
| 57 |
self.type_info_group_ptr = type_info_group._as_ptr() |
| 58 |
|
| 59 |
def get(self, typeid): |
| 60 |
res = llop.get_group_member(GCData.TYPE_INFO_PTR, |
| 61 |
self.type_info_group_ptr, |
| 62 |
typeid) |
| 63 |
_check_valid_type_info(res) |
| 64 |
return res |
| 65 |
|
| 66 |
def get_varsize(self, typeid): |
| 67 |
res = llop.get_group_member(GCData.VARSIZE_TYPE_INFO_PTR, |
| 68 |
self.type_info_group_ptr, |
| 69 |
typeid) |
| 70 |
_check_valid_type_info_varsize(res) |
| 71 |
return res |
| 72 |
|
| 73 |
def q_is_varsize(self, typeid): |
| 74 |
infobits = self.get(typeid).infobits |
| 75 |
return (infobits & T_IS_VARSIZE) != 0 |
| 76 |
|
| 77 |
def q_has_gcptr_in_varsize(self, typeid): |
| 78 |
infobits = self.get(typeid).infobits |
| 79 |
return (infobits & T_HAS_GCPTR_IN_VARSIZE) != 0 |
| 80 |
|
| 81 |
def q_has_gcptr(self, typeid): |
| 82 |
infobits = self.get(typeid).infobits |
| 83 |
return (infobits & T_HAS_GCPTR) != 0 |
| 84 |
|
| 85 |
def q_is_gcarrayofgcptr(self, typeid): |
| 86 |
infobits = self.get(typeid).infobits |
| 87 |
return (infobits & T_IS_GCARRAY_OF_GCPTR) != 0 |
| 88 |
|
| 89 |
def q_cannot_pin(self, typeid): |
| 90 |
typeinfo = self.get(typeid) |
| 91 |
ANY = (T_HAS_GCPTR | T_IS_WEAKREF) |
| 92 |
return (typeinfo.infobits & ANY) != 0 or bool(typeinfo.customdata) |
| 93 |
|
| 94 |
def q_finalizer_handlers(self): |
| 95 |
adr = self.finalizer_handlers |
| 96 |
return llmemory.cast_adr_to_ptr(adr, lltype.Ptr(FIN_HANDLER_ARRAY)) |
| 97 |
|
| 98 |
def q_destructor_or_custom_trace(self, typeid): |
| 99 |
if not self.get(typeid).customdata: |
| 100 |
return lltype.nullptr(GCData.CUSTOM_FUNC_PTR.TO) |
| 101 |
return self.get(typeid).customdata.customfunc |
| 102 |
|
| 103 |
def q_is_old_style_finalizer(self, typeid): |
| 104 |
typeinfo = self.get(typeid) |
| 105 |
return (typeinfo.infobits & T_HAS_OLDSTYLE_FINALIZER) != 0 |
| 106 |
|
| 107 |
def q_offsets_to_gc_pointers(self, typeid): |
| 108 |
return self.get(typeid).ofstoptrs |
| 109 |
|
| 110 |
def q_fixed_size(self, typeid): |
| 111 |
return self.get(typeid).fixedsize |
| 112 |
|
| 113 |
def q_varsize_item_sizes(self, typeid): |
| 114 |
return self.get_varsize(typeid).varitemsize |
| 115 |
|
| 116 |
def q_varsize_offset_to_variable_part(self, typeid): |
| 117 |
return self.get_varsize(typeid).ofstovar |
| 118 |
|
| 119 |
def q_varsize_offset_to_length(self, typeid): |
| 120 |
return self.get_varsize(typeid).ofstolength |
| 121 |
|
| 122 |
def q_varsize_offsets_to_gcpointers_in_var_part(self, typeid): |
| 123 |
return self.get_varsize(typeid).varofstoptrs |
| 124 |
|
| 125 |
def q_weakpointer_offset(self, typeid): |
| 126 |
infobits = self.get(typeid).infobits |
| 127 |
if infobits & T_IS_WEAKREF: |
| 128 |
return weakptr_offset |
| 129 |
return -1 |
| 130 |
|
| 131 |
def q_member_index(self, typeid): |
| 132 |
infobits = self.get(typeid).infobits |
| 133 |
return infobits & T_MEMBER_INDEX |
| 134 |
|
| 135 |
def q_is_rpython_class(self, typeid): |
| 136 |
infobits = self.get(typeid).infobits |
| 137 |
return infobits & T_IS_RPYTHON_INSTANCE != 0 |
| 138 |
|
| 139 |
def q_has_custom_trace(self, typeid): |
| 140 |
infobits = self.get(typeid).infobits |
| 141 |
return infobits & T_HAS_CUSTOM_TRACE != 0 |
| 142 |
|
| 143 |
def q_fast_path_tracing(self, typeid): |
| 144 |
|
| 145 |
|
| 146 |
T_ANY_SLOW_FLAG = (T_HAS_GCPTR_IN_VARSIZE | |
| 147 |
T_IS_GCARRAY_OF_GCPTR | |
| 148 |
T_HAS_CUSTOM_TRACE) |
| 149 |
infobits = self.get(typeid).infobits |
| 150 |
return infobits & T_ANY_SLOW_FLAG == 0 |
| 151 |
|
| 152 |
def q_has_memory_pressure(self, typeid): |
| 153 |
infobits = self.get(typeid).infobits |
| 154 |
return infobits & T_HAS_MEMORY_PRESSURE != 0 |
| 155 |
|
| 156 |
def q_get_memory_pressure_ofs(self, typeid): |
| 157 |
infobits = self.get(typeid).infobits |
| 158 |
assert infobits & T_HAS_MEMORY_PRESSURE != 0 |
| 159 |
return self.get(typeid).customdata.memory_pressure_offset |
| 160 |
|
| 161 |
def set_query_functions(self, gc): |
| 162 |
gc.set_query_functions( |
| 163 |
self.q_is_varsize, |
| 164 |
self.q_has_gcptr_in_varsize, |
| 165 |
self.q_is_gcarrayofgcptr, |
| 166 |
self.q_finalizer_handlers, |
| 167 |
self.q_destructor_or_custom_trace, |
| 168 |
self.q_is_old_style_finalizer, |
| 169 |
self.q_offsets_to_gc_pointers, |
| 170 |
self.q_fixed_size, |
| 171 |
self.q_varsize_item_sizes, |
| 172 |
self.q_varsize_offset_to_variable_part, |
| 173 |
self.q_varsize_offset_to_length, |
| 174 |
self.q_varsize_offsets_to_gcpointers_in_var_part, |
| 175 |
self.q_weakpointer_offset, |
| 176 |
self.q_member_index, |
| 177 |
self.q_is_rpython_class, |
| 178 |
self.q_has_custom_trace, |
| 179 |
self.q_fast_path_tracing, |
| 180 |
self.q_has_gcptr, |
| 181 |
self.q_cannot_pin, |
| 182 |
self.q_has_memory_pressure, |
| 183 |
self.q_get_memory_pressure_ofs) |
| 184 |
|
| 185 |
def _has_got_custom_trace(self, typeid): |
| 186 |
type_info = self.get(typeid) |
| 187 |
type_info.infobits |= (T_HAS_CUSTOM_TRACE | T_HAS_GCPTR) |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
T_MEMBER_INDEX = 0xffff |
| 192 |
T_IS_VARSIZE = 0x010000 |
| 193 |
T_HAS_GCPTR_IN_VARSIZE = 0x020000 |
| 194 |
T_IS_GCARRAY_OF_GCPTR = 0x040000 |
| 195 |
T_IS_WEAKREF = 0x080000 |
| 196 |
T_IS_RPYTHON_INSTANCE = 0x100000 |
| 197 |
T_HAS_CUSTOM_TRACE = 0x200000 |
| 198 |
T_HAS_OLDSTYLE_FINALIZER = 0x400000 |
| 199 |
T_HAS_GCPTR = 0x1000000 |
| 200 |
T_HAS_MEMORY_PRESSURE = 0x2000000 |
| 201 |
T_KEY_MASK = intmask(0xFC000000) |
| 202 |
T_KEY_VALUE = intmask(0x58000000) |
| 203 |
|
| 204 |
def _check_valid_type_info(p): |
| 205 |
ll_assert(p.infobits & T_KEY_MASK == T_KEY_VALUE, "invalid type_id") |
| 206 |
|
| 207 |
def _check_valid_type_info_varsize(p): |
| 208 |
ll_assert(p.header.infobits & (T_KEY_MASK | T_IS_VARSIZE) == |
| 209 |
(T_KEY_VALUE | T_IS_VARSIZE), |
| 210 |
"invalid varsize type_id") |
| 211 |
|
| 212 |
def check_typeid(typeid): |
| 213 |
|
| 214 |
ll_assert(llop.is_group_member_nonzero(lltype.Bool, typeid), |
| 215 |
"invalid type_id") |
| 216 |
|
| 217 |
def has_special_memory_pressure(TYPE): |
| 218 |
if TYPE._is_varsize(): |
| 219 |
return False |
| 220 |
T = TYPE |
| 221 |
while True: |
| 222 |
if 'special_memory_pressure' in T._flds: |
| 223 |
return True |
| 224 |
if 'super' not in T._flds: |
| 225 |
return False |
| 226 |
T = T._flds['super'] |
| 227 |
|
| 228 |
def get_memory_pressure_ofs(TYPE): |
| 229 |
T = TYPE |
| 230 |
while True: |
| 231 |
if 'special_memory_pressure' in T._flds: |
| 232 |
return llmemory.offsetof(T, 'special_memory_pressure') |
| 233 |
if 'super' not in T._flds: |
| 234 |
assert False, "get_ and has_memory_pressure disagree" |
| 235 |
T = T._flds['super'] |
| 236 |
|
| 237 |
def encode_type_shape(builder, info, TYPE, index): |
| 238 |
"""Encode the shape of the TYPE into the TYPE_INFO structure 'info'.""" |
| 239 |
offsets = offsets_to_gc_pointers(TYPE) |
| 240 |
infobits = index |
| 241 |
info.ofstoptrs = builder.offsets2table(offsets, TYPE) |
| 242 |
if len(offsets) > 0: |
| 243 |
infobits |= T_HAS_GCPTR |
| 244 |
|
| 245 |
fptrs = builder.special_funcptr_for_type(TYPE) |
| 246 |
if fptrs or has_special_memory_pressure(TYPE): |
| 247 |
customdata = lltype.malloc(GCData.CUSTOM_DATA_STRUCT, flavor='raw', |
| 248 |
immortal=True) |
| 249 |
info.customdata = customdata |
| 250 |
if "destructor" in fptrs: |
| 251 |
customdata.customfunc = fptrs["destructor"] |
| 252 |
if "old_style_finalizer" in fptrs: |
| 253 |
customdata.customfunc = fptrs["old_style_finalizer"] |
| 254 |
infobits |= T_HAS_OLDSTYLE_FINALIZER |
| 255 |
if has_special_memory_pressure(TYPE): |
| 256 |
infobits |= T_HAS_MEMORY_PRESSURE |
| 257 |
info.customdata.memory_pressure_offset = get_memory_pressure_ofs(TYPE) |
| 258 |
|
| 259 |
if not TYPE._is_varsize(): |
| 260 |
info.fixedsize = llarena.round_up_for_allocation( |
| 261 |
llmemory.sizeof(TYPE), builder.GCClass.object_minimal_size) |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
else: |
| 267 |
infobits |= T_IS_VARSIZE |
| 268 |
varinfo = lltype.cast_pointer(GCData.VARSIZE_TYPE_INFO_PTR, info) |
| 269 |
info.fixedsize = llmemory.sizeof(TYPE, 0) |
| 270 |
if isinstance(TYPE, lltype.Struct): |
| 271 |
ARRAY = TYPE._flds[TYPE._arrayfld] |
| 272 |
ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld) |
| 273 |
varinfo.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY) |
| 274 |
varinfo.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0) |
| 275 |
else: |
| 276 |
assert isinstance(TYPE, lltype.GcArray) |
| 277 |
ARRAY = TYPE |
| 278 |
if (isinstance(ARRAY.OF, lltype.Ptr) |
| 279 |
and ARRAY.OF.TO._gckind == 'gc'): |
| 280 |
infobits |= T_IS_GCARRAY_OF_GCPTR |
| 281 |
varinfo.ofstolength = llmemory.ArrayLengthOffset(ARRAY) |
| 282 |
varinfo.ofstovar = llmemory.itemoffsetof(TYPE, 0) |
| 283 |
assert isinstance(ARRAY, lltype.Array) |
| 284 |
if ARRAY.OF != lltype.Void: |
| 285 |
offsets = offsets_to_gc_pointers(ARRAY.OF) |
| 286 |
else: |
| 287 |
offsets = () |
| 288 |
if len(offsets) > 0: |
| 289 |
infobits |= T_HAS_GCPTR_IN_VARSIZE | T_HAS_GCPTR |
| 290 |
varinfo.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF) |
| 291 |
varinfo.varitemsize = llmemory.sizeof(ARRAY.OF) |
| 292 |
if builder.is_weakref_type(TYPE): |
| 293 |
infobits |= T_IS_WEAKREF |
| 294 |
if is_subclass_of_object(TYPE): |
| 295 |
infobits |= T_IS_RPYTHON_INSTANCE |
| 296 |
info.infobits = infobits | T_KEY_VALUE |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
class TypeLayoutBuilder(object): |
| 302 |
can_add_new_types = True |
| 303 |
can_encode_type_shape = True |
| 304 |
|
| 305 |
size_of_fixed_type_info = llmemory.sizeof(GCData.TYPE_INFO) |
| 306 |
|
| 307 |
def __init__(self, GCClass, lltype2vtable=None): |
| 308 |
self.GCClass = GCClass |
| 309 |
self.lltype2vtable = lltype2vtable |
| 310 |
self.make_type_info_group() |
| 311 |
self.id_of_type = {} |
| 312 |
self.iseen_roots = identity_dict() |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
self.addresses_of_static_ptrs = [] |
| 318 |
|
| 319 |
self.addresses_of_static_ptrs_in_nongc = [] |
| 320 |
|
| 321 |
|
| 322 |
self.all_prebuilt_gc = [] |
| 323 |
self._special_funcptrs = {} |
| 324 |
self.offsettable_cache = {} |
| 325 |
|
| 326 |
def make_type_info_group(self): |
| 327 |
self.type_info_group = llgroup.group("typeinfo") |
| 328 |
|
| 329 |
DUMMY = lltype.Struct("dummy", ('x', lltype.Signed)) |
| 330 |
dummy = lltype.malloc(DUMMY, immortal=True, zero=True) |
| 331 |
self.type_info_group.add_member(dummy) |
| 332 |
|
| 333 |
def get_type_id(self, TYPE): |
| 334 |
try: |
| 335 |
return self.id_of_type[TYPE] |
| 336 |
except KeyError: |
| 337 |
assert self.can_add_new_types |
| 338 |
assert isinstance(TYPE, (lltype.GcStruct, lltype.GcArray)) |
| 339 |
|
| 340 |
|
| 341 |
if not TYPE._is_varsize(): |
| 342 |
fullinfo = lltype.malloc(GCData.TYPE_INFO, |
| 343 |
immortal=True, zero=True) |
| 344 |
info = fullinfo |
| 345 |
else: |
| 346 |
fullinfo = lltype.malloc(GCData.VARSIZE_TYPE_INFO, |
| 347 |
immortal=True, zero=True) |
| 348 |
info = fullinfo.header |
| 349 |
type_id = self.type_info_group.add_member(fullinfo) |
| 350 |
if self.can_encode_type_shape: |
| 351 |
encode_type_shape(self, info, TYPE, type_id.index) |
| 352 |
else: |
| 353 |
self._pending_type_shapes.append((info, TYPE, type_id.index)) |
| 354 |
|
| 355 |
self.id_of_type[TYPE] = type_id |
| 356 |
self.add_vtable_after_typeinfo(TYPE) |
| 357 |
return type_id |
| 358 |
|
| 359 |
def add_vtable_after_typeinfo(self, TYPE): |
| 360 |
|
| 361 |
|
| 362 |
if self.lltype2vtable is None: |
| 363 |
return |
| 364 |
|
| 365 |
vtable = self.lltype2vtable.get(TYPE, None) |
| 366 |
if vtable is not None: |
| 367 |
|
| 368 |
assert not TYPE._is_varsize() |
| 369 |
vtable = lltype.normalizeptr(vtable) |
| 370 |
self.type_info_group.add_member(vtable) |
| 371 |
else: |
| 372 |
|
| 373 |
|
| 374 |
assert not is_subclass_of_object(TYPE) |
| 375 |
|
| 376 |
def get_info(self, type_id): |
| 377 |
res = llop.get_group_member(GCData.TYPE_INFO_PTR, |
| 378 |
self.type_info_group._as_ptr(), |
| 379 |
type_id) |
| 380 |
_check_valid_type_info(res) |
| 381 |
return res |
| 382 |
|
| 383 |
def get_info_varsize(self, type_id): |
| 384 |
res = llop.get_group_member(GCData.VARSIZE_TYPE_INFO_PTR, |
| 385 |
self.type_info_group._as_ptr(), |
| 386 |
type_id) |
| 387 |
_check_valid_type_info_varsize(res) |
| 388 |
return res |
| 389 |
|
| 390 |
def is_weakref_type(self, TYPE): |
| 391 |
return TYPE == WEAKREF |
| 392 |
|
| 393 |
def encode_type_shapes_now(self): |
| 394 |
if not self.can_encode_type_shape: |
| 395 |
self.can_encode_type_shape = True |
| 396 |
for info, TYPE, index in self._pending_type_shapes: |
| 397 |
encode_type_shape(self, info, TYPE, index) |
| 398 |
del self._pending_type_shapes |
| 399 |
|
| 400 |
def delay_encoding(self): |
| 401 |
|
| 402 |
self._pending_type_shapes = [] |
| 403 |
self.can_encode_type_shape = False |
| 404 |
|
| 405 |
def offsets2table(self, offsets, TYPE): |
| 406 |
if len(offsets) == 0: |
| 407 |
TYPE = lltype.Void |
| 408 |
try: |
| 409 |
return self.offsettable_cache[TYPE] |
| 410 |
except KeyError: |
| 411 |
cachedarray = lltype.malloc(GCData.OFFSETS_TO_GC_PTR, |
| 412 |
len(offsets), immortal=True) |
| 413 |
for i, value in enumerate(offsets): |
| 414 |
cachedarray[i] = value |
| 415 |
self.offsettable_cache[TYPE] = cachedarray |
| 416 |
return cachedarray |
| 417 |
|
| 418 |
def close_table(self): |
| 419 |
|
| 420 |
self.can_add_new_types = False |
| 421 |
self.offsettable_cache = None |
| 422 |
return self.type_info_group |
| 423 |
|
| 424 |
def special_funcptr_for_type(self, TYPE): |
| 425 |
if TYPE in self._special_funcptrs: |
| 426 |
return self._special_funcptrs[TYPE] |
| 427 |
fptr1, is_lightweight = self.make_destructor_funcptr_for_type(TYPE) |
| 428 |
fptr2 = self.make_custom_trace_funcptr_for_type(TYPE) |
| 429 |
result = {} |
| 430 |
if fptr1: |
| 431 |
if is_lightweight: |
| 432 |
result["destructor"] = fptr1 |
| 433 |
else: |
| 434 |
result["old_style_finalizer"] = fptr1 |
| 435 |
if fptr2: |
| 436 |
result["custom_trace"] = fptr2 |
| 437 |
self._special_funcptrs[TYPE] = result |
| 438 |
return result |
| 439 |
|
| 440 |
def make_destructor_funcptr_for_type(self, TYPE): |
| 441 |
|
| 442 |
return None, False |
| 443 |
|
| 444 |
def make_custom_trace_funcptr_for_type(self, TYPE): |
| 445 |
|
| 446 |
return None |
| 447 |
|
| 448 |
def initialize_gc_query_function(self, gc): |
| 449 |
gcdata = GCData(self.type_info_group) |
| 450 |
gcdata.set_query_functions(gc) |
| 451 |
return gcdata |
| 452 |
|
| 453 |
def consider_constant(self, TYPE, value, gc): |
| 454 |
if value is not lltype.top_container(value): |
| 455 |
return |
| 456 |
if value in self.iseen_roots: |
| 457 |
return |
| 458 |
self.iseen_roots[value] = True |
| 459 |
|
| 460 |
if isinstance(TYPE, lltype.GcOpaqueType): |
| 461 |
self.consider_constant(lltype.typeOf(value.container), |
| 462 |
value.container, gc) |
| 463 |
return |
| 464 |
|
| 465 |
if isinstance(TYPE, (lltype.GcStruct, lltype.GcArray)): |
| 466 |
typeid = self.get_type_id(TYPE) |
| 467 |
hdr = gc.gcheaderbuilder.new_header(value) |
| 468 |
adr = llmemory.cast_ptr_to_adr(hdr) |
| 469 |
if gc.gcflag_dummy and self.is_dummy_struct(value): |
| 470 |
gc.init_gc_object_immortal(adr, typeid, flags=gc.gcflag_dummy) |
| 471 |
else: |
| 472 |
gc.init_gc_object_immortal(adr, typeid) |
| 473 |
self.all_prebuilt_gc.append(value) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
adr = llmemory.cast_ptr_to_adr(value._as_ptr()) |
| 480 |
if TYPE._gckind == "gc": |
| 481 |
if gc.prebuilt_gc_objects_are_static_roots or gc.DEBUG: |
| 482 |
appendto = self.addresses_of_static_ptrs |
| 483 |
else: |
| 484 |
return |
| 485 |
else: |
| 486 |
appendto = self.addresses_of_static_ptrs_in_nongc |
| 487 |
for a in gc_pointers_inside(value, adr, mutable_only=True): |
| 488 |
appendto.append(a) |
| 489 |
|
| 490 |
def is_dummy_struct(self, obj): |
| 491 |
return False |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
def offsets_to_gc_pointers(TYPE): |
| 499 |
offsets = [] |
| 500 |
if isinstance(TYPE, lltype.Struct): |
| 501 |
for name in TYPE._names: |
| 502 |
FIELD = getattr(TYPE, name) |
| 503 |
if isinstance(FIELD, lltype.Array): |
| 504 |
continue |
| 505 |
baseofs = llmemory.offsetof(TYPE, name) |
| 506 |
suboffsets = offsets_to_gc_pointers(FIELD) |
| 507 |
for s in suboffsets: |
| 508 |
try: |
| 509 |
knownzero = s == 0 |
| 510 |
except TypeError: |
| 511 |
knownzero = False |
| 512 |
if knownzero: |
| 513 |
offsets.append(baseofs) |
| 514 |
else: |
| 515 |
offsets.append(baseofs + s) |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
elif isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'gc': |
| 522 |
offsets.append(0) |
| 523 |
return offsets |
| 524 |
|
| 525 |
def gc_pointers_inside(v, adr, mutable_only=False): |
| 526 |
t = lltype.typeOf(v) |
| 527 |
if isinstance(t, lltype.Struct): |
| 528 |
skip = () |
| 529 |
if mutable_only: |
| 530 |
if t._hints.get('immutable'): |
| 531 |
return |
| 532 |
if 'immutable_fields' in t._hints: |
| 533 |
skip = t._hints['immutable_fields'].all_immutable_fields() |
| 534 |
for n, t2 in t._flds.iteritems(): |
| 535 |
if isinstance(t2, lltype.Ptr) and t2.TO._gckind == 'gc': |
| 536 |
if n not in skip: |
| 537 |
yield adr + llmemory.offsetof(t, n) |
| 538 |
elif isinstance(t2, (lltype.Array, lltype.Struct)): |
| 539 |
for a in gc_pointers_inside(getattr(v, n), |
| 540 |
adr + llmemory.offsetof(t, n), |
| 541 |
mutable_only): |
| 542 |
yield a |
| 543 |
elif isinstance(t, lltype.Array): |
| 544 |
if mutable_only and t._hints.get('immutable'): |
| 545 |
return |
| 546 |
if isinstance(t.OF, lltype.Ptr) and t.OF.TO._gckind == 'gc': |
| 547 |
for i in range(len(v.items)): |
| 548 |
yield adr + llmemory.itemoffsetof(t, i) |
| 549 |
elif isinstance(t.OF, lltype.Struct): |
| 550 |
for i in range(len(v.items)): |
| 551 |
for a in gc_pointers_inside(v.items[i], |
| 552 |
adr + llmemory.itemoffsetof(t, i), |
| 553 |
mutable_only): |
| 554 |
yield a |
| 555 |
|
| 556 |
def zero_gc_pointers(p): |
| 557 |
TYPE = lltype.typeOf(p).TO |
| 558 |
zero_gc_pointers_inside(p, TYPE) |
| 559 |
|
| 560 |
def zero_gc_pointers_inside(p, TYPE): |
| 561 |
if isinstance(TYPE, lltype.Struct): |
| 562 |
for name, FIELD in TYPE._flds.items(): |
| 563 |
if isinstance(FIELD, lltype.Ptr) and FIELD.TO._gckind == 'gc': |
| 564 |
setattr(p, name, lltype.nullptr(FIELD.TO)) |
| 565 |
elif isinstance(FIELD, lltype.ContainerType): |
| 566 |
zero_gc_pointers_inside(getattr(p, name), FIELD) |
| 567 |
elif isinstance(TYPE, lltype.Array): |
| 568 |
ITEM = TYPE.OF |
| 569 |
if isinstance(ITEM, lltype.Ptr) and ITEM.TO._gckind == 'gc': |
| 570 |
null = lltype.nullptr(ITEM.TO) |
| 571 |
for i in range(p._obj.getlength()): |
| 572 |
p[i] = null |
| 573 |
elif isinstance(ITEM, lltype.ContainerType): |
| 574 |
for i in range(p._obj.getlength()): |
| 575 |
zero_gc_pointers_inside(p[i], ITEM) |
| 576 |
|
| 577 |
def is_subclass_of_object(TYPE): |
| 578 |
while isinstance(TYPE, lltype.GcStruct): |
| 579 |
if TYPE is rclass.OBJECT: |
| 580 |
return True |
| 581 |
_, TYPE = TYPE._first_struct() |
| 582 |
return False |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
WEAKREF = lltype.GcStruct("weakref", ("weakptr", llmemory.Address)) |
| 588 |
WEAKREFPTR = lltype.Ptr(WEAKREF) |
| 589 |
sizeof_weakref= llmemory.sizeof(WEAKREF) |
| 590 |
empty_weakref = lltype.malloc(WEAKREF, immortal=True) |
| 591 |
empty_weakref.weakptr = llmemory.NULL |
| 592 |
weakptr_offset = llmemory.offsetof(WEAKREF, "weakptr") |
| 593 |
|
| 594 |
def ll_weakref_deref(wref): |
| 595 |
wref = llmemory.cast_weakrefptr_to_ptr(WEAKREFPTR, wref) |
| 596 |
return wref.weakptr |
| 597 |
|
| 598 |
def convert_weakref_to(targetptr): |
| 599 |
|
| 600 |
|
| 601 |
if not targetptr: |
| 602 |
return empty_weakref |
| 603 |
else: |
| 604 |
link = lltype.malloc(WEAKREF, immortal=True) |
| 605 |
link.weakptr = llmemory.cast_ptr_to_adr(targetptr) |
| 606 |
return link |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
FIN_TRIGGER_FUNC = lltype.FuncType([], lltype.Void) |
| 611 |
FIN_HANDLER_ARRAY = lltype.Array(('deque', llmemory.Address), |
| 612 |
('trigger', lltype.Ptr(FIN_TRIGGER_FUNC))) |