mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-07-06 11:34:38 +03:00
merge from dev
This commit is contained in:
commit
53aa46890a
6 changed files with 177 additions and 98 deletions
|
@ -7,11 +7,15 @@
|
|||
#include <mimalloc.h>
|
||||
#include <new>
|
||||
#include <vector>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
#include <thread>
|
||||
#include <mimalloc.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <mimalloc-new-delete.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
static void msleep(unsigned long msecs) { Sleep(msecs); }
|
||||
|
@ -25,14 +29,16 @@ void heap_no_delete(); // issue #202
|
|||
void heap_late_free(); // issue #204
|
||||
void padding_shrink(); // issue #209
|
||||
void various_tests();
|
||||
void test_mt_shutdown();
|
||||
|
||||
int main() {
|
||||
mi_stats_reset(); // ignore earlier allocations
|
||||
heap_thread_free_large();
|
||||
heap_no_delete();
|
||||
heap_late_free();
|
||||
padding_shrink();
|
||||
heap_no_delete();
|
||||
heap_late_free();
|
||||
padding_shrink();
|
||||
various_tests();
|
||||
//test_mt_shutdown();
|
||||
mi_stats_print(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
@ -53,20 +59,20 @@ public:
|
|||
};
|
||||
|
||||
|
||||
void various_tests() {
|
||||
void various_tests() {
|
||||
atexit(free_p);
|
||||
void* p1 = malloc(78);
|
||||
void* p2 = mi_malloc_aligned(16,24);
|
||||
free(p1);
|
||||
void* p2 = mi_malloc_aligned(16, 24);
|
||||
free(p1);
|
||||
p1 = malloc(8);
|
||||
char* s = mi_strdup("hello\n");
|
||||
|
||||
|
||||
//char* s = _strdup("hello\n");
|
||||
//char* buf = NULL;
|
||||
//size_t len;
|
||||
//_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE");
|
||||
//mi_free(buf);
|
||||
|
||||
|
||||
mi_free(p2);
|
||||
p2 = malloc(16);
|
||||
p1 = realloc(p1, 32);
|
||||
|
@ -76,7 +82,7 @@ void various_tests() {
|
|||
Test* t = new Test(42);
|
||||
delete t;
|
||||
t = new (std::nothrow) Test(42);
|
||||
delete t;
|
||||
delete t;
|
||||
}
|
||||
|
||||
class Static {
|
||||
|
@ -105,7 +111,7 @@ bool test_stl_allocator1() {
|
|||
|
||||
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;
|
||||
vec.push_back(some_struct());
|
||||
vec.pop_back();
|
||||
|
@ -117,13 +123,13 @@ bool test_stl_allocator2() {
|
|||
// Issue #202
|
||||
void heap_no_delete_worker() {
|
||||
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
|
||||
}
|
||||
|
||||
void heap_no_delete() {
|
||||
auto t1 = std::thread(heap_no_delete_worker);
|
||||
t1.join();
|
||||
t1.join();
|
||||
}
|
||||
|
||||
|
||||
|
@ -173,3 +179,29 @@ void heap_thread_free_large() {
|
|||
t1.join();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void test_mt_shutdown()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue