merge from dev

This commit is contained in:
Daan Leijen 2021-10-18 16:28:08 -07:00
commit 54659aec9e
4 changed files with 46 additions and 36 deletions

View file

@ -22,7 +22,7 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_decl_noinline __declspec(noinline) #define mi_decl_noinline __declspec(noinline)
#define mi_decl_thread __declspec(thread) #define mi_decl_thread __declspec(thread)
#define mi_decl_cache_align __declspec(align(MI_CACHE_LINE)) #define mi_decl_cache_align __declspec(align(MI_CACHE_LINE))
#elif (defined(__GNUC__) && (__GNUC__>=3)) // includes clang and icc #elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
#define mi_decl_noinline __attribute__((noinline)) #define mi_decl_noinline __attribute__((noinline))
#define mi_decl_thread __thread #define mi_decl_thread __thread
#define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE))) #define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE)))
@ -260,7 +260,7 @@ static inline bool mi_malloc_satisfies_alignment(size_t alignment, size_t size)
} }
// Overflow detecting multiply // Overflow detecting multiply
#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5 #if __has_builtin(__builtin_umul_overflow) || (defined(__GNUC__) && (__GNUC__ >= 5))
#include <limits.h> // UINT_MAX, ULONG_MAX #include <limits.h> // UINT_MAX, ULONG_MAX
#if defined(_CLOCK_T) // for Illumos #if defined(_CLOCK_T) // for Illumos
#undef _CLOCK_T #undef _CLOCK_T
@ -1039,7 +1039,7 @@ static inline void _mi_memcpy(void* dst, const void* src, size_t n) {
// This is used for example in `mi_realloc`. // This is used for example in `mi_realloc`.
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
#if (__GNUC__ >= 4) || defined(__clang__) #if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__)
// On GCC/CLang we provide a hint that the pointers are word aligned. // On GCC/CLang we provide a hint that the pointers are word aligned.
#include <string.h> #include <string.h>
static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) { static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) {

View file

@ -26,7 +26,7 @@ terms of the MIT license. A copy of the license can be found in the file
#if defined(__cplusplus) && (__cplusplus >= 201703) #if defined(__cplusplus) && (__cplusplus >= 201703)
#define mi_decl_nodiscard [[nodiscard]] #define mi_decl_nodiscard [[nodiscard]]
#elif (__GNUC__ >= 4) || defined(__clang__) // includes clang, icc, and clang-cl #elif (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) // includes clang, icc, and clang-cl
#define mi_decl_nodiscard __attribute__((warn_unused_result)) #define mi_decl_nodiscard __attribute__((warn_unused_result))
#elif (_MSC_VER >= 1700) #elif (_MSC_VER >= 1700)
#define mi_decl_nodiscard _Check_return_ #define mi_decl_nodiscard _Check_return_

View file

@ -826,7 +826,10 @@ static std_new_handler_t mi_get_new_handler() {
static bool mi_try_new_handler(bool nothrow) { static bool mi_try_new_handler(bool nothrow) {
std_new_handler_t h = mi_get_new_handler(); std_new_handler_t h = mi_get_new_handler();
if (h==NULL) { if (h==NULL) {
if (!nothrow) exit(ENOMEM); // cannot throw in plain C, use exit as we are out of memory anyway. if (!nothrow) {
_mi_error_message(EFAULT, "out of memory in 'new' call"); // cannot throw in plain C, use EFAULT to abort
abort();
}
return false; return false;
} }
else { else {

View file

@ -26,15 +26,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_thread_free_large(); // issue #221 static void heap_thread_free_large(); // issue #221
void heap_no_delete(); // issue #202 static void heap_no_delete(); // issue #202
void heap_late_free(); // issue #204 static void heap_late_free(); // issue #204
void padding_shrink(); // issue #209 static void padding_shrink(); // issue #209
void various_tests(); static void various_tests();
void test_mt_shutdown(); static void test_mt_shutdown();
void large_alloc(void); // issue #363 static void large_alloc(void); // issue #363
void fail_aslr(); // issue #372 static void fail_aslr(); // issue #372
void tsan_numa_test(); // issue #414 static void tsan_numa_test(); // issue #414
static void strdup_test(); // issue #445
int main() { int main() {
mi_stats_reset(); // ignore earlier allocations mi_stats_reset(); // ignore earlier allocations
@ -46,6 +47,7 @@ int main() {
various_tests(); various_tests();
large_alloc(); large_alloc();
tsan_numa_test(); tsan_numa_test();
strdup_test();
//test_mt_shutdown(); //test_mt_shutdown();
//fail_aslr(); //fail_aslr();
@ -69,7 +71,7 @@ public:
}; };
void various_tests() { static void various_tests() {
atexit(free_p); atexit(free_p);
void* p1 = malloc(78); void* p1 = malloc(78);
void* p2 = mi_malloc_aligned(16, 24); void* p2 = mi_malloc_aligned(16, 24);
@ -77,18 +79,13 @@ void various_tests() {
p1 = malloc(8); p1 = malloc(8);
char* s = mi_strdup("hello\n"); 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); 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);
Test* t = new Test(42); Test* t = new Test(42);
delete t; delete t;
t = new (std::nothrow) Test(42); t = new (std::nothrow) Test(42);
@ -112,7 +109,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();
@ -121,38 +118,48 @@ 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() { static 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();
return vec.size() == 0; return vec.size() == 0;
} }
// 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
}
// 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; static 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);
@ -169,7 +176,7 @@ static void alloc0(/* void* arg */)
shared_p = mi_malloc(8); shared_p = mi_malloc(8);
} }
void padding_shrink(void) static void padding_shrink(void)
{ {
auto t1 = std::thread(alloc0); auto t1 = std::thread(alloc0);
t1.join(); t1.join();
@ -178,11 +185,11 @@ void padding_shrink(void)
// Issue #221 // Issue #221
void heap_thread_free_large_worker() { static void heap_thread_free_large_worker() {
mi_free(shared_p); mi_free(shared_p);
} }
void heap_thread_free_large() { static void heap_thread_free_large() {
for (int i = 0; i < 100; i++) { 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); auto t1 = std::thread(heap_thread_free_large_worker);
@ -192,7 +199,7 @@ void heap_thread_free_large() {
void test_mt_shutdown() static void test_mt_shutdown()
{ {
const int threads = 5; const int threads = 5;
std::vector< std::future< std::vector< char* > > > ts; std::vector< std::future< std::vector< char* > > > ts;
@ -229,7 +236,7 @@ void large_alloc(void)
} }
// issue #372 // issue #372
void fail_aslr() { static void fail_aslr() {
size_t sz = (4ULL << 40); // 4TiB size_t sz = (4ULL << 40); // 4TiB
void* p = malloc(sz); void* p = malloc(sz);
printf("pointer p: %p: area up to %p\n", p, (uint8_t*)p + sz); printf("pointer p: %p: area up to %p\n", p, (uint8_t*)p + sz);
@ -237,12 +244,12 @@ void fail_aslr() {
} }
// issues #414 // issues #414
void dummy_worker() { static void dummy_worker() {
void* p = mi_malloc(0); void* p = mi_malloc(0);
mi_free(p); mi_free(p);
} }
void tsan_numa_test() { static void tsan_numa_test() {
auto t1 = std::thread(dummy_worker); auto t1 = std::thread(dummy_worker);
dummy_worker(); dummy_worker();
t1.join(); t1.join();