From 4369fe43239c56ad017911ef1b704b666a3e35e8 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Thu, 24 Jun 2021 17:29:06 +0800 Subject: [PATCH 1/3] Eliminate preprocessor warnings due to undefined "__GNUC__" with ClangCL When building some code against mimalloc with C inside Visual Studio with ClangCL, the compiler complains about __GNUC__ being undefined. Reported by Mojca Miklavec. Close #422 --- include/mimalloc-internal.h | 6 +++--- include/mimalloc.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 1e1a7966..3be872af 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -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_thread __declspec(thread) #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_thread __thread #define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE))) @@ -236,7 +236,7 @@ static inline bool mi_malloc_satisfies_alignment(size_t alignment, size_t size) } // Overflow detecting multiply -#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5 +#if __has_builtin(__builtin_umul_overflow) || (defined(__GNUC__) && (__GNUC__ >= 5)) #include // UINT_MAX, ULONG_MAX #if defined(_CLOCK_T) // for Illumos #undef _CLOCK_T @@ -903,7 +903,7 @@ static inline void _mi_memcpy(void* dst, const void* src, size_t n) { // 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. #include static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) { diff --git a/include/mimalloc.h b/include/mimalloc.h index fe5aa8f3..7ebf3e60 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -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) #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)) #elif (_MSC_VER >= 1700) #define mi_decl_nodiscard _Check_return_ From a3cf23c19fff6fd34492bee85788f2e52e41531b Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 26 Jul 2021 19:10:21 -0700 Subject: [PATCH 2/3] add test for #445 --- test/main-override.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/main-override.cpp b/test/main-override.cpp index 5fe3f9a3..d7663330 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -34,6 +34,7 @@ void various_tests(); void test_mt_shutdown(); void fail_aslr(); // issue #372 void tsan_numa_test(); // issue #414 +void strdup_test(); // issue #445 int main() { mi_stats_reset(); // ignore earlier allocations @@ -43,6 +44,7 @@ int main() { padding_shrink(); various_tests(); tsan_numa_test(); + strdup_test(); //test_mt_shutdown(); //fail_aslr(); @@ -74,18 +76,13 @@ void various_tests() { 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); free(p1); free(p2); mi_free(s); + Test* t = new Test(42); delete t; t = new (std::nothrow) Test(42); @@ -125,7 +122,17 @@ bool test_stl_allocator2() { 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 void heap_no_delete_worker() { From 32c5e4774febc4f50348bd08af5b80cfc8745857 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 26 Jul 2021 19:14:29 -0700 Subject: [PATCH 3/3] update tests with static --- test/main-override.cpp | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/test/main-override.cpp b/test/main-override.cpp index d7663330..e1795ecb 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -26,15 +26,15 @@ static void msleep(unsigned long msecs) { Sleep(msecs); } static void msleep(unsigned long msecs) { usleep(msecs * 1000UL); } #endif -void heap_thread_free_large(); // issue #221 -void heap_no_delete(); // issue #202 -void heap_late_free(); // issue #204 -void padding_shrink(); // issue #209 -void various_tests(); -void test_mt_shutdown(); -void fail_aslr(); // issue #372 -void tsan_numa_test(); // issue #414 -void strdup_test(); // issue #445 +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(); +static void fail_aslr(); // issue #372 +static void tsan_numa_test(); // issue #414 +static void strdup_test(); // issue #445 int main() { mi_stats_reset(); // ignore earlier allocations @@ -68,7 +68,7 @@ public: }; -void various_tests() { +static void various_tests() { atexit(free_p); void* p1 = malloc(78); void* p2 = mi_malloc_aligned(16, 24); @@ -106,7 +106,7 @@ public: static Static s = Static(); -bool test_stl_allocator1() { +static bool test_stl_allocator1() { std::vector > vec; vec.push_back(1); vec.pop_back(); @@ -115,7 +115,7 @@ bool test_stl_allocator1() { struct some_struct { int i; int j; double z; }; -bool test_stl_allocator2() { +static bool test_stl_allocator2() { std::vector > vec; vec.push_back(some_struct()); vec.pop_back(); @@ -135,28 +135,28 @@ static void strdup_test() { } // Issue #202 -void heap_no_delete_worker() { +static 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() { +static void heap_no_delete() { auto t1 = std::thread(heap_no_delete_worker); t1.join(); } // Issue #204 -volatile void* global_p; +static volatile void* global_p; -void t1main() { +static void t1main() { mi_heap_t* heap = mi_heap_new(); global_p = mi_heap_malloc(heap, 1024); mi_heap_delete(heap); } -void heap_late_free() { +static void heap_late_free() { auto t1 = std::thread(t1main); msleep(2000); @@ -173,7 +173,7 @@ static void alloc0(/* void* arg */) shared_p = mi_malloc(8); } -void padding_shrink(void) +static void padding_shrink(void) { auto t1 = std::thread(alloc0); t1.join(); @@ -182,11 +182,11 @@ void padding_shrink(void) // Issue #221 -void heap_thread_free_large_worker() { +static void heap_thread_free_large_worker() { mi_free(shared_p); } -void heap_thread_free_large() { +static void heap_thread_free_large() { for (int i = 0; i < 100; i++) { shared_p = mi_malloc_aligned(2*1024*1024 + 1, 8); auto t1 = std::thread(heap_thread_free_large_worker); @@ -196,7 +196,7 @@ void heap_thread_free_large() { -void test_mt_shutdown() +static void test_mt_shutdown() { const int threads = 5; std::vector< std::future< std::vector< char* > > > ts; @@ -221,7 +221,7 @@ void test_mt_shutdown() } // issue #372 -void fail_aslr() { +static void fail_aslr() { size_t sz = (4ULL << 40); // 4TiB void* p = malloc(sz); printf("pointer p: %p: area up to %p\n", p, (uint8_t*)p + sz); @@ -229,12 +229,12 @@ void fail_aslr() { } // issues #414 -void dummy_worker() { +static void dummy_worker() { void* p = mi_malloc(0); mi_free(p); } -void tsan_numa_test() { +static void tsan_numa_test() { auto t1 = std::thread(dummy_worker); dummy_worker(); t1.join();