2019-06-19 16:26:12 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2019-10-18 18:11:04 -07:00
|
|
|
#include <stdint.h>
|
2019-06-19 16:26:12 -07:00
|
|
|
|
|
|
|
#include <mimalloc.h>
|
2019-07-14 19:56:33 -07:00
|
|
|
#include <new>
|
2020-01-20 15:27:05 -08:00
|
|
|
#include <vector>
|
2020-05-04 14:31:32 -07:00
|
|
|
#include <future>
|
|
|
|
#include <iostream>
|
2019-07-14 19:56:33 -07:00
|
|
|
|
2020-02-13 10:36:39 -08:00
|
|
|
#include <thread>
|
|
|
|
#include <mimalloc.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2020-08-06 13:37:11 -07:00
|
|
|
#ifdef _WIN32
|
2020-05-04 14:31:32 -07:00
|
|
|
#include <mimalloc-new-delete.h>
|
2020-08-06 13:37:11 -07:00
|
|
|
#endif
|
2020-05-04 14:31:32 -07:00
|
|
|
|
2020-02-13 11:37:48 -08:00
|
|
|
#ifdef _WIN32
|
2020-08-29 19:30:38 -07:00
|
|
|
#include <Windows.h>
|
2020-02-13 11:37:48 -08:00
|
|
|
static void msleep(unsigned long msecs) { Sleep(msecs); }
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); }
|
|
|
|
#endif
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_thread_free_large(); // issue #221
|
|
|
|
static void heap_no_delete(); // issue #202
|
|
|
|
static void heap_late_free(); // issue #204
|
|
|
|
static void padding_shrink(); // issue #209
|
|
|
|
static void various_tests();
|
|
|
|
static void test_mt_shutdown();
|
2021-10-18 16:28:08 -07:00
|
|
|
static void large_alloc(void); // issue #363
|
2021-07-26 19:14:29 -07:00
|
|
|
static void fail_aslr(); // issue #372
|
|
|
|
static void tsan_numa_test(); // issue #414
|
2021-11-10 16:30:21 -08:00
|
|
|
static void strdup_test(); // issue #445
|
|
|
|
static void bench_alloc_large(void); // issue #xxx
|
2022-11-23 09:50:29 -08:00
|
|
|
static void heap_thread_free_huge();
|
2020-02-13 10:36:39 -08:00
|
|
|
|
2022-11-22 16:58:32 -08:00
|
|
|
static void test_stl_allocators();
|
|
|
|
|
2020-02-13 10:36:39 -08:00
|
|
|
int main() {
|
2020-02-13 11:37:48 -08:00
|
|
|
mi_stats_reset(); // ignore earlier allocations
|
2022-11-23 09:50:29 -08:00
|
|
|
heap_thread_free_huge();
|
|
|
|
/*
|
2021-11-10 16:30:21 -08:00
|
|
|
heap_thread_free_large();
|
|
|
|
heap_no_delete();
|
|
|
|
heap_late_free();
|
|
|
|
padding_shrink();
|
|
|
|
various_tests();
|
|
|
|
large_alloc();
|
|
|
|
tsan_numa_test();
|
|
|
|
strdup_test();
|
2022-11-23 09:50:29 -08:00
|
|
|
*/
|
2022-11-22 16:58:32 -08:00
|
|
|
test_stl_allocators();
|
|
|
|
|
2022-01-10 16:07:22 -08:00
|
|
|
test_mt_shutdown();
|
2021-02-24 15:17:35 -08:00
|
|
|
//fail_aslr();
|
2021-11-11 17:45:41 -08:00
|
|
|
bench_alloc_large();
|
2020-02-13 11:37:48 -08:00
|
|
|
mi_stats_print(NULL);
|
2020-02-13 10:36:39 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:26:12 -07:00
|
|
|
static void* p = malloc(8);
|
|
|
|
|
|
|
|
void free_p() {
|
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-04 09:29:33 -07:00
|
|
|
class Test {
|
|
|
|
private:
|
|
|
|
int i;
|
|
|
|
public:
|
|
|
|
Test(int x) { i = x; }
|
|
|
|
~Test() { }
|
|
|
|
};
|
|
|
|
|
2019-07-18 18:59:32 -07:00
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void various_tests() {
|
2019-06-19 16:26:12 -07:00
|
|
|
atexit(free_p);
|
|
|
|
void* p1 = malloc(78);
|
2022-01-10 16:07:22 -08:00
|
|
|
void* p2 = mi_malloc_aligned(24, 16);
|
2020-05-04 14:31:32 -07:00
|
|
|
free(p1);
|
2019-06-19 16:26:12 -07:00
|
|
|
p1 = malloc(8);
|
2019-07-22 01:36:16 -07:00
|
|
|
char* s = mi_strdup("hello\n");
|
2020-05-04 14:31:32 -07:00
|
|
|
|
2019-07-26 13:01:43 -07:00
|
|
|
mi_free(p2);
|
2019-06-19 16:26:12 -07:00
|
|
|
p2 = malloc(16);
|
|
|
|
p1 = realloc(p1, 32);
|
|
|
|
free(p1);
|
2019-10-17 09:21:20 -07:00
|
|
|
free(p2);
|
2019-07-26 13:01:43 -07:00
|
|
|
mi_free(s);
|
2021-07-26 19:10:21 -07:00
|
|
|
|
2019-07-04 09:29:33 -07:00
|
|
|
Test* t = new Test(42);
|
|
|
|
delete t;
|
2019-07-14 19:56:33 -07:00
|
|
|
t = new (std::nothrow) Test(42);
|
2020-05-04 14:31:32 -07:00
|
|
|
delete t;
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Static {
|
|
|
|
private:
|
|
|
|
void* p;
|
|
|
|
public:
|
|
|
|
Static() {
|
|
|
|
p = malloc(64);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
~Static() {
|
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static Static s = Static();
|
2019-10-18 18:11:04 -07:00
|
|
|
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static bool test_stl_allocator1() {
|
2020-01-20 17:34:29 -08:00
|
|
|
std::vector<int, mi_stl_allocator<int> > vec;
|
2020-01-20 15:27:05 -08:00
|
|
|
vec.push_back(1);
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:34:29 -08:00
|
|
|
struct some_struct { int i; int j; double z; };
|
2020-01-20 15:27:05 -08:00
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static bool test_stl_allocator2() {
|
2020-01-20 17:34:29 -08:00
|
|
|
std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
|
2020-01-20 15:27:05 -08:00
|
|
|
vec.push_back(some_struct());
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
2020-02-13 10:36:39 -08:00
|
|
|
}
|
2020-02-13 11:37:48 -08:00
|
|
|
|
2022-11-22 16:58:32 -08:00
|
|
|
static bool test_stl_allocator3() {
|
|
|
|
std::vector<int, mi_heap_stl_allocator<int> > vec;
|
|
|
|
vec.push_back(1);
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_stl_allocator4() {
|
|
|
|
std::vector<some_struct, mi_heap_stl_allocator<some_struct> > vec;
|
|
|
|
vec.push_back(some_struct());
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_stl_allocator5() {
|
|
|
|
std::vector<int, mi_heap_destroy_stl_allocator<int> > vec;
|
|
|
|
vec.push_back(1);
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_stl_allocator6() {
|
|
|
|
std::vector<some_struct, mi_heap_destroy_stl_allocator<some_struct> > vec;
|
|
|
|
vec.push_back(some_struct());
|
|
|
|
vec.pop_back();
|
|
|
|
return vec.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_stl_allocators() {
|
|
|
|
test_stl_allocator1();
|
|
|
|
test_stl_allocator2();
|
|
|
|
test_stl_allocator3();
|
|
|
|
test_stl_allocator4();
|
|
|
|
test_stl_allocator5();
|
|
|
|
test_stl_allocator6();
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:10:21 -07:00
|
|
|
// issue 445
|
|
|
|
static void strdup_test() {
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
char* s = _strdup("hello\n");
|
|
|
|
char* buf = NULL;
|
|
|
|
size_t len;
|
|
|
|
_dupenv_s(&buf, &len, "MIMALLOC_VERBOSE");
|
|
|
|
mi_free(buf);
|
|
|
|
mi_free(s);
|
|
|
|
#endif
|
|
|
|
}
|
2020-02-13 11:37:48 -08:00
|
|
|
|
|
|
|
// Issue #202
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_no_delete_worker() {
|
2020-02-13 11:37:48 -08:00
|
|
|
mi_heap_t* heap = mi_heap_new();
|
2020-05-04 14:31:32 -07:00
|
|
|
void* q = mi_heap_malloc(heap, 1024);
|
2020-02-13 11:37:48 -08:00
|
|
|
// mi_heap_delete(heap); // uncomment to prevent assertion
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_no_delete() {
|
2020-02-13 11:37:48 -08:00
|
|
|
auto t1 = std::thread(heap_no_delete_worker);
|
2020-05-04 14:31:32 -07:00
|
|
|
t1.join();
|
2020-02-13 11:37:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Issue #204
|
2021-07-26 19:14:29 -07:00
|
|
|
static volatile void* global_p;
|
2020-02-13 11:37:48 -08:00
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void t1main() {
|
2020-02-13 11:37:48 -08:00
|
|
|
mi_heap_t* heap = mi_heap_new();
|
|
|
|
global_p = mi_heap_malloc(heap, 1024);
|
|
|
|
mi_heap_delete(heap);
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_late_free() {
|
2020-02-13 11:37:48 -08:00
|
|
|
auto t1 = std::thread(t1main);
|
|
|
|
|
|
|
|
msleep(2000);
|
|
|
|
assert(global_p);
|
|
|
|
mi_free((void*)global_p);
|
|
|
|
|
|
|
|
t1.join();
|
2020-02-18 20:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// issue #209
|
|
|
|
static void* shared_p;
|
|
|
|
static void alloc0(/* void* arg */)
|
|
|
|
{
|
|
|
|
shared_p = mi_malloc(8);
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void padding_shrink(void)
|
2020-02-18 20:05:30 -08:00
|
|
|
{
|
|
|
|
auto t1 = std::thread(alloc0);
|
|
|
|
t1.join();
|
|
|
|
mi_free(shared_p);
|
|
|
|
}
|
2020-04-06 08:32:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Issue #221
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_thread_free_large_worker() {
|
2020-04-06 08:32:25 -07:00
|
|
|
mi_free(shared_p);
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void heap_thread_free_large() {
|
2020-04-06 08:32:25 -07:00
|
|
|
for (int i = 0; i < 100; i++) {
|
2021-02-22 15:05:47 -08:00
|
|
|
shared_p = mi_malloc_aligned(2 * 1024 * 1024 + 1, 8);
|
2020-04-06 08:32:25 -07:00
|
|
|
auto t1 = std::thread(heap_thread_free_large_worker);
|
|
|
|
t1.join();
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 14:31:32 -07:00
|
|
|
|
2022-11-23 09:50:29 -08:00
|
|
|
static void heap_thread_free_huge_worker() {
|
|
|
|
mi_free(shared_p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void heap_thread_free_huge() {
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
shared_p = mi_malloc(1024 * 1024 * 1024);
|
|
|
|
auto t1 = std::thread(heap_thread_free_large_worker);
|
|
|
|
t1.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:31:32 -07:00
|
|
|
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void test_mt_shutdown()
|
2020-05-04 14:31:32 -07:00
|
|
|
{
|
|
|
|
const int threads = 5;
|
|
|
|
std::vector< std::future< std::vector< char* > > > ts;
|
|
|
|
|
|
|
|
auto fn = [&]()
|
|
|
|
{
|
|
|
|
std::vector< char* > ps;
|
|
|
|
ps.reserve(1000);
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
ps.emplace_back(new char[1]);
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < threads; i++)
|
|
|
|
ts.emplace_back(std::async(std::launch::async, fn));
|
|
|
|
|
|
|
|
for (auto& f : ts)
|
|
|
|
for (auto& p : f.get())
|
|
|
|
delete[] p;
|
|
|
|
|
|
|
|
std::cout << "done" << std::endl;
|
|
|
|
}
|
2021-02-22 15:05:47 -08:00
|
|
|
|
|
|
|
// issue #363
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void large_alloc(void)
|
|
|
|
{
|
|
|
|
char* a = new char[1ull << 25];
|
|
|
|
thread th([&] {
|
|
|
|
delete[] a;
|
|
|
|
});
|
|
|
|
th.join();
|
2021-02-24 15:17:35 -08:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:14:17 -08:00
|
|
|
// issue #372
|
2021-07-26 19:14:29 -07:00
|
|
|
static void fail_aslr() {
|
2021-02-24 15:14:17 -08:00
|
|
|
size_t sz = (4ULL << 40); // 4TiB
|
|
|
|
void* p = malloc(sz);
|
|
|
|
printf("pointer p: %p: area up to %p\n", p, (uint8_t*)p + sz);
|
2021-02-24 15:49:43 -08:00
|
|
|
*(int*)0x5FFFFFFF000 = 0; // should segfault
|
2021-06-17 19:47:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// issues #414
|
2021-07-26 19:14:29 -07:00
|
|
|
static void dummy_worker() {
|
2021-06-17 19:47:41 -07:00
|
|
|
void* p = mi_malloc(0);
|
2021-11-10 16:30:21 -08:00
|
|
|
mi_free(p);
|
2021-06-17 19:47:41 -07:00
|
|
|
}
|
|
|
|
|
2021-07-26 19:14:29 -07:00
|
|
|
static void tsan_numa_test() {
|
2021-06-17 19:47:41 -07:00
|
|
|
auto t1 = std::thread(dummy_worker);
|
|
|
|
dummy_worker();
|
|
|
|
t1.join();
|
2021-11-10 16:30:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|