Skip to content

Commit c7bf74b

Browse files
authored
gh-105268: Add _Py_FROM_GC() function to pycore_gc.h (#105362)
* gcmodule.c reuses _Py_AS_GC(op) for AS_GC() * Move gcmodule.c FROM_GC() implementation to a new _Py_FROM_GC() static inline function in pycore_gc.h. * _PyObject_IS_GC(): only get the type once * gc_is_finalized(à) and PyObject_GC_IsFinalized() use _PyGC_FINALIZED(), instead of _PyGCHead_FINALIZED(). * Remove _Py_CAST() in pycore_gc.h: this header file is not built with C++.
1 parent 963099e commit c7bf74b

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Include/internal/pycore_gc.h

+17-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,21 @@ typedef struct {
1919
uintptr_t _gc_prev;
2020
} PyGC_Head;
2121

22+
#define _PyGC_Head_UNUSED PyGC_Head
23+
24+
25+
/* Get an object's GC head */
2226
static inline PyGC_Head* _Py_AS_GC(PyObject *op) {
23-
return (_Py_CAST(PyGC_Head*, op) - 1);
27+
char *gc = ((char*)op) - sizeof(PyGC_Head);
28+
return (PyGC_Head*)gc;
2429
}
25-
#define _PyGC_Head_UNUSED PyGC_Head
30+
31+
/* Get the object given the GC head */
32+
static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
33+
char *op = ((char *)gc) + sizeof(PyGC_Head);
34+
return (PyObject *)op;
35+
}
36+
2637

2738
/* True if the object is currently tracked by the GC. */
2839
static inline int _PyObject_GC_IS_TRACKED(PyObject *op) {
@@ -57,19 +68,19 @@ static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {
5768
// But it is always 0 for normal code.
5869
static inline PyGC_Head* _PyGCHead_NEXT(PyGC_Head *gc) {
5970
uintptr_t next = gc->_gc_next;
60-
return _Py_CAST(PyGC_Head*, next);
71+
return (PyGC_Head*)next;
6172
}
6273
static inline void _PyGCHead_SET_NEXT(PyGC_Head *gc, PyGC_Head *next) {
63-
gc->_gc_next = _Py_CAST(uintptr_t, next);
74+
gc->_gc_next = (uintptr_t)next;
6475
}
6576

6677
// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
6778
static inline PyGC_Head* _PyGCHead_PREV(PyGC_Head *gc) {
6879
uintptr_t prev = (gc->_gc_prev & _PyGC_PREV_MASK);
69-
return _Py_CAST(PyGC_Head*, prev);
80+
return (PyGC_Head*)prev;
7081
}
7182
static inline void _PyGCHead_SET_PREV(PyGC_Head *gc, PyGC_Head *prev) {
72-
uintptr_t uprev = _Py_CAST(uintptr_t, prev);
83+
uintptr_t uprev = (uintptr_t)prev;
7384
assert((uprev & ~_PyGC_PREV_MASK) == 0);
7485
gc->_gc_prev = ((gc->_gc_prev & ~_PyGC_PREV_MASK) | uprev);
7586
}

Include/internal/pycore_object.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(PyObject *op)
326326
static inline int
327327
_PyObject_IS_GC(PyObject *obj)
328328
{
329-
return (PyType_IS_GC(Py_TYPE(obj))
330-
&& (Py_TYPE(obj)->tp_is_gc == NULL
331-
|| Py_TYPE(obj)->tp_is_gc(obj)));
329+
PyTypeObject *type = Py_TYPE(obj);
330+
return (PyType_IS_GC(type)
331+
&& (type->tp_is_gc == NULL || type->tp_is_gc(obj)));
332332
}
333333

334334
// Fast inlined version of PyType_IS_GC()

Modules/gcmodule.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ module gc
6868
// most gc_list_* functions for it.
6969
#define NEXT_MASK_UNREACHABLE (1)
7070

71-
/* Get an object's GC head */
72-
#define AS_GC(o) ((PyGC_Head *)(((char *)(o))-sizeof(PyGC_Head)))
71+
#define AS_GC(op) _Py_AS_GC(op)
72+
#define FROM_GC(gc) _Py_FROM_GC(gc)
7373

74-
/* Get the object given the GC head */
75-
#define FROM_GC(g) ((PyObject *)(((char *)(g))+sizeof(PyGC_Head)))
7674

7775
static inline int
7876
gc_is_collecting(PyGC_Head *g)
@@ -861,7 +859,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
861859
* to imagine how calling it later could create a problem for us. wr
862860
* is moved to wrcb_to_call in this case.
863861
*/
864-
if (gc_is_collecting(AS_GC(wr))) {
862+
if (gc_is_collecting(AS_GC((PyObject *)wr))) {
865863
/* it should already have been cleared above */
866864
assert(wr->wr_object == Py_None);
867865
continue;
@@ -873,7 +871,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
873871
Py_INCREF(wr);
874872

875873
/* Move wr to wrcb_to_call, for the next pass. */
876-
wrasgc = AS_GC(wr);
874+
wrasgc = AS_GC((PyObject *)wr);
877875
assert(wrasgc != next); /* wrasgc is reachable, but
878876
next isn't, so they can't
879877
be the same */
@@ -1909,7 +1907,7 @@ static PyObject *
19091907
gc_is_finalized(PyObject *module, PyObject *obj)
19101908
/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/
19111909
{
1912-
if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
1910+
if (_PyObject_IS_GC(obj) && _PyGC_FINALIZED(obj)) {
19131911
Py_RETURN_TRUE;
19141912
}
19151913
Py_RETURN_FALSE;
@@ -2409,7 +2407,7 @@ PyObject_GC_IsTracked(PyObject* obj)
24092407
int
24102408
PyObject_GC_IsFinalized(PyObject *obj)
24112409
{
2412-
if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
2410+
if (_PyObject_IS_GC(obj) && _PyGC_FINALIZED(obj)) {
24132411
return 1;
24142412
}
24152413
return 0;

0 commit comments

Comments
 (0)