Merge branch 'dev-debug' of https://github.com/microsoft/mimalloc into dev-debug

This commit is contained in:
daan 2020-02-17 10:24:15 -08:00
commit 6e08b0c0c8

View File

@ -7,7 +7,7 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <mimalloc.h> #include <mimalloc.h>
#include <mimalloc-new-delete.h> // #include <mimalloc-new-delete.h>
#include <mimalloc-override.h> #include <mimalloc-override.h>
#ifdef _WIN32 #ifdef _WIN32
@ -18,14 +18,16 @@ static void msleep(unsigned long msecs) { Sleep(msecs); }
static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); } static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); }
#endif #endif
void heap_no_delete(); static void heap_no_delete();
void heap_late_free(); static void heap_late_free();
void various_tests(); static void various_tests();
static void dangling_ptr_write();
int main() { int main() {
mi_stats_reset(); // ignore earlier allocations mi_stats_reset(); // ignore earlier allocations
// heap_no_delete(); // issue #202 // heap_no_delete(); // issue #202
// heap_late_free(); // issue #204 // heap_late_free(); // issue #204
// dangling_ptr_write();
various_tests(); various_tests();
mi_stats_print(NULL); mi_stats_print(NULL);
return 0; return 0;
@ -46,35 +48,33 @@ public:
~Test() { } ~Test() { }
}; };
void dangling_ptr_write();
void various_tests() { static void various_tests() {
atexit(free_p); atexit(free_p);
//dangling_ptr_write();
void* p1 = malloc(78); void* p1 = malloc(78);
void* p2 = mi_malloc_aligned(16,24); void* p2 = mi_malloc_aligned(16,24);
free(p1); free(p1);
p1 = malloc(8); p1 = malloc(8);
char* s = _strdup("hello\n"); char* s = _strdup("hello\n");
//char* s = _strdup("hello\n"); //char* s = _strdup("hello\n");
//char* buf = NULL; //char* buf = NULL;
//size_t len; //size_t len;
//_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE"); //_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE");
//mi_free(buf); //mi_free(buf);
mi_free(p2); mi_free(p2);
p2 = malloc(16); p2 = malloc(16);
p1 = realloc(p1, 32); p1 = realloc(p1, 32);
free(p1); free(p1);
free(p2); free(p2);
mi_free(s); mi_free(s);
s[0] = 0; s[0] = 0;
Test* t = new Test(42); Test* t = new Test(42);
delete t; delete t;
// t = new(std::nothrow) Test(42); // does not work with overriding :-( // t = new(std::nothrow) Test(42); // does not work with overriding :-(
t = new Test(42); t = new Test(42);
delete t; delete t;
} }
static void dangling_ptr_write() { static void dangling_ptr_write() {
@ -87,7 +87,7 @@ static void dangling_ptr_write() {
else { else {
p = new uint8_t[16]; p = new uint8_t[16];
// delete p; // delete sets the pointer to an invalid value generally // delete p; // delete sets the pointer to an invalid value generally
free(p); free(p);
} }
p[0] = 0; p[0] = 0;
} }
@ -110,7 +110,7 @@ public:
static Static s = Static(); static Static s = Static();
bool test_stl_allocator1() { static bool test_stl_allocator1() {
std::vector<int, mi_stl_allocator<int> > vec; std::vector<int, mi_stl_allocator<int> > vec;
vec.push_back(1); vec.push_back(1);
vec.pop_back(); vec.pop_back();
@ -119,7 +119,7 @@ bool test_stl_allocator1() {
struct some_struct { int i; int j; double z; }; struct some_struct { int i; int j; double z; };
bool test_stl_allocator2() { bool test_stl_allocator2() {
std::vector<some_struct, mi_stl_allocator<some_struct> > vec; std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
vec.push_back(some_struct()); vec.push_back(some_struct());
vec.pop_back(); vec.pop_back();
@ -129,28 +129,28 @@ bool test_stl_allocator2() {
// Issue #202 // Issue #202
void heap_no_delete_worker() { static void heap_no_delete_worker() {
mi_heap_t* heap = mi_heap_new(); mi_heap_t* heap = mi_heap_new();
void* q = mi_heap_malloc(heap,1024); void* q = mi_heap_malloc(heap,1024);
// mi_heap_delete(heap); // uncomment to prevent assertion // mi_heap_delete(heap); // uncomment to prevent assertion
} }
void heap_no_delete() { static void heap_no_delete() {
auto t1 = std::thread(heap_no_delete_worker); auto t1 = std::thread(heap_no_delete_worker);
t1.join(); t1.join();
} }
// Issue #204 // Issue #204
volatile void* global_p; volatile void* global_p;
void t1main() { static void t1main() {
mi_heap_t* heap = mi_heap_new(); mi_heap_t* heap = mi_heap_new();
global_p = mi_heap_malloc(heap, 1024); global_p = mi_heap_malloc(heap, 1024);
mi_heap_delete(heap); mi_heap_delete(heap);
} }
void heap_late_free() { static void heap_late_free() {
auto t1 = std::thread(t1main); auto t1 = std::thread(t1main);
msleep(2000); msleep(2000);
@ -158,4 +158,4 @@ void heap_late_free() {
mi_free((void*)global_p); mi_free((void*)global_p);
t1.join(); t1.join();
} }