Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

objimpl.h 14 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  1. /* The PyObject_ memory family: high-level object memory interfaces.
  2. See pymem.h for the low-level PyMem_ family.
  3. */
  4. #ifndef Py_OBJIMPL_H
  5. #define Py_OBJIMPL_H
  6. #include "pymem.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* BEWARE:
  11. Each interface exports both functions and macros. Extension modules should
  12. use the functions, to ensure binary compatibility across Python versions.
  13. Because the Python implementation is free to change internal details, and
  14. the macros may (or may not) expose details for speed, if you do use the
  15. macros you must recompile your extensions with each Python release.
  16. Never mix calls to PyObject_ memory functions with calls to the platform
  17. malloc/realloc/ calloc/free, or with calls to PyMem_.
  18. */
  19. /*
  20. Functions and macros for modules that implement new object types.
  21. - PyObject_New(type, typeobj) allocates memory for a new object of the given
  22. type, and initializes part of it. 'type' must be the C structure type used
  23. to represent the object, and 'typeobj' the address of the corresponding
  24. type object. Reference count and type pointer are filled in; the rest of
  25. the bytes of the object are *undefined*! The resulting expression type is
  26. 'type *'. The size of the object is determined by the tp_basicsize field
  27. of the type object.
  28. - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
  29. object with room for n items. In addition to the refcount and type pointer
  30. fields, this also fills in the ob_size field.
  31. - PyObject_Del(op) releases the memory allocated for an object. It does not
  32. run a destructor -- it only frees the memory. PyObject_Free is identical.
  33. - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
  34. allocate memory. Instead of a 'type' parameter, they take a pointer to a
  35. new object (allocated by an arbitrary allocator), and initialize its object
  36. header fields.
  37. Note that objects created with PyObject_{New, NewVar} are allocated using the
  38. specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
  39. enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
  40. is also #defined.
  41. In case a specific form of memory management is needed (for example, if you
  42. must use the platform malloc heap(s), or shared memory, or C++ local storage or
  43. operator new), you must first allocate the object with your custom allocator,
  44. then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
  45. specific fields: reference count, type pointer, possibly others. You should
  46. be aware that Python has no control over these objects because they don't
  47. cooperate with the Python memory manager. Such objects may not be eligible
  48. for automatic garbage collection and you have to make sure that they are
  49. released accordingly whenever their destructor gets called (cf. the specific
  50. form of memory management you're using).
  51. Unless you have specific memory management requirements, use
  52. PyObject_{New, NewVar, Del}.
  53. */
  54. /*
  55. * Raw object memory interface
  56. * ===========================
  57. */
  58. /* Functions to call the same malloc/realloc/free as used by Python's
  59. object allocator. If WITH_PYMALLOC is enabled, these may differ from
  60. the platform malloc/realloc/free. The Python object allocator is
  61. designed for fast, cache-conscious allocation of many "small" objects,
  62. and with low hidden memory overhead.
  63. PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
  64. PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
  65. PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
  66. at p.
  67. Returned pointers must be checked for NULL explicitly; no action is
  68. performed on failure other than to return NULL (no warning it printed, no
  69. exception is set, etc).
  70. For allocating objects, use PyObject_{New, NewVar} instead whenever
  71. possible. The PyObject_{Malloc, Realloc, Free} family is exposed
  72. so that you can exploit Python's small-block allocator for non-object
  73. uses. If you must use these routines to allocate object memory, make sure
  74. the object gets initialized via PyObject_{Init, InitVar} after obtaining
  75. the raw memory.
  76. */
  77. PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
  78. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  79. PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
  80. #endif
  81. PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
  82. PyAPI_FUNC(void) PyObject_Free(void *ptr);
  83. #ifndef Py_LIMITED_API
  84. /* This function returns the number of allocated memory blocks, regardless of size */
  85. PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
  86. #endif /* !Py_LIMITED_API */
  87. /* Macros */
  88. #ifdef WITH_PYMALLOC
  89. #ifndef Py_LIMITED_API
  90. PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
  91. #endif /* #ifndef Py_LIMITED_API */
  92. #endif
  93. /* Macros */
  94. #define PyObject_MALLOC PyObject_Malloc
  95. #define PyObject_REALLOC PyObject_Realloc
  96. #define PyObject_FREE PyObject_Free
  97. #define PyObject_Del PyObject_Free
  98. #define PyObject_DEL PyObject_Free
  99. /*
  100. * Generic object allocator interface
  101. * ==================================
  102. */
  103. /* Functions */
  104. PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  105. PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
  106. PyTypeObject *, Py_ssize_t);
  107. PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
  108. PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
  109. #define PyObject_New(type, typeobj) \
  110. ( (type *) _PyObject_New(typeobj) )
  111. #define PyObject_NewVar(type, typeobj, n) \
  112. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  113. /* Macros trading binary compatibility for speed. See also pymem.h.
  114. Note that these macros expect non-NULL object pointers.*/
  115. #define PyObject_INIT(op, typeobj) \
  116. ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  117. #define PyObject_INIT_VAR(op, typeobj, size) \
  118. ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
  119. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  120. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  121. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  122. value is rounded up to the closest multiple of sizeof(void *), in order to
  123. ensure that pointer fields at the end of the object are correctly aligned
  124. for the platform (this is of special importance for subclasses of, e.g.,
  125. str or int, so that pointers can be stored after the embedded data).
  126. Note that there's no memory wastage in doing this, as malloc has to
  127. return (at worst) pointer-aligned memory anyway.
  128. */
  129. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  130. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  131. #endif
  132. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  133. _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
  134. (nitems)*(typeobj)->tp_itemsize, \
  135. SIZEOF_VOID_P)
  136. #define PyObject_NEW(type, typeobj) \
  137. ( (type *) PyObject_Init( \
  138. (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  139. #define PyObject_NEW_VAR(type, typeobj, n) \
  140. ( (type *) PyObject_InitVar( \
  141. (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
  142. (typeobj), (n)) )
  143. /* This example code implements an object constructor with a custom
  144. allocator, where PyObject_New is inlined, and shows the important
  145. distinction between two steps (at least):
  146. 1) the actual allocation of the object storage;
  147. 2) the initialization of the Python specific fields
  148. in this storage with PyObject_{Init, InitVar}.
  149. PyObject *
  150. YourObject_New(...)
  151. {
  152. PyObject *op;
  153. op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  154. if (op == NULL)
  155. return PyErr_NoMemory();
  156. PyObject_Init(op, &YourTypeStruct);
  157. op->ob_field = value;
  158. ...
  159. return op;
  160. }
  161. Note that in C++, the use of the new operator usually implies that
  162. the 1st step is performed automatically for you, so in a C++ class
  163. constructor you would start directly with PyObject_Init/InitVar
  164. */
  165. #ifndef Py_LIMITED_API
  166. typedef struct {
  167. /* user context passed as the first argument to the 2 functions */
  168. void *ctx;
  169. /* allocate an arena of size bytes */
  170. void* (*alloc) (void *ctx, size_t size);
  171. /* free an arena */
  172. void (*free) (void *ctx, void *ptr, size_t size);
  173. } PyObjectArenaAllocator;
  174. /* Get the arena allocator. */
  175. PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
  176. /* Set the arena allocator. */
  177. PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  178. #endif
  179. /*
  180. * Garbage Collection Support
  181. * ==========================
  182. */
  183. /* C equivalent of gc.collect() which ignores the state of gc.enabled. */
  184. PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
  185. #ifndef Py_LIMITED_API
  186. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
  187. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
  188. #endif
  189. /* Test if a type has a GC head */
  190. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  191. /* Test if an object has a GC head */
  192. #define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
  193. (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
  194. PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
  195. #define PyObject_GC_Resize(type, op, n) \
  196. ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
  197. /* GC information is stored BEFORE the object structure. */
  198. #ifndef Py_LIMITED_API
  199. typedef union _gc_head {
  200. struct {
  201. union _gc_head *gc_next;
  202. union _gc_head *gc_prev;
  203. Py_ssize_t gc_refs;
  204. } gc;
  205. double dummy; /* force worst-case alignment */
  206. } PyGC_Head;
  207. extern PyGC_Head *_PyGC_generation0;
  208. #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
  209. /* Bit 0 is set when tp_finalize is called */
  210. #define _PyGC_REFS_MASK_FINALIZED (1 << 0)
  211. /* The (N-1) most significant bits contain the gc state / refcount */
  212. #define _PyGC_REFS_SHIFT (1)
  213. #define _PyGC_REFS_MASK (((size_t) -1) << _PyGC_REFS_SHIFT)
  214. #define _PyGCHead_REFS(g) ((g)->gc.gc_refs >> _PyGC_REFS_SHIFT)
  215. #define _PyGCHead_SET_REFS(g, v) do { \
  216. (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK) \
  217. | (((size_t)(v)) << _PyGC_REFS_SHIFT); \
  218. } while (0)
  219. #define _PyGCHead_DECREF(g) ((g)->gc.gc_refs -= 1 << _PyGC_REFS_SHIFT)
  220. #define _PyGCHead_FINALIZED(g) (((g)->gc.gc_refs & _PyGC_REFS_MASK_FINALIZED) != 0)
  221. #define _PyGCHead_SET_FINALIZED(g, v) do { \
  222. (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK_FINALIZED) \
  223. | (v != 0); \
  224. } while (0)
  225. #define _PyGC_FINALIZED(o) _PyGCHead_FINALIZED(_Py_AS_GC(o))
  226. #define _PyGC_SET_FINALIZED(o, v) _PyGCHead_SET_FINALIZED(_Py_AS_GC(o), v)
  227. #define _PyGC_REFS(o) _PyGCHead_REFS(_Py_AS_GC(o))
  228. #define _PyGC_REFS_UNTRACKED (-2)
  229. #define _PyGC_REFS_REACHABLE (-3)
  230. #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
  231. /* Tell the GC to track this object. NB: While the object is tracked the
  232. * collector it must be safe to call the ob_traverse method. */
  233. #define _PyObject_GC_TRACK(o) do { \
  234. PyGC_Head *g = _Py_AS_GC(o); \
  235. if (_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED) \
  236. Py_FatalError("GC object already tracked"); \
  237. _PyGCHead_SET_REFS(g, _PyGC_REFS_REACHABLE); \
  238. g->gc.gc_next = _PyGC_generation0; \
  239. g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
  240. g->gc.gc_prev->gc.gc_next = g; \
  241. _PyGC_generation0->gc.gc_prev = g; \
  242. } while (0);
  243. /* Tell the GC to stop tracking this object.
  244. * gc_next doesn't need to be set to NULL, but doing so is a good
  245. * way to provoke memory errors if calling code is confused.
  246. */
  247. #define _PyObject_GC_UNTRACK(o) do { \
  248. PyGC_Head *g = _Py_AS_GC(o); \
  249. assert(_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED); \
  250. _PyGCHead_SET_REFS(g, _PyGC_REFS_UNTRACKED); \
  251. g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
  252. g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
  253. g->gc.gc_next = NULL; \
  254. } while (0);
  255. /* True if the object is currently tracked by the GC. */
  256. #define _PyObject_GC_IS_TRACKED(o) \
  257. (_PyGC_REFS(o) != _PyGC_REFS_UNTRACKED)
  258. /* True if the object may be tracked by the GC in the future, or already is.
  259. This can be useful to implement some optimizations. */
  260. #define _PyObject_GC_MAY_BE_TRACKED(obj) \
  261. (PyObject_IS_GC(obj) && \
  262. (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
  263. #endif /* Py_LIMITED_API */
  264. #ifndef Py_LIMITED_API
  265. PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
  266. PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
  267. #endif /* !Py_LIMITED_API */
  268. PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
  269. PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
  270. PyAPI_FUNC(void) PyObject_GC_Track(void *);
  271. PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
  272. PyAPI_FUNC(void) PyObject_GC_Del(void *);
  273. #define PyObject_GC_New(type, typeobj) \
  274. ( (type *) _PyObject_GC_New(typeobj) )
  275. #define PyObject_GC_NewVar(type, typeobj, n) \
  276. ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
  277. /* Utility macro to help write tp_traverse functions.
  278. * To use this macro, the tp_traverse function must name its arguments
  279. * "visit" and "arg". This is intended to keep tp_traverse functions
  280. * looking as much alike as possible.
  281. */
  282. #define Py_VISIT(op) \
  283. do { \
  284. if (op) { \
  285. int vret = visit((PyObject *)(op), arg); \
  286. if (vret) \
  287. return vret; \
  288. } \
  289. } while (0)
  290. /* Test if a type supports weak references */
  291. #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
  292. #define PyObject_GET_WEAKREFS_LISTPTR(o) \
  293. ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. #endif /* !Py_OBJIMPL_H */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...