Coverage Report

Created: 2026-11-30 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/workspace/out/libfuzzer-coverage-x86_64/include/python3.16/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 >= _Py_PACK_VERSION(3, 14)
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 < _Py_PACK_VERSION(3, 11)
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
33.4M
{
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
33.4M
    return _Py_CAST(int32_t, op->ob_refcnt) < 0;
133
#else
134
    return op->ob_refcnt >= _Py_IMMORTAL_MINIMUM_REFCNT;
135
#endif
136
33.4M
}
137
33.4M
#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 >= _Py_PACK_VERSION(3, 13)) \
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 < _Py_PACK_VERSION(3, 11)
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) || defined(_Py_OPAQUE_PYOBJECT)
258
0
    // gh-158401: The stable ABI now implements Py_INCREF() as a function
259
0
    // call on all limited C API versions. _Py_IncRef() was added to Python
260
0
    // 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL,
261
0
    // whereas _Py_IncRef() doesn't.
262
0
#  if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
263
0
    _Py_IncRef(op);
264
0
#  else
265
0
    Py_IncRef(op);
266
0
#  endif
267
0
#else
268
0
    // Non-limited C API and limited C API for Python 3.9 and older access
269
0
    // directly PyObject.ob_refcnt.
270
0
#if defined(Py_GIL_DISABLED)
271
0
    uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
272
0
    uint32_t new_local = local + 1;
273
0
    if (new_local == 0) {
274
0
        _Py_INCREF_IMMORTAL_STAT_INC();
275
0
        // local is equal to _Py_IMMORTAL_REFCNT_LOCAL: do nothing
276
0
        return;
277
0
    }
278
0
    if (_Py_IsOwnedByCurrentThread(op)) {
279
0
        _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, new_local);
280
0
    }
281
0
    else {
282
0
        _Py_atomic_add_ssize(&op->ob_ref_shared, (1 << _Py_REF_SHARED_SHIFT));
283
0
    }
284
0
#elif SIZEOF_VOID_P > 4
285
0
    uint32_t cur_refcnt = op->ob_refcnt;
286
0
    if (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) {
287
0
        // the object is immortal
288
0
        _Py_INCREF_IMMORTAL_STAT_INC();
289
0
        return;
290
0
    }
291
0
    op->ob_refcnt = cur_refcnt + 1;
292
0
#else
293
0
    if (_Py_IsImmortal(op)) {
294
0
        _Py_INCREF_IMMORTAL_STAT_INC();
295
0
        return;
296
0
    }
297
0
    op->ob_refcnt++;
298
0
#endif
299
0
    _Py_INCREF_STAT_INC();
300
0
#ifdef Py_REF_DEBUG
301
0
    // Don't count the incref if the object is immortal.
302
0
    if (!_Py_IsImmortal(op)) {
303
0
        _Py_INCREF_IncRefTotal();
304
0
    }
305
0
#endif
306
0
#endif
307
0
}
308
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11)
309
#  define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
310
#endif
311
312
#if !defined(Py_LIMITED_API)
313
#if defined(Py_GIL_DISABLED)
314
// Implements Py_DECREF on objects not owned by the current thread.
315
PyAPI_FUNC(void) _Py_DecRefShared(PyObject *);
316
PyAPI_FUNC(void) _Py_DecRefSharedDebug(PyObject *, const char *, int);
317
318
// Called from Py_DECREF by the owning thread when the local refcount reaches
319
// zero. The call will deallocate the object if the shared refcount is also
320
// zero. Otherwise, the thread gives up ownership and merges the reference
321
// count fields.
322
PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *);
323
#endif  // Py_GIL_DISABLED
324
#endif  // Py_LIMITED_API
325
326
#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT)
327
// gh-158401: The stable ABI now implements Py_DECREF() as a function call
328
// on all limited C API versions. _Py_DecRef() was added to Python 3.10.0a7,
329
// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas
330
// _Py_DecRef() doesn't.
331
static inline void Py_DECREF(PyObject *op) {
332
#  if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
333
    _Py_DecRef(op);
334
#  else
335
    Py_DecRef(op);
336
#  endif
337
}
338
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
339
340
#elif defined(Py_GIL_DISABLED) && defined(Py_REF_DEBUG)
341
static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
342
{
343
    uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
344
    if (local == _Py_IMMORTAL_REFCNT_LOCAL) {
345
        _Py_DECREF_IMMORTAL_STAT_INC();
346
        return;
347
    }
348
    _Py_DECREF_STAT_INC();
349
    _Py_DECREF_DecRefTotal();
350
    if (_Py_IsOwnedByCurrentThread(op)) {
351
        if (local == 0) {
352
            _Py_NegativeRefcount(filename, lineno, op);
353
        }
354
        local--;
355
        _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
356
        if (local == 0) {
357
            _Py_MergeZeroLocalRefcount(op);
358
        }
359
    }
360
    else {
361
        _Py_DecRefSharedDebug(op, filename, lineno);
362
    }
363
}
364
#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
365
366
#elif defined(Py_GIL_DISABLED)
367
static inline void Py_DECREF(PyObject *op)
368
{
369
    uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
370
    if (local == _Py_IMMORTAL_REFCNT_LOCAL) {
371
        _Py_DECREF_IMMORTAL_STAT_INC();
372
        return;
373
    }
374
    _Py_DECREF_STAT_INC();
375
    if (_Py_IsOwnedByCurrentThread(op)) {
376
        local--;
377
        _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
378
        if (local == 0) {
379
            _Py_MergeZeroLocalRefcount(op);
380
        }
381
    }
382
    else {
383
        _Py_DecRefShared(op);
384
    }
385
}
386
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
387
388
#elif defined(Py_REF_DEBUG)
389
390
static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
391
{
392
#if SIZEOF_VOID_P > 4
393
    /* If an object has been freed, it will have a negative full refcnt
394
     * If it has not it been freed, will have a very large refcnt */
395
    if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((uint32_t)-1) - (1<<20))) {
396
#else
397
    if (op->ob_refcnt <= 0) {
398
#endif
399
        _Py_NegativeRefcount(filename, lineno, op);
400
    }
401
    if (_Py_IsImmortal(op)) {
402
        _Py_DECREF_IMMORTAL_STAT_INC();
403
        return;
404
    }
405
    _Py_DECREF_STAT_INC();
406
    _Py_DECREF_DecRefTotal();
407
    if (--op->ob_refcnt == 0) {
408
        _Py_Dealloc(op);
409
    }
410
}
411
#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
412
413
#else
414
415
static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op)
416
33.4M
{
417
    // Non-limited C API and limited C API for Python 3.9 and older access
418
    // directly PyObject.ob_refcnt.
419
33.4M
    if (_Py_IsImmortal(op)) {
420
3.86k
        _Py_DECREF_IMMORTAL_STAT_INC();
421
3.86k
        return;
422
3.86k
    }
423
33.4M
    _Py_DECREF_STAT_INC();
424
33.4M
    if (--op->ob_refcnt == 0) {
425
33.3M
        _Py_Dealloc(op);
426
33.3M
    }
427
33.4M
}
428
33.4M
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
429
#endif
430
431
432
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
433
 * and tp_dealloc implementations.
434
 *
435
 * Note that "the obvious" code can be deadly:
436
 *
437
 *     Py_XDECREF(op);
438
 *     op = NULL;
439
 *
440
 * Typically, `op` is something like self->containee, and `self` is done
441
 * using its `containee` member.  In the code sequence above, suppose
442
 * `containee` is non-NULL with a refcount of 1.  Its refcount falls to
443
 * 0 on the first line, which can trigger an arbitrary amount of code,
444
 * possibly including finalizers (like __del__ methods or weakref callbacks)
445
 * coded in Python, which in turn can release the GIL and allow other threads
446
 * to run, etc.  Such code may even invoke methods of `self` again, or cause
447
 * cyclic gc to trigger, but-- oops! --self->containee still points to the
448
 * object being torn down, and it may be in an insane state while being torn
449
 * down.  This has in fact been a rich historic source of miserable (rare &
450
 * hard-to-diagnose) segfaulting (and other) bugs.
451
 *
452
 * The safe way is:
453
 *
454
 *      Py_CLEAR(op);
455
 *
456
 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
457
 * triggered as a side-effect of `op` getting torn down no longer believes
458
 * `op` points to a valid object.
459
 *
460
 * There are cases where it's safe to use the naive code, but they're brittle.
461
 * For example, if `op` points to a Python integer, you know that destroying
462
 * one of those can't cause problems -- but in part that relies on that
463
 * Python integers aren't currently weakly referencable.  Best practice is
464
 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
465
 *
466
 * gh-98724: Use a temporary variable to only evaluate the macro argument once,
467
 * to avoid the duplication of side effects if the argument has side effects.
468
 *
469
 * gh-99701: If the PyObject* type is used with casting arguments to PyObject*,
470
 * the code can be miscompiled with strict aliasing because of type punning.
471
 * With strict aliasing, a compiler considers that two pointers of different
472
 * types cannot read or write the same memory which enables optimization
473
 * opportunities.
474
 *
475
 * If available, use _Py_TYPEOF() to use the 'op' type for temporary variables,
476
 * and so avoid type punning. Otherwise, use memcpy() which causes type erasure
477
 * and so prevents the compiler to reuse an old cached 'op' value after
478
 * Py_CLEAR().
479
 *
480
 * On C++11 and newer, use "auto". On MSVC, check also _MSVC_LANG since
481
 * __cplusplus is 199711L unless the /Zc:__cplusplus flag is used.
482
 */
483
#ifdef _Py_TYPEOF
484
#define Py_CLEAR(op) \
485
    do { \
486
        _Py_TYPEOF(&(op)) _tmp_op_ptr = &(op); \
487
        _Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \
488
        if (_tmp_old_op != _Py_NULL) { \
489
            *_tmp_op_ptr = _Py_NULL; \
490
            Py_DECREF(_PyObject_CAST(_tmp_old_op)); \
491
        } \
492
    } while (0)
493
#elif defined(__cplusplus) && (__cplusplus >= 201103L ||  _MSVC_LANG >= 201103L)
494
#define Py_CLEAR(op) \
495
    do { \
496
        auto _tmp_op_ptr = &(op); \
497
        auto _tmp_old_op = (*_tmp_op_ptr); \
498
        if (_tmp_old_op != _Py_NULL) { \
499
            *_tmp_op_ptr = _Py_NULL; \
500
            Py_DECREF(_PyObject_CAST(_tmp_old_op)); \
501
        } \
502
    } while (0)
503
#else
504
#define Py_CLEAR(op) \
505
    do { \
506
        PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \
507
        PyObject *_tmp_old_op; \
508
        memcpy(&_tmp_old_op, _tmp_op_ptr, sizeof(PyObject*)); \
509
        if (_tmp_old_op != _Py_NULL) { \
510
            PyObject *_null_ptr = _Py_NULL; \
511
            memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \
512
            Py_DECREF(_tmp_old_op); \
513
        } \
514
    } while (0)
515
#endif
516
517
518
/* Safely decref `dst` and set `dst` to `src`.
519
 *
520
 * As in case of Py_CLEAR "the obvious" code can be deadly:
521
 *
522
 *     Py_DECREF(dst);
523
 *     dst = src;
524
 *
525
 * The safe way is:
526
 *
527
 *      Py_SETREF(dst, src);
528
 *
529
 * That arranges to set `dst` to `src` _before_ decref'ing, so that any code
530
 * triggered as a side-effect of `dst` getting torn down no longer believes
531
 * `dst` points to a valid object.
532
 *
533
 * Temporary variables are used to only evaluate macro arguments once and so
534
 * avoid the duplication of side effects. _Py_TYPEOF(), C++ auto, or memcpy()
535
 * is used to avoid a miscompilation caused by type punning. See Py_CLEAR()
536
 * comment for implementation details about type punning.
537
 *
538
 * The memcpy() implementation does not emit a compiler warning if 'src' has
539
 * not the same type than 'src': any pointer type is accepted for 'src'.
540
 */
541
#ifdef _Py_TYPEOF
542
#define Py_SETREF(dst, src) \
543
    do { \
544
        _Py_TYPEOF(&(dst)) _tmp_dst_ptr = &(dst); \
545
        _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
546
        *_tmp_dst_ptr = (src); \
547
        Py_DECREF(_PyObject_CAST(_tmp_old_dst)); \
548
    } while (0)
549
#elif defined(__cplusplus) && (__cplusplus >= 201103L ||  _MSVC_LANG >= 201103L)
550
#define Py_SETREF(dst, src) \
551
    do { \
552
        auto _tmp_dst_ptr = &(dst); \
553
        auto _tmp_old_dst = (*_tmp_dst_ptr); \
554
        *_tmp_dst_ptr = (src); \
555
        Py_DECREF(_PyObject_CAST(_tmp_old_dst)); \
556
    } while (0)
557
#else
558
#define Py_SETREF(dst, src) \
559
    do { \
560
        PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
561
        PyObject *_tmp_old_dst; \
562
        PyObject *_tmp_src = _PyObject_CAST(src); \
563
        memcpy(&_tmp_old_dst, _tmp_dst_ptr, sizeof(PyObject*)); \
564
        memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
565
        Py_DECREF(_tmp_old_dst); \
566
    } while (0)
567
#endif
568
569
/* Py_XSETREF() is a variant of Py_SETREF() that uses Py_XDECREF() instead of
570
 * Py_DECREF().
571
 */
572
#ifdef _Py_TYPEOF
573
#define Py_XSETREF(dst, src) \
574
    do { \
575
        _Py_TYPEOF(&(dst)) _tmp_dst_ptr = &(dst); \
576
        _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
577
        *_tmp_dst_ptr = (src); \
578
        Py_XDECREF(_PyObject_CAST(_tmp_old_dst)); \
579
    } while (0)
580
#elif defined(__cplusplus) && (__cplusplus >= 201103L ||  _MSVC_LANG >= 201103L)
581
#define Py_XSETREF(dst, src) \
582
    do { \
583
        auto _tmp_dst_ptr = &(dst); \
584
        auto _tmp_old_dst = (*_tmp_dst_ptr); \
585
        *_tmp_dst_ptr = (src); \
586
        Py_XDECREF(_PyObject_CAST(_tmp_old_dst)); \
587
    } while (0)
588
#else
589
#define Py_XSETREF(dst, src) \
590
    do { \
591
        PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
592
        PyObject *_tmp_old_dst; \
593
        PyObject *_tmp_src = _PyObject_CAST(src); \
594
        memcpy(&_tmp_old_dst, _tmp_dst_ptr, sizeof(PyObject*)); \
595
        memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
596
        Py_XDECREF(_tmp_old_dst); \
597
    } while (0)
598
#endif
599
600
601
/* Function to use in case the object pointer can be NULL: */
602
static inline void Py_XINCREF(PyObject *op)
603
0
{
604
0
    if (op != _Py_NULL) {
605
0
        Py_INCREF(op);
606
0
    }
607
0
}
608
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11)
609
#  define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op))
610
#endif
611
612
static inline void Py_XDECREF(PyObject *op)
613
27.7k
{
614
27.7k
    if (op != _Py_NULL) {
615
11.2k
        Py_DECREF(op);
616
11.2k
    }
617
27.7k
}
618
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11)
619
27.7k
#  define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op))
620
#endif
621
622
// Create a new strong reference to an object:
623
// increment the reference count of the object and return the object.
624
PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
625
626
// Similar to Py_NewRef(), but the object can be NULL.
627
PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
628
629
static inline PyObject* _Py_NewRef(PyObject *obj)
630
0
{
631
0
    Py_INCREF(obj);
632
0
    return obj;
633
0
}
634
635
static inline PyObject* _Py_XNewRef(PyObject *obj)
636
0
{
637
0
    Py_XINCREF(obj);
638
0
    return obj;
639
0
}
640
641
// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
642
// Names overridden with macros by static inline functions for best
643
// performances.
644
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11)
645
#  define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
646
#  define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
647
#else
648
#  define Py_NewRef(obj) _Py_NewRef(obj)
649
#  define Py_XNewRef(obj) _Py_XNewRef(obj)
650
#endif
651
652
653
#ifdef __cplusplus
654
}
655
#endif
656
#endif   // !_Py_REFCOUNT_H