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>
|
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-02-13 11:37:48 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
static void msleep(unsigned long msecs) { Sleep(msecs); }
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void heap_no_delete();
|
|
|
|
void heap_late_free();
|
|
|
|
void various_tests();
|
2020-02-13 10:36:39 -08:00
|
|
|
|
|
|
|
int main() {
|
2020-02-13 11:37:48 -08:00
|
|
|
mi_stats_reset(); // ignore earlier allocations
|
|
|
|
// heap_no_delete(); // issue #202
|
|
|
|
// heap_late_free(); // issue #204
|
2020-02-13 13:12:19 -08:00
|
|
|
various_tests();
|
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
|
|
|
|
2020-02-13 11:37:48 -08:00
|
|
|
void various_tests() {
|
2019-06-19 16:26:12 -07:00
|
|
|
atexit(free_p);
|
|
|
|
void* p1 = malloc(78);
|
2019-07-22 01:36:16 -07:00
|
|
|
void* p2 = mi_malloc_aligned(16,24);
|
2019-08-10 09:39:36 -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-02-13 10:36:39 -08:00
|
|
|
|
|
|
|
//char* s = _strdup("hello\n");
|
|
|
|
//char* buf = NULL;
|
|
|
|
//size_t len;
|
|
|
|
//_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE");
|
|
|
|
//mi_free(buf);
|
|
|
|
|
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);
|
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);
|
2019-07-22 10:27:14 -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
|
|
|
|
|
|
|
|
2020-01-20 15:27:05 -08:00
|
|
|
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
|
|
|
|
2020-01-20 17:34:29 -08:00
|
|
|
bool test_stl_allocator2() {
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Issue #202
|
|
|
|
void heap_no_delete_worker() {
|
|
|
|
mi_heap_t* heap = mi_heap_new();
|
|
|
|
void* q = mi_heap_malloc(heap,1024);
|
|
|
|
// mi_heap_delete(heap); // uncomment to prevent assertion
|
|
|
|
}
|
|
|
|
|
|
|
|
void heap_no_delete() {
|
|
|
|
auto t1 = std::thread(heap_no_delete_worker);
|
|
|
|
t1.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Issue #204
|
|
|
|
volatile void* global_p;
|
|
|
|
|
|
|
|
void t1main() {
|
|
|
|
mi_heap_t* heap = mi_heap_new();
|
|
|
|
global_p = mi_heap_malloc(heap, 1024);
|
|
|
|
mi_heap_delete(heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void heap_late_free() {
|
|
|
|
auto t1 = std::thread(t1main);
|
|
|
|
|
|
|
|
msleep(2000);
|
|
|
|
assert(global_p);
|
|
|
|
mi_free((void*)global_p);
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
}
|