1 #ifndef Py_INTERNAL_OBJECT_H
2 #define Py_INTERNAL_OBJECT_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include "pycore_pystate.h"   /* _PyRuntime */
12 
13 PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
14 PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
15 
16 /* Tell the GC to track this object.
17  *
18  * NB: While the object is tracked by the collector, it must be safe to call the
19  * ob_traverse method.
20  *
21  * Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
22  * because it's not object header.  So we don't use _PyGCHead_PREV() and
23  * _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
24  *
25  * The PyObject_GC_Track() function is the public version of this macro.
26  */
_PyObject_GC_TRACK_impl(const char * filename,int lineno,PyObject * op)27 static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
28                                            PyObject *op)
29 {
30     _PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op),
31                           "object already tracked by the garbage collector",
32                           filename, lineno, "_PyObject_GC_TRACK");
33 
34     PyGC_Head *gc = _Py_AS_GC(op);
35     _PyObject_ASSERT_FROM(op,
36                           (gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0,
37                           "object is in generation which is garbage collected",
38                           filename, lineno, "_PyObject_GC_TRACK");
39 
40     PyGC_Head *last = (PyGC_Head*)(_PyRuntime.gc.generation0->_gc_prev);
41     _PyGCHead_SET_NEXT(last, gc);
42     _PyGCHead_SET_PREV(gc, last);
43     _PyGCHead_SET_NEXT(gc, _PyRuntime.gc.generation0);
44     _PyRuntime.gc.generation0->_gc_prev = (uintptr_t)gc;
45 }
46 
47 #define _PyObject_GC_TRACK(op) \
48     _PyObject_GC_TRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
49 
50 /* Tell the GC to stop tracking this object.
51  *
52  * Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
53  * must be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
54  *
55  * The object must be tracked by the GC.
56  *
57  * The PyObject_GC_UnTrack() function is the public version of this macro.
58  */
_PyObject_GC_UNTRACK_impl(const char * filename,int lineno,PyObject * op)59 static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
60                                              PyObject *op)
61 {
62     _PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op),
63                           "object not tracked by the garbage collector",
64                           filename, lineno, "_PyObject_GC_UNTRACK");
65 
66     PyGC_Head *gc = _Py_AS_GC(op);
67     PyGC_Head *prev = _PyGCHead_PREV(gc);
68     PyGC_Head *next = _PyGCHead_NEXT(gc);
69     _PyGCHead_SET_NEXT(prev, next);
70     _PyGCHead_SET_PREV(next, prev);
71     gc->_gc_next = 0;
72     gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
73 }
74 
75 #define _PyObject_GC_UNTRACK(op) \
76     _PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 #endif /* !Py_INTERNAL_OBJECT_H */
82