jpayne@69
|
1 #ifndef Py_CPYTHON_OBJIMPL_H
|
jpayne@69
|
2 # error "this header file must not be included directly"
|
jpayne@69
|
3 #endif
|
jpayne@69
|
4
|
jpayne@69
|
5 #ifdef __cplusplus
|
jpayne@69
|
6 extern "C" {
|
jpayne@69
|
7 #endif
|
jpayne@69
|
8
|
jpayne@69
|
9 /* This function returns the number of allocated memory blocks, regardless of size */
|
jpayne@69
|
10 PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
|
jpayne@69
|
11
|
jpayne@69
|
12 /* Macros */
|
jpayne@69
|
13 #ifdef WITH_PYMALLOC
|
jpayne@69
|
14 PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
|
jpayne@69
|
15 #endif
|
jpayne@69
|
16
|
jpayne@69
|
17
|
jpayne@69
|
18 typedef struct {
|
jpayne@69
|
19 /* user context passed as the first argument to the 2 functions */
|
jpayne@69
|
20 void *ctx;
|
jpayne@69
|
21
|
jpayne@69
|
22 /* allocate an arena of size bytes */
|
jpayne@69
|
23 void* (*alloc) (void *ctx, size_t size);
|
jpayne@69
|
24
|
jpayne@69
|
25 /* free an arena */
|
jpayne@69
|
26 void (*free) (void *ctx, void *ptr, size_t size);
|
jpayne@69
|
27 } PyObjectArenaAllocator;
|
jpayne@69
|
28
|
jpayne@69
|
29 /* Get the arena allocator. */
|
jpayne@69
|
30 PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
|
jpayne@69
|
31
|
jpayne@69
|
32 /* Set the arena allocator. */
|
jpayne@69
|
33 PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
|
jpayne@69
|
34
|
jpayne@69
|
35
|
jpayne@69
|
36 PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
|
jpayne@69
|
37 PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
|
jpayne@69
|
38
|
jpayne@69
|
39
|
jpayne@69
|
40 /* Test if an object has a GC head */
|
jpayne@69
|
41 #define PyObject_IS_GC(o) \
|
jpayne@69
|
42 (PyType_IS_GC(Py_TYPE(o)) \
|
jpayne@69
|
43 && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
|
jpayne@69
|
44
|
jpayne@69
|
45 /* GC information is stored BEFORE the object structure. */
|
jpayne@69
|
46 typedef struct {
|
jpayne@69
|
47 // Pointer to next object in the list.
|
jpayne@69
|
48 // 0 means the object is not tracked
|
jpayne@69
|
49 uintptr_t _gc_next;
|
jpayne@69
|
50
|
jpayne@69
|
51 // Pointer to previous object in the list.
|
jpayne@69
|
52 // Lowest two bits are used for flags documented later.
|
jpayne@69
|
53 uintptr_t _gc_prev;
|
jpayne@69
|
54 } PyGC_Head;
|
jpayne@69
|
55
|
jpayne@69
|
56 #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
|
jpayne@69
|
57
|
jpayne@69
|
58 /* True if the object is currently tracked by the GC. */
|
jpayne@69
|
59 #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)
|
jpayne@69
|
60
|
jpayne@69
|
61 /* True if the object may be tracked by the GC in the future, or already is.
|
jpayne@69
|
62 This can be useful to implement some optimizations. */
|
jpayne@69
|
63 #define _PyObject_GC_MAY_BE_TRACKED(obj) \
|
jpayne@69
|
64 (PyObject_IS_GC(obj) && \
|
jpayne@69
|
65 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
|
jpayne@69
|
66
|
jpayne@69
|
67
|
jpayne@69
|
68 /* Bit flags for _gc_prev */
|
jpayne@69
|
69 /* Bit 0 is set when tp_finalize is called */
|
jpayne@69
|
70 #define _PyGC_PREV_MASK_FINALIZED (1)
|
jpayne@69
|
71 /* Bit 1 is set when the object is in generation which is GCed currently. */
|
jpayne@69
|
72 #define _PyGC_PREV_MASK_COLLECTING (2)
|
jpayne@69
|
73 /* The (N-2) most significant bits contain the real address. */
|
jpayne@69
|
74 #define _PyGC_PREV_SHIFT (2)
|
jpayne@69
|
75 #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
|
jpayne@69
|
76
|
jpayne@69
|
77 // Lowest bit of _gc_next is used for flags only in GC.
|
jpayne@69
|
78 // But it is always 0 for normal code.
|
jpayne@69
|
79 #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next)
|
jpayne@69
|
80 #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p))
|
jpayne@69
|
81
|
jpayne@69
|
82 // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
|
jpayne@69
|
83 #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
|
jpayne@69
|
84 #define _PyGCHead_SET_PREV(g, p) do { \
|
jpayne@69
|
85 assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \
|
jpayne@69
|
86 (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \
|
jpayne@69
|
87 | ((uintptr_t)(p)); \
|
jpayne@69
|
88 } while (0)
|
jpayne@69
|
89
|
jpayne@69
|
90 #define _PyGCHead_FINALIZED(g) \
|
jpayne@69
|
91 (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
|
jpayne@69
|
92 #define _PyGCHead_SET_FINALIZED(g) \
|
jpayne@69
|
93 ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
|
jpayne@69
|
94
|
jpayne@69
|
95 #define _PyGC_FINALIZED(o) \
|
jpayne@69
|
96 _PyGCHead_FINALIZED(_Py_AS_GC(o))
|
jpayne@69
|
97 #define _PyGC_SET_FINALIZED(o) \
|
jpayne@69
|
98 _PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
|
jpayne@69
|
99
|
jpayne@69
|
100
|
jpayne@69
|
101 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
|
jpayne@69
|
102 PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
|
jpayne@69
|
103
|
jpayne@69
|
104
|
jpayne@69
|
105 /* Test if a type supports weak references */
|
jpayne@69
|
106 #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
|
jpayne@69
|
107
|
jpayne@69
|
108 #define PyObject_GET_WEAKREFS_LISTPTR(o) \
|
jpayne@69
|
109 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
|
jpayne@69
|
110
|
jpayne@69
|
111 #ifdef __cplusplus
|
jpayne@69
|
112 }
|
jpayne@69
|
113 #endif
|