File tree Expand file tree Collapse file tree 5 files changed +54
-55
lines changed
Expand file tree Collapse file tree 5 files changed +54
-55
lines changed Original file line number Diff line number Diff line change @@ -169,6 +169,40 @@ PyAPI_FUNC(int) _PyMem_GetAllocatorName(
169169 PYMEM_ALLOCATOR_NOT_SET does nothing. */
170170PyAPI_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
Original file line number Diff line number Diff line change 11#ifndef Py_OBJECT_H
22#define Py_OBJECT_H
33
4- #include "pymem.h" /* _Py_tracemalloc_config */
5-
64#ifdef __cplusplus
75extern "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. */
391389PyAPI_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. */
395395PyAPI_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-
415398PyAPI_FUNC (void ) _Py_Dealloc (PyObject * );
416399
417400static inline void _Py_INCREF (PyObject * op )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11#include "Python.h"
2+ #include "pycore_pymem.h"
23#include "pycore_traceback.h"
34#include "hashtable.h"
45#include "frameobject.h"
Original file line number Diff line number Diff 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
18091825void
18101826_Py_ForgetReference (PyObject * op )
You can’t perform that action at this time.
0 commit comments