/src/cpython-install/include/python3.15/refcount.h
Line | Count | Source |
1 | | #ifndef _Py_REFCOUNT_H |
2 | | #define _Py_REFCOUNT_H |
3 | | #ifdef __cplusplus |
4 | | extern "C" { |
5 | | #endif |
6 | | |
7 | | |
8 | | #if !defined(_Py_OPAQUE_PYOBJECT) |
9 | | /* |
10 | | Immortalization: |
11 | | |
12 | | The following indicates the immortalization strategy depending on the amount |
13 | | of available bits in the reference count field. All strategies are backwards |
14 | | compatible but the specific reference count value or immortalization check |
15 | | might change depending on the specializations for the underlying system. |
16 | | |
17 | | Proper deallocation of immortal instances requires distinguishing between |
18 | | statically allocated immortal instances vs those promoted by the runtime to be |
19 | | immortal. The latter should be the only instances that require |
20 | | cleanup during runtime finalization. |
21 | | */ |
22 | | |
23 | | #if SIZEOF_VOID_P > 4 |
24 | | /* |
25 | | In 64+ bit systems, any object whose 32 bit reference count is >= 2**31 |
26 | | will be treated as immortal. |
27 | | |
28 | | Using the lower 32 bits makes the value backwards compatible by allowing |
29 | | C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely |
30 | | increase and decrease the objects reference count. |
31 | | |
32 | | In order to offer sufficient resilience to C extensions using the stable ABI |
33 | | compiled against 3.11 or earlier, we set the initial value near the |
34 | | middle of the range (2**31, 2**32). That way the refcount can be |
35 | | off by ~1 billion without affecting immortality. |
36 | | |
37 | | Reference count increases will use saturated arithmetic, taking advantage of |
38 | | having all the lower 32 bits set, which will avoid the reference count to go |
39 | | beyond the refcount limit. Immortality checks for reference count decreases will |
40 | | be done by checking the bit sign flag in the lower 32 bits. |
41 | | |
42 | | To ensure that once an object becomes immortal, it remains immortal, the threshold |
43 | | for omitting increfs is much higher than for omitting decrefs. Consequently, once |
44 | | the refcount for an object exceeds _Py_IMMORTAL_MINIMUM_REFCNT it will gradually |
45 | | increase over time until it reaches _Py_IMMORTAL_INITIAL_REFCNT. |
46 | | */ |
47 | | #define _Py_IMMORTAL_INITIAL_REFCNT (3ULL << 30) |
48 | | #define _Py_IMMORTAL_MINIMUM_REFCNT (1ULL << 31) |
49 | | #define _Py_STATIC_FLAG_BITS ((Py_ssize_t)(_Py_STATICALLY_ALLOCATED_FLAG | _Py_IMMORTAL_FLAGS)) |
50 | | #define _Py_STATIC_IMMORTAL_INITIAL_REFCNT (((Py_ssize_t)_Py_IMMORTAL_INITIAL_REFCNT) | (_Py_STATIC_FLAG_BITS << 48)) |
51 | | |
52 | | #else |
53 | | /* |
54 | | In 32 bit systems, an object will be treated as immortal if its reference |
55 | | count equals or exceeds _Py_IMMORTAL_MINIMUM_REFCNT (2**30). |
56 | | |
57 | | Using the lower 30 bits makes the value backwards compatible by allowing |
58 | | C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely |
59 | | increase and decrease the objects reference count. The object would lose its |
60 | | immortality, but the execution would still be correct. |
61 | | |
62 | | Reference count increases and decreases will first go through an immortality |
63 | | check by comparing the reference count field to the minimum immortality refcount. |
64 | | */ |
65 | | #define _Py_IMMORTAL_INITIAL_REFCNT ((Py_ssize_t)(5L << 28)) |
66 | | #define _Py_IMMORTAL_MINIMUM_REFCNT ((Py_ssize_t)(1L << 30)) |
67 | | #define _Py_STATIC_IMMORTAL_INITIAL_REFCNT ((Py_ssize_t)(7L << 28)) |
68 | | #define _Py_STATIC_IMMORTAL_MINIMUM_REFCNT ((Py_ssize_t)(6L << 28)) |
69 | | #endif |
70 | | |
71 | | // Py_GIL_DISABLED builds indicate immortal objects using `ob_ref_local`, which is |
72 | | // always 32-bits. |
73 | | #ifdef Py_GIL_DISABLED |
74 | | #define _Py_IMMORTAL_REFCNT_LOCAL UINT32_MAX |
75 | | #endif |
76 | | |
77 | | |
78 | | #ifdef Py_GIL_DISABLED |
79 | | // The shared reference count uses the two least-significant bits to store |
80 | | // flags. The remaining bits are used to store the reference count. |
81 | | # define _Py_REF_SHARED_SHIFT 2 |
82 | | # define _Py_REF_SHARED_FLAG_MASK 0x3 |
83 | | |
84 | | // The shared flags are initialized to zero. |
85 | | # define _Py_REF_SHARED_INIT 0x0 |
86 | | # define _Py_REF_MAYBE_WEAKREF 0x1 |
87 | | # define _Py_REF_QUEUED 0x2 |
88 | | # define _Py_REF_MERGED 0x3 |
89 | | |
90 | | // Create a shared field from a refcnt and desired flags |
91 | | # define _Py_REF_SHARED(refcnt, flags) \ |
92 | | (((refcnt) << _Py_REF_SHARED_SHIFT) + (flags)) |
93 | | #endif // Py_GIL_DISABLED |
94 | | #endif // _Py_OPAQUE_PYOBJECT |
95 | | |
96 | | // Py_REFCNT() implementation for the stable ABI |
97 | | PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob); |
98 | | |
99 | | #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000 |
100 | | // Stable ABI implements Py_REFCNT() as a function call |
101 | | // on limited C API version 3.14 and newer, and on abi3t. |
102 | | #elif defined(_Py_OPAQUE_PYOBJECT) |
103 | | // Py_REFCNT() is also a function call in abi3t. |
104 | | #else |
105 | 0 | static inline Py_ssize_t _Py_REFCNT(PyObject *ob) { |
106 | 0 | #if !defined(Py_GIL_DISABLED) |
107 | 0 | return ob->ob_refcnt; |
108 | 0 | #else |
109 | 0 | uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local); |
110 | 0 | if (local == _Py_IMMORTAL_REFCNT_LOCAL) { |
111 | 0 | return _Py_IMMORTAL_INITIAL_REFCNT; |
112 | 0 | } |
113 | 0 | Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared); |
114 | 0 | return _Py_STATIC_CAST(Py_ssize_t, local) + |
115 | 0 | Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT); |
116 | 0 | #endif |
117 | 0 | } |
118 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
119 | | # define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST(ob)) |
120 | | #else |
121 | | # define Py_REFCNT(ob) _Py_REFCNT(ob) |
122 | | #endif |
123 | | #endif |
124 | | |
125 | | #ifndef _Py_OPAQUE_PYOBJECT |
126 | | static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op) |
127 | 284k | { |
128 | | #if defined(Py_GIL_DISABLED) |
129 | | return (_Py_atomic_load_uint32_relaxed(&op->ob_ref_local) == |
130 | | _Py_IMMORTAL_REFCNT_LOCAL); |
131 | | #elif SIZEOF_VOID_P > 4 |
132 | 284k | return _Py_CAST(int32_t, op->ob_refcnt) < 0; |
133 | | #else |
134 | | return op->ob_refcnt >= _Py_IMMORTAL_MINIMUM_REFCNT; |
135 | | #endif |
136 | 284k | } |
137 | 284k | #define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op)) |
138 | | |
139 | | |
140 | | static inline Py_ALWAYS_INLINE int _Py_IsStaticImmortal(PyObject *op) |
141 | 0 | { |
142 | 0 | #if defined(Py_GIL_DISABLED) || SIZEOF_VOID_P > 4 |
143 | 0 | return (op->ob_flags & _Py_STATICALLY_ALLOCATED_FLAG) != 0; |
144 | 0 | #else |
145 | 0 | return op->ob_refcnt >= _Py_STATIC_IMMORTAL_MINIMUM_REFCNT; |
146 | 0 | #endif |
147 | 0 | } |
148 | | #define _Py_IsStaticImmortal(op) _Py_IsStaticImmortal(_PyObject_CAST(op)) |
149 | | #endif // !defined(_Py_OPAQUE_PYOBJECT) |
150 | | |
151 | | // Py_SET_REFCNT() implementation for stable ABI |
152 | | PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt); |
153 | | |
154 | 0 | static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { |
155 | 0 | assert(refcnt >= 0); |
156 | 0 | #if (defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030d0000) \ |
157 | 0 | || defined(_Py_OPAQUE_PYOBJECT) |
158 | 0 | // Stable ABI implements Py_SET_REFCNT() as a function call |
159 | 0 | // on limited C API version 3.13 and newer, and abi3t. |
160 | 0 | _Py_SetRefcnt(ob, refcnt); |
161 | 0 | #else |
162 | 0 | // This immortal check is for code that is unaware of immortal objects. |
163 | 0 | // The runtime tracks these objects and we should avoid as much |
164 | 0 | // as possible having extensions inadvertently change the refcnt |
165 | 0 | // of an immortalized object. |
166 | 0 | if (_Py_IsImmortal(ob)) { |
167 | 0 | return; |
168 | 0 | } |
169 | 0 | #ifndef Py_GIL_DISABLED |
170 | 0 | #if SIZEOF_VOID_P > 4 |
171 | 0 | ob->ob_refcnt = (uint32_t)refcnt; |
172 | 0 | #else |
173 | 0 | ob->ob_refcnt = refcnt; |
174 | 0 | #endif |
175 | 0 | #else |
176 | 0 | if (_Py_IsOwnedByCurrentThread(ob)) { |
177 | 0 | if ((size_t)refcnt > (size_t)UINT32_MAX) { |
178 | 0 | // On overflow, make the object immortal |
179 | 0 | ob->ob_tid = _Py_UNOWNED_TID; |
180 | 0 | ob->ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL; |
181 | 0 | ob->ob_ref_shared = 0; |
182 | 0 | } |
183 | 0 | else { |
184 | 0 | // Set local refcount to desired refcount and shared refcount |
185 | 0 | // to zero, but preserve the shared refcount flags. |
186 | 0 | ob->ob_ref_local = _Py_STATIC_CAST(uint32_t, refcnt); |
187 | 0 | ob->ob_ref_shared &= _Py_REF_SHARED_FLAG_MASK; |
188 | 0 | } |
189 | 0 | } |
190 | 0 | else { |
191 | 0 | // Set local refcount to zero and shared refcount to desired refcount. |
192 | 0 | // Mark the object as merged. |
193 | 0 | ob->ob_tid = _Py_UNOWNED_TID; |
194 | 0 | ob->ob_ref_local = 0; |
195 | 0 | ob->ob_ref_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED); |
196 | 0 | } |
197 | 0 | #endif // Py_GIL_DISABLED |
198 | 0 | #endif // Py_LIMITED_API |
199 | 0 | } |
200 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
201 | | # define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt)) |
202 | | #endif |
203 | | |
204 | | |
205 | | /* |
206 | | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement |
207 | | reference counts. Py_DECREF calls the object's deallocator function when |
208 | | the refcount falls to 0; for |
209 | | objects that don't contain references to other objects or heap memory |
210 | | this can be the standard function free(). Both macros can be used |
211 | | wherever a void expression is allowed. The argument must not be a |
212 | | NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead. |
213 | | The macro _Py_NewReference(op) initialize reference counts to 1, and |
214 | | in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional |
215 | | bookkeeping appropriate to the special build. |
216 | | |
217 | | We assume that the reference count field can never overflow; this can |
218 | | be proven when the size of the field is the same as the pointer size, so |
219 | | we ignore the possibility. Provided a C int is at least 32 bits (which |
220 | | is implicitly assumed in many parts of this code), that's enough for |
221 | | about 2**31 references to an object. |
222 | | |
223 | | XXX The following became out of date in Python 2.2, but I'm not sure |
224 | | XXX what the full truth is now. Certainly, heap-allocated type objects |
225 | | XXX can and should be deallocated. |
226 | | Type objects should never be deallocated; the type pointer in an object |
227 | | is not considered to be a reference to the type object, to save |
228 | | complications in the deallocation function. (This is actually a |
229 | | decision that's up to the implementer of each new type so if you want, |
230 | | you can count such references to the type object.) |
231 | | */ |
232 | | |
233 | | #if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API) |
234 | | PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno, |
235 | | PyObject *op); |
236 | | PyAPI_FUNC(void) _Py_INCREF_IncRefTotal(void); |
237 | | PyAPI_FUNC(void) _Py_DECREF_DecRefTotal(void); |
238 | | #endif // Py_REF_DEBUG && !Py_LIMITED_API |
239 | | |
240 | | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
241 | | |
242 | | |
243 | | /* |
244 | | These are provided as conveniences to Python runtime embedders, so that |
245 | | they can have object code that is not dependent on Python compilation flags. |
246 | | */ |
247 | | PyAPI_FUNC(void) Py_IncRef(PyObject *); |
248 | | PyAPI_FUNC(void) Py_DecRef(PyObject *); |
249 | | |
250 | | // Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL. |
251 | | // Private functions used by Py_INCREF() and Py_DECREF(). |
252 | | PyAPI_FUNC(void) _Py_IncRef(PyObject *); |
253 | | PyAPI_FUNC(void) _Py_DecRef(PyObject *); |
254 | | |
255 | | static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) |
256 | 0 | { |
257 | 0 | #if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ |
258 | 0 | || defined(_Py_OPAQUE_PYOBJECT) |
259 | 0 | // Stable ABI implements Py_INCREF() as a function call on limited C API |
260 | 0 | // version 3.12 and newer, abi3t, and on Python built in debug mode. |
261 | 0 | // _Py_IncRef() was added to Python 3.10.0a7, use Py_IncRef() on older versions. |
262 | 0 | // Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't. |
263 | 0 | # if Py_LIMITED_API+0 >= 0x030a00A7 |
264 | 0 | _Py_IncRef(op); |
265 | 0 | # else |
266 | 0 | Py_IncRef(op); |
267 | 0 | # endif |
268 | 0 | #else |
269 | 0 | // Non-limited C API and limited C API for Python 3.9 and older access |
270 | 0 | // directly PyObject.ob_refcnt. |
271 | 0 | #if defined(Py_GIL_DISABLED) |
272 | 0 | uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); |
273 | 0 | uint32_t new_local = local + 1; |
274 | 0 | if (new_local == 0) { |
275 | 0 | _Py_INCREF_IMMORTAL_STAT_INC(); |
276 | 0 | // local is equal to _Py_IMMORTAL_REFCNT_LOCAL: do nothing |
277 | 0 | return; |
278 | 0 | } |
279 | 0 | if (_Py_IsOwnedByCurrentThread(op)) { |
280 | 0 | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, new_local); |
281 | 0 | } |
282 | 0 | else { |
283 | 0 | _Py_atomic_add_ssize(&op->ob_ref_shared, (1 << _Py_REF_SHARED_SHIFT)); |
284 | 0 | } |
285 | 0 | #elif SIZEOF_VOID_P > 4 |
286 | 0 | uint32_t cur_refcnt = op->ob_refcnt; |
287 | 0 | if (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) { |
288 | 0 | // the object is immortal |
289 | 0 | _Py_INCREF_IMMORTAL_STAT_INC(); |
290 | 0 | return; |
291 | 0 | } |
292 | 0 | op->ob_refcnt = cur_refcnt + 1; |
293 | 0 | #else |
294 | 0 | if (_Py_IsImmortal(op)) { |
295 | 0 | _Py_INCREF_IMMORTAL_STAT_INC(); |
296 | 0 | return; |
297 | 0 | } |
298 | 0 | op->ob_refcnt++; |
299 | 0 | #endif |
300 | 0 | _Py_INCREF_STAT_INC(); |
301 | 0 | #ifdef Py_REF_DEBUG |
302 | 0 | // Don't count the incref if the object is immortal. |
303 | 0 | if (!_Py_IsImmortal(op)) { |
304 | 0 | _Py_INCREF_IncRefTotal(); |
305 | 0 | } |
306 | 0 | #endif |
307 | 0 | #endif |
308 | 0 | } |
309 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
310 | | # define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op)) |
311 | | #endif |
312 | | |
313 | | #if !defined(Py_LIMITED_API) |
314 | | #if defined(Py_GIL_DISABLED) |
315 | | // Implements Py_DECREF on objects not owned by the current thread. |
316 | | PyAPI_FUNC(void) _Py_DecRefShared(PyObject *); |
317 | | PyAPI_FUNC(void) _Py_DecRefSharedDebug(PyObject *, const char *, int); |
318 | | |
319 | | // Called from Py_DECREF by the owning thread when the local refcount reaches |
320 | | // zero. The call will deallocate the object if the shared refcount is also |
321 | | // zero. Otherwise, the thread gives up ownership and merges the reference |
322 | | // count fields. |
323 | | PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *); |
324 | | #endif // Py_GIL_DISABLED |
325 | | #endif // Py_LIMITED_API |
326 | | |
327 | | #if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ |
328 | | || defined(_Py_OPAQUE_PYOBJECT) |
329 | | // Stable ABI implements Py_DECREF() as a function call on limited C API |
330 | | // version 3.12 and newer, abi3t, and on Python built in debug mode. |
331 | | // _Py_DecRef() was added to Python 3.10.0a7, use Py_DecRef() on older versions. |
332 | | // Py_DecRef() accepts NULL whereas _Py_DecRef() doesn't. |
333 | | static inline void Py_DECREF(PyObject *op) { |
334 | | # if Py_LIMITED_API+0 >= 0x030a00A7 |
335 | | _Py_DecRef(op); |
336 | | # else |
337 | | Py_DecRef(op); |
338 | | # endif |
339 | | } |
340 | | #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) |
341 | | |
342 | | #elif defined(Py_GIL_DISABLED) && defined(Py_REF_DEBUG) |
343 | | static inline void Py_DECREF(const char *filename, int lineno, PyObject *op) |
344 | | { |
345 | | uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); |
346 | | if (local == _Py_IMMORTAL_REFCNT_LOCAL) { |
347 | | _Py_DECREF_IMMORTAL_STAT_INC(); |
348 | | return; |
349 | | } |
350 | | _Py_DECREF_STAT_INC(); |
351 | | _Py_DECREF_DecRefTotal(); |
352 | | if (_Py_IsOwnedByCurrentThread(op)) { |
353 | | if (local == 0) { |
354 | | _Py_NegativeRefcount(filename, lineno, op); |
355 | | } |
356 | | local--; |
357 | | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); |
358 | | if (local == 0) { |
359 | | _Py_MergeZeroLocalRefcount(op); |
360 | | } |
361 | | } |
362 | | else { |
363 | | _Py_DecRefSharedDebug(op, filename, lineno); |
364 | | } |
365 | | } |
366 | | #define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op)) |
367 | | |
368 | | #elif defined(Py_GIL_DISABLED) |
369 | | static inline void Py_DECREF(PyObject *op) |
370 | | { |
371 | | uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); |
372 | | if (local == _Py_IMMORTAL_REFCNT_LOCAL) { |
373 | | _Py_DECREF_IMMORTAL_STAT_INC(); |
374 | | return; |
375 | | } |
376 | | _Py_DECREF_STAT_INC(); |
377 | | if (_Py_IsOwnedByCurrentThread(op)) { |
378 | | local--; |
379 | | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); |
380 | | if (local == 0) { |
381 | | _Py_MergeZeroLocalRefcount(op); |
382 | | } |
383 | | } |
384 | | else { |
385 | | _Py_DecRefShared(op); |
386 | | } |
387 | | } |
388 | | #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) |
389 | | |
390 | | #elif defined(Py_REF_DEBUG) |
391 | | |
392 | | static inline void Py_DECREF(const char *filename, int lineno, PyObject *op) |
393 | | { |
394 | | #if SIZEOF_VOID_P > 4 |
395 | | /* If an object has been freed, it will have a negative full refcnt |
396 | | * If it has not it been freed, will have a very large refcnt */ |
397 | | if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((uint32_t)-1) - (1<<20))) { |
398 | | #else |
399 | | if (op->ob_refcnt <= 0) { |
400 | | #endif |
401 | | _Py_NegativeRefcount(filename, lineno, op); |
402 | | } |
403 | | if (_Py_IsImmortal(op)) { |
404 | | _Py_DECREF_IMMORTAL_STAT_INC(); |
405 | | return; |
406 | | } |
407 | | _Py_DECREF_STAT_INC(); |
408 | | _Py_DECREF_DecRefTotal(); |
409 | | if (--op->ob_refcnt == 0) { |
410 | | _Py_Dealloc(op); |
411 | | } |
412 | | } |
413 | | #define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op)) |
414 | | |
415 | | #else |
416 | | |
417 | | static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) |
418 | 284k | { |
419 | | // Non-limited C API and limited C API for Python 3.9 and older access |
420 | | // directly PyObject.ob_refcnt. |
421 | 284k | if (_Py_IsImmortal(op)) { |
422 | 142k | _Py_DECREF_IMMORTAL_STAT_INC(); |
423 | 142k | return; |
424 | 142k | } |
425 | 142k | _Py_DECREF_STAT_INC(); |
426 | 142k | if (--op->ob_refcnt == 0) { |
427 | 142k | _Py_Dealloc(op); |
428 | 142k | } |
429 | 142k | } |
430 | 284k | #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) |
431 | | #endif |
432 | | |
433 | | |
434 | | /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear |
435 | | * and tp_dealloc implementations. |
436 | | * |
437 | | * Note that "the obvious" code can be deadly: |
438 | | * |
439 | | * Py_XDECREF(op); |
440 | | * op = NULL; |
441 | | * |
442 | | * Typically, `op` is something like self->containee, and `self` is done |
443 | | * using its `containee` member. In the code sequence above, suppose |
444 | | * `containee` is non-NULL with a refcount of 1. Its refcount falls to |
445 | | * 0 on the first line, which can trigger an arbitrary amount of code, |
446 | | * possibly including finalizers (like __del__ methods or weakref callbacks) |
447 | | * coded in Python, which in turn can release the GIL and allow other threads |
448 | | * to run, etc. Such code may even invoke methods of `self` again, or cause |
449 | | * cyclic gc to trigger, but-- oops! --self->containee still points to the |
450 | | * object being torn down, and it may be in an insane state while being torn |
451 | | * down. This has in fact been a rich historic source of miserable (rare & |
452 | | * hard-to-diagnose) segfaulting (and other) bugs. |
453 | | * |
454 | | * The safe way is: |
455 | | * |
456 | | * Py_CLEAR(op); |
457 | | * |
458 | | * That arranges to set `op` to NULL _before_ decref'ing, so that any code |
459 | | * triggered as a side-effect of `op` getting torn down no longer believes |
460 | | * `op` points to a valid object. |
461 | | * |
462 | | * There are cases where it's safe to use the naive code, but they're brittle. |
463 | | * For example, if `op` points to a Python integer, you know that destroying |
464 | | * one of those can't cause problems -- but in part that relies on that |
465 | | * Python integers aren't currently weakly referencable. Best practice is |
466 | | * to use Py_CLEAR() even if you can't think of a reason for why you need to. |
467 | | * |
468 | | * gh-98724: Use a temporary variable to only evaluate the macro argument once, |
469 | | * to avoid the duplication of side effects if the argument has side effects. |
470 | | * |
471 | | * gh-99701: If the PyObject* type is used with casting arguments to PyObject*, |
472 | | * the code can be miscompiled with strict aliasing because of type punning. |
473 | | * With strict aliasing, a compiler considers that two pointers of different |
474 | | * types cannot read or write the same memory which enables optimization |
475 | | * opportunities. |
476 | | * |
477 | | * If available, use _Py_TYPEOF() to use the 'op' type for temporary variables, |
478 | | * and so avoid type punning. Otherwise, use memcpy() which causes type erasure |
479 | | * and so prevents the compiler to reuse an old cached 'op' value after |
480 | | * Py_CLEAR(). |
481 | | */ |
482 | | #ifdef _Py_TYPEOF |
483 | | #define Py_CLEAR(op) \ |
484 | | do { \ |
485 | | _Py_TYPEOF(op)* _tmp_op_ptr = &(op); \ |
486 | | _Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \ |
487 | | if (_tmp_old_op != NULL) { \ |
488 | | *_tmp_op_ptr = _Py_NULL; \ |
489 | | Py_DECREF(_tmp_old_op); \ |
490 | | } \ |
491 | | } while (0) |
492 | | #else |
493 | | #define Py_CLEAR(op) \ |
494 | | do { \ |
495 | | PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \ |
496 | | PyObject *_tmp_old_op = (*_tmp_op_ptr); \ |
497 | | if (_tmp_old_op != NULL) { \ |
498 | | PyObject *_null_ptr = _Py_NULL; \ |
499 | | memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \ |
500 | | Py_DECREF(_tmp_old_op); \ |
501 | | } \ |
502 | | } while (0) |
503 | | #endif |
504 | | |
505 | | |
506 | | /* Function to use in case the object pointer can be NULL: */ |
507 | | static inline void Py_XINCREF(PyObject *op) |
508 | 0 | { |
509 | 0 | if (op != _Py_NULL) { |
510 | 0 | Py_INCREF(op); |
511 | 0 | } |
512 | 0 | } |
513 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
514 | | # define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op)) |
515 | | #endif |
516 | | |
517 | | static inline void Py_XDECREF(PyObject *op) |
518 | 0 | { |
519 | 0 | if (op != _Py_NULL) { |
520 | 0 | Py_DECREF(op); |
521 | 0 | } |
522 | 0 | } |
523 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
524 | | # define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op)) |
525 | | #endif |
526 | | |
527 | | // Create a new strong reference to an object: |
528 | | // increment the reference count of the object and return the object. |
529 | | PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj); |
530 | | |
531 | | // Similar to Py_NewRef(), but the object can be NULL. |
532 | | PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj); |
533 | | |
534 | | static inline PyObject* _Py_NewRef(PyObject *obj) |
535 | 0 | { |
536 | 0 | Py_INCREF(obj); |
537 | 0 | return obj; |
538 | 0 | } |
539 | | |
540 | | static inline PyObject* _Py_XNewRef(PyObject *obj) |
541 | 0 | { |
542 | 0 | Py_XINCREF(obj); |
543 | 0 | return obj; |
544 | 0 | } |
545 | | |
546 | | // Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI. |
547 | | // Names overridden with macros by static inline functions for best |
548 | | // performances. |
549 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
550 | | # define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) |
551 | | # define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) |
552 | | #else |
553 | | # define Py_NewRef(obj) _Py_NewRef(obj) |
554 | | # define Py_XNewRef(obj) _Py_XNewRef(obj) |
555 | | #endif |
556 | | |
557 | | |
558 | | #ifdef __cplusplus |
559 | | } |
560 | | #endif |
561 | | #endif // !_Py_REFCOUNT_H |