Skip to content

Commit 83ea621

Browse files
authored
gh-162214: Backport mimalloc changes to fix -Wundef warnings (#163074)
* commit 31765b252c42922e5dd5dd1fa8038147eec0de87: Backport applicable macro and configuration fixes (#1506) * commit 256e4f7b8ef6a6b628e49df430746e69a34dd956: Fix slice-size typo and CMake configuration checks (#1506) * commit cd9251e9280905dc485a713d1e0fc7293a4460e0: Make public headers -Wundef-safe and suppress it in static.c, see also #1506 * commit 0fe41040c07b2adc346801f2a520a3299f63615e: make internal headers -Wundef clean and include limits.h in types.h (see #1506)
1 parent 5639e5d commit 83ea621

11 files changed

Lines changed: 69 additions & 29 deletions

File tree

‎Include/internal/mimalloc/mimalloc.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ terms of the MIT license. A copy of the license can be found in the file
1515
// ------------------------------------------------------
1616

1717
#ifdef __cplusplus
18-
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
18+
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900) // C++11
1919
#define mi_attr_noexcept noexcept
2020
#else
2121
#define mi_attr_noexcept throw()
@@ -30,7 +30,7 @@ terms of the MIT license. A copy of the license can be found in the file
3030
#define mi_decl_nodiscard __attribute__((warn_unused_result))
3131
#elif defined(_HAS_NODISCARD)
3232
#define mi_decl_nodiscard _NODISCARD
33-
#elif (_MSC_VER >= 1700)
33+
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
3434
#define mi_decl_nodiscard _Check_return_
3535
#else
3636
#define mi_decl_nodiscard
@@ -513,7 +513,7 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_heap_alloc_new_n(mi_h
513513

514514
#include <cstddef> // std::size_t
515515
#include <cstdint> // PTRDIFF_MAX
516-
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
516+
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900) // C++11
517517
#include <type_traits> // std::true_type
518518
#include <utility> // std::forward
519519
#endif
@@ -527,7 +527,7 @@ template<class T> struct _mi_stl_allocator_common {
527527
typedef value_type* pointer;
528528
typedef value_type const* const_pointer;
529529

530-
#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
530+
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
531531
using propagate_on_container_copy_assignment = std::true_type;
532532
using propagate_on_container_move_assignment = std::true_type;
533533
using propagate_on_container_swap = std::true_type;
@@ -562,7 +562,7 @@ template<class T> struct mi_stl_allocator : public _mi_stl_allocator_common<T> {
562562
mi_decl_nodiscard pointer allocate(size_type count, const void* = 0) { return static_cast<pointer>(mi_new_n(count, sizeof(value_type))); }
563563
#endif
564564

565-
#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
565+
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
566566
using is_always_equal = std::true_type;
567567
#endif
568568
};
@@ -571,7 +571,7 @@ template<class T1,class T2> bool operator==(const mi_stl_allocator<T1>& , const
571571
template<class T1,class T2> bool operator!=(const mi_stl_allocator<T1>& , const mi_stl_allocator<T2>& ) mi_attr_noexcept { return false; }
572572

573573

574-
#if (__cplusplus >= 201103L) || (_MSC_VER >= 1900) // C++11
574+
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900) // C++11
575575
#define MI_HAS_HEAP_STL_ALLOCATOR 1
576576

577577
#include <memory> // std::shared_ptr
@@ -591,7 +591,7 @@ template<class T, bool _mi_destroy> struct _mi_heap_stl_allocator_common : publi
591591
mi_decl_nodiscard pointer allocate(size_type count, const void* = 0) { return static_cast<pointer>(mi_heap_alloc_new_n(this->heap.get(), count, sizeof(value_type))); }
592592
#endif
593593

594-
#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
594+
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
595595
using is_always_equal = std::false_type;
596596
#endif
597597

‎Include/internal/mimalloc/mimalloc/atomic.h‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ terms of the MIT license. A copy of the license can be found in the file
1414
#define WIN32_LEAN_AND_MEAN
1515
#endif
1616
#include <windows.h>
17-
#elif !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
18-
#define MI_USE_PTHREADS
17+
#endif
18+
#if !defined(MI_USE_PTHREADS)
19+
#if !defined(_WIN32) && !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
20+
#define MI_USE_PTHREADS 1
21+
#else
22+
#define MI_USE_PTHREADS 0
23+
#endif
24+
#endif
25+
#if MI_USE_PTHREADS
1926
#include <pthread.h>
2027
#endif
2128

@@ -511,7 +518,7 @@ static inline void mi_atomic_yield_sleep( size_t* ticks, const size_t ticks_unti
511518
// These should be light-weight in-process only locks.
512519
// Only used for reserving arena's and to maintain the abandoned list.
513520
// ----------------------------------------------------------------------
514-
#if _MSC_VER
521+
#if defined(_MSC_VER)
515522
#pragma warning(disable:26110) // unlock with holding lock
516523
#endif
517524

@@ -543,7 +550,7 @@ static inline void mi_lock_done(mi_lock_t* lock) {
543550
(void)(lock);
544551
}
545552

546-
#elif defined(MI_USE_PTHREADS)
553+
#elif MI_USE_PTHREADS
547554

548555
#include <string.h> // memcpy
549556
void _mi_error_message(int err, const char* fmt, ...);

‎Include/internal/mimalloc/mimalloc/internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void _mi_segment_page_abandon(mi_page_t* page, mi_segments_tld_t* tld);
236236
bool _mi_segment_try_reclaim_abandoned( mi_heap_t* heap, bool try_all, mi_segments_tld_t* tld);
237237
void _mi_segment_collect(mi_segment_t* segment, bool force);
238238

239-
#if MI_HUGE_PAGE_ABANDON
239+
#if defined(MI_HUGE_PAGE_ABANDON) && MI_HUGE_PAGE_ABANDON
240240
void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);
241241
#else
242242
void _mi_segment_huge_page_reset(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);

‎Include/internal/mimalloc/mimalloc/prim.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ static inline void* mi_prim_tls_slot(size_t slot) mi_attr_noexcept {
183183
__asm__ volatile ("mv %0, tp" : "=r" (tcb));
184184
res = tcb[slot];
185185
#elif defined(__APPLE__) && defined(__POWERPC__) // ppc, issue #781
186+
#if !MI_USE_PTHREADS
187+
#error PowerPC TLS slots require MI_USE_PTHREADS
188+
#endif
186189
MI_UNUSED(ofs);
187190
res = pthread_getspecific(slot);
188191
#endif
@@ -405,6 +408,10 @@ static inline mi_heap_t* mi_prim_get_default_heap(void) {
405408

406409
#elif defined(MI_TLS_PTHREAD_SLOT_OFS)
407410

411+
#if !MI_USE_PTHREADS
412+
#error MI_TLS_PTHREAD_SLOT_OFS requires MI_USE_PTHREADS
413+
#endif
414+
408415
static inline mi_heap_t** mi_prim_tls_pthread_heap_slot(void) {
409416
pthread_t self = pthread_self();
410417
#if defined(__DragonFly__)
@@ -423,6 +430,10 @@ static inline mi_heap_t* mi_prim_get_default_heap(void) {
423430

424431
#elif defined(MI_TLS_PTHREAD)
425432

433+
#if !MI_USE_PTHREADS
434+
#error MI_TLS_PTHREAD requires MI_USE_PTHREADS
435+
#endif
436+
426437
extern mi_decl_hidden pthread_key_t _mi_heap_default_key;
427438
static inline mi_heap_t* mi_prim_get_default_heap(void) {
428439
mi_heap_t* heap = (mi_unlikely(_mi_heap_default_key == (pthread_key_t)(-1)) ? _mi_heap_main_get() : (mi_heap_t*)pthread_getspecific(_mi_heap_default_key));

‎Include/internal/mimalloc/mimalloc/track.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defined, undefined, or not accessible at all:
4343
4444
-------------------------------------------------------------------------------------------------------*/
4545

46-
#if MI_TRACK_VALGRIND
46+
#if defined(MI_TRACK_VALGRIND) && MI_TRACK_VALGRIND
4747
// valgrind tool
4848

4949
#define MI_TRACK_ENABLED 1
@@ -60,7 +60,7 @@ defined, undefined, or not accessible at all:
6060
#define mi_track_mem_undefined(p,size) VALGRIND_MAKE_MEM_UNDEFINED(p,size)
6161
#define mi_track_mem_noaccess(p,size) VALGRIND_MAKE_MEM_NOACCESS(p,size)
6262

63-
#elif MI_TRACK_ASAN
63+
#elif defined(MI_TRACK_ASAN) && MI_TRACK_ASAN
6464
// address sanitizer
6565

6666
#define MI_TRACK_ENABLED 1
@@ -75,7 +75,7 @@ defined, undefined, or not accessible at all:
7575
#define mi_track_mem_undefined(p,size) ASAN_UNPOISON_MEMORY_REGION(p,size)
7676
#define mi_track_mem_noaccess(p,size) ASAN_POISON_MEMORY_REGION(p,size)
7777

78-
#elif MI_TRACK_ETW
78+
#elif defined(MI_TRACK_ETW) && MI_TRACK_ETW
7979
// windows event tracing
8080

8181
#define MI_TRACK_ENABLED 1

‎Include/internal/mimalloc/mimalloc/types.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ terms of the MIT license. A copy of the license can be found in the file
2626
#include <stddef.h> // ptrdiff_t
2727
#include <stdint.h> // uintptr_t, uint16_t, etc
2828
#include <stdbool.h> // bool
29+
#include <limits.h> // LONG_MAX
2930
#include "atomic.h" // _Atomic
3031

3132
#ifdef _MSC_VER
@@ -83,9 +84,12 @@ terms of the MIT license. A copy of the license can be found in the file
8384

8485
// Reserve extra padding at the end of each block to be more resilient against heap block overflows.
8586
// The padding can detect buffer overflow on free.
86-
#if !defined(MI_PADDING) && (MI_SECURE>=3 || MI_DEBUG>=1 || (MI_TRACK_VALGRIND || MI_TRACK_ASAN || MI_TRACK_ETW))
87+
#if !defined(MI_PADDING) && (MI_SECURE>=3 || MI_DEBUG>=1 || (defined(MI_TRACK_VALGRIND) && MI_TRACK_VALGRIND) || (defined(MI_TRACK_ASAN) && MI_TRACK_ASAN) || (defined(MI_TRACK_ETW) && MI_TRACK_ETW))
8788
#define MI_PADDING 1
8889
#endif
90+
#if !defined(MI_PADDING)
91+
#define MI_PADDING 0
92+
#endif
8993

9094
// Check padding bytes; allows byte-precise buffer overflow detection
9195
#if !defined(MI_PADDING_CHECK) && MI_PADDING && (MI_SECURE>=3 || MI_DEBUG>=1)
@@ -99,7 +103,7 @@ terms of the MIT license. A copy of the license can be found in the file
99103
#define MI_ENCODE_FREELIST 1
100104
#endif
101105

102-
#if (MI_ENCODE_FREELIST && (MI_SECURE>=4 || MI_DEBUG!=0))
106+
#if defined(MI_ENCODE_FREELIST) && (MI_SECURE>=4 || MI_DEBUG!=0)
103107
#define MI_CHECK_DOUBLE_FREE 1
104108
#endif
105109

@@ -228,7 +232,7 @@ typedef int32_t mi_ssize_t;
228232

229233
// we never allocate more than PTRDIFF_MAX (see also <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
230234
// on 64-bit+ systems we also limit the maximum allocation size such that the slice count fits in 32-bits. (issue #877)
231-
#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLIZE_SIZE * UINT32_MAX))
235+
#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLICE_SIZE * UINT32_MAX))
232236
#define MI_MAX_ALLOC_SIZE (MI_SEGMENT_SLICE_SIZE * (UINT32_MAX-1))
233237
#else
234238
#define MI_MAX_ALLOC_SIZE PTRDIFF_MAX
@@ -270,7 +274,7 @@ typedef enum mi_delayed_e {
270274

271275
// The `in_full` and `has_aligned` page flags are put in a union to efficiently
272276
// test if both are false (`full_aligned == 0`) in the `mi_free` routine.
273-
#if !MI_TSAN
277+
#if !(defined(MI_TSAN) && MI_TSAN)
274278
typedef union mi_page_flags_s {
275279
uint8_t full_aligned;
276280
struct {
@@ -347,7 +351,7 @@ typedef struct mi_page_s {
347351
size_t block_size; // size available in each block (always `>0`)
348352
uint8_t* page_start; // start of the page area containing the blocks
349353

350-
#if (MI_ENCODE_FREELIST || MI_PADDING)
354+
#if (defined(MI_ENCODE_FREELIST) || MI_PADDING)
351355
uintptr_t keys[2]; // two random keys to encode the free lists (see `_mi_block_next`) or padding canary
352356
#endif
353357

‎Objects/mimalloc/prim/emscripten/prim.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
207207
// Thread init/done
208208
//----------------------------------------------------------------
209209

210-
#if defined(MI_USE_PTHREADS)
210+
#if MI_USE_PTHREADS
211211

212212
// use pthread local storage keys to detect thread ending
213213
// (and used with MI_TLS_PTHREADS for the default heap)

‎Objects/mimalloc/prim/unix/prim.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,13 @@ void _mi_prim_out_stderr( const char* msg ) {
804804
#if !defined(MI_USE_ENVIRON) || (MI_USE_ENVIRON!=0)
805805
// On Posix systemsr use `environ` to access environment variables
806806
// even before the C runtime is initialized.
807-
#if defined(__APPLE__) && defined(__has_include) && __has_include(<crt_externs.h>)
807+
#if defined(__APPLE__) && defined(__has_include)
808+
#if __has_include(<crt_externs.h>)
808809
#include <crt_externs.h>
810+
#define MI_UNIX_HAS_CRT_EXTERNS_H 1
811+
#endif
812+
#endif
813+
#if MI_UNIX_HAS_CRT_EXTERNS_H
809814
static char** mi_get_environ(void) {
810815
return (*_NSGetEnviron());
811816
}
@@ -939,7 +944,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
939944
// Thread init/done
940945
//----------------------------------------------------------------
941946

942-
#if defined(MI_USE_PTHREADS)
947+
#if MI_USE_PTHREADS
943948

944949
// use pthread local storage keys to detect thread ending
945950
// (and used with MI_TLS_PTHREADS for the default heap)

‎Objects/mimalloc/prim/wasi/prim.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ int _mi_prim_free(void* addr, size_t size ) {
6161
}
6262
#endif
6363

64-
#if defined(MI_USE_PTHREADS)
64+
#if MI_USE_PTHREADS
6565
static pthread_mutex_t mi_heap_grow_mutex = PTHREAD_MUTEX_INITIALIZER;
6666
#endif
6767

6868
static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
6969
void* p = NULL;
7070
if (try_alignment <= 1) {
7171
// `sbrk` is not thread safe in general so try to protect it (we could skip this on WASM but leave it in for now)
72-
#if defined(MI_USE_PTHREADS)
72+
#if MI_USE_PTHREADS
7373
pthread_mutex_lock(&mi_heap_grow_mutex);
7474
#endif
7575
p = mi_memory_grow(size);
76-
#if defined(MI_USE_PTHREADS)
76+
#if MI_USE_PTHREADS
7777
pthread_mutex_unlock(&mi_heap_grow_mutex);
7878
#endif
7979
}
@@ -83,7 +83,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
8383
// to allocate aligned use a lock to try to avoid thread interaction
8484
// between getting the current size and actual allocation
8585
// (also, `sbrk` is not thread safe in general)
86-
#if defined(MI_USE_PTHREADS)
86+
#if MI_USE_PTHREADS
8787
pthread_mutex_lock(&mi_heap_grow_mutex);
8888
#endif
8989
{
@@ -94,7 +94,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
9494
base = mi_memory_grow(alloc_size);
9595
}
9696
}
97-
#if defined(MI_USE_PTHREADS)
97+
#if MI_USE_PTHREADS
9898
pthread_mutex_unlock(&mi_heap_grow_mutex);
9999
#endif
100100
if (base != NULL) {

‎Objects/mimalloc/prim/windows/prim.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ terms of the MIT license. A copy of the license can be found in the file
1414
#include <stdlib.h> // atexit
1515

1616
// xbox has no console IO and cannot use LoadLibrary or GetModuleHandle
17-
#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
17+
#if !defined(WINAPI_FAMILY_PARTITION)
18+
#define MI_WIN_DESKTOP 1
19+
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
1820
#define MI_WIN_DESKTOP 1
1921
#endif
2022

0 commit comments

Comments
 (0)