Skip to content

Commit a83bca7

Browse files
committed
fixes for M1; disable interpose use zones; fix pedantic warnings
1 parent c8b5b74 commit a83bca7

File tree

6 files changed

+48
-51
lines changed

6 files changed

+48
-51
lines changed

CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ option(MI_XMALLOC "Enable abort() call on memory allocation failure by
1212
option(MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF)
1313
option(MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF)
1414
option(MI_SEE_ASM "Generate assembly files" OFF)
15-
option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON)
16-
option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" OFF) # enables interpose as well
15+
option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" OFF)
16+
option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON)
1717
option(MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF)
1818
option(MI_BUILD_SHARED "Build shared library" ON)
1919
option(MI_BUILD_STATIC "Build static library" ON)
@@ -76,15 +76,12 @@ if(MI_OVERRIDE)
7676
# use zone's on macOS
7777
message(STATUS " Use malloc zone to override malloc (MI_OSX_ZONE=ON)")
7878
list(APPEND mi_sources src/alloc-override-osx.c)
79-
list(APPEND mi_defines MI_OSX_ZONE=1)
80-
if(NOT MI_INTERPOSE)
81-
message(STATUS " (enabling INTERPOSE as well since zone's require this)")
82-
set(MI_INTERPOSE "ON")
83-
endif()
79+
list(APPEND mi_defines MI_OSX_ZONE=1)
8480
endif()
8581
if(MI_INTERPOSE)
8682
# use interpose on macOS
8783
message(STATUS " Use interpose to override malloc (MI_INTERPOSE=ON)")
84+
message(STATUS " WARNING: interpose does not seem to work reliably on the M1; use -DMI_OSX_ZONE=ON instead")
8885
list(APPEND mi_defines MI_INTERPOSE)
8986
endif()
9087
endif()

include/mimalloc-atomic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static inline void mi_atomic_yield(void) {
295295
}
296296
#elif defined(__aarch64__)
297297
static inline void mi_atomic_yield(void) {
298-
asm volatile("wfe");
298+
__asm__ volatile("wfe");
299299
}
300300
#elif (defined(__arm__) && __ARM_ARCH__ >= 7)
301301
static inline void mi_atomic_yield(void) {
@@ -307,7 +307,7 @@ static inline void mi_atomic_yield(void) {
307307
}
308308
#elif defined(__armel__) || defined(__ARMEL__)
309309
static inline void mi_atomic_yield(void) {
310-
asm volatile ("nop" ::: "memory");
310+
__asm__ volatile ("nop" ::: "memory");
311311
}
312312
#endif
313313
#elif defined(__sun)

include/mimalloc-internal.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ static inline void* mi_tls_slot(size_t slot) mi_attr_noexcept {
713713
void** tcb; UNUSED(ofs);
714714
#if defined(__APPLE__) // M1, issue #343
715715
__asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb));
716+
tcb = (void**)((uintptr_t)tcb & ~0x07UL); // clear lower 3 bits
716717
#else
717718
__asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb));
718719
#endif
@@ -740,6 +741,7 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept {
740741
void** tcb; UNUSED(ofs);
741742
#if defined(__APPLE__) // M1, issue #343
742743
__asm__ volatile ("mrs %0, tpidrro_el0" : "=r" (tcb));
744+
tcb = (void**)((uintptr_t)tcb & ~0x07UL); // clear lower 3 bits
743745
#else
744746
__asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tcb));
745747
#endif
@@ -748,10 +750,7 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept {
748750
}
749751

750752
static inline uintptr_t _mi_thread_id(void) mi_attr_noexcept {
751-
#if defined(__aarch64__) && defined(__APPLE__) // M1
752-
// on macOS on the M1, slot 0 does not seem to work, so we fall back to portable C for now. See issue #354
753-
return (uintptr_t)&_mi_heap_default;
754-
#elif defined(__BIONIC__) && (defined(__arm__) || defined(__aarch64__))
753+
#if defined(__BIONIC__) && (defined(__arm__) || defined(__aarch64__))
755754
// on Android, slot 1 is the thread ID (pointer to pthread internal struct)
756755
return (uintptr_t)mi_tls_slot(1);
757756
#else

src/alloc-override-osx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void* zone_valloc(malloc_zone_t* zone, size_t size) {
6464

6565
static void zone_free(malloc_zone_t* zone, void* p) {
6666
UNUSED(zone);
67-
return mi_free(p);
67+
mi_free(p);
6868
}
6969

7070
static void* zone_realloc(malloc_zone_t* zone, void* p, size_t newsize) {

src/alloc-override.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ terms of the MIT license. A copy of the license can be found in the file
2222
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
2323
// use aliasing to alias the exported function to one of our `mi_` functions
2424
#if (defined(__GNUC__) && __GNUC__ >= 9)
25-
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)))
25+
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
2626
#else
27-
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")))
27+
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
2828
#endif
2929
#define MI_FORWARD1(fun,x) MI_FORWARD(fun)
3030
#define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
@@ -75,10 +75,10 @@ terms of the MIT license. A copy of the license can be found in the file
7575
// we just override new/delete which does work in a static library.
7676
#else
7777
// On all other systems forward to our API
78-
void* malloc(size_t size) MI_FORWARD1(mi_malloc, size);
79-
void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n);
80-
void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize);
81-
void free(void* p) MI_FORWARD0(mi_free, p);
78+
void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
79+
void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)
80+
void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)
81+
void free(void* p) MI_FORWARD0(mi_free, p)
8282
#endif
8383

8484
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
@@ -96,18 +96,18 @@ terms of the MIT license. A copy of the license can be found in the file
9696
// see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
9797
// ------------------------------------------------------
9898
#include <new>
99-
void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p);
100-
void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p);
99+
void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
100+
void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
101101

102-
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n);
103-
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n);
102+
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
103+
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
104104

105105
void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
106106
void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
107107

108108
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
109-
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n);
110-
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n);
109+
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
110+
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
111111
#endif
112112

113113
#if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
@@ -128,30 +128,30 @@ terms of the MIT license. A copy of the license can be found in the file
128128
// used by GCC and CLang).
129129
// See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
130130
// ------------------------------------------------------
131-
void _ZdlPv(void* p) MI_FORWARD0(mi_free,p); // delete
132-
void _ZdaPv(void* p) MI_FORWARD0(mi_free,p); // delete[]
133-
void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n);
134-
void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n);
131+
void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
132+
void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
133+
void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
134+
void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
135135
void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
136136
void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
137137
void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
138138
void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
139139

140140
typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
141141
#if (MI_INTPTR_SIZE==8)
142-
void* _Znwm(size_t n) MI_FORWARD1(mi_new,n); // new 64-bit
143-
void* _Znam(size_t n) MI_FORWARD1(mi_new,n); // new[] 64-bit
144-
void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
145-
void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
142+
void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
143+
void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
144+
void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
145+
void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
146146
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
147147
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
148148
void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
149149
void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
150150
#elif (MI_INTPTR_SIZE==4)
151-
void* _Znwj(size_t n) MI_FORWARD1(mi_new,n); // new 64-bit
152-
void* _Znaj(size_t n) MI_FORWARD1(mi_new,n); // new[] 64-bit
153-
void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
154-
void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
151+
void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
152+
void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
153+
void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
154+
void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
155155
void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
156156
void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
157157
void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
@@ -170,13 +170,13 @@ extern "C" {
170170
// Posix & Unix functions definitions
171171
// ------------------------------------------------------
172172

173-
void cfree(void* p) MI_FORWARD0(mi_free, p);
174-
void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize);
175-
size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p);
173+
void cfree(void* p) MI_FORWARD0(mi_free, p)
174+
void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
175+
size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
176176
#if !defined(__ANDROID__)
177-
size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p);
177+
size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
178178
#else
179-
size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p);
179+
size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
180180
#endif
181181

182182
// no forwarding here due to aliasing/name mangling issues
@@ -199,11 +199,11 @@ void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(a
199199

200200
#if defined(__GLIBC__) && defined(__linux__)
201201
// forward __libc interface (needed for glibc-based Linux distributions)
202-
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size);
203-
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size);
204-
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size);
205-
void __libc_free(void* p) MI_FORWARD0(mi_free,p);
206-
void __libc_cfree(void* p) MI_FORWARD0(mi_free,p);
202+
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
203+
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
204+
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)
205+
void __libc_free(void* p) MI_FORWARD0(mi_free,p)
206+
void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)
207207

208208
void* __libc_valloc(size_t size) { return mi_valloc(size); }
209209
void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }

test/test-stress.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static bool allow_large_objects = true; // allow very large objects?
3737
static size_t use_one_size = 0; // use single object size of `N * sizeof(uintptr_t)`?
3838

3939

40+
// #define USE_STD_MALLOC
4041
#ifdef USE_STD_MALLOC
4142
#define custom_calloc(n,s) calloc(n,s)
4243
#define custom_realloc(p,s) realloc(p,s)
@@ -250,10 +251,10 @@ int main(int argc, char** argv) {
250251
test_leak();
251252
#endif
252253

253-
#ifndef NDEBUG
254-
mi_collect(true);
255-
#endif
256254
#ifndef USE_STD_MALLOC
255+
#ifndef NDEBUG
256+
mi_collect(true);
257+
#endif
257258
mi_stats_print(NULL);
258259
#endif
259260
//bench_end_program();

0 commit comments

Comments
 (0)