| 1 |
#ifndef Py_INTERNAL_OBJECT_H |
| 2 |
#define Py_INTERNAL_OBJECT_H |
| 3 |
#ifdef __cplusplus |
| 4 |
extern "C" { |
| 5 |
#endif |
| 6 |
|
| 7 |
#ifndef Py_BUILD_CORE |
| 8 |
# error "this header requires Py_BUILD_CORE define" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <stdbool.h> |
| 12 |
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED() |
| 13 |
#include "pycore_emscripten_trampoline.h" // _PyCFunction_TrampolineCall() |
| 14 |
#include "pycore_interp.h" // PyInterpreterState.gc |
| 15 |
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED |
| 16 |
#include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 17 |
|
| 18 |
|
| 19 |
#define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1) |
| 20 |
|
| 21 |
// gh-121528, gh-118997: Similar to _Py_IsImmortal() but be more loose when |
| 22 |
// comparing the reference count to stay compatible with C extensions built |
| 23 |
// with the stable ABI 3.11 or older. Such extensions implement INCREF/DECREF |
| 24 |
// as refcnt++ and refcnt-- without taking in account immortal objects. For |
| 25 |
// example, the reference count of an immortal object can change from |
| 26 |
// _Py_IMMORTAL_REFCNT to _Py_IMMORTAL_REFCNT+1 (INCREF) or |
| 27 |
// _Py_IMMORTAL_REFCNT-1 (DECREF). |
| 28 |
// |
| 29 |
// This function should only be used in assertions. Otherwise, _Py_IsImmortal() |
| 30 |
// must be used instead. |
| 31 |
static inline int _Py_IsImmortalLoose(PyObject *op) |
| 32 |
{ |
| 33 |
#if defined(Py_GIL_DISABLED) |
| 34 |
return _Py_IsImmortal(op); |
| 35 |
#else |
| 36 |
return (op->ob_refcnt >= _Py_IMMORTAL_REFCNT_LOOSE); |
| 37 |
#endif |
| 38 |
} |
| 39 |
#define _Py_IsImmortalLoose(op) _Py_IsImmortalLoose(_PyObject_CAST(op)) |
| 40 |
|
| 41 |
|
| 42 |
/* Check if an object is consistent. For example, ensure that the reference |
| 43 |
counter is greater than or equal to 1, and ensure that ob_type is not NULL. |
| 44 |
|
| 45 |
Call _PyObject_AssertFailed() if the object is inconsistent. |
| 46 |
|
| 47 |
If check_content is zero, only check header fields: reduce the overhead. |
| 48 |
|
| 49 |
The function always return 1. The return value is just here to be able to |
| 50 |
write: |
| 51 |
|
| 52 |
assert(_PyObject_CheckConsistency(obj, 1)); */ |
| 53 |
extern int _PyObject_CheckConsistency(PyObject *op, int check_content); |
| 54 |
|
| 55 |
extern void _PyDebugAllocatorStats(FILE *out, const char *block_name, |
| 56 |
int num_blocks, size_t sizeof_block); |
| 57 |
|
| 58 |
extern void _PyObject_DebugTypeStats(FILE *out); |
| 59 |
|
| 60 |
#ifdef Py_TRACE_REFS |
| 61 |
// Forget a reference registered by _Py_NewReference(). Function called by |
| 62 |
// _Py_Dealloc(). |
| 63 |
// |
| 64 |
// On a free list, the function can be used before modifying an object to |
| 65 |
// remove the object from traced objects. Then _Py_NewReference() or |
| 66 |
// _Py_NewReferenceNoTotal() should be called again on the object to trace |
| 67 |
// it again. |
| 68 |
extern void _Py_ForgetReference(PyObject *); |
| 69 |
#endif |
| 70 |
|
| 71 |
// Export for shared _testinternalcapi extension |
| 72 |
PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *); |
| 73 |
|
| 74 |
/* We need to maintain an internal copy of Py{Var}Object_HEAD_INIT to avoid |
| 75 |
designated initializer conflicts in C++20. If we use the definition in |
| 76 |
object.h, we will be mixing designated and non-designated initializers in |
| 77 |
pycore objects which is forbiddent in C++20. However, if we then use |
| 78 |
designated initializers in object.h then Extensions without designated break. |
| 79 |
Furthermore, we can't use designated initializers in Extensions since these |
| 80 |
are not supported pre-C++20. Thus, keeping an internal copy here is the most |
| 81 |
backwards compatible solution */ |
| 82 |
#if defined(Py_GIL_DISABLED) |
| 83 |
#define _PyObject_HEAD_INIT(type) \ |
| 84 |
{ \ |
| 85 |
.ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL, \ |
| 86 |
.ob_type = (type) \ |
| 87 |
} |
| 88 |
#else |
| 89 |
#define _PyObject_HEAD_INIT(type) \ |
| 90 |
{ \ |
| 91 |
.ob_refcnt = _Py_IMMORTAL_REFCNT, \ |
| 92 |
.ob_type = (type) \ |
| 93 |
} |
| 94 |
#endif |
| 95 |
#define _PyVarObject_HEAD_INIT(type, size) \ |
| 96 |
{ \ |
| 97 |
.ob_base = _PyObject_HEAD_INIT(type), \ |
| 98 |
.ob_size = size \ |
| 99 |
} |
| 100 |
|
| 101 |
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc( |
| 102 |
const char *func, |
| 103 |
const char *message); |
| 104 |
|
| 105 |
#define _Py_FatalRefcountError(message) \ |
| 106 |
_Py_FatalRefcountErrorFunc(__func__, (message)) |
| 107 |
|
| 108 |
#define _PyReftracerTrack(obj, operation) \ |
| 109 |
do { \ |
| 110 |
struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer; \ |
| 111 |
if (tracer->tracer_func != NULL) { \ |
| 112 |
void *data = tracer->tracer_data; \ |
| 113 |
tracer->tracer_func((obj), (operation), data); \ |
| 114 |
} \ |
| 115 |
} while(0) |
| 116 |
|
| 117 |
#ifdef Py_REF_DEBUG |
| 118 |
/* The symbol is only exposed in the API for the sake of extensions |
| 119 |
built against the pre-3.12 stable ABI. */ |
| 120 |
PyAPI_DATA(Py_ssize_t) _Py_RefTotal; |
| 121 |
|
| 122 |
extern void _Py_AddRefTotal(PyThreadState *, Py_ssize_t); |
| 123 |
extern void _Py_IncRefTotal(PyThreadState *); |
| 124 |
extern void _Py_DecRefTotal(PyThreadState *); |
| 125 |
|
| 126 |
# define _Py_DEC_REFTOTAL(interp) \ |
| 127 |
interp->object_state.reftotal-- |
| 128 |
#endif |
| 129 |
|
| 130 |
// Increment reference count by n |
| 131 |
static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n) |
| 132 |
{ |
| 133 |
if (_Py_IsImmortal(op)) { |
| 134 |
return; |
| 135 |
} |
| 136 |
#ifdef Py_REF_DEBUG |
| 137 |
_Py_AddRefTotal(_PyThreadState_GET(), n); |
| 138 |
#endif |
| 139 |
#if !defined(Py_GIL_DISABLED) |
| 140 |
op->ob_refcnt += n; |
| 141 |
#else |
| 142 |
if (_Py_IsOwnedByCurrentThread(op)) { |
| 143 |
uint32_t local = op->ob_ref_local; |
| 144 |
Py_ssize_t refcnt = (Py_ssize_t)local + n; |
| 145 |
# if PY_SSIZE_T_MAX > UINT32_MAX |
| 146 |
if (refcnt > (Py_ssize_t)UINT32_MAX) { |
| 147 |
// Make the object immortal if the 32-bit local reference count |
| 148 |
// would overflow. |
| 149 |
refcnt = _Py_IMMORTAL_REFCNT_LOCAL; |
| 150 |
} |
| 151 |
# endif |
| 152 |
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, (uint32_t)refcnt); |
| 153 |
} |
| 154 |
else { |
| 155 |
_Py_atomic_add_ssize(&op->ob_ref_shared, (n << _Py_REF_SHARED_SHIFT)); |
| 156 |
} |
| 157 |
#endif |
| 158 |
// Although the ref count was increased by `n` (which may be greater than 1) |
| 159 |
// it is only a single increment (i.e. addition) operation, so only 1 refcnt |
| 160 |
// increment operation is counted. |
| 161 |
_Py_INCREF_STAT_INC(); |
| 162 |
} |
| 163 |
#define _Py_RefcntAdd(op, n) _Py_RefcntAdd(_PyObject_CAST(op), n) |
| 164 |
|
| 165 |
extern void _Py_SetImmortal(PyObject *op); |
| 166 |
extern void _Py_SetImmortalUntracked(PyObject *op); |
| 167 |
|
| 168 |
// Checks if an object has a single, unique reference. If the caller holds a |
| 169 |
// unique reference, it may be able to safely modify the object in-place. |
| 170 |
static inline int |
| 171 |
_PyObject_IsUniquelyReferenced(PyObject *ob) |
| 172 |
{ |
| 173 |
#if !defined(Py_GIL_DISABLED) |
| 174 |
return Py_REFCNT(ob) == 1; |
| 175 |
#else |
| 176 |
// NOTE: the entire ob_ref_shared field must be zero, including flags, to |
| 177 |
// ensure that other threads cannot concurrently create new references to |
| 178 |
// this object. |
| 179 |
return (_Py_IsOwnedByCurrentThread(ob) && |
| 180 |
_Py_atomic_load_uint32_relaxed(&ob->ob_ref_local) == 1 && |
| 181 |
_Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared) == 0); |
| 182 |
#endif |
| 183 |
} |
| 184 |
|
| 185 |
// Makes an immortal object mortal again with the specified refcnt. Should only |
| 186 |
// be used during runtime finalization. |
| 187 |
static inline void _Py_SetMortal(PyObject *op, Py_ssize_t refcnt) |
| 188 |
{ |
| 189 |
if (op) { |
| 190 |
assert(_Py_IsImmortalLoose(op)); |
| 191 |
#ifdef Py_GIL_DISABLED |
| 192 |
op->ob_tid = _Py_UNOWNED_TID; |
| 193 |
op->ob_ref_local = 0; |
| 194 |
op->ob_ref_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED); |
| 195 |
#else |
| 196 |
op->ob_refcnt = refcnt; |
| 197 |
#endif |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/* _Py_ClearImmortal() should only be used during runtime finalization. */ |
| 202 |
static inline void _Py_ClearImmortal(PyObject *op) |
| 203 |
{ |
| 204 |
if (op) { |
| 205 |
_Py_SetMortal(op, 1); |
| 206 |
Py_DECREF(op); |
| 207 |
} |
| 208 |
} |
| 209 |
#define _Py_ClearImmortal(op) \ |
| 210 |
do { \ |
| 211 |
_Py_ClearImmortal(_PyObject_CAST(op)); \ |
| 212 |
op = NULL; \ |
| 213 |
} while (0) |
| 214 |
|
| 215 |
// Mark an object as supporting deferred reference counting. This is a no-op |
| 216 |
// in the default (with GIL) build. Objects that use deferred reference |
| 217 |
// counting should be tracked by the GC so that they are eventually collected. |
| 218 |
extern void _PyObject_SetDeferredRefcount(PyObject *op); |
| 219 |
|
| 220 |
static inline int |
| 221 |
_PyObject_HasDeferredRefcount(PyObject *op) |
| 222 |
{ |
| 223 |
#ifdef Py_GIL_DISABLED |
| 224 |
return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_DEFERRED); |
| 225 |
#else |
| 226 |
return 0; |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
#if !defined(Py_GIL_DISABLED) |
| 231 |
static inline void |
| 232 |
_Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct) |
| 233 |
{ |
| 234 |
if (_Py_IsImmortal(op)) { |
| 235 |
return; |
| 236 |
} |
| 237 |
_Py_DECREF_STAT_INC(); |
| 238 |
#ifdef Py_REF_DEBUG |
| 239 |
_Py_DEC_REFTOTAL(PyInterpreterState_Get()); |
| 240 |
#endif |
| 241 |
if (--op->ob_refcnt != 0) { |
| 242 |
assert(op->ob_refcnt > 0); |
| 243 |
} |
| 244 |
else { |
| 245 |
#ifdef Py_TRACE_REFS |
| 246 |
_Py_ForgetReference(op); |
| 247 |
#endif |
| 248 |
_PyReftracerTrack(op, PyRefTracer_DESTROY); |
| 249 |
destruct(op); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static inline void |
| 254 |
_Py_DECREF_NO_DEALLOC(PyObject *op) |
| 255 |
{ |
| 256 |
if (_Py_IsImmortal(op)) { |
| 257 |
return; |
| 258 |
} |
| 259 |
_Py_DECREF_STAT_INC(); |
| 260 |
#ifdef Py_REF_DEBUG |
| 261 |
_Py_DEC_REFTOTAL(PyInterpreterState_Get()); |
| 262 |
#endif |
| 263 |
op->ob_refcnt--; |
| 264 |
#ifdef Py_DEBUG |
| 265 |
if (op->ob_refcnt <= 0) { |
| 266 |
_Py_FatalRefcountError("Expected a positive remaining refcount"); |
| 267 |
} |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
#else |
| 272 |
// TODO: implement Py_DECREF specializations for Py_GIL_DISABLED build |
| 273 |
static inline void |
| 274 |
_Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct) |
| 275 |
{ |
| 276 |
Py_DECREF(op); |
| 277 |
} |
| 278 |
|
| 279 |
static inline void |
| 280 |
_Py_DECREF_NO_DEALLOC(PyObject *op) |
| 281 |
{ |
| 282 |
Py_DECREF(op); |
| 283 |
} |
| 284 |
|
| 285 |
static inline int |
| 286 |
_Py_REF_IS_MERGED(Py_ssize_t ob_ref_shared) |
| 287 |
{ |
| 288 |
return (ob_ref_shared & _Py_REF_SHARED_FLAG_MASK) == _Py_REF_MERGED; |
| 289 |
} |
| 290 |
|
| 291 |
static inline int |
| 292 |
_Py_REF_IS_QUEUED(Py_ssize_t ob_ref_shared) |
| 293 |
{ |
| 294 |
return (ob_ref_shared & _Py_REF_SHARED_FLAG_MASK) == _Py_REF_QUEUED; |
| 295 |
} |
| 296 |
|
| 297 |
// Merge the local and shared reference count fields and add `extra` to the |
| 298 |
// refcount when merging. |
| 299 |
Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra); |
| 300 |
#endif // !defined(Py_GIL_DISABLED) |
| 301 |
|
| 302 |
#ifdef Py_REF_DEBUG |
| 303 |
# undef _Py_DEC_REFTOTAL |
| 304 |
#endif |
| 305 |
|
| 306 |
|
| 307 |
extern int _PyType_CheckConsistency(PyTypeObject *type); |
| 308 |
extern int _PyDict_CheckConsistency(PyObject *mp, int check_content); |
| 309 |
|
| 310 |
/* Update the Python traceback of an object. This function must be called |
| 311 |
when a memory block is reused from a free list. |
| 312 |
|
| 313 |
Internal function called by _Py_NewReference(). */ |
| 314 |
extern int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event, void*); |
| 315 |
|
| 316 |
// Fast inlined version of PyType_HasFeature() |
| 317 |
static inline int |
| 318 |
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) { |
| 319 |
return ((FT_ATOMIC_LOAD_ULONG_RELAXED(type->tp_flags) & feature) != 0); |
| 320 |
} |
| 321 |
|
| 322 |
extern void _PyType_InitCache(PyInterpreterState *interp); |
| 323 |
|
| 324 |
extern PyStatus _PyObject_InitState(PyInterpreterState *interp); |
| 325 |
extern void _PyObject_FiniState(PyInterpreterState *interp); |
| 326 |
extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj); |
| 327 |
|
| 328 |
/* Inline functions trading binary compatibility for speed: |
| 329 |
_PyObject_Init() is the fast version of PyObject_Init(), and |
| 330 |
_PyObject_InitVar() is the fast version of PyObject_InitVar(). |
| 331 |
|
| 332 |
These inline functions must not be called with op=NULL. */ |
| 333 |
static inline void |
| 334 |
_PyObject_Init(PyObject *op, PyTypeObject *typeobj) |
| 335 |
{ |
| 336 |
assert(op != NULL); |
| 337 |
Py_SET_TYPE(op, typeobj); |
| 338 |
assert(_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortalLoose(typeobj)); |
| 339 |
Py_INCREF(typeobj); |
| 340 |
_Py_NewReference(op); |
| 341 |
} |
| 342 |
|
| 343 |
static inline void |
| 344 |
_PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size) |
| 345 |
{ |
| 346 |
assert(op != NULL); |
| 347 |
assert(typeobj != &PyLong_Type); |
| 348 |
_PyObject_Init((PyObject *)op, typeobj); |
| 349 |
Py_SET_SIZE(op, size); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
/* Tell the GC to track this object. |
| 354 |
* |
| 355 |
* The object must not be tracked by the GC. |
| 356 |
* |
| 357 |
* NB: While the object is tracked by the collector, it must be safe to call the |
| 358 |
* ob_traverse method. |
| 359 |
* |
| 360 |
* Internal note: interp->gc.generation0->_gc_prev doesn't have any bit flags |
| 361 |
* because it's not object header. So we don't use _PyGCHead_PREV() and |
| 362 |
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations. |
| 363 |
* |
| 364 |
* See also the public PyObject_GC_Track() function. |
| 365 |
*/ |
| 366 |
static inline void _PyObject_GC_TRACK( |
| 367 |
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined |
| 368 |
#ifndef NDEBUG |
| 369 |
const char *filename, int lineno, |
| 370 |
#endif |
| 371 |
PyObject *op) |
| 372 |
{ |
| 373 |
_PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op), |
| 374 |
"object already tracked by the garbage collector", |
| 375 |
filename, lineno, __func__); |
| 376 |
#ifdef Py_GIL_DISABLED |
| 377 |
_PyObject_SET_GC_BITS(op, _PyGC_BITS_TRACKED); |
| 378 |
#else |
| 379 |
PyGC_Head *gc = _Py_AS_GC(op); |
| 380 |
_PyObject_ASSERT_FROM(op, |
| 381 |
(gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0, |
| 382 |
"object is in generation which is garbage collected", |
| 383 |
filename, lineno, __func__); |
| 384 |
|
| 385 |
PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 386 |
PyGC_Head *generation0 = interp->gc.generation0; |
| 387 |
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev); |
| 388 |
_PyGCHead_SET_NEXT(last, gc); |
| 389 |
_PyGCHead_SET_PREV(gc, last); |
| 390 |
_PyGCHead_SET_NEXT(gc, generation0); |
| 391 |
generation0->_gc_prev = (uintptr_t)gc; |
| 392 |
#endif |
| 393 |
} |
| 394 |
|
| 395 |
/* Tell the GC to stop tracking this object. |
| 396 |
* |
| 397 |
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING |
| 398 |
* must be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept. |
| 399 |
* |
| 400 |
* The object must be tracked by the GC. |
| 401 |
* |
| 402 |
* See also the public PyObject_GC_UnTrack() which accept an object which is |
| 403 |
* not tracked. |
| 404 |
*/ |
| 405 |
static inline void _PyObject_GC_UNTRACK( |
| 406 |
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined |
| 407 |
#ifndef NDEBUG |
| 408 |
const char *filename, int lineno, |
| 409 |
#endif |
| 410 |
PyObject *op) |
| 411 |
{ |
| 412 |
_PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op), |
| 413 |
"object not tracked by the garbage collector", |
| 414 |
filename, lineno, __func__); |
| 415 |
|
| 416 |
#ifdef Py_GIL_DISABLED |
| 417 |
_PyObject_CLEAR_GC_BITS(op, _PyGC_BITS_TRACKED); |
| 418 |
#else |
| 419 |
PyGC_Head *gc = _Py_AS_GC(op); |
| 420 |
PyGC_Head *prev = _PyGCHead_PREV(gc); |
| 421 |
PyGC_Head *next = _PyGCHead_NEXT(gc); |
| 422 |
_PyGCHead_SET_NEXT(prev, next); |
| 423 |
_PyGCHead_SET_PREV(next, prev); |
| 424 |
gc->_gc_next = 0; |
| 425 |
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; |
| 426 |
#endif |
| 427 |
} |
| 428 |
|
| 429 |
// Macros to accept any type for the parameter, and to automatically pass |
| 430 |
// the filename and the filename (if NDEBUG is not defined) where the macro |
| 431 |
// is called. |
| 432 |
#ifdef NDEBUG |
| 433 |
# define _PyObject_GC_TRACK(op) \ |
| 434 |
_PyObject_GC_TRACK(_PyObject_CAST(op)) |
| 435 |
# define _PyObject_GC_UNTRACK(op) \ |
| 436 |
_PyObject_GC_UNTRACK(_PyObject_CAST(op)) |
| 437 |
#else |
| 438 |
# define _PyObject_GC_TRACK(op) \ |
| 439 |
_PyObject_GC_TRACK(__FILE__, __LINE__, _PyObject_CAST(op)) |
| 440 |
# define _PyObject_GC_UNTRACK(op) \ |
| 441 |
_PyObject_GC_UNTRACK(__FILE__, __LINE__, _PyObject_CAST(op)) |
| 442 |
#endif |
| 443 |
|
| 444 |
#ifdef Py_GIL_DISABLED |
| 445 |
|
| 446 |
/* Tries to increment an object's reference count |
| 447 |
* |
| 448 |
* This is a specialized version of _Py_TryIncref that only succeeds if the |
| 449 |
* object is immortal or local to this thread. It does not handle the case |
| 450 |
* where the reference count modification requires an atomic operation. This |
| 451 |
* allows call sites to specialize for the immortal/local case. |
| 452 |
*/ |
| 453 |
static inline int |
| 454 |
_Py_TryIncrefFast(PyObject *op) { |
| 455 |
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); |
| 456 |
local += 1; |
| 457 |
if (local == 0) { |
| 458 |
// immortal |
| 459 |
return 1; |
| 460 |
} |
| 461 |
if (_Py_IsOwnedByCurrentThread(op)) { |
| 462 |
_Py_INCREF_STAT_INC(); |
| 463 |
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); |
| 464 |
#ifdef Py_REF_DEBUG |
| 465 |
_Py_IncRefTotal(_PyThreadState_GET()); |
| 466 |
#endif |
| 467 |
return 1; |
| 468 |
} |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
static inline int |
| 473 |
_Py_TryIncRefShared(PyObject *op) |
| 474 |
{ |
| 475 |
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); |
| 476 |
for (;;) { |
| 477 |
// If the shared refcount is zero and the object is either merged |
| 478 |
// or may not have weak references, then we cannot incref it. |
| 479 |
if (shared == 0 || shared == _Py_REF_MERGED) { |
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
if (_Py_atomic_compare_exchange_ssize( |
| 484 |
&op->ob_ref_shared, |
| 485 |
&shared, |
| 486 |
shared + (1 << _Py_REF_SHARED_SHIFT))) { |
| 487 |
#ifdef Py_REF_DEBUG |
| 488 |
_Py_IncRefTotal(_PyThreadState_GET()); |
| 489 |
#endif |
| 490 |
_Py_INCREF_STAT_INC(); |
| 491 |
return 1; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
/* Tries to incref the object op and ensures that *src still points to it. */ |
| 497 |
static inline int |
| 498 |
_Py_TryIncrefCompare(PyObject **src, PyObject *op) |
| 499 |
{ |
| 500 |
if (_Py_TryIncrefFast(op)) { |
| 501 |
return 1; |
| 502 |
} |
| 503 |
if (!_Py_TryIncRefShared(op)) { |
| 504 |
return 0; |
| 505 |
} |
| 506 |
if (op != _Py_atomic_load_ptr(src)) { |
| 507 |
Py_DECREF(op); |
| 508 |
return 0; |
| 509 |
} |
| 510 |
return 1; |
| 511 |
} |
| 512 |
|
| 513 |
/* Loads and increfs an object from ptr, which may contain a NULL value. |
| 514 |
Safe with concurrent (atomic) updates to ptr. |
| 515 |
NOTE: The writer must set maybe-weakref on the stored object! */ |
| 516 |
static inline PyObject * |
| 517 |
_Py_XGetRef(PyObject **ptr) |
| 518 |
{ |
| 519 |
for (;;) { |
| 520 |
PyObject *value = _Py_atomic_load_ptr(ptr); |
| 521 |
if (value == NULL) { |
| 522 |
return value; |
| 523 |
} |
| 524 |
if (_Py_TryIncrefCompare(ptr, value)) { |
| 525 |
return value; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
/* Attempts to loads and increfs an object from ptr. Returns NULL |
| 531 |
on failure, which may be due to a NULL value or a concurrent update. */ |
| 532 |
static inline PyObject * |
| 533 |
_Py_TryXGetRef(PyObject **ptr) |
| 534 |
{ |
| 535 |
PyObject *value = _Py_atomic_load_ptr(ptr); |
| 536 |
if (value == NULL) { |
| 537 |
return value; |
| 538 |
} |
| 539 |
if (_Py_TryIncrefCompare(ptr, value)) { |
| 540 |
return value; |
| 541 |
} |
| 542 |
return NULL; |
| 543 |
} |
| 544 |
|
| 545 |
/* Like Py_NewRef but also optimistically sets _Py_REF_MAYBE_WEAKREF |
| 546 |
on objects owned by a different thread. */ |
| 547 |
static inline PyObject * |
| 548 |
_Py_NewRefWithLock(PyObject *op) |
| 549 |
{ |
| 550 |
if (_Py_TryIncrefFast(op)) { |
| 551 |
return op; |
| 552 |
} |
| 553 |
#ifdef Py_REF_DEBUG |
| 554 |
_Py_IncRefTotal(_PyThreadState_GET()); |
| 555 |
#endif |
| 556 |
_Py_INCREF_STAT_INC(); |
| 557 |
for (;;) { |
| 558 |
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); |
| 559 |
Py_ssize_t new_shared = shared + (1 << _Py_REF_SHARED_SHIFT); |
| 560 |
if ((shared & _Py_REF_SHARED_FLAG_MASK) == 0) { |
| 561 |
new_shared |= _Py_REF_MAYBE_WEAKREF; |
| 562 |
} |
| 563 |
if (_Py_atomic_compare_exchange_ssize( |
| 564 |
&op->ob_ref_shared, |
| 565 |
&shared, |
| 566 |
new_shared)) { |
| 567 |
return op; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
static inline PyObject * |
| 573 |
_Py_XNewRefWithLock(PyObject *obj) |
| 574 |
{ |
| 575 |
if (obj == NULL) { |
| 576 |
return NULL; |
| 577 |
} |
| 578 |
return _Py_NewRefWithLock(obj); |
| 579 |
} |
| 580 |
|
| 581 |
static inline void |
| 582 |
_PyObject_SetMaybeWeakref(PyObject *op) |
| 583 |
{ |
| 584 |
if (_Py_IsImmortal(op)) { |
| 585 |
return; |
| 586 |
} |
| 587 |
for (;;) { |
| 588 |
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); |
| 589 |
if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) { |
| 590 |
// Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states. |
| 591 |
return; |
| 592 |
} |
| 593 |
if (_Py_atomic_compare_exchange_ssize( |
| 594 |
&op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) { |
| 595 |
return; |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
extern int _PyObject_ResurrectEndSlow(PyObject *op); |
| 601 |
#endif |
| 602 |
|
| 603 |
// Temporarily resurrects an object during deallocation. The refcount is set |
| 604 |
// to one. |
| 605 |
static inline void |
| 606 |
_PyObject_ResurrectStart(PyObject *op) |
| 607 |
{ |
| 608 |
assert(Py_REFCNT(op) == 0); |
| 609 |
#ifdef Py_REF_DEBUG |
| 610 |
_Py_IncRefTotal(_PyThreadState_GET()); |
| 611 |
#endif |
| 612 |
#ifdef Py_GIL_DISABLED |
| 613 |
_Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_ThreadId()); |
| 614 |
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 1); |
| 615 |
_Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, 0); |
| 616 |
#else |
| 617 |
Py_SET_REFCNT(op, 1); |
| 618 |
#endif |
| 619 |
} |
| 620 |
|
| 621 |
// Undoes an object resurrection by decrementing the refcount without calling |
| 622 |
// _Py_Dealloc(). Returns 0 if the object is dead (the normal case), and |
| 623 |
// deallocation should continue. Returns 1 if the object is still alive. |
| 624 |
static inline int |
| 625 |
_PyObject_ResurrectEnd(PyObject *op) |
| 626 |
{ |
| 627 |
#ifdef Py_REF_DEBUG |
| 628 |
_Py_DecRefTotal(_PyThreadState_GET()); |
| 629 |
#endif |
| 630 |
#ifndef Py_GIL_DISABLED |
| 631 |
Py_SET_REFCNT(op, Py_REFCNT(op) - 1); |
| 632 |
return Py_REFCNT(op) != 0; |
| 633 |
#else |
| 634 |
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); |
| 635 |
Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared); |
| 636 |
if (_Py_IsOwnedByCurrentThread(op) && local == 1 && shared == 0) { |
| 637 |
// Fast-path: object has a single refcount and is owned by this thread |
| 638 |
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); |
| 639 |
return 0; |
| 640 |
} |
| 641 |
// Slow-path: object has a shared refcount or is not owned by this thread |
| 642 |
return _PyObject_ResurrectEndSlow(op); |
| 643 |
#endif |
| 644 |
} |
| 645 |
|
| 646 |
/* Tries to incref op and returns 1 if successful or 0 otherwise. */ |
| 647 |
static inline int |
| 648 |
_Py_TryIncref(PyObject *op) |
| 649 |
{ |
| 650 |
#ifdef Py_GIL_DISABLED |
| 651 |
return _Py_TryIncrefFast(op) || _Py_TryIncRefShared(op); |
| 652 |
#else |
| 653 |
if (Py_REFCNT(op) > 0) { |
| 654 |
Py_INCREF(op); |
| 655 |
return 1; |
| 656 |
} |
| 657 |
return 0; |
| 658 |
#endif |
| 659 |
} |
| 660 |
|
| 661 |
#ifdef Py_REF_DEBUG |
| 662 |
extern void _PyInterpreterState_FinalizeRefTotal(PyInterpreterState *); |
| 663 |
extern void _Py_FinalizeRefTotal(_PyRuntimeState *); |
| 664 |
extern void _PyDebug_PrintTotalRefs(void); |
| 665 |
#endif |
| 666 |
|
| 667 |
#ifdef Py_TRACE_REFS |
| 668 |
extern void _Py_AddToAllObjects(PyObject *op); |
| 669 |
extern void _Py_PrintReferences(PyInterpreterState *, FILE *); |
| 670 |
extern void _Py_PrintReferenceAddresses(PyInterpreterState *, FILE *); |
| 671 |
#endif |
| 672 |
|
| 673 |
|
| 674 |
/* Return the *address* of the object's weaklist. The address may be |
| 675 |
* dereferenced to get the current head of the weaklist. This is useful |
| 676 |
* for iterating over the linked list of weakrefs, especially when the |
| 677 |
* list is being modified externally (e.g. refs getting removed). |
| 678 |
* |
| 679 |
* The returned pointer should not be used to change the head of the list |
| 680 |
* nor should it be used to add, remove, or swap any refs in the list. |
| 681 |
* That is the sole responsibility of the code in weakrefobject.c. |
| 682 |
*/ |
| 683 |
static inline PyObject ** |
| 684 |
_PyObject_GET_WEAKREFS_LISTPTR(PyObject *op) |
| 685 |
{ |
| 686 |
if (PyType_Check(op) && |
| 687 |
((PyTypeObject *)op)->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { |
| 688 |
PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 689 |
managed_static_type_state *state = _PyStaticType_GetState( |
| 690 |
interp, (PyTypeObject *)op); |
| 691 |
return _PyStaticType_GET_WEAKREFS_LISTPTR(state); |
| 692 |
} |
| 693 |
// Essentially _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(): |
| 694 |
Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset; |
| 695 |
return (PyObject **)((char *)op + offset); |
| 696 |
} |
| 697 |
|
| 698 |
/* This is a special case of _PyObject_GET_WEAKREFS_LISTPTR(). |
| 699 |
* Only the most fundamental lookup path is used. |
| 700 |
* Consequently, static types should not be used. |
| 701 |
* |
| 702 |
* For static builtin types the returned pointer will always point |
| 703 |
* to a NULL tp_weaklist. This is fine for any deallocation cases, |
| 704 |
* since static types are never deallocated and static builtin types |
| 705 |
* are only finalized at the end of runtime finalization. |
| 706 |
* |
| 707 |
* If the weaklist for static types is actually needed then use |
| 708 |
* _PyObject_GET_WEAKREFS_LISTPTR(). |
| 709 |
*/ |
| 710 |
static inline PyWeakReference ** |
| 711 |
_PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(PyObject *op) |
| 712 |
{ |
| 713 |
assert(!PyType_Check(op) || |
| 714 |
((PyTypeObject *)op)->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 715 |
Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset; |
| 716 |
return (PyWeakReference **)((char *)op + offset); |
| 717 |
} |
| 718 |
|
| 719 |
// Fast inlined version of PyObject_IS_GC() |
| 720 |
static inline int |
| 721 |
_PyObject_IS_GC(PyObject *obj) |
| 722 |
{ |
| 723 |
PyTypeObject *type = Py_TYPE(obj); |
| 724 |
return (PyType_IS_GC(type) |
| 725 |
&& (type->tp_is_gc == NULL || type->tp_is_gc(obj))); |
| 726 |
} |
| 727 |
|
| 728 |
// Fast inlined version of PyObject_Hash() |
| 729 |
static inline Py_hash_t |
| 730 |
_PyObject_HashFast(PyObject *op) |
| 731 |
{ |
| 732 |
if (PyUnicode_CheckExact(op)) { |
| 733 |
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED( |
| 734 |
_PyASCIIObject_CAST(op)->hash); |
| 735 |
if (hash != -1) { |
| 736 |
return hash; |
| 737 |
} |
| 738 |
} |
| 739 |
return PyObject_Hash(op); |
| 740 |
} |
| 741 |
|
| 742 |
// Fast inlined version of PyType_IS_GC() |
| 743 |
#define _PyType_IS_GC(t) _PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) |
| 744 |
|
| 745 |
static inline size_t |
| 746 |
_PyType_PreHeaderSize(PyTypeObject *tp) |
| 747 |
{ |
| 748 |
return ( |
| 749 |
#ifndef Py_GIL_DISABLED |
| 750 |
_PyType_IS_GC(tp) * sizeof(PyGC_Head) + |
| 751 |
#endif |
| 752 |
_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER) * 2 * sizeof(PyObject *) |
| 753 |
); |
| 754 |
} |
| 755 |
|
| 756 |
void _PyObject_GC_Link(PyObject *op); |
| 757 |
|
| 758 |
// Usage: assert(_Py_CheckSlotResult(obj, "__getitem__", result != NULL)); |
| 759 |
extern int _Py_CheckSlotResult( |
| 760 |
PyObject *obj, |
| 761 |
const char *slot_name, |
| 762 |
int success); |
| 763 |
|
| 764 |
// Test if a type supports weak references |
| 765 |
static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) { |
| 766 |
return (type->tp_weaklistoffset != 0); |
| 767 |
} |
| 768 |
|
| 769 |
extern PyObject* _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems); |
| 770 |
extern PyObject *_PyType_NewManagedObject(PyTypeObject *type); |
| 771 |
|
| 772 |
extern PyTypeObject* _PyType_CalculateMetaclass(PyTypeObject *, PyObject *); |
| 773 |
extern PyObject* _PyType_GetDocFromInternalDoc(const char *, const char *); |
| 774 |
extern PyObject* _PyType_GetTextSignatureFromInternalDoc(const char *, const char *, int); |
| 775 |
extern int _PyObject_SetAttributeErrorContext(PyObject *v, PyObject* name); |
| 776 |
|
| 777 |
void _PyObject_InitInlineValues(PyObject *obj, PyTypeObject *tp); |
| 778 |
extern int _PyObject_StoreInstanceAttribute(PyObject *obj, |
| 779 |
PyObject *name, PyObject *value); |
| 780 |
extern bool _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name, |
| 781 |
PyObject **attr); |
| 782 |
|
| 783 |
#ifdef Py_GIL_DISABLED |
| 784 |
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-1) |
| 785 |
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-2) |
| 786 |
#else |
| 787 |
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-3) |
| 788 |
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-4) |
| 789 |
#endif |
| 790 |
|
| 791 |
typedef union { |
| 792 |
PyDictObject *dict; |
| 793 |
} PyManagedDictPointer; |
| 794 |
|
| 795 |
static inline PyManagedDictPointer * |
| 796 |
_PyObject_ManagedDictPointer(PyObject *obj) |
| 797 |
{ |
| 798 |
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT); |
| 799 |
return (PyManagedDictPointer *)((char *)obj + MANAGED_DICT_OFFSET); |
| 800 |
} |
| 801 |
|
| 802 |
static inline PyDictObject * |
| 803 |
_PyObject_GetManagedDict(PyObject *obj) |
| 804 |
{ |
| 805 |
PyManagedDictPointer *dorv = _PyObject_ManagedDictPointer(obj); |
| 806 |
return (PyDictObject *)FT_ATOMIC_LOAD_PTR_ACQUIRE(dorv->dict); |
| 807 |
} |
| 808 |
|
| 809 |
static inline PyDictValues * |
| 810 |
_PyObject_InlineValues(PyObject *obj) |
| 811 |
{ |
| 812 |
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES); |
| 813 |
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT); |
| 814 |
assert(Py_TYPE(obj)->tp_basicsize == sizeof(PyObject)); |
| 815 |
return (PyDictValues *)((char *)obj + sizeof(PyObject)); |
| 816 |
} |
| 817 |
|
| 818 |
extern PyObject ** _PyObject_ComputedDictPointer(PyObject *); |
| 819 |
extern int _PyObject_IsInstanceDictEmpty(PyObject *); |
| 820 |
|
| 821 |
// Export for 'math' shared extension |
| 822 |
PyAPI_FUNC(PyObject*) _PyObject_LookupSpecial(PyObject *, PyObject *); |
| 823 |
|
| 824 |
extern int _PyObject_IsAbstract(PyObject *); |
| 825 |
|
| 826 |
PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method); |
| 827 |
extern PyObject* _PyObject_NextNotImplemented(PyObject *); |
| 828 |
|
| 829 |
// Pickle support. |
| 830 |
// Export for '_datetime' shared extension |
| 831 |
PyAPI_FUNC(PyObject*) _PyObject_GetState(PyObject *); |
| 832 |
|
| 833 |
/* C function call trampolines to mitigate bad function pointer casts. |
| 834 |
* |
| 835 |
* Typical native ABIs ignore additional arguments or fill in missing |
| 836 |
* values with 0/NULL in function pointer cast. Compilers do not show |
| 837 |
* warnings when a function pointer is explicitly casted to an |
| 838 |
* incompatible type. |
| 839 |
* |
| 840 |
* Bad fpcasts are an issue in WebAssembly. WASM's indirect_call has strict |
| 841 |
* function signature checks. Argument count, types, and return type must |
| 842 |
* match. |
| 843 |
* |
| 844 |
* Third party code unintentionally rely on problematic fpcasts. The call |
| 845 |
* trampoline mitigates common occurrences of bad fpcasts on Emscripten. |
| 846 |
*/ |
| 847 |
#if !(defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)) |
| 848 |
#define _PyCFunction_TrampolineCall(meth, self, args) \ |
| 849 |
(meth)((self), (args)) |
| 850 |
#define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \ |
| 851 |
(meth)((self), (args), (kw)) |
| 852 |
#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE |
| 853 |
|
| 854 |
// Export these 2 symbols for '_pickle' shared extension |
| 855 |
PyAPI_DATA(PyTypeObject) _PyNone_Type; |
| 856 |
PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type; |
| 857 |
|
| 858 |
// Maps Py_LT to Py_GT, ..., Py_GE to Py_LE. |
| 859 |
// Export for the stable ABI. |
| 860 |
PyAPI_DATA(int) _Py_SwappedOp[]; |
| 861 |
|
| 862 |
extern void _Py_GetConstant_Init(void); |
| 863 |
|
| 864 |
#ifdef __cplusplus |
| 865 |
} |
| 866 |
#endif |
| 867 |
#endif /* !Py_INTERNAL_OBJECT_H */ |