def unwrap(space, w_obj): gcrefobj = space.interpclass_w(w_obj) if isinstance(gcrefobj, W_GcRef): gcref = gcrefobj.gcref else: gcref = rgc.cast_instance_to_gcref(w_obj) return gcref
def test_get_memory_usage(): class X(object): pass x1 = X() n = rgc.get_rpy_memory_usage(rgc.cast_instance_to_gcref(x1)) assert n >= 8 and n <= 64
def get_referrers(space, args_w): """Return the list of objects that directly refer to any of objs.""" roots = rgc.get_rpy_roots() pending_w = _get_objects_from_rpy(roots) arguments_w = {} for w_obj in args_w: arguments_w[w_obj] = None # continue by following every W_Root. Same remark about hashes as # in get_objects(). result_w = {} seen_w = {} while len(pending_w) > 0: previous_w = pending_w pending_w = [] for w_obj in previous_w: if w_obj not in seen_w: seen_w[w_obj] = None gcref = rgc.cast_instance_to_gcref(w_obj) referents_w = [] _list_w_obj_referents(gcref, referents_w) for w_subobj in referents_w: if w_subobj in arguments_w: result_w[w_obj] = None pending_w += referents_w return space.newlist(result_w.keys())
def get_referents(space, args_w): """Return a list of objects directly referred to by any of the arguments. Approximative: follow references recursively until it finds app-level objects. May return several times the same object, too.""" result = [] for w_obj in args_w: gcref = rgc.cast_instance_to_gcref(w_obj) _list_w_obj_referents(gcref, result) return space.newlist(result)
def test_get_referents(): class X(object): __slots__ = ['stuff'] x1 = X() x1.stuff = X() x2 = X() lst = rgc.get_rpy_referents(rgc.cast_instance_to_gcref(x1)) lst2 = [rgc.try_cast_gcref_to_instance(X, x) for x in lst] assert x1.stuff in lst2 assert x2 not in lst2
def fn(): s = lltype.malloc(S) gcref1 = lltype.cast_opaque_ptr(llmemory.GCREF, s) check(gcref1, False) f = Foo() gcref3 = rgc.cast_instance_to_gcref(f) check(gcref3, True) return 0
def fn(): foo = Foo() gcref1 = rgc.cast_instance_to_gcref(foo) assert rgc.try_cast_gcref_to_instance(Foo, gcref1) is foo assert rgc.try_cast_gcref_to_instance(FooBar, gcref1) is None assert rgc.try_cast_gcref_to_instance(Biz, gcref1) is None foobar = FooBar() gcref2 = rgc.cast_instance_to_gcref(foobar) assert rgc.try_cast_gcref_to_instance(Foo, gcref2) is foobar assert rgc.try_cast_gcref_to_instance(FooBar, gcref2) is foobar assert rgc.try_cast_gcref_to_instance(Biz, gcref2) is None s = lltype.malloc(S) gcref3 = lltype.cast_opaque_ptr(llmemory.GCREF, s) assert rgc.try_cast_gcref_to_instance(Foo, gcref3) is None assert rgc.try_cast_gcref_to_instance(FooBar, gcref3) is None assert rgc.try_cast_gcref_to_instance(Biz, gcref3) is None return 0
def get_objects(space): """Return a list of all app-level objects.""" roots = rgc.get_rpy_roots() pending_w = _get_objects_from_rpy(roots) # continue by following every W_Root. Note that this will force a hash # on every W_Root, which is kind of bad, but not on every RPython object, # which is really good. result_w = {} while len(pending_w) > 0: previous_w = pending_w pending_w = [] for w_obj in previous_w: if w_obj not in result_w: result_w[w_obj] = None gcref = rgc.cast_instance_to_gcref(w_obj) _list_w_obj_referents(gcref, pending_w) return space.newlist(result_w.keys())