diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 8fa8caf2..86927a74 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -796,21 +796,21 @@ static inline mi_threadid_t _mi_thread_id(void) mi_attr_noexcept { } #elif defined(__GNUC__) && \ - (defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)) + (defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)) -// TLS register on x86 is in the FS or GS register, see: https://akkadia.org/drepper/tls.pdf +// see also https://akkadia.org/drepper/tls.pdf for more info on the TLS register. static inline void* mi_tls_slot(size_t slot) mi_attr_noexcept { void* res; const size_t ofs = (slot*sizeof(void*)); #if defined(__i386__) - __asm__("movl %%gs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : ); // 32-bit always uses GS + __asm__("movl %%gs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : ); // x86 32-bit always uses GS #elif defined(__APPLE__) && defined(__x86_64__) __asm__("movq %%gs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : ); // x86_64 macOSX uses GS #elif defined(__x86_64__) && (MI_INTPTR_SIZE==4) __asm__("movl %%fs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : ); // x32 ABI #elif defined(__x86_64__) __asm__("movq %%fs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : ); // x86_64 Linux, BSD uses FS -#elif defined(__arm__) +#elif defined(__arm__) // arm32: defined but currently not used (see issue #495) void** tcb; MI_UNUSED(ofs); __asm__ volatile ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tcb)); res = tcb[slot]; @@ -827,7 +827,7 @@ static inline void* mi_tls_slot(size_t slot) mi_attr_noexcept { return res; } -// setting is only used on macOSX for now +// setting a tls slot is only used on macOSX for now static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept { const size_t ofs = (slot*sizeof(void*)); #if defined(__i386__) @@ -855,8 +855,8 @@ static inline void mi_tls_slot_set(size_t slot, void* value) mi_attr_noexcept { } static inline mi_threadid_t _mi_thread_id(void) mi_attr_noexcept { -#if defined(__arm__) || (defined(__ANDROID__) && defined(__aarch64__)) - // issue #384, #495: on arm32 and arm32/arm64 Android, slot 1 is the thread ID (pointer to pthread internal struct) +#if defined(__ANDROID__) && (defined(__arm__) || defined(__aarch64__)) + // issue #384, #495: on arm Android, slot 1 is the thread ID (pointer to pthread internal struct) return (uintptr_t)mi_tls_slot(1); #else // in all our other targets, slot 0 is the pointer to the thread control block @@ -864,7 +864,7 @@ static inline mi_threadid_t _mi_thread_id(void) mi_attr_noexcept { #endif } #else -// otherwise use standard C +// otherwise use portable C static inline mi_threadid_t _mi_thread_id(void) mi_attr_noexcept { return (uintptr_t)&_mi_heap_default; } diff --git a/include/mimalloc-types.h b/include/mimalloc-types.h index fbfee9c5..9f97f8f5 100644 --- a/include/mimalloc-types.h +++ b/include/mimalloc-types.h @@ -164,7 +164,7 @@ typedef int32_t mi_ssize_t; #if (MI_MEDIUM_OBJ_WSIZE_MAX >= 655360) #error "mimalloc internal: define more bins" #endif -#if (MI_ALIGNED_MAX > MI_SEGMENT_SIZE/2) +#if (MI_ALIGNMENT_MAX > MI_SEGMENT_SIZE/2) #error "mimalloc internal: the max aligned boundary is too large for the segment size" #endif #if (MI_ALIGNED_MAX % MI_SEGMENT_SLICE_SIZE != 0) diff --git a/include/mimalloc.h b/include/mimalloc.h index 26a8ac0d..0533e730 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -166,7 +166,7 @@ mi_decl_export void mi_process_info(size_t* elapsed_msecs, size_t* user_msecs, s // Note that `alignment` always follows `size` for consistency with unaligned // allocation, but unfortunately this differs from `posix_memalign` and `aligned_alloc`. // ------------------------------------------------------------------------------------- -#define MI_ALIGNED_MAX (1024*1024UL) // maximum supported alignment is 1MiB +#define MI_ALIGNMENT_MAX (1024*1024UL) // maximum supported alignment is 1MiB mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2); mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1); diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index acf42e76..fce0fd74 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -18,7 +18,7 @@ terms of the MIT license. A copy of the license can be found in the file static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept { mi_assert_internal(size <= PTRDIFF_MAX); - mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNED_MAX); + mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNMENT_MAX); const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)` const size_t padsize = size + MI_PADDING_SIZE; @@ -55,9 +55,9 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t #endif return NULL; } - if (mi_unlikely(alignment > MI_ALIGNED_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers) + if (mi_unlikely(alignment > MI_ALIGNMENT_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers) #if MI_DEBUG > 0 - _mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNED_MAX, size, alignment); + _mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNMENT_MAX, size, alignment); #endif return NULL; } diff --git a/src/alloc-posix.c b/src/alloc-posix.c index 03171081..ee5babe1 100644 --- a/src/alloc-posix.c +++ b/src/alloc-posix.c @@ -103,7 +103,10 @@ void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { int mi_reallocarr( void* p, size_t count, size_t size ) mi_attr_noexcept { // NetBSD mi_assert(p != NULL); - if (p == NULL) return EINVAL; // should we set errno as well? + if (p == NULL) { + errno = EINVAL; + return EINVAL; + } void** op = (void**)p; void* newp = mi_reallocarray(*op, count, size); if (mi_unlikely(newp == NULL)) return errno; diff --git a/test/test-api.c b/test/test-api.c index 56835d00..96817337 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -159,20 +159,26 @@ int main(void) { void* p = mi_malloc_aligned(4097,4096); size_t usable = mi_usable_size(p); result = usable >= 4097 && usable < 10000; mi_free(p); }); CHECK_BODY("malloc-aligned6", { - void* p; bool ok = true; - for (int i = 1; i < 8 && ok; i++) { - size_t align = (size_t)1 << i; - p = mi_malloc_aligned(2*align, align); - ok = (p != NULL && (uintptr_t)(p) % align == 0); mi_free(p); + for (size_t align = 1; align <= MI_ALIGNMENT_MAX && ok; align *= 2) { + void* ps[8]; + for (int i = 0; i < 8 && ok; i++) { + ps[i] = mi_malloc_aligned(align/2 /*size*/, align); + if (ps[i] == NULL || (uintptr_t)(ps[i]) % align != 0) { + ok = false; + } + } + for (int i = 0; i < 8 && ok; i++) { + mi_free(ps[i]); + } } result = ok; }); CHECK_BODY("malloc-aligned7", { - void* p = mi_malloc_aligned(1024,MI_ALIGNED_MAX); mi_free(p); + void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX); mi_free(p); }); CHECK_BODY("malloc-aligned8", { - void* p = mi_malloc_aligned(1024,2*MI_ALIGNED_MAX); mi_free(p); + void* p = mi_malloc_aligned(1024,2*MI_ALIGNMENT_MAX); mi_free(p); }); CHECK_BODY("malloc-aligned-at1", { void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);