Test smallest allocation of large type (test_malloc)
authorIvan Maidanski <ivmai@mail.ru>
Mon, 4 Dec 2017 21:08:40 +0000 (00:08 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Fri, 22 Dec 2017 07:22:27 +0000 (10:22 +0300)
(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
tests/test_malloc.c

index 2a69dbc..8b0f963 100644 (file)
@@ -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();
index 4c7e088..338b9e8 100644 (file)
@@ -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;
 }