8000 gh-105268: Add _Py_FROM_GC() function to pycore_gc.h · vstinner/cpython@74ceaf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74ceaf8

Browse files
committed
pythongh-105268: Add _Py_FROM_GC() function to pycore_gc.h
* 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().
1 parent 2c49c75 commit 74ceaf8

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Include/internal/pycore_gc.h

Lines changed: 13 additions & 2 deletions
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) {

Include/internal/pycore_object.h

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 6 deletions
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(op)
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)
@@ -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)
0