From d6a56dd99d5abe4e1f6af555976ee8dc64e1ec8a Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 1 Dec 2021 22:43:13 +0000 Subject: [PATCH] introduces netbsd's reallocarr overriding. and couple of tests. --- include/mimalloc-override.h | 1 + include/mimalloc.h | 1 + src/alloc-override.c | 1 + src/alloc-posix.c | 14 ++++++++++++++ test/main-override.c | 8 +++++++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/mimalloc-override.h b/include/mimalloc-override.h index 2362bfbc..c00fc594 100644 --- a/include/mimalloc-override.h +++ b/include/mimalloc-override.h @@ -48,6 +48,7 @@ not accidentally mix pointers from different allocators). #define valloc(n) mi_valloc(n) #define pvalloc(n) mi_pvalloc(n) #define reallocarray(p,s,n) mi_reallocarray(p,s,n) +#define reallocarr(p,s,n) mi_reallocarr(p,s,n) #define memalign(a,n) mi_memalign(a,n) #define aligned_alloc(a,n) mi_aligned_alloc(a,n) #define posix_memalign(p,a,n) mi_posix_memalign(p,a,n) diff --git a/include/mimalloc.h b/include/mimalloc.h index a2835d02..414d628d 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -351,6 +351,7 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_pvalloc(size_t size) mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(1); mi_decl_nodiscard mi_decl_export void* mi_reallocarray(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_alloc_size2(2,3); +mi_decl_nodiscard mi_decl_export int mi_reallocarr(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_alloc_size2(2,3); mi_decl_nodiscard mi_decl_export void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept; mi_decl_nodiscard mi_decl_export void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept; diff --git a/src/alloc-override.c b/src/alloc-override.c index 5906bd20..d24cd8bc 100644 --- a/src/alloc-override.c +++ b/src/alloc-override.c @@ -183,6 +183,7 @@ size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p); void* valloc(size_t size) { return mi_valloc(size); } void* pvalloc(size_t size) { return mi_pvalloc(size); } void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); } +int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(&p, count, size); } void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); } int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); } void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); } diff --git a/src/alloc-posix.c b/src/alloc-posix.c index eef70ab5..6465304c 100644 --- a/src/alloc-posix.c +++ b/src/alloc-posix.c @@ -90,6 +90,20 @@ void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { return newp; } +int mi_reallocarr( void* p, size_t count, size_t size ) mi_attr_noexcept { // NetBSD + void** op = (void** )p; + int serrno = errno; + void* newp = mi_reallocn(p,count,size); + if (mi_unlikely(newp == NULL)) { + errno = ENOMEM; + return errno; + } else { + *op = newp; + errno = serrno; + return 0; + } +} + void* mi__expand(void* p, size_t newsize) mi_attr_noexcept { // Microsoft void* res = mi_expand(p, newsize); if (res == NULL) errno = ENOMEM; diff --git a/test/main-override.c b/test/main-override.c index 1bec1179..284fdd20 100644 --- a/test/main-override.c +++ b/test/main-override.c @@ -3,7 +3,7 @@ #include #include -#include +#include int main() { mi_version(); // ensure mimalloc library is linked @@ -25,6 +25,12 @@ int main() { //free(p1); //p2 = malloc(32); //mi_free(p2); + p1 = malloc(24); + p2 = reallocarray(p1, 16, 16); + free(p2); + p1 = malloc(24); + assert(reallocarr(&p1, 16, 16) == 0); + free(p1); mi_stats_print(NULL); return 0; }