fix padding issue with zero sized allocation (issue #209)

This commit is contained in:
daan 2020-02-18 20:05:30 -08:00
parent 82684042be
commit ec61224db0
2 changed files with 22 additions and 1 deletions

View File

@ -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!=NULL);
mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id()); // heaps are thread local mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id()); // heaps are thread local
mi_assert(size <= MI_SMALL_SIZE_MAX); 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); 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); void* p = _mi_page_malloc(heap, page, size + MI_PADDING_SIZE);
mi_assert_internal(p==NULL || mi_usable_size(p) >= size); mi_assert_internal(p==NULL || mi_usable_size(p) >= size);

View File

@ -22,12 +22,14 @@ static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); }
void heap_no_delete(); void heap_no_delete();
void heap_late_free(); void heap_late_free();
void padding_shrink();
void various_tests(); void various_tests();
int main() { int main() {
mi_stats_reset(); // ignore earlier allocations mi_stats_reset(); // ignore earlier allocations
// heap_no_delete(); // issue #202 // heap_no_delete(); // issue #202
// heap_late_free(); // issue #204 // heap_late_free(); // issue #204
padding_shrink(); // issue #209
various_tests(); various_tests();
mi_stats_print(NULL); mi_stats_print(NULL);
return 0; return 0;
@ -141,3 +143,17 @@ void heap_late_free() {
t1.join(); t1.join();
} }
// 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);
}