blob: f475ed001d02f80d047fc22f466c270e9a161cf4 [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
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000104
Victor Stinner0507bf52013-07-07 02:05:46 +0200105/* Macros */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106#define PyObject_MALLOC PyObject_Malloc
107#define PyObject_REALLOC PyObject_Realloc
108#define PyObject_FREE PyObject_Free
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109#define PyObject_Del PyObject_Free
Victor Stinner0507bf52013-07-07 02:05:46 +0200110#define PyObject_DEL PyObject_Free
111
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000112
Guido van Rossumb18618d2000-05-03 23:44:39 +0000113/*
114 * Generic object allocator interface
115 * ==================================
116 */
117
118/* Functions */
Mark Hammond91a681d2002-08-12 07:21:58 +0000119PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
120PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121 PyTypeObject *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +0000122PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124
Guido van Rossumb18618d2000-05-03 23:44:39 +0000125#define PyObject_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 ( (type *) _PyObject_New(typeobj) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127#define PyObject_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 ( (type *) _PyObject_NewVar((typeobj), (n)) )
Guido van Rossuma3309961993-07-28 09:05:47 +0000129
Victor Stinnerb4435e22018-10-26 14:35:00 +0200130/* Inline functions trading binary compatibility for speed:
131 PyObject_INIT() is the fast version of PyObject_Init(), and
132 PyObject_INIT_VAR() is the fast version of PyObject_InitVar.
133 See also pymem.h.
134
135 These inline functions expect non-NULL object pointers. */
Victor Stinner542497a2018-10-29 14:49:24 +0100136static inline PyObject*
Victor Stinnerb509d522018-11-23 14:27:38 +0100137_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
Victor Stinnerb4435e22018-10-26 14:35:00 +0200138{
139 assert(op != NULL);
140 Py_TYPE(op) = typeobj;
141 _Py_NewReference(op);
142 return op;
143}
144
Victor Stinnerb509d522018-11-23 14:27:38 +0100145#define PyObject_INIT(op, typeobj) \
146 _PyObject_INIT(_PyObject_CAST(op), (typeobj))
147
Victor Stinner542497a2018-10-29 14:49:24 +0100148static inline PyVarObject*
Victor Stinnerb509d522018-11-23 14:27:38 +0100149_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
Victor Stinnerb4435e22018-10-26 14:35:00 +0200150{
151 assert(op != NULL);
152 Py_SIZE(op) = size;
153 PyObject_INIT((PyObject *)op, typeobj);
154 return op;
155}
Guido van Rossum5a849141996-07-21 02:23:54 +0000156
Victor Stinnerb509d522018-11-23 14:27:38 +0100157#define PyObject_INIT_VAR(op, typeobj, size) \
158 _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
159
Guido van Rossumb18618d2000-05-03 23:44:39 +0000160#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
Tim Peters6d483d32001-10-06 21:27:34 +0000161
Tim Petersf2a67da2001-10-07 03:54:51 +0000162/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
163 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
164 value is rounded up to the closest multiple of sizeof(void *), in order to
165 ensure that pointer fields at the end of the object are correctly aligned
166 for the platform (this is of special importance for subclasses of, e.g.,
Serhiy Storchaka95949422013-08-27 19:40:23 +0300167 str or int, so that pointers can be stored after the embedded data).
Tim Peters6d483d32001-10-06 21:27:34 +0000168
Tim Petersf2a67da2001-10-07 03:54:51 +0000169 Note that there's no memory wastage in doing this, as malloc has to
170 return (at worst) pointer-aligned memory anyway.
Tim Peters6d483d32001-10-06 21:27:34 +0000171*/
Tim Petersf2a67da2001-10-07 03:54:51 +0000172#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
173# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
174#endif
175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176#define _PyObject_VAR_SIZE(typeobj, nitems) \
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +0200177 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
178 (nitems)*(typeobj)->tp_itemsize, \
179 SIZEOF_VOID_P)
Guido van Rossum5a849141996-07-21 02:23:54 +0000180
Guido van Rossumb18618d2000-05-03 23:44:39 +0000181#define PyObject_NEW(type, typeobj) \
182( (type *) PyObject_Init( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
Tim Peters6d483d32001-10-06 21:27:34 +0000184
Tim Petersf2a67da2001-10-07 03:54:51 +0000185#define PyObject_NEW_VAR(type, typeobj, n) \
186( (type *) PyObject_InitVar( \
187 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
188 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000189
190/* This example code implements an object constructor with a custom
191 allocator, where PyObject_New is inlined, and shows the important
192 distinction between two steps (at least):
193 1) the actual allocation of the object storage;
194 2) the initialization of the Python specific fields
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 in this storage with PyObject_{Init, InitVar}.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000196
197 PyObject *
198 YourObject_New(...)
199 {
200 PyObject *op;
201
202 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
203 if (op == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000205
Tim Peters8b078f92002-04-28 04:11:46 +0000206 PyObject_Init(op, &YourTypeStruct);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000207
208 op->ob_field = value;
209 ...
210 return op;
211 }
212
213 Note that in C++, the use of the new operator usually implies that
214 the 1st step is performed automatically for you, so in a C++ class
Tim Peters8b078f92002-04-28 04:11:46 +0000215 constructor you would start directly with PyObject_Init/InitVar
216*/
Guido van Rossum5a849141996-07-21 02:23:54 +0000217
Victor Stinner0507bf52013-07-07 02:05:46 +0200218
219
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000220/*
221 * Garbage Collection Support
222 * ==========================
223 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000224
Łukasz Langafef7e942016-09-09 21:47:46 -0700225/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
Neal Norwitz7b216c52006-03-04 20:01:53 +0000226PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
Guido van Rossume13ddc92003-04-17 17:29:22 +0000227
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000228/* Test if a type has a GC head */
229#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000230
Martin v. Löwis41290682006-02-16 14:56:14 +0000231PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000232#define PyObject_GC_Resize(type, op, n) \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100233 ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000234
Victor Stinner1a6be912018-11-13 12:52:18 +0100235
Victor Stinner1a6be912018-11-13 12:52:18 +0100236
Mark Hammond91a681d2002-08-12 07:21:58 +0000237PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
Victor Stinner1a6be912018-11-13 12:52:18 +0100239
240/* Tell the GC to track this object.
241 *
242 * See also private _PyObject_GC_TRACK() macro. */
Mark Hammond91a681d2002-08-12 07:21:58 +0000243PyAPI_FUNC(void) PyObject_GC_Track(void *);
Victor Stinner1a6be912018-11-13 12:52:18 +0100244
245/* Tell the GC to stop tracking this object.
246 *
247 * See also private _PyObject_GC_UNTRACK() macro. */
Mark Hammond91a681d2002-08-12 07:21:58 +0000248PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
Victor Stinner1a6be912018-11-13 12:52:18 +0100249
Mark Hammond91a681d2002-08-12 07:21:58 +0000250PyAPI_FUNC(void) PyObject_GC_Del(void *);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000251
252#define PyObject_GC_New(type, typeobj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 ( (type *) _PyObject_GC_New(typeobj) )
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000254#define PyObject_GC_NewVar(type, typeobj, n) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
Neil Schemenauer3e7b8932002-04-12 02:38:45 +0000256
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000257
Tim Peterseda29302004-07-15 04:05:59 +0000258/* Utility macro to help write tp_traverse functions.
259 * To use this macro, the tp_traverse function must name its arguments
260 * "visit" and "arg". This is intended to keep tp_traverse functions
261 * looking as much alike as possible.
262 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263#define Py_VISIT(op) \
264 do { \
265 if (op) { \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100266 int vret = visit(_PyObject_CAST(op), arg); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (vret) \
268 return vret; \
269 } \
270 } while (0)
Jim Fultonaa6389e2004-07-14 19:08:17 +0000271
Christian Tismerea62ce72018-06-09 20:32:25 +0200272#ifndef Py_LIMITED_API
Victor Stinnere4211062018-11-23 17:00:00 +0100273# define Py_CPYTHON_OBJIMPL_H
274# include "cpython/objimpl.h"
275# undef Py_CPYTHON_OBJIMPL_H
Christian Tismerea62ce72018-06-09 20:32:25 +0200276#endif
Fred Drake41deb1e2001-02-01 05:27:45 +0000277
Guido van Rossuma3309961993-07-28 09:05:47 +0000278#ifdef __cplusplus
279}
280#endif
281#endif /* !Py_OBJIMPL_H */