[pypy-commit] pypy default: Add rgc.register_custom_light_finalizer(), similar to

arigo noreply at buildbot.pypy.org
Wed Jan 28 15:38:45 CET 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r75559:07bf50ea0b54
Date: 2015-01-28 15:32 +0100
http://bitbucket.org/pypy/pypy/changeset/07bf50ea0b54/

Log:	Add rgc.register_custom_light_finalizer(), similar to
	rgc.register_custom_trace_hook().

diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -684,7 +684,7 @@
 class RegisterGcTraceEntry(ExtRegistryEntry):
     _about_ = register_custom_trace_hook
 
-    def compute_result_annotation(self, *args_s):
+    def compute_result_annotation(self, s_tp, s_lambda_func):
         pass
 
     def specialize_call(self, hop):
@@ -692,3 +692,26 @@
         lambda_func = hop.args_s[1].const
         hop.exception_cannot_occur()
         hop.rtyper.custom_trace_funcs.append((TP, lambda_func()))
+
+def register_custom_light_finalizer(TP, lambda_func):
+    """ This function does not do anything, but called from any annotated
+    place, will tell that "func" is used as a lightweight finalizer for TP.
+    The func must be specified as "lambda: func" in this call, for internal
+    reasons.
+    """
+
+class RegisterCustomLightFinalizer(ExtRegistryEntry):
+    _about_ = register_custom_light_finalizer
+
+    def compute_result_annotation(self, s_tp, s_lambda_func):
+        pass
+
+    def specialize_call(self, hop):
+        from rpython.rtyper.llannotation import SomePtr
+        TP = hop.args_s[0].const
+        lambda_func = hop.args_s[1].const
+        ll_func = lambda_func()
+        args_s = [SomePtr(lltype.Ptr(TP))]
+        funcptr = hop.rtyper.annotate_helper_fn(ll_func, args_s)
+        hop.exception_cannot_occur()
+        lltype.attachRuntimeTypeInfo(TP, destrptr=funcptr)
diff --git a/rpython/translator/c/test/test_newgc.py b/rpython/translator/c/test/test_newgc.py
--- a/rpython/translator/c/test/test_newgc.py
+++ b/rpython/translator/c/test/test_newgc.py
@@ -473,6 +473,31 @@
         res = self.run('custom_trace', 0)
         assert res == 10000
 
+    def define_custom_light_finalizer(cls):
+        from rpython.rtyper.annlowlevel import llhelper
+        #
+        T = lltype.Struct('T', ('count', lltype.Signed))
+        t = lltype.malloc(T, zero=True, immortal=True, flavor='raw')
+        #
+        S = lltype.GcStruct('S', rtti=True)
+        def customlightfinlz(addr):
+            t.count += 1
+        lambda_customlightfinlz = lambda: customlightfinlz
+        #
+        def setup():
+            rgc.register_custom_light_finalizer(S, lambda_customlightfinlz)
+            for i in range(10000):
+                lltype.malloc(S)
+        def f(n):
+            setup()
+            llop.gc__collect(lltype.Void)
+            return t.count
+        return f
+
+    def test_custom_light_finalizer(self):
+        res = self.run('custom_light_finalizer', 0)
+        assert res == 10000
+
     def define_weakref(cls):
         import weakref
 


More information about the pypy-commit mailing list