Author: Armin Rigo <[email protected]>
Branch: stmgc-c7
Changeset: r74448:76875c1d32f0
Date: 2014-11-11 17:18 +0100
http://bitbucket.org/pypy/pypy/changeset/76875c1d32f0/

Log:    llify the class Hashtable, in preparation for a custom tracing hook.

diff --git a/rpython/memory/gctransform/stmframework.py 
b/rpython/memory/gctransform/stmframework.py
--- a/rpython/memory/gctransform/stmframework.py
+++ b/rpython/memory/gctransform/stmframework.py
@@ -10,6 +10,15 @@
 from rpython.rlib import rstm
 
 
+def invokecallback(root, visit_fn):
+    """Used as a callback for gc.trace().  There is also a custom tracer
+    in rpython.rlib.rstm that checks if the callback it gets is exactly
+    'invokecallback', and if so, it knows that the 'arg' is actually a
+    C-level visit function.
+    """
+    visit_fn(root)
+
+
 class StmFrameworkGCTransformer(BaseFrameworkGCTransformer):
 
     def _declare_functions(self, GCClass, getfn, s_gc, s_typeid16):
@@ -24,8 +33,6 @@
             getfn(pypy_stmcb_size_rounded_up, [llannotation.SomeAddress()],
                   annmodel.SomeInteger()))
         #
-        def invokecallback(root, visit_fn):
-            visit_fn(root)
         def pypy_stmcb_trace(obj, visit_fn):
             gc.trace(obj, invokecallback, visit_fn)
         pypy_stmcb_trace.c_name = "pypy_stmcb_trace"
diff --git a/rpython/rlib/rstm.py b/rpython/rlib/rstm.py
--- a/rpython/rlib/rstm.py
+++ b/rpython/rlib/rstm.py
@@ -181,37 +181,34 @@
                                        ('index', lltype.Unsigned),
                                        ('object', llmemory.GCREF))
 
+def ll_hashtable_get(h, key):
+    # 'key' must be a plain integer.  Returns a GCREF.
+    return llop.stm_hashtable_read(llmemory.GCREF, h, h.ll_raw_hashtable, key)
 
-class Hashtable(object):
+def ll_hashtable_set(h, key, value):
+    llop.stm_hashtable_write(lltype.Void, h, h.ll_raw_hashtable, key, value)
 
-    def __new__(cls):
-        "NOT_RPYTHON: for tests, return a HashtableForTest instance"
-        return HashtableForTest()
+_HASHTABLE_OBJ = lltype.GcStruct('HASHTABLE_OBJ',
+                                 ('ll_raw_hashtable', _STM_HASHTABLE_P),
+                                 adtmeths={'get': ll_hashtable_get,
+                                           'set': ll_hashtable_set})
 
-    def __init__(self):
-        # Pass a null pointer to _STM_HASHTABLE_ENTRY to 
stm_hashtable_create().
-        # Make sure we see a malloc() of it, so that its typeid is correctly
-        # initialized.  It can be done in a NonConstant(False) path so that
-        # the C compiler will actually drop it.
-        if NonConstant(False):
-            p = lltype.malloc(_STM_HASHTABLE_ENTRY)
-        else:
-            p = lltype.nullptr(_STM_HASHTABLE_ENTRY)
-        self.ll_raw_hashtable = llop.stm_hashtable_create(_STM_HASHTABLE_P, p)
+#def ll_hashtable_trace(...)
 
-    @rgc.must_be_light_finalizer
-    def __del__(self):
-        llop.stm_hashtable_free(lltype.Void, self.ll_raw_hashtable)
-
-    def get(self, key):
-        # 'key' must be a plain integer.  Returns a GCREF.
-        return llop.stm_hashtable_read(llmemory.GCREF, self,
-                                       self.ll_raw_hashtable, key)
-
-    def set(self, key, value):
-        llop.stm_hashtable_write(lltype.Void, self,
-                                 self.ll_raw_hashtable, key, value)
-
+def create_hashtable():
+    if not we_are_translated():
+        return HashtableForTest()      # for tests
+    # Pass a null pointer to _STM_HASHTABLE_ENTRY to stm_hashtable_create().
+    # Make sure we see a malloc() of it, so that its typeid is correctly
+    # initialized.  It can be done in a NonConstant(False) path so that
+    # the C compiler will actually drop it.
+    if NonConstant(False):
+        p = lltype.malloc(_STM_HASHTABLE_ENTRY)
+    else:
+        p = lltype.nullptr(_STM_HASHTABLE_ENTRY)
+    h = lltype.malloc(_HASHTABLE_OBJ)
+    h.ll_raw_hashtable = llop.stm_hashtable_create(_STM_HASHTABLE_P, p)
+    return h
 
 class HashtableForTest(object):
     _NULL = lltype.nullptr(llmemory.GCREF.TO)
diff --git a/rpython/translator/stm/test/test_ztranslated.py 
b/rpython/translator/stm/test/test_ztranslated.py
--- a/rpython/translator/stm/test/test_ztranslated.py
+++ b/rpython/translator/stm/test/test_ztranslated.py
@@ -559,7 +559,7 @@
             pass
 
         def main(argv):
-            h = rstm.Hashtable()
+            h = rstm.create_hashtable()
             p = h.get(-1234)
             assert p == lltype.nullptr(llmemory.GCREF.TO)
             #
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to