blob: b577be2df3b5fd0a7527fa15176d445c5048e796 [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
59be aware that Python no control over these objects because they don't
60cooperate 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*/
Mark Hammond91a681d2002-08-12 07:21:58 +000097PyAPI_FUNC(void *) PyObject_Malloc(size_t);
98PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
99PyAPI_FUNC(void) PyObject_Free(void *);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100
Antoine Pitrouf9d0b122012-12-09 14:28:26 +0100101/* This function returns the number of allocated memory blocks, regardless of size */
102PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000103
Guido van Rossumb18618d2000-05-03 23:44:39 +0000104/* Macros */
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000105#ifdef WITH_PYMALLOC
David Malcolm49526f42012-06-22 14:55:41 -0400106#ifndef Py_LIMITED_API
107PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
108#endif /* #ifndef Py_LIMITED_API */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109#ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
Mark Hammond91a681d2002-08-12 07:21:58 +0000110PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
111PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
112PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
113PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
114PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +0000115PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
116PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
117PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
118PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p);
119PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes);
120PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes);
121PyAPI_FUNC(void) _PyMem_DebugFree(void *p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122#define PyObject_MALLOC _PyObject_DebugMalloc
123#define PyObject_Malloc _PyObject_DebugMalloc
124#define PyObject_REALLOC _PyObject_DebugRealloc
125#define PyObject_Realloc _PyObject_DebugRealloc
126#define PyObject_FREE _PyObject_DebugFree
127#define PyObject_Free _PyObject_DebugFree
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
130#define PyObject_MALLOC PyObject_Malloc
131#define PyObject_REALLOC PyObject_Realloc
132#define PyObject_FREE PyObject_Free
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000133#endif
134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135#else /* ! WITH_PYMALLOC */
136#define PyObject_MALLOC PyMem_MALLOC
137#define PyObject_REALLOC PyMem_REALLOC
138#define PyObject_FREE PyMem_FREE
Tim Peters8b078f92002-04-28 04:11:46 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140#endif /* WITH_PYMALLOC */
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142#define PyObject_Del PyObject_Free
143#define PyObject_DEL PyObject_FREE
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000144
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145/*
146 * Generic object allocator interface
147 * ==================================
148 */
149
150/* Functions */
Mark Hammond91a681d2002-08-12 07:21:58 +0000151PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
152PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000153 PyTypeObject *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +0000154PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000155PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156
Guido van Rossumb18618d2000-05-03 23:44:39 +0000157#define PyObject_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 ( (type *) _PyObject_New(typeobj) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159#define PyObject_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 ( (type *) _PyObject_NewVar((typeobj), (n)) )
Guido van Rossuma3309961993-07-28 09:05:47 +0000161
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +0000162/* Macros trading binary compatibility for speed. See also pymem.h.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163 Note that these macros expect non-NULL object pointers.*/
164#define PyObject_INIT(op, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166#define PyObject_INIT_VAR(op, typeobj, size) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000168
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
Tim Peters6d483d32001-10-06 21:27:34 +0000170
Tim Petersf2a67da2001-10-07 03:54:51 +0000171/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
172 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
173 value is rounded up to the closest multiple of sizeof(void *), in order to
174 ensure that pointer fields at the end of the object are correctly aligned
175 for the platform (this is of special importance for subclasses of, e.g.,
176 str or long, so that pointers can be stored after the embedded data).
Tim Peters6d483d32001-10-06 21:27:34 +0000177
Tim Petersf2a67da2001-10-07 03:54:51 +0000178 Note that there's no memory wastage in doing this, as malloc has to
179 return (at worst) pointer-aligned memory anyway.
Tim Peters6d483d32001-10-06 21:27:34 +0000180*/
Tim Petersf2a67da2001-10-07 03:54:51 +0000181#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
182# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
183#endif
184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185#define _PyObject_VAR_SIZE(typeobj, nitems) \
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +0200186 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
187 (nitems)*(typeobj)->tp_itemsize, \
188 SIZEOF_VOID_P)
Guido van Rossum5a849141996-07-21 02:23:54 +0000189
Guido van Rossumb18618d2000-05-03 23:44:39 +0000190#define PyObject_NEW(type, typeobj) \
191( (type *) PyObject_Init( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
Tim Peters6d483d32001-10-06 21:27:34 +0000193
Tim Petersf2a67da2001-10-07 03:54:51 +0000194#define PyObject_NEW_VAR(type, typeobj, n) \
195( (type *) PyObject_InitVar( \
196 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
197 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000198
199/* This example code implements an object constructor with a custom
200 allocator, where PyObject_New is inlined, and shows the important
201 distinction between two steps (at least):
202 1) the actual allocation of the object storage;
203 2) the initialization of the Python specific fields
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 in this storage with PyObject_{Init, InitVar}.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000205
206 PyObject *
207 YourObject_New(...)
208 {
209 PyObject *op;
210
211 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
212 if (op == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000214
Tim Peters8b078f92002-04-28 04:11:46 +0000215 PyObject_Init(op, &YourTypeStruct);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000216
217 op->ob_field = value;
218 ...
219 return op;
220 }
221
222 Note that in C++, the use of the new operator usually implies that
223 the 1st step is performed automatically for you, so in a C++ class
Tim Peters8b078f92002-04-28 04:11:46 +0000224 constructor you would start directly with PyObject_Init/InitVar
225*/
Guido van Rossum5a849141996-07-21 02:23:54 +0000226
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000227/*
228 * Garbage Collection Support
229 * ==========================
230 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000231
Guido van Rossume13ddc92003-04-17 17:29:22 +0000232/* C equivalent of gc.collect(). */
Neal Norwitz7b216c52006-03-04 20:01:53 +0000233PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
Guido van Rossume13ddc92003-04-17 17:29:22 +0000234
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000235/* Test if a type has a GC head */
236#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000237
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000238/* Test if an object has a GC head */
Christian Heimes90aa7642007-12-19 02:45:37 +0000239#define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000241
Martin v. Löwis41290682006-02-16 14:56:14 +0000242PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000243#define PyObject_GC_Resize(type, op, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000245
Tim Peters8b078f92002-04-28 04:11:46 +0000246/* GC information is stored BEFORE the object structure. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000247#ifndef Py_LIMITED_API
Tim Peters9e4ca102001-10-11 18:31:31 +0000248typedef union _gc_head {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 struct {
250 union _gc_head *gc_next;
251 union _gc_head *gc_prev;
252 Py_ssize_t gc_refs;
253 } gc;
Gregory P. Smithe348c8d2012-12-10 18:05:05 -0800254 double dummy; /* force worst-case alignment */
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000255} PyGC_Head;
256
Neil Schemenauerb1094f02002-05-04 05:36:06 +0000257extern PyGC_Head *_PyGC_generation0;
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000258
Neil Schemenaueref997232002-03-28 21:06:16 +0000259#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261#define _PyGC_REFS_UNTRACKED (-2)
262#define _PyGC_REFS_REACHABLE (-3)
263#define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
Tim Petersea405632002-07-02 00:52:30 +0000264
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000265/* Tell the GC to track this object. NB: While the object is tracked the
266 * collector it must be safe to call the ob_traverse method. */
267#define _PyObject_GC_TRACK(o) do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PyGC_Head *g = _Py_AS_GC(o); \
269 if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
270 Py_FatalError("GC object already tracked"); \
271 g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
272 g->gc.gc_next = _PyGC_generation0; \
273 g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
274 g->gc.gc_prev->gc.gc_next = g; \
275 _PyGC_generation0->gc.gc_prev = g; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000276 } while (0);
277
Tim Peters6fc13d92002-07-02 18:12:35 +0000278/* Tell the GC to stop tracking this object.
279 * gc_next doesn't need to be set to NULL, but doing so is a good
280 * way to provoke memory errors if calling code is confused.
281 */
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000282#define _PyObject_GC_UNTRACK(o) do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 PyGC_Head *g = _Py_AS_GC(o); \
284 assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
285 g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
286 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
287 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
288 g->gc.gc_next = NULL; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000289 } while (0);
290
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000291/* True if the object is currently tracked by the GC. */
292#define _PyObject_GC_IS_TRACKED(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED)
294
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000295/* True if the object may be tracked by the GC in the future, or already is.
296 This can be useful to implement some optimizations. */
297#define _PyObject_GC_MAY_BE_TRACKED(obj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 (PyObject_IS_GC(obj) && \
299 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000300#endif /* Py_LIMITED_API */
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000301
Mark Hammond91a681d2002-08-12 07:21:58 +0000302PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
303PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000304PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +0000305PyAPI_FUNC(void) PyObject_GC_Track(void *);
306PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
307PyAPI_FUNC(void) PyObject_GC_Del(void *);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000308
309#define PyObject_GC_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 ( (type *) _PyObject_GC_New(typeobj) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000311#define PyObject_GC_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000313
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000314
Tim Peterseda29302004-07-15 04:05:59 +0000315/* Utility macro to help write tp_traverse functions.
316 * To use this macro, the tp_traverse function must name its arguments
317 * "visit" and "arg". This is intended to keep tp_traverse functions
318 * looking as much alike as possible.
319 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320#define Py_VISIT(op) \
321 do { \
322 if (op) { \
323 int vret = visit((PyObject *)(op), arg); \
324 if (vret) \
325 return vret; \
326 } \
327 } while (0)
Jim Fultonaa6389e2004-07-14 19:08:17 +0000328
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000329
Fred Drake41deb1e2001-02-01 05:27:45 +0000330/* Test if a type supports weak references */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000331#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
Fred Drake41deb1e2001-02-01 05:27:45 +0000332
333#define PyObject_GET_WEAKREFS_LISTPTR(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
Fred Drake41deb1e2001-02-01 05:27:45 +0000335
Guido van Rossuma3309961993-07-28 09:05:47 +0000336#ifdef __cplusplus
337}
338#endif
339#endif /* !Py_OBJIMPL_H */