From 6eb69ca2a45056477d062ba1eb82c3528794d1e7 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 5 Dec 2017 00:08:40 +0300 Subject: [PATCH] Test smallest allocation of large type (test_malloc) (Cherry-pick commits 29d91db, 937b173 from 'master' branch.) * src/atomic_ops_malloc.c (AO_malloc_large): Add assertion that the stored size is greater than LOG_MAX_SIZE. * src/atomic_ops_malloc.c (AO_malloc): Add assertions for log_size. * tests/test_malloc.c (LOG_MAX_SIZE, CHUNK_SIZE): New macro (copied from atomic_ops_malloc.c). * tests/test_malloc.c (main): Call AO_free(0), AO_malloc(0); add comment. * tests/test_malloc.c [HAVE_MMAP] (main): Call AO_malloc(CHUNK_SIZE-sizeof(AO_t)+1). --- src/atomic_ops_malloc.c | 3 +++ tests/test_malloc.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/atomic_ops_malloc.c b/src/atomic_ops_malloc.c index 2a69dbc..8b0f963 100644 --- a/src/atomic_ops_malloc.c +++ b/src/atomic_ops_malloc.c @@ -162,6 +162,7 @@ AO_malloc_large(size_t sz) /* The header will force us to waste ALIGNMENT bytes, incl. header. */ /* Round to multiple of CHUNK_SIZE. */ sz = SIZET_SAT_ADD(sz, ALIGNMENT + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1); + assert(sz > LOG_MAX_SIZE); result = get_mmaped(sz); if (result == 0) return 0; result += ALIGNMENT; @@ -303,6 +304,8 @@ AO_malloc(size_t sz) if (sz > CHUNK_SIZE - sizeof(AO_t)) return AO_malloc_large(sz); log_sz = msb(sz + (sizeof(AO_t) - 1)); + assert(log_sz <= LOG_MAX_SIZE); + assert(((size_t)1 << log_sz) >= sz + sizeof(AO_t)); result = AO_stack_pop(AO_free_list+log_sz); while (0 == result) { void * chunk = get_chunk(); diff --git a/tests/test_malloc.c b/tests/test_malloc.c index 4c7e088..338b9e8 100644 --- a/tests/test_malloc.c +++ b/tests/test_malloc.c @@ -218,6 +218,12 @@ void * run_one_test(void * arg) { return arg; /* use arg to suppress compiler warning */ } +#ifndef LOG_MAX_SIZE +# define LOG_MAX_SIZE 16 +#endif + +#define CHUNK_SIZE (1 << LOG_MAX_SIZE) + int main(int argc, char **argv) { int nthreads; @@ -236,6 +242,14 @@ int main(int argc, char **argv) { printf("Performing %d reversals of %d element lists in %d threads\n", N_REVERSALS, LIST_LENGTH, nthreads); AO_malloc_enable_mmap(); + + /* Test various corner cases. */ + AO_free(NULL); + AO_free(AO_malloc(0)); +# ifdef HAVE_MMAP + AO_free(AO_malloc(CHUNK_SIZE - (sizeof(AO_t)-1))); /* large alloc */ +# endif + run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free"); return 0; } -- 2.40.0