diff options
-rw-r--r-- | libsimple.c | 62 | ||||
-rw-r--r-- | libsimple/aligned_realloc.h | 4 |
2 files changed, 63 insertions, 3 deletions
diff --git a/libsimple.c b/libsimple.c index a2ae4bc..5232833 100644 --- a/libsimple.c +++ b/libsimple.c @@ -88,7 +88,7 @@ int main(void) { struct allocinfo *volatile info; - void *ptr; + void *ptr, *old; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; const char *cs; @@ -2248,6 +2248,66 @@ main(void) assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); libsimple_default_failure_exit = 1; +#ifdef LIBSIMPLE_HAVE_ALIGNED_REALLOC + assert((ptr = libsimple_aligned_realloc(NULL, 16, 5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 16); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_aligned_realloc(old = ptr, 32, 10))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 10 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 32); + assert(ptr != old); + free(old); + } + free(ptr); + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert(!libsimple_aligned_realloc(NULL, 8, 1) && errno == ENOMEM); + assert(!alloc_fail_in); + } +#else + assert(libsimple_aligned_realloc(NULL, 8, 1) && errno == ENOSYS); +#endif + +#ifdef LIBSIMPLE_HAVE_ALIGNED_REALLOC + assert((ptr = libsimple_aligned_reallocarray(NULL, 16, 5, 3))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 15 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 16); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_aligned_reallocarray(old = ptr, 32, 10, 2))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 20 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 32); + assert(ptr != old); + free(old); + } + free(ptr); + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert(!libsimple_aligned_reallocarray(NULL, 8, 1, 1) && errno == ENOMEM); + assert(!alloc_fail_in); + } +#else + assert(libsimple_aligned_reallocarray(NULL, 8, 1, 1) && errno == ENOSYS); +#endif + assert(libsimple_memeq("abcxyz", "abc123", 3)); assert(!libsimple_memeq("abcxyz", "abc123", 4)); assert(libsimple_memeq("abcxyz", "abcx23", 4)); diff --git a/libsimple/aligned_realloc.h b/libsimple/aligned_realloc.h index f0350e1..1da7ba7 100644 --- a/libsimple/aligned_realloc.h +++ b/libsimple/aligned_realloc.h @@ -32,7 +32,7 @@ _LIBSIMPLE_GCC_ONLY(__attribute__((__alloc_align__(2), __alloc_size__(3), __warn # define LIBSIMPLE_HAVE_ALIGNED_REALLOC # include <malloc.h> static inline void * -libsimple_aligned_realloc(void *__ptr, size_t __alignment, size_t __n) /* TODO test, musl */ +libsimple_aligned_realloc(void *__ptr, size_t __alignment, size_t __n) /* TODO musl */ { size_t __old = malloc_usable_size(__ptr); #if __STDC_VERSION__ >= 201112L || defined(_ISOC11_SOURCE) @@ -200,7 +200,7 @@ libsimple_aligned_reallocf(void *__ptr, size_t __alignment, size_t __n) /* TODO */ _LIBSIMPLE_GCC_ONLY(__attribute__((__alloc_align__(2), __alloc_size__(3, 4), __warn_unused_result__))) static inline void * -libsimple_aligned_reallocarray(void *__ptr, size_t __alignment, size_t __n, size_t __m) /* TODO test */ +libsimple_aligned_reallocarray(void *__ptr, size_t __alignment, size_t __n, size_t __m) { if (LIBSIMPLE_UMUL_OVERFLOW(__n, __m, &__n, SIZE_MAX)) { errno = ENOMEM; |