8000 gh-129185: Remove internal TRACE_RAW_MALLOC macro (#129218) · python/cpython@e579cdb · GitHub
[go: up one dir, main page]

Skip to content

Commit e579cdb

Browse files
authored
gh-129185: Remove internal TRACE_RAW_MALLOC macro (#129218)
Always build tracemalloc with PyMem_RawMalloc() hooks.
1 parent 46c7e13 commit e579cdb

File tree

2 files changed

+3
-51
lines changed

2 files changed

+3
-51
lines changed

Include/internal/pycore_tracemalloc.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ extern "C" {
1111
#include "pycore_hashtable.h" // _Py_hashtable_t
1212

1313

14-
/* Trace memory blocks allocated by PyMem_RawMalloc() */
15-
#define TRACE_RAW_MALLOC
16-
17-
1814
struct _PyTraceMalloc_Config {
1915
/* Module initialized?
2016
Variable protected by the GIL */
@@ -74,9 +70,7 @@ struct _tracemalloc_runtime_state {
7470
PyMemAllocatorEx obj;
7571
} allocators;
7672

77-
#if defined(TRACE_RAW_MALLOC)
7873
PyThread_type_lock tables_lock;
79-
#endif
8074
/* Size in bytes of currently traced memory.
8175
Protected by TABLES_LOCK(). */
8276
size_t traced_memory;

Python/tracemalloc.c

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
3333
#define allocators _PyRuntime.tracemalloc.allocators
3434

3535

36-
#if defined(TRACE_RAW_MALLOC)
3736
/* This lock is needed because tracemalloc_free() is called without
3837
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
3938
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
40-
# define tables_lock _PyRuntime.tracemalloc.tables_lock
41-
# define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
42-
# define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
43-
#else
44-
/* variables are protected by the GIL */
45-
# define TABLES_LOCK()
46-
# define TABLES_UNLOCK()
47-
#endif
39+
#define tables_lock _PyRuntime.tracemalloc.tables_lock
40+
#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
41+
#define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
4842

4943

5044
#define DEFAULT_DOMAIN 0
@@ -98,9 +92,6 @@ tracemalloc_error(const char *format, ...)
9892
#endif
9993

10094

101-
#if defined(TRACE_RAW_MALLOC)
102-
#define REENTRANT_THREADLOCAL
103-
10495
#define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key
10596

10697
/* Any non-NULL pointer can be used */
@@ -137,25 +128,6 @@ set_reentrant(int reentrant)
137128
}
138129
}
139130

140-
#else
141-
142-
/* TRACE_RAW_MALLOC not defined: variable protected by the GIL */
143-
static int tracemalloc_reentrant = 0;
144-
145-
static int
146-
get_reentrant(void)
147-
{
148-
return tracemalloc_reentrant;
149-
}
150-
151-
static void
152-
set_reentrant(int reentrant)
153-
{
154-
assert(reentrant != tracemalloc_reentrant);
155-
tracemalloc_reentrant = reentrant;
156-
}
157-
#endif
158-
159131

160132
static Py_uhash_t
161133
hashtable_hash_pyobject(const void *key)
@@ -709,7 +681,6 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
709681
}
710682

711683

712-
#ifdef TRACE_RAW_MALLOC
713684
static void*
714685
tracemalloc_raw_malloc(void *ctx, size_t size)
715686
{
@@ -729,7 +700,6 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
729700
{
730701
return tracemalloc_realloc(1, ctx, ptr, new_size);
731702
}
732-
#endif /* TRACE_RAW_MALLOC */
733703

734704

735705
static void
@@ -767,20 +737,16 @@ _PyTraceMalloc_Init(void)
767737

768738
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
769739

770-
#ifdef REENTRANT_THREADLOCAL
771740
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
772741
return _PyStatus_NO_MEMORY();
773742
}
774-
#endif
775743

776-
#if defined(TRACE_RAW_MALLOC)
777744
if (tables_lock == NULL) {
778745
tables_lock = PyThread_allocate_lock();
779746
if (tables_lock == NULL) {
780747
return _PyStatus_NO_MEMORY();
781748
}
782749
}
783-
#endif
784750

785751
tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
786752
hashtable_compare_unicode,
@@ -826,16 +792,12 @@ tracemalloc_deinit(void)
826792
_Py_hashtable_destroy(tracemalloc_tracebacks);
827793
_Py_hashtable_destroy(tracemalloc_filenames);
828794

829-
#if defined(TRACE_RAW_MALLOC)
830795
if (tables_lock != NULL) {
831796
PyThread_free_lock(tables_lock);
832797
tables_lock = NULL;
833798
}
834-
#endif
835799

836-
#ifdef REENTRANT_THREADLOCAL
837800
PyThread_tss_delete(&tracemalloc_reentrant_key);
838-
#endif
839801
}
840802

841803

@@ -866,7 +828,6 @@ _PyTraceMalloc_Start(int max_nframe)
866828
}
867829

868830
PyMemAllocatorEx alloc;
869-
#ifdef TRACE_RAW_MALLOC
870831
alloc.malloc = tracemalloc_raw_malloc;
871832
alloc.calloc = tracemalloc_raw_calloc;
872833
alloc.realloc = tracemalloc_raw_realloc;
@@ -875,7 +836,6 @@ _PyTraceMalloc_Start(int max_nframe)
875836
alloc.ctx = &allocators.raw;
876837
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
877838
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
878-
#endif
879839

880840
alloc.malloc = tracemalloc_malloc_gil;
881841
alloc.calloc = tracemalloc_calloc_gil;
@@ -916,9 +876,7 @@ _PyTraceMalloc_Stop(void)
916876
tracemalloc_config.tracing = 0;
917877

918878
/* unregister the hook on memory allocators */
919-
#ifdef TRACE_RAW_MALLOC
920879
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
921-
#endif
922880
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
923881
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
924882

0 commit comments

Comments
 (0)
0