blob: 2f802e92a6cdf82b1018aac9caede6dd5bf24f35 [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
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Victor Stinnerf58bd7c2020-02-05 13:12:19 +01009/* Inline functions trading binary compatibility for speed:
10 PyObject_INIT() is the fast version of PyObject_Init(), and
11 PyObject_INIT_VAR() is the fast version of PyObject_InitVar().
12
13 These inline functions must not be called with op=NULL. */
14static inline PyObject*
15_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
16{
17 assert(op != NULL);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010018 Py_SET_TYPE(op, typeobj);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +010019 if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
20 Py_INCREF(typeobj);
21 }
22 _Py_NewReference(op);
23 return op;
24}
25
26#define PyObject_INIT(op, typeobj) \
27 _PyObject_INIT(_PyObject_CAST(op), (typeobj))
28
29static inline PyVarObject*
30_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
31{
32 assert(op != NULL);
Victor Stinnerb10dc3e2020-02-07 12:05:12 +010033 Py_SET_SIZE(op, size);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +010034 PyObject_INIT((PyObject *)op, typeobj);
35 return op;
36}
37
38#define PyObject_INIT_VAR(op, typeobj, size) \
39 _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
40
41
Victor Stinnere4211062018-11-23 17:00:00 +010042/* This function returns the number of allocated memory blocks, regardless of size */
43PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
44
45/* Macros */
46#ifdef WITH_PYMALLOC
47PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
48#endif
49
50
51typedef struct {
52 /* user context passed as the first argument to the 2 functions */
53 void *ctx;
54
55 /* allocate an arena of size bytes */
56 void* (*alloc) (void *ctx, size_t size);
57
58 /* free an arena */
59 void (*free) (void *ctx, void *ptr, size_t size);
60} PyObjectArenaAllocator;
61
62/* Get the arena allocator. */
63PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
64
65/* Set the arena allocator. */
66PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
67
68
69PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
70PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
71
72
73/* Test if an object has a GC head */
74#define PyObject_IS_GC(o) \
75 (PyType_IS_GC(Py_TYPE(o)) \
76 && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
77
78/* GC information is stored BEFORE the object structure. */
79typedef struct {
80 // Pointer to next object in the list.
81 // 0 means the object is not tracked
82 uintptr_t _gc_next;
83
84 // Pointer to previous object in the list.
85 // Lowest two bits are used for flags documented later.
86 uintptr_t _gc_prev;
87} PyGC_Head;
88
89#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
90
91/* True if the object is currently tracked by the GC. */
92#define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)
93
94/* True if the object may be tracked by the GC in the future, or already is.
95 This can be useful to implement some optimizations. */
96#define _PyObject_GC_MAY_BE_TRACKED(obj) \
97 (PyObject_IS_GC(obj) && \
98 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
99
100
101/* Bit flags for _gc_prev */
102/* Bit 0 is set when tp_finalize is called */
103#define _PyGC_PREV_MASK_FINALIZED (1)
104/* Bit 1 is set when the object is in generation which is GCed currently. */
105#define _PyGC_PREV_MASK_COLLECTING (2)
106/* The (N-2) most significant bits contain the real address. */
107#define _PyGC_PREV_SHIFT (2)
108#define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
109
110// Lowest bit of _gc_next is used for flags only in GC.
111// But it is always 0 for normal code.
112#define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next)
113#define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p))
114
115// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
116#define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
117#define _PyGCHead_SET_PREV(g, p) do { \
118 assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \
119 (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \
120 | ((uintptr_t)(p)); \
121 } while (0)
122
123#define _PyGCHead_FINALIZED(g) \
124 (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
125#define _PyGCHead_SET_FINALIZED(g) \
126 ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
127
128#define _PyGC_FINALIZED(o) \
129 _PyGCHead_FINALIZED(_Py_AS_GC(o))
130#define _PyGC_SET_FINALIZED(o) \
131 _PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
132
133
134PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
135PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
136
137
138/* Test if a type supports weak references */
139#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
140
Victor Stinner38aefc52020-04-06 14:07:02 +0200141PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
Victor Stinnere4211062018-11-23 17:00:00 +0100142
143#ifdef __cplusplus
144}
145#endif