diff --git a/src/alloc.c b/src/alloc.c index b1c4cd34..efa35f58 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -62,6 +62,11 @@ extern inline mi_decl_restrict void* mi_heap_malloc_small(mi_heap_t* heap, size_ mi_assert(heap!=NULL); mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id()); // heaps are thread local mi_assert(size <= MI_SMALL_SIZE_MAX); + #if (MI_PADDING) + if (size == 0) { + size = sizeof(void*); + } + #endif mi_page_t* page = _mi_heap_get_free_small_page(heap,size + MI_PADDING_SIZE); void* p = _mi_page_malloc(heap, page, size + MI_PADDING_SIZE); mi_assert_internal(p==NULL || mi_usable_size(p) >= size); diff --git a/test/main-override.cpp b/test/main-override.cpp index 957b7872..18d49df3 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -22,12 +22,14 @@ static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); } void heap_no_delete(); void heap_late_free(); +void padding_shrink(); void various_tests(); int main() { mi_stats_reset(); // ignore earlier allocations // heap_no_delete(); // issue #202 // heap_late_free(); // issue #204 + padding_shrink(); // issue #209 various_tests(); mi_stats_print(NULL); return 0; @@ -140,4 +142,18 @@ void heap_late_free() { mi_free((void*)global_p); t1.join(); -} \ No newline at end of file +} + +// issue #209 +static void* shared_p; +static void alloc0(/* void* arg */) +{ + shared_p = mi_malloc(8); +} + +void padding_shrink(void) +{ + auto t1 = std::thread(alloc0); + t1.join(); + mi_free(shared_p); +}