@@ -30,6 +30,55 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
3030extern void _PyType_InitCache (PyInterpreterState * interp );
3131
3232
33+ /* Immortal Objects
34+ *
35+ * An "immortal" object is one for which Py_DECREF() will never try
36+ * to deallocate it.
37+ *
38+ * At the moment this API is strictly internal. However, if it proves
39+ * helpful for extension authors we may move it to the public API. */
40+
41+ #define _Py_IMMORTAL_OBJECTS 1
42+
43+ /* The implementation-independent API is only the following functions: */
44+ PyAPI_FUNC (int ) _PyObject_IsImmortal (PyObject * );
45+ PyAPI_FUNC (void ) _PyObject_SetImmortal (PyObject * );
46+
47+ /* In the actual implementation we set the refcount to some positive
48+ * value that we would never expect to be reachable through use of
49+ * Py_INCREF() in a program.
50+ *
51+ * The only parts that should be used directly are the two
52+ * _Py*Object_HEAD_IMMORTAL_INIT() macros.
53+ */
54+
55+ /* _PyObject_IMMORTAL_BIT is the bit in the refcount value (Py_ssize_t)
56+ * that we use to mark an object as immortal. It shouldn't ever be
57+ * part of the public API.
58+ *
59+ * The GC bit-shifts refcounts left by two, and after that shift we still
60+ * need this to be >> 0, so leave three high zero bits (the sign bit and
61+ * room for a shift of two.) */
62+ #define _PyObject_IMMORTAL_BIT (1LL << (8 * sizeof(Py_ssize_t) - 4))
63+
64+ /* _PyObject_IMMORTAL_INIT_REFCNT is the initial value we use for
65+ * immortal objects. It shouldn't ever be part of the public API.
66+ *
67+ * We leave plenty of room to preserve _PyObject_IMMORTAL_BIT. */
68+ #define _PyObject_IMMORTAL_INIT_REFCNT \
69+ (_PyObject_IMMORTAL_BIT + (_PyObject_IMMORTAL_BIT / 2))
70+
71+ /* These macros are drop-in replacements for the corresponding
72+ * Py*Object_HEAD_INIT() macros. They will probably become
73+ * part of the public API. */
74+ #define _PyObject_HEAD_IMMORTAL_INIT (type ) \
75+ { _PyObject_EXTRA_INIT _PyObject_IMMORTAL_INIT_REFCNT, type },
76+ #define _PyVarObject_HEAD_IMMORTAL_INIT (type , size ) \
77+ { PyObject_HEAD_IMMORTAL_INIT(type) size },
78+
79+ /* end Immortal Objects */
80+
81+
3382/* Inline functions trading binary compatibility for speed:
3483 _PyObject_Init() is the fast version of PyObject_Init(), and
3584 _PyObject_InitVar() is the fast version of PyObject_InitVar().
0 commit comments