|
4 | 4 | extern "C" { |
5 | 5 | #endif |
6 | 6 |
|
| 7 | +// Define this to get precise tracking of stackrefs. |
| 8 | +#define Py_STACKREF_DEBUG 1 |
| 9 | + |
7 | 10 | #ifndef Py_BUILD_CORE |
8 | 11 | # error "this header requires Py_BUILD_CORE define" |
9 | 12 | #endif |
@@ -49,6 +52,105 @@ extern "C" { |
49 | 52 | CPython refcounting operations on it! |
50 | 53 | */ |
51 | 54 |
|
| 55 | + |
| 56 | +#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG) |
| 57 | + |
| 58 | +typedef union _PyStackRef { |
| 59 | + uint64_t index; |
| 60 | +} _PyStackRef; |
| 61 | + |
| 62 | +#define Py_TAG_BITS 0 |
| 63 | + |
| 64 | +PyAPI_FUNC(PyObject *) _Py_stackref_get_object(_PyStackRef ref); |
| 65 | +PyAPI_FUNC(PyObject *) _Py_stackref_close(_PyStackRef ref); |
| 66 | +PyAPI_FUNC(_PyStackRef) _Py_stackref_create(PyObject *obj, const char *filename, int linenumber); |
| 67 | +extern void _Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref); |
| 68 | + |
| 69 | +static const _PyStackRef PyStackRef_NULL = { .index = 0 }; |
| 70 | + |
| 71 | +#define PyStackRef_None ((_PyStackRef){ .index = 1 } ) |
| 72 | +#define PyStackRef_False ((_PyStackRef){ .index = 2 }) |
| 73 | +#define PyStackRef_True ((_PyStackRef){ .index = 3 }) |
| 74 | + |
| 75 | +static inline int |
| 76 | +PyStackRef_IsNull(_PyStackRef ref) |
| 77 | +{ |
| 78 | + return ref.index == 0; |
| 79 | +} |
| 80 | + |
| 81 | +static inline int |
| 82 | +PyStackRef_IsTrue(_PyStackRef ref) |
| 83 | +{ |
| 84 | + return _Py_stackref_get_object(ref) == Py_True; |
| 85 | +} |
| 86 | + |
| 87 | +static inline int |
| 88 | +PyStackRef_IsFalse(_PyStackRef ref) |
| 89 | +{ |
| 90 | + return _Py_stackref_get_object(ref) == Py_False; |
| 91 | +} |
| 92 | + |
| 93 | +static inline int |
| 94 | +PyStackRef_IsNone(_PyStackRef ref) |
| 95 | +{ |
| 96 | + return _Py_stackref_get_object(ref) == Py_None; |
| 97 | +} |
| 98 | + |
| 99 | +static inline PyObject * |
| 100 | +PyStackRef_AsPyObjectBorrow(_PyStackRef ref) |
| 101 | +{ |
| 102 | + return _Py_stackref_get_object(ref); |
| 103 | +} |
| 104 | + |
| 105 | +static inline PyObject * |
| 106 | +PyStackRef_AsPyObjectSteal(_PyStackRef ref) |
| 107 | +{ |
| 108 | + return _Py_stackref_close(ref); |
| 109 | +} |
| 110 | + |
| 111 | +static inline _PyStackRef |
| 112 | +_PyStackRef_FromPyObjectNew(PyObject *obj, const char *filename, int linenumber) |
| 113 | +{ |
| 114 | + Py_INCREF(obj); |
| 115 | + return _Py_stackref_create(obj, filename, linenumber); |
| 116 | +} |
| 117 | +#define PyStackRef_FromPyObjectNew(obj) _PyStackRef_FromPyObjectNew(_PyObject_CAST(obj), __FILE__, __LINE__) |
| 118 | + |
| 119 | +static inline _PyStackRef |
| 120 | +_PyStackRef_FromPyObjectSteal(PyObject *obj, const char *filename, int linenumber) |
| 121 | +{ |
| 122 | + return _Py_stackref_create(obj, filename, linenumber); |
| 123 | +} |
| 124 | +#define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj), __FILE__, __LINE__) |
| 125 | + |
| 126 | +static inline _PyStackRef |
| 127 | +_PyStackRef_FromPyObjectImmortal(PyObject *obj, const char *filename, int linenumber) |
| 128 | +{ |
| 129 | + assert(_Py_IsImmortal(obj)); |
| 130 | + return _Py_stackref_create(obj, filename, linenumber); |
| 131 | +} |
| 132 | +#define PyStackRef_FromPyObjectImmortal(obj) _PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj), __FILE__, __LINE__) |
| 133 | + |
| 134 | +static inline void |
| 135 | +PyStackRef_CLOSE(_PyStackRef ref) |
| 136 | +{ |
| 137 | + PyObject *obj = _Py_stackref_close(ref); |
| 138 | + Py_DECREF(obj); |
| 139 | +} |
| 140 | + |
| 141 | +static inline _PyStackRef |
| 142 | +_PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber) |
| 143 | +{ |
| 144 | + PyObject *obj = _Py_stackref_get_object(ref); |
| 145 | + Py_INCREF(obj); |
| 146 | + return _Py_stackref_create(obj, filename, linenumber); |
| 147 | +} |
| 148 | +#define PyStackRef_DUP(REF) _PyStackRef_DUP(REF, __FILE__, __LINE__) |
| 149 | + |
| 150 | +#define PyStackRef_CLOSE_SPECIALIZED(stackref, dealloc) PyStackRef_CLOSE(stackref) |
| 151 | + |
| 152 | +#else |
| 153 | + |
52 | 154 | typedef union _PyStackRef { |
53 | 155 | uintptr_t bits; |
54 | 156 | } _PyStackRef; |
@@ -200,12 +302,15 @@ static const _PyStackRef PyStackRef_NULL = { .bits = 0 }; |
200 | 302 | #define PyStackRef_IsTrue(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_True) |
201 | 303 | #define PyStackRef_IsFalse(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_False) |
202 | 304 |
|
| 305 | +#endif |
| 306 | + |
203 | 307 | // Converts a PyStackRef back to a PyObject *, converting the |
204 | 308 | // stackref to a new reference. |
205 | 309 | #define PyStackRef_AsPyObjectNew(stackref) Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref)) |
206 | 310 |
|
207 | 311 | #define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref)) |
208 | 312 |
|
| 313 | + |
209 | 314 | #define PyStackRef_CLEAR(op) \ |
210 | 315 | do { \ |
211 | 316 | _PyStackRef *_tmp_op_ptr = &(op); \ |
|
0 commit comments