From 8359523f246f7736094ae6ac32214c9ba5aedb76 Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Tue, 23 Jun 2020 10:38:29 +0800 Subject: [PATCH] fix mingw --- CMakeLists.txt | 18 +++++++++++++----- src/pal/pal_windows.h | 1 + src/test/func/malloc/malloc.cc | 28 +++++++++++++++++++--------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 07fe14f91..702e65934 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ endif() include(CheckCXXCompilerFlag) include(CheckCSourceCompiles) +include(CheckCXXSourceCompiles) option(USE_SNMALLOC_STATS "Track allocation stats" OFF) option(SNMALLOC_CI_BUILD "Disable features not sensible for CI" OFF) @@ -27,6 +28,9 @@ size_t malloc_usable_size(const void* ptr) { return 0; } int main() { return 0; } " CONST_QUALIFIED_MALLOC_USABLE_SIZE) +check_cxx_compiler_flag(-rdynamic SNMALLOC_RDYNAMIC) +check_cxx_compiler_flag(-ftls-model=initial-exec SNMALLOC_STATIC_TLS) + if ((CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT SNMALLOC_CI_BUILD)) option(USE_POSIX_COMMIT_CHECKS "Instrument Posix PAL to check for access to unused blocks of memory." Off) else () @@ -172,14 +176,18 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY) set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG") else() add_compile_options(-fno-exceptions -fno-rtti -g -fomit-frame-pointer) + # Static TLS model is unsupported on Haiku. # All symbols are always dynamic on haiku and -rdynamic is redundant (and unsupported). - if (NOT CMAKE_SYSTEM_NAME MATCHES "Haiku") + if (SNMALLOC_STATIC_TLS) add_compile_options(-ftls-model=initial-exec) - if(SNMALLOC_CI_BUILD OR (${CMAKE_BUILD_TYPE} MATCHES "Debug")) - # Get better stack traces in CI and Debug. - target_link_libraries(snmalloc_lib INTERFACE "-rdynamic") - endif() + endif() + + if(SNMALLOC_CI_BUILD OR (${CMAKE_BUILD_TYPE} MATCHES "Debug")) + # Get better stack traces in CI and Debug. + if (SNMALLOC_RDYNAMIC) + target_link_libraries(snmalloc_lib INTERFACE "-rdynamic") + endif() endif() if(SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE) diff --git a/src/pal/pal_windows.h b/src/pal/pal_windows.h index 39e055603..618be3b29 100644 --- a/src/pal/pal_windows.h +++ b/src/pal/pal_windows.h @@ -6,6 +6,7 @@ #ifdef _WIN32 # ifndef _MSC_VER # include +# include # endif # define WIN32_LEAN_AND_MEAN # ifndef NOMINMAX diff --git a/src/test/func/malloc/malloc.cc b/src/test/func/malloc/malloc.cc index 029428d74..1d54525d6 100644 --- a/src/test/func/malloc/malloc.cc +++ b/src/test/func/malloc/malloc.cc @@ -4,6 +4,12 @@ #define SNMALLOC_NAME_MANGLE(a) our_##a #include "../../../override/malloc.cc" +#if defined(_WIN32) && !defined(_MSC_VER) +# define ST_FMT "I" +#else +# define ST_FMT "z" +#endif + using namespace snmalloc; void check_result(size_t size, size_t align, void* p, int err, bool null) @@ -25,7 +31,7 @@ void check_result(size_t size, size_t align, void* p, int err, bool null) if ((align == 1) && (alloc_size != expected_size)) { printf( - "Usable size is %zu, but required to be %zu.\n", + "Usable size is %" ST_FMT "u, but required to be %" ST_FMT "u.\n", alloc_size, expected_size); abort(); @@ -33,7 +39,8 @@ void check_result(size_t size, size_t align, void* p, int err, bool null) if ((align != 1) && (alloc_size < expected_size)) { printf( - "Usable size is %zu, but required to be at least %zu.\n", + "Usable size is %" ST_FMT "u, but required to be at least %" ST_FMT + "u.\n", alloc_size, expected_size); abort(); @@ -41,7 +48,8 @@ void check_result(size_t size, size_t align, void* p, int err, bool null) if (static_cast(reinterpret_cast(p) % align) != 0) { printf( - "Address is 0x%zx, but required to be aligned to 0x%zx.\n", + "Address is 0x%" ST_FMT "x, but required to be aligned to 0x%" ST_FMT + "x.\n", reinterpret_cast(p), align); abort(); @@ -52,7 +60,7 @@ void check_result(size_t size, size_t align, void* p, int err, bool null) void test_calloc(size_t nmemb, size_t size, int err, bool null) { - fprintf(stderr, "calloc(%zu, %zu)\n", nmemb, size); + fprintf(stderr, "calloc(%" ST_FMT "u, %" ST_FMT "u)\n", nmemb, size); errno = 0; void* p = our_calloc(nmemb, size); @@ -73,7 +81,8 @@ void test_realloc(void* p, size_t size, int err, bool null) if (p != nullptr) old_size = our_malloc_usable_size(p); - fprintf(stderr, "realloc(%p(%zu), %zu)\n", p, old_size, size); + fprintf( + stderr, "realloc(%p(%" ST_FMT "u), %" ST_FMT "u)\n", p, old_size, size); errno = 0; auto new_p = our_realloc(p, size); // Realloc failure case, deallocate original block @@ -84,7 +93,8 @@ void test_realloc(void* p, size_t size, int err, bool null) void test_posix_memalign(size_t size, size_t align, int err, bool null) { - fprintf(stderr, "posix_memalign(&p, %zu, %zu)\n", align, size); + fprintf( + stderr, "posix_memalign(&p, %" ST_FMT "u, %" ST_FMT "u)\n", align, size); void* p = nullptr; errno = our_posix_memalign(&p, align, size); check_result(size, align, p, err, null); @@ -92,7 +102,7 @@ void test_posix_memalign(size_t size, size_t align, int err, bool null) void test_memalign(size_t size, size_t align, int err, bool null) { - fprintf(stderr, "memalign(%zu, %zu)\n", align, size); + fprintf(stderr, "memalign(%" ST_FMT "u, %" ST_FMT "u)\n", align, size); errno = 0; void* p = our_memalign(align, size); check_result(size, align, p, err, null); @@ -112,7 +122,7 @@ int main(int argc, char** argv) for (sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++) { const size_t size = 1ULL << sc; - printf("malloc: %zu\n", size); + printf("malloc: %" ST_FMT "u\n", size); check_result(size, 1, our_malloc(size), SUCCESS, false); check_result(size + 1, 1, our_malloc(size + 1), SUCCESS, false); } @@ -160,7 +170,7 @@ int main(int argc, char** argv) for (sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++) { const size_t size2 = 1ULL << sc2; - printf("size1: %zu, size2:%zu\n", size, size2); + printf("size1: %" ST_FMT "u, size2:%" ST_FMT "u\n", size, size2); test_realloc(our_malloc(size), size2, SUCCESS, false); test_realloc(our_malloc(size + 1), size2, SUCCESS, false); }