summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt2
-rw-r--r--test/main-override-static.c7
-rw-r--r--test/main-override.cpp70
-rw-r--r--test/test-stress.c28
4 files changed, 82 insertions, 25 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 054d940..bb8dc97 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -16,7 +16,7 @@ if (NOT CMAKE_BUILD_TYPE)
endif()
# Import mimalloc (if installed)
-find_package(mimalloc 1.7 REQUIRED NO_SYSTEM_ENVIRONMENT_PATH)
+find_package(mimalloc 2.0 REQUIRED NO_SYSTEM_ENVIRONMENT_PATH)
message(STATUS "Found mimalloc installed at: ${MIMALLOC_LIBRARY_DIR} (${MIMALLOC_VERSION_DIR})")
# overriding with a dynamic library
diff --git a/test/main-override-static.c b/test/main-override-static.c
index a5088d3..adc07ae 100644
--- a/test/main-override-static.c
+++ b/test/main-override-static.c
@@ -7,6 +7,7 @@
#include <mimalloc.h>
#include <mimalloc-override.h> // redefines malloc etc.
+
static void double_free1();
static void double_free2();
static void corrupt_free();
@@ -19,7 +20,6 @@ static void negative_stat(void);
static void alloc_huge(void);
static void test_heap_walk(void);
-
int main() {
mi_version();
mi_stats_reset();
@@ -32,8 +32,8 @@ int main() {
// invalid_free();
// test_reserved();
// negative_stat();
- test_heap_walk();
// alloc_huge();
+ test_heap_walk();
void* p1 = malloc(78);
void* p2 = malloc(24);
@@ -47,7 +47,7 @@ int main() {
free(p1);
free(p2);
free(s);
-
+
/* now test if override worked by allocating/freeing across the api's*/
//p1 = mi_malloc(32);
//free(p1);
@@ -383,4 +383,3 @@ static void mi_bins(void) {
}
}
#endif
-
diff --git a/test/main-override.cpp b/test/main-override.cpp
index f748c75..e0dba5a 100644
--- a/test/main-override.cpp
+++ b/test/main-override.cpp
@@ -32,22 +32,27 @@ static void heap_late_free(); // issue #204
static void padding_shrink(); // issue #209
static void various_tests();
static void test_mt_shutdown();
+static void large_alloc(void); // issue #363
static void fail_aslr(); // issue #372
static void tsan_numa_test(); // issue #414
-static void strdup_test(); // issue #445
+static void strdup_test(); // issue #445
+static void bench_alloc_large(void); // issue #xxx
int main() {
mi_stats_reset(); // ignore earlier allocations
- heap_thread_free_large();
- heap_no_delete();
- heap_late_free();
- padding_shrink();
- various_tests();
- tsan_numa_test();
- strdup_test();
+
+ heap_thread_free_large();
+ heap_no_delete();
+ heap_late_free();
+ padding_shrink();
+ various_tests();
+ large_alloc();
+ tsan_numa_test();
+ strdup_test();
test_mt_shutdown();
//fail_aslr();
+ bench_alloc_large();
mi_stats_print(NULL);
return 0;
}
@@ -188,7 +193,7 @@ static void heap_thread_free_large_worker() {
static void heap_thread_free_large() {
for (int i = 0; i < 100; i++) {
- shared_p = mi_malloc_aligned(2*1024*1024 + 1, 8);
+ shared_p = mi_malloc_aligned(2 * 1024 * 1024 + 1, 8);
auto t1 = std::thread(heap_thread_free_large_worker);
t1.join();
}
@@ -220,6 +225,18 @@ static void test_mt_shutdown()
std::cout << "done" << std::endl;
}
+// issue #363
+using namespace std;
+
+void large_alloc(void)
+{
+ char* a = new char[1ull << 25];
+ thread th([&] {
+ delete[] a;
+ });
+ th.join();
+}
+
// issue #372
static void fail_aslr() {
size_t sz = (4ULL << 40); // 4TiB
@@ -231,11 +248,42 @@ static void fail_aslr() {
// issues #414
static void dummy_worker() {
void* p = mi_malloc(0);
- mi_free(p);
+ mi_free(p);
}
static void tsan_numa_test() {
auto t1 = std::thread(dummy_worker);
dummy_worker();
t1.join();
-} \ No newline at end of file
+}
+
+// issue #?
+#include <chrono>
+#include <random>
+#include <iostream>
+
+static void bench_alloc_large(void) {
+ static constexpr int kNumBuffers = 20;
+ static constexpr size_t kMinBufferSize = 5 * 1024 * 1024;
+ static constexpr size_t kMaxBufferSize = 25 * 1024 * 1024;
+ std::unique_ptr<char[]> buffers[kNumBuffers];
+
+ std::random_device rd;
+ std::mt19937 gen(42); //rd());
+ std::uniform_int_distribution<> size_distribution(kMinBufferSize, kMaxBufferSize);
+ std::uniform_int_distribution<> buf_number_distribution(0, kNumBuffers - 1);
+
+ static constexpr int kNumIterations = 2000;
+ const auto start = std::chrono::steady_clock::now();
+ for (int i = 0; i < kNumIterations; ++i) {
+ int buffer_idx = buf_number_distribution(gen);
+ size_t new_size = size_distribution(gen);
+ buffers[buffer_idx] = std::make_unique<char[]>(new_size);
+ }
+ const auto end = std::chrono::steady_clock::now();
+ const auto num_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
+ const auto us_per_allocation = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / kNumIterations;
+ std::cout << kNumIterations << " allocations Done in " << num_ms << "ms." << std::endl;
+ std::cout << "Avg " << us_per_allocation << " us per allocation" << std::endl;
+}
+
diff --git a/test/test-stress.c b/test/test-stress.c
index 498b7ec..ff5fffe 100644
--- a/test/test-stress.c
+++ b/test/test-stress.c
@@ -39,12 +39,12 @@ static size_t use_one_size = 0; // use single object size of `N * s
// #define USE_STD_MALLOC
#ifdef USE_STD_MALLOC
-#define custom_calloc(n,s) calloc(n,s)
+#define custom_calloc(n,s) malloc(n*s)
#define custom_realloc(p,s) realloc(p,s)
#define custom_free(p) free(p)
#else
#include <mimalloc.h>
-#define custom_calloc(n,s) mi_calloc(n,s)
+#define custom_calloc(n,s) mi_malloc(n*s)
#define custom_realloc(p,s) mi_realloc(p,s)
#define custom_free(p) mi_free(p)
#endif
@@ -182,17 +182,20 @@ static void run_os_threads(size_t nthreads, void (*entry)(intptr_t tid));
static void test_stress(void) {
uintptr_t r = rand();
for (int n = 0; n < ITER; n++) {
- run_os_threads(THREADS, &stress);
+ run_os_threads(THREADS, &stress);
for (int i = 0; i < TRANSFERS; i++) {
if (chance(50, &r) || n + 1 == ITER) { // free all on last run, otherwise free half of the transfers
void* p = atomic_exchange_ptr(&transfer[i], NULL);
free_items(p);
}
}
- // mi_collect(false);
-#if !defined(NDEBUG) || defined(MI_TSAN)
+ #ifndef NDEBUG
+ //mi_collect(false);
+ //mi_debug_show_arenas();
+ #endif
+ #if !defined(NDEBUG) || defined(MI_TSAN)
if ((n + 1) % 10 == 0) { printf("- iterations left: %3d\n", ITER - (n + 1)); }
-#endif
+ #endif
}
}
@@ -244,16 +247,23 @@ int main(int argc, char** argv) {
// Run ITER full iterations where half the objects in the transfer buffer survive to the next round.
srand(0x7feb352d);
- // mi_stats_reset();
+
+ //mi_reserve_os_memory(512ULL << 20, true, true);
+
+#if !defined(NDEBUG) && !defined(USE_STD_MALLOC)
+ mi_stats_reset();
+#endif
+
#ifdef STRESS
- test_stress();
+ test_stress();
#else
- test_leak();
+ test_leak();
#endif
#ifndef USE_STD_MALLOC
#ifndef NDEBUG
mi_collect(true);
+ //mi_debug_show_arenas();
#endif
mi_stats_print(NULL);
#endif