]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129185: Remove internal TRACE_RAW_MALLOC macro (#129218)
authorVictor Stinner <vstinner@python.org>
Thu, 23 Jan 2025 12:49:35 +0000 (13:49 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Jan 2025 12:49:35 +0000 (13:49 +0100)
Always build tracemalloc with PyMem_RawMalloc() hooks.

Include/internal/pycore_tracemalloc.h
Python/tracemalloc.c

index 9fed393ac9e60efa3b4cdf29b5b2577a33f5f2dc..3fb0aa133fc41db4dcaf42df30e367a699729ce6 100644 (file)
@@ -11,10 +11,6 @@ extern "C" {
 #include "pycore_hashtable.h"     // _Py_hashtable_t
 
 
-/* Trace memory blocks allocated by PyMem_RawMalloc() */
-#define TRACE_RAW_MALLOC
-
-
 struct _PyTraceMalloc_Config {
     /* Module initialized?
        Variable protected by the GIL */
@@ -74,9 +70,7 @@ struct _tracemalloc_runtime_state {
         PyMemAllocatorEx obj;
     } allocators;
 
-#if defined(TRACE_RAW_MALLOC)
     PyThread_type_lock tables_lock;
-#endif
     /* Size in bytes of currently traced memory.
        Protected by TABLES_LOCK(). */
     size_t traced_memory;
index 62065e85ac835071ca830c09ef8d1d9b269548ee..20651016a80ec205d1840865fc9105ad0ac79a3c 100644 (file)
@@ -33,18 +33,12 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
 #define allocators _PyRuntime.tracemalloc.allocators
 
 
-#if defined(TRACE_RAW_MALLOC)
 /* This lock is needed because tracemalloc_free() is called without
    the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
    would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
-#  define tables_lock _PyRuntime.tracemalloc.tables_lock
-#  define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
-#  define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
-#else
-   /* variables are protected by the GIL */
-#  define TABLES_LOCK()
-#  define TABLES_UNLOCK()
-#endif
+#define tables_lock _PyRuntime.tracemalloc.tables_lock
+#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
+#define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
 
 
 #define DEFAULT_DOMAIN 0
@@ -98,9 +92,6 @@ tracemalloc_error(const char *format, ...)
 #endif
 
 
-#if defined(TRACE_RAW_MALLOC)
-#define REENTRANT_THREADLOCAL
-
 #define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key
 
 /* Any non-NULL pointer can be used */
@@ -137,25 +128,6 @@ set_reentrant(int reentrant)
     }
 }
 
-#else
-
-/* TRACE_RAW_MALLOC not defined: variable protected by the GIL */
-static int tracemalloc_reentrant = 0;
-
-static int
-get_reentrant(void)
-{
-    return tracemalloc_reentrant;
-}
-
-static void
-set_reentrant(int reentrant)
-{
-    assert(reentrant != tracemalloc_reentrant);
-    tracemalloc_reentrant = reentrant;
-}
-#endif
-
 
 static Py_uhash_t
 hashtable_hash_pyobject(const void *key)
@@ -709,7 +681,6 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
 }
 
 
-#ifdef TRACE_RAW_MALLOC
 static void*
 tracemalloc_raw_malloc(void *ctx, size_t size)
 {
@@ -729,7 +700,6 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
 {
     return tracemalloc_realloc(1, ctx, ptr, new_size);
 }
-#endif   /* TRACE_RAW_MALLOC */
 
 
 static void
@@ -767,20 +737,16 @@ _PyTraceMalloc_Init(void)
 
     PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
 
-#ifdef REENTRANT_THREADLOCAL
     if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
         return _PyStatus_NO_MEMORY();
     }
-#endif
 
-#if defined(TRACE_RAW_MALLOC)
     if (tables_lock == NULL) {
         tables_lock = PyThread_allocate_lock();
         if (tables_lock == NULL) {
             return _PyStatus_NO_MEMORY();
         }
     }
-#endif
 
     tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
                                           hashtable_compare_unicode,
@@ -826,16 +792,12 @@ tracemalloc_deinit(void)
     _Py_hashtable_destroy(tracemalloc_tracebacks);
     _Py_hashtable_destroy(tracemalloc_filenames);
 
-#if defined(TRACE_RAW_MALLOC)
     if (tables_lock != NULL) {
         PyThread_free_lock(tables_lock);
         tables_lock = NULL;
     }
-#endif
 
-#ifdef REENTRANT_THREADLOCAL
     PyThread_tss_delete(&tracemalloc_reentrant_key);
-#endif
 }
 
 
@@ -866,7 +828,6 @@ _PyTraceMalloc_Start(int max_nframe)
     }
 
     PyMemAllocatorEx alloc;
-#ifdef TRACE_RAW_MALLOC
     alloc.malloc = tracemalloc_raw_malloc;
     alloc.calloc = tracemalloc_raw_calloc;
     alloc.realloc = tracemalloc_raw_realloc;
@@ -875,7 +836,6 @@ _PyTraceMalloc_Start(int max_nframe)
     alloc.ctx = &allocators.raw;
     PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
-#endif
 
     alloc.malloc = tracemalloc_malloc_gil;
     alloc.calloc = tracemalloc_calloc_gil;
@@ -916,9 +876,7 @@ _PyTraceMalloc_Stop(void)
     tracemalloc_config.tracing = 0;
 
     /* unregister the hook on memory allocators */
-#ifdef TRACE_RAW_MALLOC
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
-#endif
     PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
     PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);