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