8000 bpo-39542: Make _Py_NewReference() opaque in C API (GH-18346) · python/cpython@40e547d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 40e547d

Browse files
authored
bpo-39542: Make _Py_NewReference() opaque in C API (GH-18346)
_Py_NewReference() becomes a regular opaque function, rather than a static inline function in the C API (object.h), to better hide implementation details. Move _Py_tracemalloc_config from public pymem.h to internal pycore_pymem.h header. Make _Py_AddToAllObjects() private.
1 parent 2545fa8 commit 40e547d

File tree

5 files changed

+54
-55
lines changed

5 files changed

+54
-55
lines changed

Include/internal/pycore_pymem.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,40 @@ PyAPI_FUNC(int) _PyMem_GetAllocatorName(
169169
PYMEM_ALLOCATOR_NOT_SET does nothing. */
170170
PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
171171

172+
/* bpo-35053: Expose _Py_tracemalloc_config for _Py_NewReference()
173+
which access directly _Py_tracemalloc_config.tracing for best
174+
performances. */
175+
struct _PyTraceMalloc_Config {
176+
/* Module initialized?
177+
Variable protected by the GIL */
178+
enum {
179+
TRACEMALLOC_NOT_INITIALIZED,
180+
TRACEMALLOC_INITIALIZED,
181+
TRACEMALLOC_FINALIZED
182+
} initialized;
183+
184+
/* Is tracemalloc tracing memory allocations?
185+
Variable protected by the GIL */
186+
int tracing;
187+
188+
/* limit of the number of frames in a traceback, 1 by default.
189+
Variable protected by the GIL. */
190+
int max_nframe;
191+
192+
/* use domain in trace key?
193+
Variable protected by the GIL. */
194+
int use_domain;
195+
};
196+
197+
#define _PyTraceMalloc_Config_INIT \
198+
{.initialized = TRACEMALLOC_NOT_INITIALIZED, \
199+
.tracing = 0, \
200+
.max_nframe = 1, \
201+
.use_domain = 0}
202+
203+
PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;
204+
205+
172206
#ifdef __cplusplus
173207
}
174208
#endif

Include/object.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef Py_OBJECT_H
22
#define Py_OBJECT_H
33

4-
#include "pymem.h" /* _Py_tracemalloc_config */
5-
64
#ifdef __cplusplus
75
extern "C" {
86
#endif
@@ -390,28 +388,13 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
390388
when a memory block is reused from a free list. */
391389
PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
392390

391+
PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
392+
393393
#ifdef Py_TRACE_REFS
394394
/* Py_TRACE_REFS is such major surgery that we call external routines. */
395395
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
396-
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
397396
#endif
398397

399-
400-
static inline void _Py_NewReference(PyObject *op)
401-
{
402-
if (_Py_tracemalloc_config.tracing) {
403-
_PyTraceMalloc_NewReference(op);
404-
}
405-
#ifdef Py_REF_DEBUG
406-
_Py_RefTotal++;
407-
#endif
408-
Py_REFCNT(op) = 1;
409-
#ifdef Py_TRACE_REFS
410-
_Py_AddToAllObjects(op, 1);
411-
#endif
412-
}
413-
414-
415398
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
416399

417400
static inline void _Py_INCREF(PyObject *op)

Include/pymem.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,41 +101,6 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
101101
#define PyMem_Del PyMem_Free
102102
#define PyMem_DEL PyMem_FREE
103103

104-
/* bpo-35053: expose _Py_tracemalloc_config for performance:
105-
_Py_NewReference() needs an efficient check to test if tracemalloc is
106-
tracing.
107-
108-
It has to be defined in pymem.h, before object.h is included. */
109-
struct _PyTraceMalloc_Config {
110-
/* Module initialized?
111-
Variable protected by the GIL */
112-
enum {
113-
TRACEMALLOC_NOT_INITIALIZED,
114-
TRACEMALLOC_INITIALIZED,
115-
TRACEMALLOC_FINALIZED
116-
} initialized;
117-
118-
/* Is tracemalloc tracing memory allocations?
119-
Variable protected by the GIL */
120-
int tracing;
121-
122-
/* limit of the number of frames in a traceback, 1 by default.
123-
Variable protected by the GIL. */
124-
int max_nframe;
125-
126-
/* use domain in trace key?
127-
Variable protected by the GIL. */
128-
int use_domain;
129-
};
130-
131-
PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;
132-
133-
#define _PyTraceMalloc_Config_INIT \
134-
{.initialized = TRACEMALLOC_NOT_INITIALIZED, \
135-
.tracing = 0, \
136-
.max_nframe = 1, \
137-
.use_domain = 0}
138-
139104

140105
#ifndef Py_LIMITED_API
141106
# define Py_CPYTHON_PYMEM_H

Modules/_tracemalloc.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_pymem.h"
23
#include "pycore_traceback.h"
34
#include "hashtable.h"
45
#include "frameobject.h"

Objects/object.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static PyObject refchain = {&refchain, &refchain};
9393
* way, though; exceptions include statically allocated type objects, and
9494
* statically allocated singletons (like Py_True and Py_None).
9595
*/
96-
void
96+
static void
9797
_Py_AddToAllObjects(PyObject *op, int force)
9898
{
9999
#ifdef Py_DEBUG
@@ -1805,6 +1805,22 @@ _PyTypes_Init(void)
18051805
}
18061806

18071807

1808+
void
1809+
_Py_NewReference(PyObject *op)
1810+
{
1811+
if (_Py_tracemalloc_config.tracing) {
1812+
_PyTraceMalloc_NewReference(op);
1813+
}
1814+
#ifdef Py_REF_DEBUG
1815+
_Py_RefTotal++;
1816+
#endif
1817+
Py_REFCNT(op) = 1;
1818+
#ifdef Py_TRACE_REFS
1819+
_Py_AddToAllObjects(op, 1);
1820+
#endif
1821+
}
1822+
1823+
18081824
#ifdef Py_TRACE_REFS
18091825
void
18101826
_Py_ForgetReference(PyObject *op)

0 commit comments

Comments
 (0)
0