From 61949770511e6e4e3313403bd0e3548894eb34b6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Nov 2026 17:34:08 +0100 Subject: [PATCH] gh-162214: Avoid undefined macros in mimalloc Replace "#if MACRO" with "#ifdef MACRO" to avoid compiler warnings when the MACRO is not defined and building Python with -Wundef. Updated check on macros: * MI_HUGE_PAGE_ABANDON * MI_LARGE_PAGE_SIZE * MI_OSX_ZONE * MI_TLS_SLOT * MI_TRACK_ASAN * MI_TRACK_ETW * MI_TRACK_VALGRIND * MI_TSAN * _MSC_VER Fix typo: MI_SEGMENT_SLIZE_SIZE => MI_SEGMENT_SLICE_SIZE (Z => S). --- Include/internal/mimalloc/mimalloc/atomic.h | 2 +- Include/internal/mimalloc/mimalloc/internal.h | 2 +- Include/internal/mimalloc/mimalloc/prim.h | 2 +- Include/internal/mimalloc/mimalloc/track.h | 6 +++--- Include/internal/mimalloc/mimalloc/types.h | 4 ++-- Objects/mimalloc/alloc.c | 2 +- Objects/mimalloc/free.c | 8 ++++---- Objects/mimalloc/init.c | 2 +- Objects/mimalloc/options.c | 2 +- Objects/mimalloc/os.c | 2 +- Objects/mimalloc/page-queue.c | 2 +- Objects/mimalloc/page.c | 10 +++++----- Objects/mimalloc/segment-map.c | 2 +- Objects/mimalloc/segment.c | 4 ++-- Objects/mimalloc/static.c | 2 +- Objects/mimalloc/stats.c | 2 +- 16 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Include/internal/mimalloc/mimalloc/atomic.h b/Include/internal/mimalloc/mimalloc/atomic.h index b090e22c9d80c88..2810466a1a9ccaf 100644 --- a/Include/internal/mimalloc/mimalloc/atomic.h +++ b/Include/internal/mimalloc/mimalloc/atomic.h @@ -511,7 +511,7 @@ static inline void mi_atomic_yield_sleep( size_t* ticks, const size_t ticks_unti // These should be light-weight in-process only locks. // Only used for reserving arena's and to maintain the abandoned list. // ---------------------------------------------------------------------- -#if _MSC_VER +#if defined(_MSC_VER) #pragma warning(disable:26110) // unlock with holding lock #endif diff --git a/Include/internal/mimalloc/mimalloc/internal.h b/Include/internal/mimalloc/mimalloc/internal.h index 83bf6348a5561c3..a77a32482d75156 100644 --- a/Include/internal/mimalloc/mimalloc/internal.h +++ b/Include/internal/mimalloc/mimalloc/internal.h @@ -236,7 +236,7 @@ void _mi_segment_page_abandon(mi_page_t* page, mi_segments_tld_t* tld); bool _mi_segment_try_reclaim_abandoned( mi_heap_t* heap, bool try_all, mi_segments_tld_t* tld); void _mi_segment_collect(mi_segment_t* segment, bool force); -#if MI_HUGE_PAGE_ABANDON +#if defined(MI_HUGE_PAGE_ABANDON) void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block_t* block); #else void _mi_segment_huge_page_reset(mi_segment_t* segment, mi_page_t* page, mi_block_t* block); diff --git a/Include/internal/mimalloc/mimalloc/prim.h b/Include/internal/mimalloc/mimalloc/prim.h index fce93b132af2904..342bb378bc970d6 100644 --- a/Include/internal/mimalloc/mimalloc/prim.h +++ b/Include/internal/mimalloc/mimalloc/prim.h @@ -385,7 +385,7 @@ static inline mi_heap_t* mi_prim_get_default_heap(void); #endif -#if MI_TLS_SLOT +#ifdef MI_TLS_SLOT # if !defined(MI_HAS_TLS_SLOT) # error "trying to use a TLS slot for the default heap, but the mi_prim_tls_slot primitives are not defined" # endif diff --git a/Include/internal/mimalloc/mimalloc/track.h b/Include/internal/mimalloc/mimalloc/track.h index 9a12dabe0158942..54d16f82bf1f1b1 100644 --- a/Include/internal/mimalloc/mimalloc/track.h +++ b/Include/internal/mimalloc/mimalloc/track.h @@ -43,7 +43,7 @@ defined, undefined, or not accessible at all: -------------------------------------------------------------------------------------------------------*/ -#if MI_TRACK_VALGRIND +#if defined(MI_TRACK_VALGRIND) // valgrind tool #define MI_TRACK_ENABLED 1 @@ -60,7 +60,7 @@ defined, undefined, or not accessible at all: #define mi_track_mem_undefined(p,size) VALGRIND_MAKE_MEM_UNDEFINED(p,size) #define mi_track_mem_noaccess(p,size) VALGRIND_MAKE_MEM_NOACCESS(p,size) -#elif MI_TRACK_ASAN +#elif defined(MI_TRACK_ASAN) // address sanitizer #define MI_TRACK_ENABLED 1 @@ -75,7 +75,7 @@ defined, undefined, or not accessible at all: #define mi_track_mem_undefined(p,size) ASAN_UNPOISON_MEMORY_REGION(p,size) #define mi_track_mem_noaccess(p,size) ASAN_POISON_MEMORY_REGION(p,size) -#elif MI_TRACK_ETW +#elif defined(MI_TRACK_ETW) // windows event tracing #define MI_TRACK_ENABLED 1 diff --git a/Include/internal/mimalloc/mimalloc/types.h b/Include/internal/mimalloc/mimalloc/types.h index e5b9e844ceebf96..ef39656911aa212 100644 --- a/Include/internal/mimalloc/mimalloc/types.h +++ b/Include/internal/mimalloc/mimalloc/types.h @@ -228,7 +228,7 @@ typedef int32_t mi_ssize_t; // we never allocate more than PTRDIFF_MAX (see also ) // on 64-bit+ systems we also limit the maximum allocation size such that the slice count fits in 32-bits. (issue #877) -#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLIZE_SIZE * UINT32_MAX)) +#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLICE_SIZE * UINT32_MAX)) #define MI_MAX_ALLOC_SIZE (MI_SEGMENT_SLICE_SIZE * (UINT32_MAX-1)) #else #define MI_MAX_ALLOC_SIZE PTRDIFF_MAX @@ -270,7 +270,7 @@ typedef enum mi_delayed_e { // The `in_full` and `has_aligned` page flags are put in a union to efficiently // test if both are false (`full_aligned == 0`) in the `mi_free` routine. -#if !MI_TSAN +#if !defined(MI_TSAN) typedef union mi_page_flags_s { uint8_t full_aligned; struct { diff --git a/Objects/mimalloc/alloc.c b/Objects/mimalloc/alloc.c index 7d93547970b7971..0a51f05a292c789 100644 --- a/Objects/mimalloc/alloc.c +++ b/Objects/mimalloc/alloc.c @@ -86,7 +86,7 @@ extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_ } } - #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN + #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !defined(MI_TSAN) if (!zero && !mi_page_is_huge(page)) { mi_debug_fill(page, block, MI_DEBUG_UNINIT, mi_page_usable_block_size(page)); } diff --git a/Objects/mimalloc/free.c b/Objects/mimalloc/free.c index ba8d7682414ba99..86d098ae1a310c6 100644 --- a/Objects/mimalloc/free.c +++ b/Objects/mimalloc/free.c @@ -35,7 +35,7 @@ static inline void mi_free_block_local(mi_page_t* page, mi_block_t* block, bool if mi_unlikely(mi_check_is_double_free(page, block)) return; if (!was_guarded) { mi_check_padding(page, block); } if (track_stats) { mi_stat_free(page, block); } - #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN + #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !defined(MI_TSAN) if (!mi_page_is_huge(page)) { // huge page content may be already decommitted size_t dbgsize = mi_page_block_size(page); if (dbgsize > 1*MI_MiB) { dbgsize = 1*MI_MiB; } mi_debug_fill(page, block, MI_DEBUG_FREED, dbgsize); @@ -302,7 +302,7 @@ static void mi_decl_noinline mi_free_block_mt(mi_page_t* page, mi_segment_t* seg // first see if the segment was abandoned and if we can reclaim it into our thread if (_mi_option_get_fast(mi_option_abandoned_reclaim_on_free) != 0 && - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON segment->page_kind != MI_PAGE_HUGE && #endif mi_atomic_load_relaxed(&segment->thread_id) == 0 && // segment is abandoned? @@ -329,7 +329,7 @@ static void mi_decl_noinline mi_free_block_mt(mi_page_t* page, mi_segment_t* seg _mi_padding_shrink(page, block, sizeof(mi_block_t)); if (segment->kind == MI_SEGMENT_HUGE) { - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON // huge page segments are always abandoned and can be freed immediately _mi_segment_huge_page_free(segment, page, block); return; @@ -341,7 +341,7 @@ static void mi_decl_noinline mi_free_block_mt(mi_page_t* page, mi_segment_t* seg #endif } else { - #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN // note: when tracking, cannot use mi_usable_size with multi-threading + #if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !defined(MI_TSAN) // note: when tracking, cannot use mi_usable_size with multi-threading size_t dbgsize = mi_usable_size(block); if (dbgsize > 1*MI_MiB) { dbgsize = 1*MI_MiB; } mi_debug_fill(page, block, MI_DEBUG_FREED, dbgsize); #endif diff --git a/Objects/mimalloc/init.c b/Objects/mimalloc/init.c index d5157351d99eaad..6bbbfc4da9dad64 100644 --- a/Objects/mimalloc/init.c +++ b/Objects/mimalloc/init.c @@ -647,7 +647,7 @@ static void mi_process_init_once(void) mi_attr_noexcept { // Initialize the process; called by thread_init or the process loader void mi_process_init(void) mi_attr_noexcept { - #if _MSC_VER < 1920 + #if defined(_MSC_VER) && _MSC_VER < 1920 mi_heap_main_init(); // vs2017 can dynamically re-initialize _mi_heap_main #endif mi_atomic_do_once { diff --git a/Objects/mimalloc/options.c b/Objects/mimalloc/options.c index d139e57f2c8f9cd..419e6ed5cc08566 100644 --- a/Objects/mimalloc/options.c +++ b/Objects/mimalloc/options.c @@ -236,7 +236,7 @@ void mi_options_print(void) mi_attr_noexcept #if MI_GUARDED _mi_message("guarded build: %s\n", mi_option_get(mi_option_guarded_sample_rate) != 0 ? "enabled" : "disabled"); #endif - #if MI_TSAN + #ifdef MI_TSAN _mi_message("thread santizer enabled\n"); #endif } diff --git a/Objects/mimalloc/os.c b/Objects/mimalloc/os.c index b6e2ecbbd080e2d..8b52fb586da2af7 100644 --- a/Objects/mimalloc/os.c +++ b/Objects/mimalloc/os.c @@ -279,7 +279,7 @@ static void* mi_os_prim_alloc_aligned(size_t size, size_t alignment, bool commit } else { // if not aligned, free it, overallocate, and unmap around it - #if !MI_TRACK_ASAN + #if !defined(MI_TRACK_ASAN) // CPython: downgraded from _mi_warning_message, the warning is noisy in the test suite (gh-112027) _mi_verbose_message("unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x%zx bytes, address: %p, alignment: 0x%zx, commit: %d)\n", size, p, alignment, commit); #endif diff --git a/Objects/mimalloc/page-queue.c b/Objects/mimalloc/page-queue.c index bfa69870c968e54..48497854002975d 100644 --- a/Objects/mimalloc/page-queue.c +++ b/Objects/mimalloc/page-queue.c @@ -253,7 +253,7 @@ static bool mi_page_queue_is_consistent(const mi_page_queue_t* queue); static void mi_page_queue_push(mi_heap_t* heap, mi_page_queue_t* queue, mi_page_t* page) { mi_assert_internal(mi_page_heap(page) == heap); mi_assert_internal(!mi_page_queue_contains(queue, page)); - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON mi_assert_internal(_mi_page_segment(page)->kind != MI_SEGMENT_HUGE); #endif mi_assert_internal(mi_page_block_size(page) == queue->block_size || diff --git a/Objects/mimalloc/page.c b/Objects/mimalloc/page.c index 2f26d04aaaa4d87..7904cb2937fcc33 100644 --- a/Objects/mimalloc/page.c +++ b/Objects/mimalloc/page.c @@ -297,7 +297,7 @@ void _mi_page_reclaim(mi_heap_t* heap, mi_page_t* page) { mi_assert_internal(mi_page_heap(page) == heap); mi_assert_internal(mi_page_thread_free_flag(page) != MI_NEVER_DELAYED_FREE); - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON mi_assert_internal(_mi_page_segment(page)->kind != MI_SEGMENT_HUGE); #endif @@ -310,7 +310,7 @@ void _mi_page_reclaim(mi_heap_t* heap, mi_page_t* page) { // allocate a fresh page from a segment static mi_page_t* mi_page_fresh_alloc(mi_heap_t* heap, mi_page_queue_t* pq, size_t block_size, size_t page_alignment) { - #if !MI_HUGE_PAGE_ABANDON + #if !defined(MI_HUGE_PAGE_ABANDON) mi_assert_internal(pq != NULL); mi_assert_internal(mi_heap_contains_queue(heap, pq)); mi_assert_internal(page_alignment > 0 || block_size > MI_MEDIUM_OBJ_SIZE_MAX || block_size == pq->block_size); @@ -320,7 +320,7 @@ static mi_page_t* mi_page_fresh_alloc(mi_heap_t* heap, mi_page_queue_t* pq, size // this may be out-of-memory, or an abandoned page was reclaimed (and in our queue) return NULL; } - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON mi_assert_internal(pq==NULL || _mi_page_segment(page)->page_kind != MI_PAGE_HUGE); #endif mi_assert_internal(page_alignment >0 || block_size > MI_MEDIUM_OBJ_SIZE_MAX || _mi_page_segment(page)->kind != MI_SEGMENT_HUGE); @@ -994,7 +994,7 @@ static mi_page_t* mi_large_huge_page_alloc(mi_heap_t* heap, size_t size, size_t size_t block_size = _mi_os_good_alloc_size(size); mi_assert_internal(mi_bin(block_size) == MI_BIN_HUGE || page_alignment > 0); bool is_huge = (block_size > MI_LARGE_OBJ_SIZE_MAX || page_alignment > 0); - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON mi_page_queue_t* pq = (is_huge ? NULL : mi_page_queue(heap, block_size)); #else mi_page_queue_t* pq = mi_page_queue(heap, is_huge ? MI_LARGE_OBJ_SIZE_MAX+1 : block_size); @@ -1008,7 +1008,7 @@ static mi_page_t* mi_large_huge_page_alloc(mi_heap_t* heap, size_t size, size_t mi_assert_internal(mi_page_is_huge(page)); mi_assert_internal(_mi_page_segment(page)->kind == MI_SEGMENT_HUGE); mi_assert_internal(_mi_page_segment(page)->used==1); - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON mi_assert_internal(_mi_page_segment(page)->thread_id==0); // abandoned, not in the huge queue mi_page_set_heap(page, NULL); #endif diff --git a/Objects/mimalloc/segment-map.c b/Objects/mimalloc/segment-map.c index a40c3fe1bf4773c..3d0e8feafeb734e 100644 --- a/Objects/mimalloc/segment-map.c +++ b/Objects/mimalloc/segment-map.c @@ -17,7 +17,7 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc/atomic.h" // Reduce total address space to reduce .bss (due to the `mi_segment_map`) -#if (MI_INTPTR_SIZE > 4) && MI_TRACK_ASAN +#if (MI_INTPTR_SIZE > 4) && defined(MI_TRACK_ASAN) #define MI_SEGMENT_MAP_MAX_ADDRESS (128*1024ULL*MI_GiB) // 128 TiB (see issue #881) #elif (MI_INTPTR_SIZE > 4) #define MI_SEGMENT_MAP_MAX_ADDRESS (48*1024ULL*MI_GiB) // 48 TiB diff --git a/Objects/mimalloc/segment.c b/Objects/mimalloc/segment.c index 660beb325ee8f8f..1c1c875bdc146c8 100644 --- a/Objects/mimalloc/segment.c +++ b/Objects/mimalloc/segment.c @@ -1597,7 +1597,7 @@ static mi_page_t* mi_segment_huge_page_alloc(size_t size, size_t page_alignment, if (segment == NULL || page==NULL) return NULL; mi_assert_internal(segment->used==1); mi_assert_internal(mi_page_block_size(page) >= size); - #if MI_HUGE_PAGE_ABANDON + #ifdef MI_HUGE_PAGE_ABANDON segment->thread_id = 0; // huge segments are immediately abandoned #endif @@ -1624,7 +1624,7 @@ static mi_page_t* mi_segment_huge_page_alloc(size_t size, size_t page_alignment, return page; } -#if MI_HUGE_PAGE_ABANDON +#ifdef MI_HUGE_PAGE_ABANDON // free huge block from another thread void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block_t* block) { // huge page segments are always abandoned and can be freed immediately by any thread diff --git a/Objects/mimalloc/static.c b/Objects/mimalloc/static.c index 9e06ce05aaa5bbc..f4c0bcd0567559d 100644 --- a/Objects/mimalloc/static.c +++ b/Objects/mimalloc/static.c @@ -36,6 +36,6 @@ terms of the MIT license. A copy of the license can be found in the file #include "segment-map.c" #include "stats.c" #include "prim/prim.c" -#if MI_OSX_ZONE +#ifdef MI_OSX_ZONE #include "prim/osx/alloc-override-zone.c" #endif diff --git a/Objects/mimalloc/stats.c b/Objects/mimalloc/stats.c index 70d366e26c074ef..dd28cfc254f0171 100644 --- a/Objects/mimalloc/stats.c +++ b/Objects/mimalloc/stats.c @@ -536,7 +536,7 @@ static void mi_heap_buf_print_count_bin(mi_heap_buf_t* hbuf, const char* prefix, const size_t binsize = _mi_bin_size(bin); const size_t pagesize = (binsize <= MI_SMALL_OBJ_SIZE_MAX ? MI_SMALL_PAGE_SIZE : (binsize <= MI_MEDIUM_OBJ_SIZE_MAX ? MI_MEDIUM_PAGE_SIZE : - #if MI_LARGE_PAGE_SIZE + #ifdef MI_LARGE_PAGE_SIZE (binsize <= MI_LARGE_OBJ_SIZE_MAX ? MI_LARGE_PAGE_SIZE : 0) #else 0