Skip to content

Commit bcda8f1

Browse files
authored
bpo-35081: Add Include/internal/pycore_object.h (GH-10640)
Move _PyObject_GC_TRACK() and _PyObject_GC_UNTRACK() from Include/objimpl.h to Include/internal/pycore_object.h.
1 parent aac1f81 commit bcda8f1

34 files changed

+93
-50
lines changed

‎Include/internal/pycore_object.h‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef Py_INTERNAL_OBJECT_H
2+
#define Py_INTERNAL_OBJECT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN defined"
9+
#endif
10+
11+
/* Tell the GC to track this object.
12+
*
13+
* NB: While the object is tracked by the collector, it must be safe to call the
14+
* ob_traverse method.
15+
*
16+
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
17+
* because it's not object header. So we don't use _PyGCHead_PREV() and
18+
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
19+
*
20+
* The PyObject_GC_Track() function is the public version of this macro.
21+
*/
22+
#define _PyObject_GC_TRACK(o) do { \
23+
PyGC_Head *g = _Py_AS_GC(o); \
24+
if (g->_gc_next != 0) { \
25+
Py_FatalError("GC object already tracked"); \
26+
} \
27+
assert((g->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0); \
28+
PyGC_Head *last = (PyGC_Head*)(_PyRuntime.gc.generation0->_gc_prev); \
29+
_PyGCHead_SET_NEXT(last, g); \
30+
_PyGCHead_SET_PREV(g, last); \
31+
_PyGCHead_SET_NEXT(g, _PyRuntime.gc.generation0); \
32+
_PyRuntime.gc.generation0->_gc_prev = (uintptr_t)g; \
33+
} while (0);
34+
35+
/* Tell the GC to stop tracking this object.
36+
*
37+
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING must
38+
* be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
39+
*
40+
* The PyObject_GC_UnTrack() function is the public version of this macro.
41+
*/
42+
#define _PyObject_GC_UNTRACK(o) do { \
43+
PyGC_Head *g = _Py_AS_GC(o); \
44+
PyGC_Head *prev = _PyGCHead_PREV(g); \
45+
PyGC_Head *next = _PyGCHead_NEXT(g); \
46+
assert(next != NULL); \
47+
_PyGCHead_SET_NEXT(prev, next); \
48+
_PyGCHead_SET_PREV(next, prev); \
49+
g->_gc_next = 0; \
50+
g->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; \
51+
} while (0);
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
#endif /* !Py_INTERNAL_OBJECT_H */

‎Include/objimpl.h‎

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -323,51 +323,6 @@ typedef struct {
323323
_PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
324324
#endif /* !defined(Py_LIMITED_API) */
325325

326-
327-
#if defined(Py_BUILD_CORE) || defined(Py_BUILD_CORE_BUILTIN)
328-
/* Tell the GC to track this object.
329-
*
330-
* NB: While the object is tracked by the collector, it must be safe to call the
331-
* ob_traverse method.
332-
*
333-
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
334-
* because it's not object header. So we don't use _PyGCHead_PREV() and
335-
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
336-
*
337-
* The PyObject_GC_Track() function is the public version of this macro.
338-
*/
339-
#define _PyObject_GC_TRACK(o) do { \
340-
PyGC_Head *g = _Py_AS_GC(o); \
341-
if (g->_gc_next != 0) { \
342-
Py_FatalError("GC object already tracked"); \
343-
} \
344-
assert((g->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0); \
345-
PyGC_Head *last = (PyGC_Head*)(_PyRuntime.gc.generation0->_gc_prev); \
346-
_PyGCHead_SET_NEXT(last, g); \
347-
_PyGCHead_SET_PREV(g, last); \
348-
_PyGCHead_SET_NEXT(g, _PyRuntime.gc.generation0); \
349-
_PyRuntime.gc.generation0->_gc_prev = (uintptr_t)g; \
350-
} while (0);
351-
352-
/* Tell the GC to stop tracking this object.
353-
*
354-
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING must
355-
* be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
356-
*
357-
* The PyObject_GC_UnTrack() function is the public version of this macro.
358-
*/
359-
#define _PyObject_GC_UNTRACK(o) do { \
360-
PyGC_Head *g = _Py_AS_GC(o); \
361-
PyGC_Head *prev = _PyGCHead_PREV(g); \
362-
PyGC_Head *next = _PyGCHead_NEXT(g); \
363-
assert(next != NULL); \
364-
_PyGCHead_SET_NEXT(prev, next); \
365-
_PyGCHead_SET_PREV(next, prev); \
366-
g->_gc_next = 0; \
367-
g->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; \
368-
} while (0);
369-
#endif /* defined(Py_BUILD_CORE) || defined(Py_BUILD_CORE_BUILTIN) */
370-
371326
#ifndef Py_LIMITED_API
372327
PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
373328
PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);

‎Modules/_io/bufferedio.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define PY_SSIZE_T_CLEAN
1111
#include "Python.h"
12+
#include "pycore_object.h"
1213
#include "pycore_pystate.h"
1314
#include "structmember.h"
1415
#include "pythread.h"

‎Modules/_io/bytesio.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_object.h"
23
#include "structmember.h" /* for offsetof() */
34
#include "_iomodule.h"
45

‎Modules/_io/fileio.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define PY_SSIZE_T_CLEAN
44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "structmember.h"
67
#ifdef HAVE_SYS_TYPES_H
78
#include <sys/types.h>

‎Modules/_io/iobase.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#define PY_SSIZE_T_CLEAN
1212
#include "Python.h"
13+
#include "pycore_object.h"
1314
#include "structmember.h"
1415
#include "_iomodule.h"
1516

‎Modules/_io/stringio.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Python.h"
33
#include "structmember.h"
44
#include "pycore_accu.h"
5+
#include "pycore_object.h"
56
#include "_iomodule.h"
67

78
/* Implementation note: the buffer is always at least one character longer

‎Modules/_io/textio.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
11+
#include "pycore_object.h"
1112
#include "structmember.h"
1213
#include "_iomodule.h"
1314

‎Modules/_io/winconsoleio.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
11+
#include "pycore_object.h"
1112

1213
#ifdef MS_WINDOWS
1314

@@ -556,7 +557,7 @@ read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) {
556557
Py_BEGIN_ALLOW_THREADS
557558
DWORD off = 0;
558559
while (off < maxlen) {
559-
DWORD n = (DWORD)-1;
560+
DWORD n = (DWORD)-1;
560561
DWORD len = min(maxlen - off, BUFSIZ);
561562
SetLastError(0);
562563
BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL);

‎Modules/gcmodule.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "Python.h"
2727
#include "pycore_context.h"
28+
#include "pycore_object.h"
2829
#include "pycore_pymem.h"
2930
#include "pycore_pystate.h"
3031
#include "frameobject.h" /* for PyFrame_ClearFreeList */

0 commit comments

Comments
 (0)