Author: Armin Rigo <[email protected]>
Branch: cpyext-gc-support-2
Changeset: r81952:a6a694f2a019
Date: 2016-01-26 17:51 +0100
http://bitbucket.org/pypy/pypy/changeset/a6a694f2a019/

Log:    more copying

diff --git a/pypy/module/cpyext/include/object.h 
b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -17,7 +17,8 @@
 #define staticforward static
 
 #define PyObject_HEAD  \
-    long ob_refcnt;       \
+    Py_ssize_t ob_refcnt;        \
+    Py_ssize_t ob_pypy_link;     \
     struct _typeobject *ob_type;
 
 #define PyObject_VAR_HEAD              \
@@ -25,7 +26,7 @@
        Py_ssize_t ob_size; /* Number of items in variable part */
 
 #define PyObject_HEAD_INIT(type)       \
-       1, type,
+       1, 0, type,
 
 #define PyVarObject_HEAD_INIT(type, size)      \
        PyObject_HEAD_INIT(type) size,
@@ -40,19 +41,19 @@
 
 #ifdef PYPY_DEBUG_REFCOUNT
 /* Slow version, but useful for debugging */
-#define Py_INCREF(ob)   (Py_IncRef((PyObject *)ob))
-#define Py_DECREF(ob)   (Py_DecRef((PyObject *)ob))
-#define Py_XINCREF(ob)  (Py_IncRef((PyObject *)ob))
-#define Py_XDECREF(ob)  (Py_DecRef((PyObject *)ob))
+#define Py_INCREF(ob)   (Py_IncRef((PyObject *)(ob)))
+#define Py_DECREF(ob)   (Py_DecRef((PyObject *)(ob)))
+#define Py_XINCREF(ob)  (Py_IncRef((PyObject *)(ob)))
+#define Py_XDECREF(ob)  (Py_DecRef((PyObject *)(ob)))
 #else
 /* Fast version */
-#define Py_INCREF(ob)   (((PyObject *)ob)->ob_refcnt++)
-#define Py_DECREF(ob)                                   \
+#define Py_INCREF(ob)   (((PyObject *)(ob))->ob_refcnt++)
+#define Py_DECREF(op)                                   \
     do {                                                \
-        if (((PyObject *)ob)->ob_refcnt > 1)            \
-            ((PyObject *)ob)->ob_refcnt--;              \
+        if (--((PyObject *)(op))->ob_refcnt != 0)       \
+            ;                                           \
         else                                            \
-            Py_DecRef((PyObject *)ob);                  \
+            _Py_Dealloc((PyObject *)(op));              \
     } while (0)
 
 #define Py_XINCREF(op) do { if ((op) == NULL) ; else Py_INCREF(op); } while (0)
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -134,6 +134,9 @@
 #________________________________________________________
 # refcounted object support
 
+class InvalidPointerException(Exception):
+    pass
+
 DEBUG_REFCOUNT = False
 
 def debug_refcount(*args, **kwargs):
@@ -229,48 +232,13 @@
     return get_typedescr(w_type.instancetypedef).realize(space, ref)
 
 
-# XXX Optimize these functions and put them into macro definitions
+@cpython_api([PyObject], lltype.Void)
+def Py_IncRef(space, obj):
+    incref(obj)
+
 @cpython_api([PyObject], lltype.Void)
 def Py_DecRef(space, obj):
-    ZZZ
-    if not obj:
-        return
-    assert lltype.typeOf(obj) == PyObject
-
-    obj.c_ob_refcnt -= 1
-    if DEBUG_REFCOUNT:
-        debug_refcount("DECREF", obj, obj.c_ob_refcnt, frame_stackdepth=3)
-    if obj.c_ob_refcnt == 0:
-        state = space.fromcache(RefcountState)
-        ptr = rffi.cast(ADDR, obj)
-        if ptr not in state.py_objects_r2w:
-            # this is a half-allocated object, lets call the deallocator
-            # without modifying the r2w/w2r dicts
-            _Py_Dealloc(space, obj)
-        else:
-            w_obj = state.py_objects_r2w[ptr]
-            del state.py_objects_r2w[ptr]
-            w_type = space.type(w_obj)
-            if not w_type.is_cpytype():
-                _Py_Dealloc(space, obj)
-            del state.py_objects_w2r[w_obj]
-            # if the object was a container for borrowed references
-            state.delete_borrower(w_obj)
-    else:
-        if not we_are_translated() and obj.c_ob_refcnt < 0:
-            message = "Negative refcount for obj %s with type %s" % (
-                obj, rffi.charp2str(obj.c_ob_type.c_tp_name))
-            print >>sys.stderr, message
-            assert False, message
-
-@cpython_api([PyObject], lltype.Void)
-def Py_IncRef(space, obj):
-    if not obj:
-        return
-    obj.c_ob_refcnt += 1
-    assert obj.c_ob_refcnt > 0
-    if DEBUG_REFCOUNT:
-        debug_refcount("INCREF", obj, obj.c_ob_refcnt, frame_stackdepth=3)
+    decref(space, obj)
 
 @cpython_api([PyObject], lltype.Void)
 def _Py_NewReference(space, obj):
@@ -279,6 +247,7 @@
     assert isinstance(w_type, W_TypeObject)
     get_typedescr(w_type.instancetypedef).realize(space, obj)
 
+@cpython_api([PyObject], lltype.Void)
 def _Py_Dealloc(space, obj):
     from pypy.module.cpyext.api import generic_cpy_call_dont_decref
     pto = obj.c_ob_type
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to