2019-06-27 23:36:19 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2018, Microsoft Research, Daan Leijen
|
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
|
|
|
"LICENSE" at the root of this distribution.
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Testing allocators is difficult as bugs may only surface after particular
|
|
|
|
allocation patterns. The main approach to testing _mimalloc_ is therefore
|
|
|
|
to have extensive internal invariant checking (see `page_is_valid` in `page.c`
|
2020-01-18 06:59:55 +03:00
|
|
|
for example), which is enabled in debug mode with `-DMI_DEBUG_FULL=ON`.
|
2019-06-27 23:36:19 +03:00
|
|
|
The main testing is then to run `mimalloc-bench` [1] using full invariant checking
|
|
|
|
to catch any potential problems over a wide range of intensive allocation bench
|
|
|
|
marks.
|
|
|
|
|
|
|
|
However, this does not test well for the entire API surface. In this test file
|
|
|
|
we therefore test the API over various inputs. Please add more tests :-)
|
|
|
|
|
|
|
|
[1] https://github.com/daanx/mimalloc-bench
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
2020-01-07 03:44:55 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2020-01-07 03:18:22 +03:00
|
|
|
#include <vector>
|
2020-01-07 03:44:55 +03:00
|
|
|
#endif
|
|
|
|
|
2019-06-27 23:36:19 +03:00
|
|
|
#include "mimalloc.h"
|
|
|
|
#include "mimalloc-internal.h"
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Test macros: CHECK(name,predicate) and CHECK_BODY(name,body)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static int ok = 0;
|
|
|
|
static int failed = 0;
|
|
|
|
|
|
|
|
#define CHECK_BODY(name,body) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr,"test: %s... ", name ); \
|
|
|
|
bool result = true; \
|
|
|
|
do { body } while(false); \
|
|
|
|
if (!(result)) { \
|
|
|
|
failed++; \
|
|
|
|
fprintf(stderr, \
|
|
|
|
"\n FAILED: %s:%d:\n %s\n", \
|
|
|
|
__FILE__, \
|
|
|
|
__LINE__, \
|
|
|
|
#body); \
|
|
|
|
/* exit(1); */ \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
ok++; \
|
|
|
|
fprintf(stderr,"ok.\n"); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define CHECK(name,expr) CHECK_BODY(name,{ result = (expr); })
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Test functions
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
bool test_heap1();
|
|
|
|
bool test_heap2();
|
2020-01-07 03:18:22 +03:00
|
|
|
bool test_stl_allocator1();
|
|
|
|
bool test_stl_allocator2();
|
2019-06-27 23:36:19 +03:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Main testing
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
int main() {
|
2019-08-20 17:49:40 +03:00
|
|
|
mi_option_disable(mi_option_verbose);
|
2019-06-27 23:36:19 +03:00
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// Malloc
|
|
|
|
// ---------------------------------------------------
|
|
|
|
|
|
|
|
CHECK_BODY("malloc-zero",{
|
|
|
|
void* p = mi_malloc(0); mi_free(p);
|
|
|
|
});
|
|
|
|
CHECK_BODY("malloc-nomem1",{
|
|
|
|
result = (mi_malloc(SIZE_MAX/2) == NULL);
|
|
|
|
});
|
|
|
|
CHECK_BODY("malloc-null",{
|
|
|
|
mi_free(NULL);
|
|
|
|
});
|
2020-01-18 06:59:55 +03:00
|
|
|
CHECK_BODY("calloc-overflow",{
|
|
|
|
// use (size_t)&mi_calloc to get some number without triggering compiler warnings
|
|
|
|
result = (mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000) == NULL);
|
|
|
|
});
|
2020-01-19 07:30:12 +03:00
|
|
|
CHECK_BODY("calloc0",{
|
2020-01-20 04:33:36 +03:00
|
|
|
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
|
2020-01-19 07:30:12 +03:00
|
|
|
});
|
2019-06-27 23:36:19 +03:00
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// Extended
|
|
|
|
// ---------------------------------------------------
|
|
|
|
#if defined(MI_MALLOC_OVERRIDE) && !defined(_WIN32)
|
|
|
|
CHECK_BODY("posix_memalign1", {
|
2019-07-08 04:11:21 +03:00
|
|
|
void* p = &p;
|
2019-06-27 23:36:19 +03:00
|
|
|
int err = posix_memalign(&p, sizeof(void*), 32);
|
2019-07-08 04:11:21 +03:00
|
|
|
mi_assert((err==0 && (uintptr_t)p % sizeof(void*) == 0) || p==&p);
|
2019-06-27 23:36:19 +03:00
|
|
|
mi_free(p);
|
|
|
|
result = (err==0);
|
|
|
|
});
|
|
|
|
CHECK_BODY("posix_memalign_no_align", {
|
2019-07-08 04:11:21 +03:00
|
|
|
void* p = &p;
|
2019-06-27 23:36:19 +03:00
|
|
|
int err = posix_memalign(&p, 3, 32);
|
2019-07-08 04:11:21 +03:00
|
|
|
mi_assert(p==&p);
|
2019-06-27 23:36:19 +03:00
|
|
|
result = (err==EINVAL);
|
|
|
|
});
|
|
|
|
CHECK_BODY("posix_memalign_zero", {
|
2019-07-08 04:11:21 +03:00
|
|
|
void* p = &p;
|
2019-06-27 23:36:19 +03:00
|
|
|
int err = posix_memalign(&p, sizeof(void*), 0);
|
|
|
|
mi_free(p);
|
|
|
|
result = (err==0);
|
|
|
|
});
|
|
|
|
CHECK_BODY("posix_memalign_nopow2", {
|
2019-07-08 04:11:21 +03:00
|
|
|
void* p = &p;
|
2019-06-27 23:36:19 +03:00
|
|
|
int err = posix_memalign(&p, 3*sizeof(void*), 32);
|
2019-07-08 04:11:21 +03:00
|
|
|
result = (err==EINVAL && p==&p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
CHECK_BODY("posix_memalign_nomem", {
|
2019-07-08 04:11:21 +03:00
|
|
|
void* p = &p;
|
2019-06-27 23:36:19 +03:00
|
|
|
int err = posix_memalign(&p, sizeof(void*), SIZE_MAX);
|
2019-07-08 04:11:21 +03:00
|
|
|
result = (err==ENOMEM && p==&p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// Aligned API
|
|
|
|
// ---------------------------------------------------
|
|
|
|
CHECK_BODY("malloc-aligned1", {
|
2019-09-12 03:49:28 +03:00
|
|
|
void* p = mi_malloc_aligned(32,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
CHECK_BODY("malloc-aligned2", {
|
2019-09-12 03:49:28 +03:00
|
|
|
void* p = mi_malloc_aligned(48,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
CHECK_BODY("malloc-aligned-at1", {
|
2019-09-12 03:49:28 +03:00
|
|
|
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
CHECK_BODY("malloc-aligned-at2", {
|
2019-09-12 03:49:28 +03:00
|
|
|
void* p = mi_malloc_aligned_at(50,32,8); result = (p != NULL && ((uintptr_t)(p) + 8) % 32 == 0); mi_free(p);
|
2019-06-27 23:36:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// Heaps
|
|
|
|
// ---------------------------------------------------
|
|
|
|
CHECK("heap_destroy", test_heap1());
|
|
|
|
CHECK("heap_delete", test_heap2());
|
|
|
|
|
2019-07-02 17:23:24 +03:00
|
|
|
//mi_stats_print(NULL);
|
|
|
|
|
2019-07-09 03:43:10 +03:00
|
|
|
// ---------------------------------------------------
|
|
|
|
// various
|
|
|
|
// ---------------------------------------------------
|
|
|
|
CHECK_BODY("realpath", {
|
|
|
|
char* s = mi_realpath( ".", NULL );
|
|
|
|
// printf("realpath: %s\n",s);
|
|
|
|
mi_free(s);
|
|
|
|
});
|
|
|
|
|
2020-01-07 03:18:22 +03:00
|
|
|
CHECK("stl_allocator1", test_stl_allocator1());
|
|
|
|
CHECK("stl_allocator2", test_stl_allocator2());
|
|
|
|
|
2019-06-27 23:36:19 +03:00
|
|
|
// ---------------------------------------------------
|
|
|
|
// Done
|
|
|
|
// ---------------------------------------------------[]
|
|
|
|
fprintf(stderr,"\n\n---------------------------------------------\n"
|
|
|
|
"succeeded: %i\n"
|
|
|
|
"failed : %i\n\n", ok, failed);
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// Larger test functions
|
|
|
|
// ---------------------------------------------------
|
|
|
|
|
|
|
|
bool test_heap1() {
|
|
|
|
mi_heap_t* heap = mi_heap_new();
|
|
|
|
int* p1 = mi_heap_malloc_tp(heap,int);
|
|
|
|
int* p2 = mi_heap_malloc_tp(heap,int);
|
|
|
|
*p1 = *p2 = 43;
|
|
|
|
mi_heap_destroy(heap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test_heap2() {
|
|
|
|
mi_heap_t* heap = mi_heap_new();
|
|
|
|
int* p1 = mi_heap_malloc_tp(heap,int);
|
|
|
|
int* p2 = mi_heap_malloc_tp(heap,int);
|
|
|
|
mi_heap_delete(heap);
|
|
|
|
*p1 = 42;
|
|
|
|
mi_free(p1);
|
|
|
|
mi_free(p2);
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-07 03:18:22 +03:00
|
|
|
|
|
|
|
bool test_stl_allocator1() {
|
|
|
|
#ifdef __cplusplus
|
2020-01-21 02:27:05 +03:00
|
|
|
std::vector<int, mi_stl_allocator<int> > vec;
|
2020-01-07 03:18:22 +03:00
|
|
|
vec.push_back(1);
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-21 02:27:05 +03:00
|
|
|
struct some_struct { int i; int j; double z; };
|
|
|
|
|
2020-01-07 03:18:22 +03:00
|
|
|
bool test_stl_allocator2() {
|
|
|
|
#ifdef __cplusplus
|
2020-01-21 02:27:05 +03:00
|
|
|
std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
|
2020-01-07 03:18:22 +03:00
|
|
|
vec.push_back(some_struct());
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|