@@ -20,9 +20,6 @@ module _tracemalloc
2020
2121_Py_DECLARE_STR (anon_unknown , "<unknown>" );
2222
23- /* Trace memory blocks allocated by PyMem_RawMalloc() */
24- #define TRACE_RAW_MALLOC
25-
2623/* Forward declaration */
2724static void tracemalloc_stop (void );
2825static void * raw_malloc (size_t size );
@@ -35,19 +32,14 @@ static void raw_free(void *ptr);
3532#define TO_PTR (key ) ((const void *)(uintptr_t)(key))
3633#define FROM_PTR (key ) ((uintptr_t)(key))
3734
38- /* Protected by the GIL */
39- static struct {
40- PyMemAllocatorEx mem ;
41- PyMemAllocatorEx raw ;
42- PyMemAllocatorEx obj ;
43- } allocators ;
35+ #define allocators _PyRuntime.tracemalloc.allocators
4436
4537
4638#if defined(TRACE_RAW_MALLOC )
4739/* This lock is needed because tracemalloc_free() is called without
4840 the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
4941 would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
50- static PyThread_type_lock tables_lock ;
42+ # define tables_lock _PyRuntime.tracemalloc.tables_lock
5143# define TABLES_LOCK () PyThread_acquire_lock(tables_lock, 1)
5244# define TABLES_UNLOCK () PyThread_release_lock(tables_lock)
5345#else
@@ -59,33 +51,8 @@ static PyThread_type_lock tables_lock;
5951
6052#define DEFAULT_DOMAIN 0
6153
62- /* Pack the frame_t structure to reduce the memory footprint on 64-bit
63- architectures: 12 bytes instead of 16. */
64- typedef struct
65- #ifdef __GNUC__
66- __attribute__((packed ))
67- #elif defined(_MSC_VER )
68- #pragma pack(push, 4)
69- #endif
70- {
71- /* filename cannot be NULL: "<unknown>" is used if the Python frame
72- filename is NULL */
73- PyObject * filename ;
74- unsigned int lineno ;
75- } frame_t ;
76- #ifdef _MSC_VER
77- #pragma pack(pop)
78- #endif
79-
80-
81- typedef struct {
82- Py_uhash_t hash ;
83- /* Number of frames stored */
84- uint16_t nframe ;
85- /* Total number of frames the traceback had */
86- uint16_t total_nframe ;
87- frame_t frames [1 ];
88- } traceback_t ;
54+ typedef struct tracemalloc_frame frame_t ;
55+ typedef struct tracemalloc_traceback traceback_t ;
8956
9057#define TRACEBACK_SIZE (NFRAME ) \
9158 (sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
@@ -96,7 +63,8 @@ typedef struct {
9663static const unsigned long MAX_NFRAME = Py_MIN (UINT16_MAX , ((SIZE_MAX - sizeof (traceback_t )) / sizeof (frame_t ) + 1 ));
9764
9865
99- static traceback_t tracemalloc_empty_traceback ;
66+ #define tracemalloc_empty_traceback _PyRuntime.tracemalloc.empty_traceback
67+
10068
10169/* Trace of a memory block */
10270typedef struct {
@@ -108,35 +76,13 @@ typedef struct {
10876} trace_t ;
10977
11078
111- /* Size in bytes of currently traced memory.
112- Protected by TABLES_LOCK(). */
113- static size_t tracemalloc_traced_memory = 0 ;
114-
115- /* Peak size in bytes of traced memory.
116- Protected by TABLES_LOCK(). */
117- static size_t tracemalloc_peak_traced_memory = 0 ;
118-
119- /* Hash table used as a set to intern filenames:
120- PyObject* => PyObject*.
121- Protected by the GIL */
122- static _Py_hashtable_t * tracemalloc_filenames = NULL ;
123-
124- /* Buffer to store a new traceback in traceback_new().
125- Protected by the GIL. */
126- static traceback_t * tracemalloc_traceback = NULL ;
127-
128- /* Hash table used as a set to intern tracebacks:
129- traceback_t* => traceback_t*
130- Protected by the GIL */
131- static _Py_hashtable_t * tracemalloc_tracebacks = NULL ;
132-
133- /* pointer (void*) => trace (trace_t*).
134- Protected by TABLES_LOCK(). */
135- static _Py_hashtable_t * tracemalloc_traces = NULL ;
136-
137- /* domain (unsigned int) => traces (_Py_hashtable_t).
138- Protected by TABLES_LOCK(). */
139- static _Py_hashtable_t * tracemalloc_domains = NULL ;
79+ #define tracemalloc_traced_memory _PyRuntime.tracemalloc.traced_memory
80+ #define tracemalloc_peak_traced_memory _PyRuntime.tracemalloc.peak_traced_memory
81+ #define tracemalloc_filenames _PyRuntime.tracemalloc.filenames
82+ #define tracemalloc_traceback _PyRuntime.tracemalloc.traceback
83+ #define tracemalloc_tracebacks _PyRuntime.tracemalloc.tracebacks
84+ #define tracemalloc_traces _PyRuntime.tracemalloc.traces
85+ #define tracemalloc_domains _PyRuntime.tracemalloc.domains
14086
14187
14288#ifdef TRACE_DEBUG
@@ -157,7 +103,7 @@ tracemalloc_error(const char *format, ...)
157103#if defined(TRACE_RAW_MALLOC )
158104#define REENTRANT_THREADLOCAL
159105
160- static Py_tss_t tracemalloc_reentrant_key = Py_tss_NEEDS_INIT ;
106+ #define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key
161107
162108/* Any non-NULL pointer can be used */
163109#define REENTRANT Py_True
0 commit comments