blob: ca4009bcdb4c15798c27c26abc4e0e87a7048052 [file] [log] [blame]
Victor Stinnere4211062018-11-23 17:00:00 +01001#ifndef Py_CPYTHON_OBJIMPL_H
2# error "this header file must not be included directly"
3#endif
4
Victor Stinner92055202020-04-08 00:38:15 +02005#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
6
7/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
8 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
9 value is rounded up to the closest multiple of sizeof(void *), in order to
10 ensure that pointer fields at the end of the object are correctly aligned
11 for the platform (this is of special importance for subclasses of, e.g.,
12 str or int, so that pointers can be stored after the embedded data).
13
14 Note that there's no memory wastage in doing this, as malloc has to
15 return (at worst) pointer-aligned memory anyway.
16*/
17#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
18# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
19#endif
20
21#define _PyObject_VAR_SIZE(typeobj, nitems) \
22 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
23 (nitems)*(typeobj)->tp_itemsize, \
24 SIZEOF_VOID_P)
25
26
27/* This example code implements an object constructor with a custom
28 allocator, where PyObject_New is inlined, and shows the important
29 distinction between two steps (at least):
30 1) the actual allocation of the object storage;
31 2) the initialization of the Python specific fields
32 in this storage with PyObject_{Init, InitVar}.
33
34 PyObject *
35 YourObject_New(...)
36 {
37 PyObject *op;
38
39 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
40 if (op == NULL)
41 return PyErr_NoMemory();
42
43 PyObject_Init(op, &YourTypeStruct);
44
45 op->ob_field = value;
46 ...
47 return op;
48 }
49
50 Note that in C++, the use of the new operator usually implies that
51 the 1st step is performed automatically for you, so in a C++ class
52 constructor you would start directly with PyObject_Init/InitVar. */
53
54
Victor Stinnerf58bd7c2020-02-05 13:12:19 +010055/* Inline functions trading binary compatibility for speed:
56 PyObject_INIT() is the fast version of PyObject_Init(), and
57 PyObject_INIT_VAR() is the fast version of PyObject_InitVar().
58
59 These inline functions must not be called with op=NULL. */
60static inline PyObject*
61_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
62{
63 assert(op != NULL);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010064 Py_SET_TYPE(op, typeobj);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +010065 if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
66 Py_INCREF(typeobj);
67 }
68 _Py_NewReference(op);
69 return op;
70}
71
72#define PyObject_INIT(op, typeobj) \
73 _PyObject_INIT(_PyObject_CAST(op), (typeobj))
74
75static inline PyVarObject*
76_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
77{
78 assert(op != NULL);
Victor Stinnerb10dc3e2020-02-07 12:05:12 +010079 Py_SET_SIZE(op, size);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +010080 PyObject_INIT((PyObject *)op, typeobj);
81 return op;
82}
83
84#define PyObject_INIT_VAR(op, typeobj, size) \
85 _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
86
87
Victor Stinnere4211062018-11-23 17:00:00 +010088/* This function returns the number of allocated memory blocks, regardless of size */
89PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
90
91/* Macros */
92#ifdef WITH_PYMALLOC
93PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
94#endif
95
96
97typedef struct {
98 /* user context passed as the first argument to the 2 functions */
99 void *ctx;
100
101 /* allocate an arena of size bytes */
102 void* (*alloc) (void *ctx, size_t size);
103
104 /* free an arena */
105 void (*free) (void *ctx, void *ptr, size_t size);
106} PyObjectArenaAllocator;
107
108/* Get the arena allocator. */
109PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
110
111/* Set the arena allocator. */
112PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
113
114
115PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
116PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
117
118
Hai Shi675d9a32020-04-15 02:11:20 +0800119/* Test if an object implements the garbage collector protocol */
120PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
121
Victor Stinnere4211062018-11-23 17:00:00 +0100122
Victor Stinner01355982020-04-13 11:38:42 +0200123/* Code built with Py_BUILD_CORE must include pycore_gc.h instead which
124 defines a different _PyGC_FINALIZED() macro. */
125#ifndef Py_BUILD_CORE
126 // Kept for backward compatibility with Python 3.8
127# define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
128#endif
Victor Stinnere4211062018-11-23 17:00:00 +0100129
130PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
131PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
132
133
134/* Test if a type supports weak references */
135#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
136
Victor Stinner38aefc52020-04-06 14:07:02 +0200137PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);