merge from dev

This commit is contained in:
daan 2020-01-20 19:06:08 -08:00
commit 394a7a92ab
92 changed files with 2994 additions and 1010 deletions

View file

@ -13,7 +13,7 @@ if (NOT CMAKE_BUILD_TYPE)
endif()
# Import mimalloc (if installed)
find_package(mimalloc 1.3 REQUIRED NO_SYSTEM_ENVIRONMENT_PATH)
find_package(mimalloc 1.4 REQUIRED NO_SYSTEM_ENVIRONMENT_PATH)
message(STATUS "Found mimalloc installed at: ${MIMALLOC_TARGET_DIR}")
# overriding with a dynamic library

View file

@ -178,7 +178,6 @@ static void corrupt_free();
int main() {
mi_version();
// mi_bins();
// detect double frees and heap corruption
// double_free1();
@ -274,4 +273,3 @@ static void corrupt_free() {
malloc(SZ);
}
}

View file

@ -6,6 +6,7 @@
#include <mimalloc.h>
#include <new>
#include <vector>
static void* p = malloc(8);
@ -69,3 +70,18 @@ public:
static Static s = Static();
bool test_stl_allocator1() {
std::vector<int, mi_stl_allocator<int> > vec;
vec.push_back(1);
vec.pop_back();
return vec.size() == 0;
}
struct some_struct { int i; int j; double z; };
bool test_stl_allocator2() {
std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
vec.push_back(some_struct());
vec.pop_back();
return vec.size() == 0;
}

View file

@ -9,7 +9,7 @@ terms of the MIT license. A copy of the license can be found in the file
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`
for example), which is enabled in debug mode with `-DMI_CHECK_FULL=ON`.
for example), which is enabled in debug mode with `-DMI_DEBUG_FULL=ON`.
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.
@ -25,6 +25,11 @@ we therefore test the API over various inputs. Please add more tests :-)
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#ifdef __cplusplus
#include <vector>
#endif
#include "mimalloc.h"
#include "mimalloc-internal.h"
@ -61,6 +66,8 @@ static int failed = 0;
// ---------------------------------------------------------------------------
bool test_heap1();
bool test_heap2();
bool test_stl_allocator1();
bool test_stl_allocator2();
// ---------------------------------------------------------------------------
// Main testing
@ -81,6 +88,13 @@ int main() {
CHECK_BODY("malloc-null",{
mi_free(NULL);
});
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);
});
CHECK_BODY("calloc0",{
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
});
// ---------------------------------------------------
// Extended
@ -150,6 +164,9 @@ int main() {
mi_free(s);
});
CHECK("stl_allocator1", test_stl_allocator1());
CHECK("stl_allocator2", test_stl_allocator2());
// ---------------------------------------------------
// Done
// ---------------------------------------------------[]
@ -182,3 +199,27 @@ bool test_heap2() {
mi_free(p2);
return true;
}
bool test_stl_allocator1() {
#ifdef __cplusplus
std::vector<int, mi_stl_allocator<int> > vec;
vec.push_back(1);
vec.pop_back();
return vec.size() == 0;
#else
return true;
#endif
}
struct some_struct { int i; int j; double z; };
bool test_stl_allocator2() {
#ifdef __cplusplus
std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
vec.push_back(some_struct());
vec.pop_back();
return vec.size() == 0;
#else
return true;
#endif
}

View file

@ -119,7 +119,7 @@ static void free_items(void* p) {
static void stress(intptr_t tid) {
//bench_start_thread();
uintptr_t r = tid * 43;
const size_t max_item_shift = 5; // 128
const size_t max_item_shift = 5; // 128
const size_t max_item_retained_shift = max_item_shift + 2;
size_t allocs = 100 * ((size_t)SCALE) * (tid % 8 + 1); // some threads do more
size_t retain = allocs / 2;
@ -135,7 +135,7 @@ static void stress(intptr_t tid) {
allocs--;
if (data_top >= data_size) {
data_size += 100000;
data = (void**)custom_realloc(data, data_size * sizeof(void*));
data = (void**)custom_realloc(data, data_size * sizeof(void*));
}
data[data_top++] = alloc_items(1ULL << (pick(&r) % max_item_shift), &r);
}
@ -255,7 +255,6 @@ static void* atomic_exchange_ptr(volatile void** p, void* newval) {
#else
#include <pthread.h>
#include <stdatomic.h>
static void* thread_entry(void* param) {
stress((uintptr_t)param);
@ -275,8 +274,16 @@ static void run_os_threads(size_t nthreads) {
custom_free(threads);
}
#ifdef __cplusplus
#include <atomic>
static void* atomic_exchange_ptr(volatile void** p, void* newval) {
return std::atomic_exchange_explicit((volatile std::atomic<void*>*)p, newval, std::memory_order_acquire);
}
#else
#include <stdatomic.h>
static void* atomic_exchange_ptr(volatile void** p, void* newval) {
return atomic_exchange_explicit((volatile _Atomic(void*)*)p, newval, memory_order_acquire);
}
#endif
#endif