blob: 4eeb8dfe50cd5bf668831f85d9e16cedefacc69d [file] [log] [blame]
Tim Peters8b078f92002-04-28 04:11:46 +00001/* The PyObject_ memory family: high-level object memory interfaces.
2 See pymem.h for the low-level PyMem_ family.
3*/
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
Fred Drake3cf4d2b2000-07-09 00:55:06 +00005#ifndef Py_OBJIMPL_H
6#define Py_OBJIMPL_H
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00007
8#include "pymem.h"
9
Fred Drake3cf4d2b2000-07-09 00:55:06 +000010#ifdef __cplusplus
11extern "C" {
12#endif
13
Tim Peters8b078f92002-04-28 04:11:46 +000014/* BEWARE:
15
16 Each interface exports both functions and macros. Extension modules should
17 use the functions, to ensure binary compatibility across Python versions.
18 Because the Python implementation is free to change internal details, and
19 the macros may (or may not) expose details for speed, if you do use the
20 macros you must recompile your extensions with each Python release.
21
22 Never mix calls to PyObject_ memory functions with calls to the platform
23 malloc/realloc/ calloc/free, or with calls to PyMem_.
24*/
25
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000027Functions and macros for modules that implement new object types.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Tim Peters8b078f92002-04-28 04:11:46 +000029 - PyObject_New(type, typeobj) allocates memory for a new object of the given
30 type, and initializes part of it. 'type' must be the C structure type used
31 to represent the object, and 'typeobj' the address of the corresponding
32 type object. Reference count and type pointer are filled in; the rest of
33 the bytes of the object are *undefined*! The resulting expression type is
34 'type *'. The size of the object is determined by the tp_basicsize field
35 of the type object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Tim Peters8b078f92002-04-28 04:11:46 +000037 - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
38 object with room for n items. In addition to the refcount and type pointer
39 fields, this also fills in the ob_size field.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040
Tim Peters8b078f92002-04-28 04:11:46 +000041 - PyObject_Del(op) releases the memory allocated for an object. It does not
42 run a destructor -- it only frees the memory. PyObject_Free is identical.
Guido van Rossumb18618d2000-05-03 23:44:39 +000043
Tim Peters8b078f92002-04-28 04:11:46 +000044 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
45 allocate memory. Instead of a 'type' parameter, they take a pointer to a
46 new object (allocated by an arbitrary allocator), and initialize its object
47 header fields.
Guido van Rossumb18618d2000-05-03 23:44:39 +000048
Tim Peters8b078f92002-04-28 04:11:46 +000049Note that objects created with PyObject_{New, NewVar} are allocated using the
50specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
51enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
52is also #defined.
Guido van Rossumb18618d2000-05-03 23:44:39 +000053
Tim Peters8b078f92002-04-28 04:11:46 +000054In case a specific form of memory management is needed (for example, if you
55must use the platform malloc heap(s), or shared memory, or C++ local storage or
56operator new), you must first allocate the object with your custom allocator,
57then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
58specific fields: reference count, type pointer, possibly others. You should
Alexey517da1e2018-02-07 02:07:30 +030059be aware that Python has no control over these objects because they don't
Tim Peters8b078f92002-04-28 04:11:46 +000060cooperate with the Python memory manager. Such objects may not be eligible
61for automatic garbage collection and you have to make sure that they are
62released accordingly whenever their destructor gets called (cf. the specific
Guido van Rossumb18618d2000-05-03 23:44:39 +000063form of memory management you're using).
64
Tim Peters8b078f92002-04-28 04:11:46 +000065Unless you have specific memory management requirements, use
66PyObject_{New, NewVar, Del}.
67*/
Guido van Rossumb18618d2000-05-03 23:44:39 +000068
Tim Peters6d483d32001-10-06 21:27:34 +000069/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000070 * Raw object memory interface
71 * ===========================
72 */
73
Tim Peterse9e74522002-04-12 05:21:34 +000074/* Functions to call the same malloc/realloc/free as used by Python's
75 object allocator. If WITH_PYMALLOC is enabled, these may differ from
76 the platform malloc/realloc/free. The Python object allocator is
77 designed for fast, cache-conscious allocation of many "small" objects,
Tim Peters8b078f92002-04-28 04:11:46 +000078 and with low hidden memory overhead.
79
80 PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
81
82 PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
83 PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
84 at p.
85
86 Returned pointers must be checked for NULL explicitly; no action is
87 performed on failure other than to return NULL (no warning it printed, no
88 exception is set, etc).
89
90 For allocating objects, use PyObject_{New, NewVar} instead whenever
91 possible. The PyObject_{Malloc, Realloc, Free} family is exposed
92 so that you can exploit Python's small-block allocator for non-object
93 uses. If you must use these routines to allocate object memory, make sure
94 the object gets initialized via PyObject_{Init, InitVar} after obtaining
95 the raw memory.
96*/
Victor Stinner0507bf52013-07-07 02:05:46 +020097PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020098#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinnerdb067af2014-05-02 22:31:14 +020099PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200100#endif
Victor Stinner0507bf52013-07-07 02:05:46 +0200101PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
102PyAPI_FUNC(void) PyObject_Free(void *ptr);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000103
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300104#ifndef Py_LIMITED_API
Antoine Pitrouf9d0b122012-12-09 14:28:26 +0100105/* This function returns the number of allocated memory blocks, regardless of size */
106PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300107#endif /* !Py_LIMITED_API */
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000108
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109/* Macros */
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000110#ifdef WITH_PYMALLOC
David Malcolm49526f42012-06-22 14:55:41 -0400111#ifndef Py_LIMITED_API
Victor Stinner6bf992a2017-12-06 17:26:10 +0100112PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
David Malcolm49526f42012-06-22 14:55:41 -0400113#endif /* #ifndef Py_LIMITED_API */
Victor Stinner0507bf52013-07-07 02:05:46 +0200114#endif
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000115
Victor Stinner0507bf52013-07-07 02:05:46 +0200116/* Macros */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117#define PyObject_MALLOC PyObject_Malloc
118#define PyObject_REALLOC PyObject_Realloc
119#define PyObject_FREE PyObject_Free
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120#define PyObject_Del PyObject_Free
Victor Stinner0507bf52013-07-07 02:05:46 +0200121#define PyObject_DEL PyObject_Free
122
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000123
Guido van Rossumb18618d2000-05-03 23:44:39 +0000124/*
125 * Generic object allocator interface
126 * ==================================
127 */
128
129/* Functions */
Mark Hammond91a681d2002-08-12 07:21:58 +0000130PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
131PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132 PyTypeObject *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +0000133PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136#define PyObject_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 ( (type *) _PyObject_New(typeobj) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000138#define PyObject_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 ( (type *) _PyObject_NewVar((typeobj), (n)) )
Guido van Rossuma3309961993-07-28 09:05:47 +0000140
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +0000141/* Macros trading binary compatibility for speed. See also pymem.h.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 Note that these macros expect non-NULL object pointers.*/
143#define PyObject_INIT(op, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145#define PyObject_INIT_VAR(op, typeobj, size) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000147
Guido van Rossumb18618d2000-05-03 23:44:39 +0000148#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
Tim Peters6d483d32001-10-06 21:27:34 +0000149
Tim Petersf2a67da2001-10-07 03:54:51 +0000150/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
151 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
152 value is rounded up to the closest multiple of sizeof(void *), in order to
153 ensure that pointer fields at the end of the object are correctly aligned
154 for the platform (this is of special importance for subclasses of, e.g.,
Serhiy Storchaka95949422013-08-27 19:40:23 +0300155 str or int, so that pointers can be stored after the embedded data).
Tim Peters6d483d32001-10-06 21:27:34 +0000156
Tim Petersf2a67da2001-10-07 03:54:51 +0000157 Note that there's no memory wastage in doing this, as malloc has to
158 return (at worst) pointer-aligned memory anyway.
Tim Peters6d483d32001-10-06 21:27:34 +0000159*/
Tim Petersf2a67da2001-10-07 03:54:51 +0000160#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
161# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
162#endif
163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164#define _PyObject_VAR_SIZE(typeobj, nitems) \
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +0200165 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
166 (nitems)*(typeobj)->tp_itemsize, \
167 SIZEOF_VOID_P)
Guido van Rossum5a849141996-07-21 02:23:54 +0000168
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169#define PyObject_NEW(type, typeobj) \
170( (type *) PyObject_Init( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
Tim Peters6d483d32001-10-06 21:27:34 +0000172
Tim Petersf2a67da2001-10-07 03:54:51 +0000173#define PyObject_NEW_VAR(type, typeobj, n) \
174( (type *) PyObject_InitVar( \
175 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
176 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000177
178/* This example code implements an object constructor with a custom
179 allocator, where PyObject_New is inlined, and shows the important
180 distinction between two steps (at least):
181 1) the actual allocation of the object storage;
182 2) the initialization of the Python specific fields
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 in this storage with PyObject_{Init, InitVar}.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000184
185 PyObject *
186 YourObject_New(...)
187 {
188 PyObject *op;
189
190 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
191 if (op == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000193
Tim Peters8b078f92002-04-28 04:11:46 +0000194 PyObject_Init(op, &YourTypeStruct);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000195
196 op->ob_field = value;
197 ...
198 return op;
199 }
200
201 Note that in C++, the use of the new operator usually implies that
202 the 1st step is performed automatically for you, so in a C++ class
Tim Peters8b078f92002-04-28 04:11:46 +0000203 constructor you would start directly with PyObject_Init/InitVar
204*/
Guido van Rossum5a849141996-07-21 02:23:54 +0000205
Victor Stinner0507bf52013-07-07 02:05:46 +0200206#ifndef Py_LIMITED_API
207typedef struct {
208 /* user context passed as the first argument to the 2 functions */
209 void *ctx;
210
211 /* allocate an arena of size bytes */
212 void* (*alloc) (void *ctx, size_t size);
213
214 /* free an arena */
215 void (*free) (void *ctx, void *ptr, size_t size);
216} PyObjectArenaAllocator;
217
218/* Get the arena allocator. */
219PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
220
221/* Set the arena allocator. */
222PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
223#endif
224
225
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000226/*
227 * Garbage Collection Support
228 * ==========================
229 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000230
Łukasz Langafef7e942016-09-09 21:47:46 -0700231/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
Neal Norwitz7b216c52006-03-04 20:01:53 +0000232PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
Guido van Rossume13ddc92003-04-17 17:29:22 +0000233
Antoine Pitroufef34e32013-05-19 01:11:58 +0200234#ifndef Py_LIMITED_API
235PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
Łukasz Langafef7e942016-09-09 21:47:46 -0700236PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
Antoine Pitroufef34e32013-05-19 01:11:58 +0200237#endif
238
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000239/* Test if a type has a GC head */
240#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000241
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000242/* Test if an object has a GC head */
Christian Tismerea62ce72018-06-09 20:32:25 +0200243#ifndef Py_LIMITED_API
Christian Heimes90aa7642007-12-19 02:45:37 +0000244#define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
Christian Tismerea62ce72018-06-09 20:32:25 +0200246#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000247
Martin v. Löwis41290682006-02-16 14:56:14 +0000248PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000249#define PyObject_GC_Resize(type, op, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000251
Tim Peters8b078f92002-04-28 04:11:46 +0000252/* GC information is stored BEFORE the object structure. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000253#ifndef Py_LIMITED_API
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900254typedef struct {
255 // Pointer to next object in the list.
256 // 0 means the object is not tracked
257 uintptr_t _gc_next;
258
259 // Pointer to previous object in the list.
260 // Lowest two bits are used for flags documented later.
261 uintptr_t _gc_prev;
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000262} PyGC_Head;
263
Neil Schemenauerb1094f02002-05-04 05:36:06 +0000264extern PyGC_Head *_PyGC_generation0;
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000265
Neil Schemenaueref997232002-03-28 21:06:16 +0000266#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
267
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900268/* Bit flags for _gc_prev */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200269/* Bit 0 is set when tp_finalize is called */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900270#define _PyGC_PREV_MASK_FINALIZED (1)
271/* Bit 1 is set when the object is in generation which is GCed currently. */
272#define _PyGC_PREV_MASK_COLLECTING (2)
273/* The (N-2) most significant bits contain the real address. */
274#define _PyGC_PREV_SHIFT (2)
275#define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200276
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900277// Lowest bit of _gc_next is used for flags only in GC.
278// But it is always 0 for normal code.
279#define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next)
280#define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p))
Antoine Pitrou796564c2013-07-30 19:59:21 +0200281
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900282// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
283#define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
284#define _PyGCHead_SET_PREV(g, p) do { \
285 assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \
286 (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \
287 | ((uintptr_t)(p)); \
Antoine Pitrou796564c2013-07-30 19:59:21 +0200288 } while (0)
289
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900290#define _PyGCHead_FINALIZED(g) (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
291#define _PyGCHead_SET_FINALIZED(g) ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
292
Antoine Pitrou796564c2013-07-30 19:59:21 +0200293#define _PyGC_FINALIZED(o) _PyGCHead_FINALIZED(_Py_AS_GC(o))
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900294#define _PyGC_SET_FINALIZED(o) _PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
Antoine Pitrou796564c2013-07-30 19:59:21 +0200295
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900296/* Tell the GC to track this object.
297 *
298 * NB: While the object is tracked by the collector, it must be safe to call the
299 * ob_traverse method.
300 *
301 * Internal note: _PyGC_generation0->_gc_prev doesn't have any bit flags
302 * because it's not object header. So we don't use _PyGCHead_PREV() and
303 * _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
304 */
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000305#define _PyObject_GC_TRACK(o) do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 PyGC_Head *g = _Py_AS_GC(o); \
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900307 if (g->_gc_next != 0) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_FatalError("GC object already tracked"); \
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900309 } \
310 assert((g->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0); \
311 PyGC_Head *last = (PyGC_Head*)(_PyGC_generation0->_gc_prev); \
312 _PyGCHead_SET_NEXT(last, g); \
313 _PyGCHead_SET_PREV(g, last); \
314 _PyGCHead_SET_NEXT(g, _PyGC_generation0); \
315 _PyGC_generation0->_gc_prev = (uintptr_t)g; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000316 } while (0);
317
Tim Peters6fc13d92002-07-02 18:12:35 +0000318/* Tell the GC to stop tracking this object.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900319 *
320 * Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING must
321 * be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
Tim Peters6fc13d92002-07-02 18:12:35 +0000322 */
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000323#define _PyObject_GC_UNTRACK(o) do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyGC_Head *g = _Py_AS_GC(o); \
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900325 PyGC_Head *prev = _PyGCHead_PREV(g); \
326 PyGC_Head *next = _PyGCHead_NEXT(g); \
327 assert(next != NULL); \
328 _PyGCHead_SET_NEXT(prev, next); \
329 _PyGCHead_SET_PREV(next, prev); \
330 g->_gc_next = 0; \
331 g->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000332 } while (0);
333
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000334/* True if the object is currently tracked by the GC. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900335#define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000337/* True if the object may be tracked by the GC in the future, or already is.
338 This can be useful to implement some optimizations. */
339#define _PyObject_GC_MAY_BE_TRACKED(obj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 (PyObject_IS_GC(obj) && \
341 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000342#endif /* Py_LIMITED_API */
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000343
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300344#ifndef Py_LIMITED_API
Victor Stinnerdb067af2014-05-02 22:31:14 +0200345PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
346PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300347#endif /* !Py_LIMITED_API */
Mark Hammond91a681d2002-08-12 07:21:58 +0000348PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000349PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +0000350PyAPI_FUNC(void) PyObject_GC_Track(void *);
351PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
352PyAPI_FUNC(void) PyObject_GC_Del(void *);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000353
354#define PyObject_GC_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 ( (type *) _PyObject_GC_New(typeobj) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000356#define PyObject_GC_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000358
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000359
Tim Peterseda29302004-07-15 04:05:59 +0000360/* Utility macro to help write tp_traverse functions.
361 * To use this macro, the tp_traverse function must name its arguments
362 * "visit" and "arg". This is intended to keep tp_traverse functions
363 * looking as much alike as possible.
364 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365#define Py_VISIT(op) \
366 do { \
367 if (op) { \
368 int vret = visit((PyObject *)(op), arg); \
369 if (vret) \
370 return vret; \
371 } \
372 } while (0)
Jim Fultonaa6389e2004-07-14 19:08:17 +0000373
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000374
Fred Drake41deb1e2001-02-01 05:27:45 +0000375/* Test if a type supports weak references */
Christian Tismerea62ce72018-06-09 20:32:25 +0200376#ifndef Py_LIMITED_API
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000377#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
Fred Drake41deb1e2001-02-01 05:27:45 +0000378
379#define PyObject_GET_WEAKREFS_LISTPTR(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
Christian Tismerea62ce72018-06-09 20:32:25 +0200381#endif
Fred Drake41deb1e2001-02-01 05:27:45 +0000382
Guido van Rossuma3309961993-07-28 09:05:47 +0000383#ifdef __cplusplus
384}
385#endif
386#endif /* !Py_OBJIMPL_H */