From 49a1c8389033aa8cf5d50777af00935f53701acd Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 5 Nov 2018 21:09:40 +0100 Subject: Add tests and small fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- allocn.c | 2 +- libsimple.c | 836 +++++++++++++++++++++++++++++++++++++++++++++++++- libsimple.h | 2 +- libsimple/memalign.h | 1 + libsimple/memalignz.h | 14 +- libsimple/pvallocz.h | 8 +- libsimple/vallocz.h | 8 +- 7 files changed, 853 insertions(+), 18 deletions(-) diff --git a/allocn.c b/allocn.c index bc199d2..d54542c 100644 --- a/allocn.c +++ b/allocn.c @@ -223,7 +223,7 @@ main(void) if (have_custom_malloc()) { assert((info = get_allocinfo(ptr))); assert(info->size == 432); - assert(info->alignment == 2 *sizeof(void *)); + assert(info->alignment == 2 * sizeof(void *)); assert(!info->zeroed); assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); } diff --git a/libsimple.c b/libsimple.c index 6e1318f..5d77549 100644 --- a/libsimple.c +++ b/libsimple.c @@ -98,6 +98,9 @@ main(void) char buf[1024], *s; int intarray[10]; size_t i, j, n; + size_t pagesize; + + pagesize = (size_t)sysconf(_SC_PAGESIZE); assert(libsimple_default_failure_exit == 1); @@ -1095,7 +1098,6 @@ main(void) assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); } free(ptr); - ptr = NULL; assert(!libsimple_posix_memalignz(&ptr, 0, 8 * sizeof(void *), 8)); @@ -1122,6 +1124,628 @@ main(void) free(ptr); ptr = NULL; + assert((ptr = libsimple_memalignz(0, 4, 9))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 9 || info->size == 12); + assert(info->alignment == 4); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_memalignz(1, 2, 7))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 7 || info->size == 8); + assert(info->alignment == 2); + assert(info->zeroed == 7 || info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_memalign(2, 5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 || info->size == 6); + assert(info->alignment == 2); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert(!libsimple_memalignzn(0, 128, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignzn(1, 128, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignn(128, 0) && errno == EINVAL); + errno = 0; + + assert(!libsimple_memalignzn(0, 1024, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_memalignzn(1, 1024, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_memalignn(1024, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + + assert(!libsimple_memalignz(0, 65, 100) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignz(1, 65, 100) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalign(65, 100) && errno == EINVAL); + errno = 0; + + assert(!libsimple_memalignzn(0, 65, 100, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignzn(1, 65, 100, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignn(65, 100, 0) && errno == EINVAL); + errno = 0; + + assert(!libsimple_memalignz(0, 0, 100) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignz(1, 0, 100) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalign(0, 100) && errno == EINVAL); + errno = 0; + + assert(!libsimple_memalignzn(0, 0, 100, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignzn(1, 0, 100, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_memalignn(0, 100, 0) && errno == EINVAL); + errno = 0; + + assert((ptr = libsimple_memalignzn(0, 1, 9, 9, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 81); + assert(info->alignment == 1); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_memalignzn(1, 2, 9, 9, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 162); + assert(info->alignment == 2); + assert(info->zeroed == 162); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_memalignn(4, 9, 9, 3, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 243 || info->size == 244); + assert(info->alignment == 4); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_enmemalignz(1, 1, 2, 9))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 9 || info->size == 10); + assert(info->alignment == 2); + assert(info->zeroed == 9 || info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_ememalignz(1, 2, 7))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 7 || info->size == 8); + assert(info->alignment == 2); + assert(info->zeroed == 7 || info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_enmemalignz(1, 0, 2, 8))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 8); + assert(info->alignment == 2); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_ememalignz(0, 4, 4))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 4); + assert(info->alignment == 4); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_enmemalign(1, 4, 8))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 8); + assert(info->alignment == 4); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_ememalign(8, 3))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 || info->size == 8); + assert(info->alignment == 8); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_vallocz(0, 9))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 9 || info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_vallocz(1, 7))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 7 || info->size == pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 7 || info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_valloc(5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 || info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_vallocz(1, 3 * pagesize))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 3 * pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_vallocz(0, 4 * pagesize))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 4 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_valloc(5 * pagesize))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_valloczn(1, 3 * pagesize, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 6 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 6 * pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_valloczn(0, 4 * pagesize, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 8 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_vallocn(5 * pagesize, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 10 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert(!libsimple_valloczn(0, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_valloczn(1, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_vallocn(0) && errno == EINVAL); + errno = 0; + + assert(!libsimple_valloczn(0, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_valloczn(1, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_vallocn(SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + + assert((ptr = libsimple_pvallocz(0, 9))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(1, 7))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvalloc(5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 || info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(1, pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(1, pagesize))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(1, pagesize + 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 2 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 2 * pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(1, 3 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 3 * pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocz(0, 4 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 4 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvalloc(5 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvalloczn(1, 3 * pagesize - 1, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 6 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == 6 * pagesize); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvalloczn(0, 4 * pagesize - 1, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 8 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvallocn(5 * pagesize - 1, 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 10 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)info->alignment)); + } + free(ptr); + ptr = NULL; + + assert(!libsimple_pvalloczn(0, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_pvalloczn(1, 0) && errno == EINVAL); + errno = 0; + assert(!libsimple_pvallocn(0) && errno == EINVAL); + errno = 0; + + assert(!libsimple_pvalloczn(0, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_pvalloczn(1, SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + assert(!libsimple_pvallocn(SIZE_MAX, 2, 0) && errno == ENOMEM); + errno = 0; + + assert((ptr = libsimple_valloczn(0, 9, 9, pagesize - 1, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 81 * (pagesize - 1)); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_valloczn(1, 9, 8, pagesize - 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 72 * (pagesize - 2)); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_vallocn(9, (pagesize - 1), 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 9 * (pagesize - 1)); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_envallocz(1, 1, 5 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 * pagesize - 1); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_evallocz(1, 3 * pagesize + 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 * pagesize + 1); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_envallocz(1, 0, pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize - 1); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_evallocz(0, pagesize + 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize + 1); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_envalloc(1, 127))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 127); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_evalloc(3 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 * pagesize - 1); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_pvalloczn(0, 9, 9, pagesize - 1, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 81 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_pvalloczn(1, 9, 8, pagesize - 2, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 72 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_pvallocn(9, (pagesize - 1), 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 9 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + + assert((ptr = libsimple_enpvallocz(1, 1, 5 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_epvallocz(1, 3 * pagesize + 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 4 * pagesize); + assert(info->alignment == pagesize); + assert(info->zeroed == info->size); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_enpvallocz(1, 0, pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_epvallocz(0, pagesize + 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 2 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_enpvalloc(1, 127))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + + assert((ptr = libsimple_epvalloc(3 * pagesize - 1))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 3 * pagesize); + assert(info->alignment == pagesize); + assert(!info->zeroed); + assert(!((uintptr_t)ptr % (uintptr_t)(info->alignment))); + } + free(ptr); + ptr = NULL; + if (have_custom_malloc()) { alloc_fail_in = 1; assert_exit_ptr(libsimple_enmallocz(5, 0, 20)); @@ -1174,8 +1798,218 @@ main(void) alloc_fail_in = 1; assert(libsimple_posix_memalignz(&ptr, 1, 16 * sizeof(void *), 16) == ENOMEM && !errno); assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!libsimple_memalignz(0, 4 * sizeof(void *), 8) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!libsimple_memalignz(1, 16 * sizeof(void *), 16) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmemalignz(3, 1, sizeof(void *), 4)); + assert(exit_status == 3); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 102; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_ememalignz(1, sizeof(void *), 4)); + assert(exit_status == 102); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmemalignz(5, 0, sizeof(void *), 4)); + assert(exit_status == 5); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 103; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_ememalignz(0, sizeof(void *), 4)); + assert(exit_status == 103); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmemalign(7, sizeof(void *), 4)); + assert(exit_status == 7); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 104; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_ememalign(sizeof(void *), 4)); + assert(exit_status == 104); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert(!libsimple_vallocz(0, 8) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!libsimple_vallocz(1, 16) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_envallocz(3, 1, 4)); + assert(exit_status == 3); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 102; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_evallocz(1, 4)); + assert(exit_status == 102); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_envallocz(5, 0, 4)); + assert(exit_status == 5); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 103; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_evallocz(0, 4)); + assert(exit_status == 103); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_envalloc(7, 4)); + assert(exit_status == 7); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 104; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_evalloc(4)); + assert(exit_status == 104); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert(!libsimple_pvallocz(0, 8) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!libsimple_pvallocz(1, 16) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enpvallocz(3, 1, 4)); + assert(exit_status == 3); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 102; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_epvallocz(1, 4)); + assert(exit_status == 102); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enpvallocz(5, 0, 4)); + assert(exit_status == 5); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 103; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_epvallocz(0, 4)); + assert(exit_status == 103); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enpvalloc(7, 4)); + assert(exit_status == 7); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 104; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_epvalloc(4)); + assert(exit_status == 104); + assert_stderr("%s: libsimple_vmemalloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; } + assert_exit_ptr(libsimple_enmemalignz(3, 1, 0, 4)); + assert(exit_status == 3); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 102; + assert_exit_ptr(libsimple_ememalignz(1, 0, 4)); + assert(exit_status == 102); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + assert(!alloc_fail_in); + + assert_exit_ptr(libsimple_enmemalignz(5, 0, 0, 4)); + assert(exit_status == 5); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 103; + assert_exit_ptr(libsimple_ememalignz(0, 0, 4)); + assert(exit_status == 103); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + assert(!alloc_fail_in); + + assert_exit_ptr(libsimple_enmemalign(7, 0, 4)); + assert(exit_status == 7); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 104; + assert_exit_ptr(libsimple_ememalign(0, 4)); + assert(exit_status == 104); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + libsimple_default_failure_exit = 1; + + assert_exit_ptr(libsimple_enmemalignz(3, 1, 3, 4)); + assert(exit_status == 3); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 102; + assert_exit_ptr(libsimple_ememalignz(1, 3, 4)); + assert(exit_status == 102); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + assert(!alloc_fail_in); + + assert_exit_ptr(libsimple_enmemalignz(5, 0, 3, 4)); + assert(exit_status == 5); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 103; + assert_exit_ptr(libsimple_ememalignz(0, 3, 4)); + assert(exit_status == 103); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + assert(!alloc_fail_in); + + assert_exit_ptr(libsimple_enmemalign(7, 3, 4)); + assert(exit_status == 7); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + + libsimple_default_failure_exit = 104; + assert_exit_ptr(libsimple_ememalign(3, 4)); + assert(exit_status == 104); + assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); + libsimple_default_failure_exit = 1; + assert(libsimple_memeq("abcxyz", "abc123", 3)); assert(!libsimple_memeq("abcxyz", "abc123", 4)); assert(libsimple_memeq("abcxyz", "abcx23", 4)); diff --git a/libsimple.h b/libsimple.h index b2f9aec..2c39098 100644 --- a/libsimple.h +++ b/libsimple.h @@ -55,6 +55,7 @@ extern int libsimple_default_failure_exit; +#include "libsimple/printf.h" #include "libsimple/definitions.h" #include "libsimple/memalloc.h" #include "libsimple/strdup.h" @@ -77,7 +78,6 @@ extern int libsimple_default_failure_exit; #include "libsimple/posix_memalign.h" #include "libsimple/env.h" #include "libsimple/time.h" -#include "libsimple/printf.h" #include "libsimple/mem.h" #include "libsimple/str.h" #include "libsimple/strn.h" diff --git a/libsimple/memalign.h b/libsimple/memalign.h index 169ec57..d533751 100644 --- a/libsimple/memalign.h +++ b/libsimple/memalign.h @@ -187,6 +187,7 @@ static inline void *libsimple_ememalign(size_t __alignment, size_t __n) #endif + /** * Dynamically allocates heap allocated, uninitialised, * memory with custom alignment diff --git a/libsimple/memalignz.h b/libsimple/memalignz.h index 224443b..7eab805 100644 --- a/libsimple/memalignz.h +++ b/libsimple/memalignz.h @@ -22,7 +22,7 @@ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(2), __warn_unused_result__))) static inline void * -libsimple_vmemalignzn(int __clear, size_t __alignment, size_t __n, va_list __ap) /* TODO test ([v]memalign[z]n) */ +libsimple_vmemalignzn(int __clear, size_t __alignment, size_t __n, va_list __ap) { if (!__alignment || (__alignment & (__alignment - 1UL))) { errno = EINVAL; @@ -52,7 +52,7 @@ libsimple_vmemalignzn(int __clear, size_t __alignment, size_t __n, va_list __ap) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(2), __alloc_size__(3), __warn_unused_result__))) static inline void * -libsimple_memalignz(int __clear, size_t __alignment, size_t __n) /* TODO test (memalign[z]) */ +libsimple_memalignz(int __clear, size_t __alignment, size_t __n) { if (!__alignment || (__alignment & (__alignment - 1UL))) { errno = EINVAL; @@ -119,11 +119,11 @@ libsimple_memalignzn(int __clear, size_t __alignment, size_t __n, ... /*, (size_ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(3), __alloc_size__(4), __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_enmemalignz(int __status, int __clear, size_t __alignment, size_t __n) /* TODO test (e[n]memalign[z]) */ +libsimple_enmemalignz(int __status, int __clear, size_t __alignment, size_t __n) { if (!__alignment || (__alignment & (__alignment - 1UL))) { errno = EINVAL; - return NULL; + libsimple_enprintf(__status, "libsimple_vmemalignz:"); } return libsimple_enmemalloc(__status, __n, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -159,7 +159,7 @@ libsimple_enmemalignz(int __status, int __clear, size_t __alignment, size_t __n) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(3), __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_envmemalignzn(int __status, int __clear, size_t __alignment, size_t __n, va_list __ap) /* TODO test (e[n][v]memalign[z]n) */ +libsimple_envmemalignzn(int __status, int __clear, size_t __alignment, size_t __n, va_list __ap) { if (!__alignment || (__alignment & (__alignment - 1UL))) { errno = EINVAL; @@ -229,7 +229,7 @@ libsimple_enmemalignzn(int __status, int __clear, size_t __alignment, size_t __n */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(2), __alloc_size__(3), __warn_unused_result__, __returns_nonnull__))) static inline void *libsimple_ememalignz(int __clear, size_t __alignment, size_t __n) -{ return libsimple_enmemalignz(libsimple_default_failure_exit, __alignment, __clear, __n); } +{ return libsimple_enmemalignz(libsimple_default_failure_exit, __clear, __alignment, __n); } #ifndef ememalignz # define ememalignz libsimple_ememalignz #endif @@ -258,7 +258,7 @@ static inline void *libsimple_ememalignz(int __clear, size_t __alignment, size_t */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_align__(2), __warn_unused_result__, __returns_nonnull__))) static inline void *libsimple_evmemalignzn(int __clear, size_t __alignment, size_t __n, va_list __ap) -{ return libsimple_envmemalignzn(libsimple_default_failure_exit, __alignment, __clear, __n, __ap); } +{ return libsimple_envmemalignzn(libsimple_default_failure_exit, __clear, __alignment, __n, __ap); } #ifndef evmemalignzn # define evmemalignzn libsimple_evmemalignzn #endif diff --git a/libsimple/pvallocz.h b/libsimple/pvallocz.h index 7821991..a61f80b 100644 --- a/libsimple/pvallocz.h +++ b/libsimple/pvallocz.h @@ -24,7 +24,7 @@ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__))) static inline void * -libsimple_vpvalloczn(int __clear, size_t __n, va_list __ap) /* TODO test ([v]pvalloc[z]n) */ +libsimple_vpvalloczn(int __clear, size_t __n, va_list __ap) { return libsimple_memalloc(0, LIBSIMPLE_MEMALLOC_1_VA_PRODUCT_SIZE, __n, __ap, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -54,7 +54,7 @@ libsimple_vpvalloczn(int __clear, size_t __n, va_list __ap) /* TODO test ([v]pva */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_size__(2), __warn_unused_result__))) static inline void * -libsimple_pvallocz(int __clear, size_t __n) /* TODO test (pvalloc[z]) */ +libsimple_pvallocz(int __clear, size_t __n) { return libsimple_memalloc(__n, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -123,7 +123,7 @@ libsimple_pvalloczn(int __clear, size_t __n, ... /*, (size_t)0 */) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_size__(3), __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_enpvallocz(int __status, int __clear, size_t __n) /* TODO test (e[n]pvalloc[z]) */ +libsimple_enpvallocz(int __status, int __clear, size_t __n) { return libsimple_enmemalloc(__status, __n, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -163,7 +163,7 @@ libsimple_enpvallocz(int __status, int __clear, size_t __n) /* TODO test (e[n]pv */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_envpvalloczn(int __status, int __clear, size_t __n, va_list __ap) /* TODO test (e[n][v]pvalloc[z]n) */ +libsimple_envpvalloczn(int __status, int __clear, size_t __n, va_list __ap) { return libsimple_enmemalloc(__status, 0, LIBSIMPLE_MEMALLOC_1_VA_PRODUCT_SIZE, __n, __ap, diff --git a/libsimple/vallocz.h b/libsimple/vallocz.h index a4765c5..3d8b4ec 100644 --- a/libsimple/vallocz.h +++ b/libsimple/vallocz.h @@ -20,7 +20,7 @@ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__))) static inline void * -libsimple_vvalloczn(int __clear, size_t __n, va_list __ap) /* TODO test ([v]valloc[z]n) */ +libsimple_vvalloczn(int __clear, size_t __n, va_list __ap) { return libsimple_memalloc(0, LIBSIMPLE_MEMALLOC_1_VA_PRODUCT_SIZE, __n, __ap, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -45,7 +45,7 @@ libsimple_vvalloczn(int __clear, size_t __n, va_list __ap) /* TODO test ([v]vall */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_size__(2), __warn_unused_result__))) static inline void * -libsimple_vallocz(int __clear, size_t __n) /* TODO test (valloc[z]) */ +libsimple_vallocz(int __clear, size_t __n) { return libsimple_memalloc(__n, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -105,7 +105,7 @@ libsimple_valloczn(int __clear, size_t __n, ... /*, (size_t)0 */) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __alloc_size__(3), __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_envallocz(int __status, int __clear, size_t __n) /* TODO test (e[n]valloc[z]) */ +libsimple_envallocz(int __status, int __clear, size_t __n) { return libsimple_enmemalloc(__status, __n, LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT, __clear, @@ -140,7 +140,7 @@ libsimple_envallocz(int __status, int __clear, size_t __n) /* TODO test (e[n]val */ _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__, __returns_nonnull__))) static inline void * -libsimple_envvalloczn(int __status, int __clear, size_t __n, va_list __ap) /* TODO test (e[n][v]valloc[z]n, e[n]vallocn) */ +libsimple_envvalloczn(int __status, int __clear, size_t __n, va_list __ap) { return libsimple_enmemalloc(__status, 0, LIBSIMPLE_MEMALLOC_1_VA_PRODUCT_SIZE, __n, __ap, -- cgit v1.2.3-70-g09d2