bpo-39542: Make _Py_NewReference() opaque in C API (GH-18346) · python/cpython@40e547d · GitHub
Skip to content

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

5 files changed

Lines changed: 54 additions & 55 deletions

File tree

Include/internal/pycore_pymem.h

Lines changed: 34 additions & 0 deletions

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

0 commit comments

Comments
 (0)