Debian Codegraph @

pypy/module/cpyext/include/objimpl.h

raw symbols SCIP
1 /* The PyObject_ memory family: high-level object memory interfaces.
2 See pymem.h for the low-level PyMem_ family.
3 */
4
5 #ifndef Py_OBJIMPL_H
6 #define Py_OBJIMPL_H
7
8 #include "pymem.h"
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 /* 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
26 /*
27 Functions and macros for modules that implement new object types.
28
29 - 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.
36
37 - 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.
40
41 - PyObject_Free(op) releases the memory allocated for an object. It does not
42 run a destructor -- it only frees the memory. PyObject_Free is identical.
43
44 - 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.
48
49 Note that objects created with PyObject_{New, NewVar} are allocated using the
50 specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
51 enabled. In addition, a special debugging allocator is used if Py_DEBUG
52 macro is also defined.
53
54 In case a specific form of memory management is needed (for example, if you
55 must use the platform malloc heap(s), or shared memory, or C++ local storage or
56 operator new), you must first allocate the object with your custom allocator,
57 then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
58 specific fields: reference count, type pointer, possibly others. You should
59 be aware that Python has no control over these objects because they don't
60 cooperate with the Python memory manager. Such objects may not be eligible
61 for automatic garbage collection and you have to make sure that they are
62 released accordingly whenever their destructor gets called (cf. the specific
63 form of memory management you're using).
64
65 Unless you have specific memory management requirements, use
66 PyObject_{New, NewVar, Del}.
67 */
68
69 /*
70 * Raw object memory interface
71 * ===========================
72 */
73
74 /* 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,
78 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 */
97
98 #ifndef PYPY_VERSION
99 PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
100 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
101 PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
102 #endif
103 PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
104 #endif
105 PyAPI_FUNC(void) PyObject_Free(void *ptr);
106
107
108 // Deprecated aliases only kept for backward compatibility.
109 // PyObject_Del and PyObject_DEL are defined with no parameter to be able to
110 // use them as function pointers (ex: tp_free = PyObject_Del).
111 #define PyObject_MALLOC PyObject_Malloc
112 #define PyObject_REALLOC PyObject_Realloc
113 #define PyObject_FREE PyObject_Free
114 #define PyObject_Del PyObject_Free
115 #define PyObject_DEL PyObject_Free
116
117
118 /*
119 * Generic object allocator interface
120 * ==================================
121 */
122
123 /* Functions */
124 PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
125 PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
126 PyTypeObject *, Py_ssize_t);
127
128 #define PyObject_INIT(op, typeobj) \
129 PyObject_Init(_PyObject_CAST(op), (typeobj))
130 #define PyObject_INIT_VAR(op, typeobj, size) \
131 PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
132
133
134 PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
135 PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
136
137 #define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
138
139 // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
140 // PyObject_MALLOC() with _PyObject_SIZE().
141 #define PyObject_NEW(type, typeobj) PyObject_New(type, typeobj)
142
143 #define PyObject_NewVar(type, typeobj, n) \
144 ( (type *) _PyObject_NewVar((typeobj), (n)) )
145
146 // Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
147 // directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
148 #define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)
149
150
151 /*
152 * Garbage Collection Support
153 * ==========================
154 */
155
156 /* C equivalent of gc.collect(). */
157 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
158 /* C API for controlling the state of the garbage collector */
159 PyAPI_FUNC(int) PyGC_Enable(void);
160 PyAPI_FUNC(int) PyGC_Disable(void);
161 PyAPI_FUNC(int) PyGC_IsEnabled(void);
162
163 /* Test if a type has a GC head */
164 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
165
166 PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
167 #define PyObject_GC_Resize(type, op, n) \
168 ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
169
170
171
172 PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
173 PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
174
175 /* Tell the GC to track this object.
176 *
177 * See also private _PyObject_GC_TRACK() macro. */
178 #ifndef PYPY_VERSION
179 PyAPI_FUNC(void) PyObject_GC_Track(void *);
180
181 /* Tell the GC to stop tracking this object.
182 *
183 * See also private _PyObject_GC_UNTRACK() macro. */
184 PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
185 #else
186 #define PyObject_GC_Track(o) do { } while(0)
187 #define _PyObject_GC_TRACK(o) do { } while(0)
188 #define _PyObject_GC_UnTrack(o) do { } while(0)
189 #define PyObject_GC_UnTrack(o) do { } while(0)
190 #endif
191
192 PyAPI_FUNC(void) PyObject_GC_Del(void *);
193
194 #define PyObject_GC_New(type, typeobj) \
195 _Py_CAST(type*, _PyObject_GC_New(typeobj))
196 #define PyObject_GC_NewVar(type, typeobj, n) \
197 _Py_CAST(type*, _PyObject_GC_NewVar((typeobj), (n)))
198
199 PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
200 PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
201
202 /* Utility macro to help write tp_traverse functions.
203 * To use this macro, the tp_traverse function must name its arguments
204 * "visit" and "arg". This is intended to keep tp_traverse functions
205 * looking as much alike as possible.
206 */
207 #define Py_VISIT(op) \
208 do { \
209 if (op) { \
210 int vret = visit(_PyObject_CAST(op), arg); \
211 if (vret) \
212 return vret; \
213 } \
214 } while (0)
215
216 #ifndef Py_LIMITED_API
217 # define Py_CPYTHON_OBJIMPL_H
218
219 /*********************
220 from cpython/objimpl.h
221 *********************/
222
223 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
224
225 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
226 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
227 value is rounded up to the closest multiple of sizeof(void *), in order to
228 ensure that pointer fields at the end of the object are correctly aligned
229 for the platform (this is of special importance for subclasses of, e.g.,
230 str or int, so that pointers can be stored after the embedded data).
231
232 Note that there's no memory wastage in doing this, as malloc has to
233 return (at worst) pointer-aligned memory anyway.
234 */
235 #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
236 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
237 #endif
238
239 #define _PyObject_VAR_SIZE(typeobj, nitems) \
240 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
241 (nitems)*(typeobj)->tp_itemsize, \
242 SIZEOF_VOID_P)
243
244 /* Test if an object implements the garbage collector protocol */
245 PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
246
247 // Kept for backward compatibility with Python 3.8
248 #define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
249
250 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
251
252 /* A dummy PyGC_Head, just to please some tests. Don't use it! */
253 typedef union _gc_head {
254 char dummy;
255 } PyGC_Head;
256
257
258 # undef Py_CPYTHON_OBJIMPL_H
259 #endif
260
261 #ifdef __cplusplus
262 }
263 #endif
264 #endif /* !Py_OBJIMPL_H */