Skip to content

Commit 8359523

Browse files
fix mingw
1 parent e615c33 commit 8359523

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

CMakeLists.txt

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ endif()
88

99
include(CheckCXXCompilerFlag)
1010
include(CheckCSourceCompiles)
11+
include(CheckCXXSourceCompiles)
1112

1213
option(USE_SNMALLOC_STATS "Track allocation stats" OFF)
1314
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; }
2728
int main() { return 0; }
2829
" CONST_QUALIFIED_MALLOC_USABLE_SIZE)
2930

31+
check_cxx_compiler_flag(-rdynamic SNMALLOC_RDYNAMIC)
32+
check_cxx_compiler_flag(-ftls-model=initial-exec SNMALLOC_STATIC_TLS)
33+
3034
if ((CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT SNMALLOC_CI_BUILD))
3135
option(USE_POSIX_COMMIT_CHECKS "Instrument Posix PAL to check for access to unused blocks of memory." Off)
3236
else ()
@@ -172,14 +176,18 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY)
172176
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")
173177
else()
174178
add_compile_options(-fno-exceptions -fno-rtti -g -fomit-frame-pointer)
179+
175180
# Static TLS model is unsupported on Haiku.
176181
# All symbols are always dynamic on haiku and -rdynamic is redundant (and unsupported).
177-
if (NOT CMAKE_SYSTEM_NAME MATCHES "Haiku")
182+
if (SNMALLOC_STATIC_TLS)
178183
add_compile_options(-ftls-model=initial-exec)
179-
if(SNMALLOC_CI_BUILD OR (${CMAKE_BUILD_TYPE} MATCHES "Debug"))
180-
# Get better stack traces in CI and Debug.
181-
target_link_libraries(snmalloc_lib INTERFACE "-rdynamic")
182-
endif()
184+
endif()
185+
186+
if(SNMALLOC_CI_BUILD OR (${CMAKE_BUILD_TYPE} MATCHES "Debug"))
187+
# Get better stack traces in CI and Debug.
188+
if (SNMALLOC_RDYNAMIC)
189+
target_link_libraries(snmalloc_lib INTERFACE "-rdynamic")
190+
endif()
183191
endif()
184192

185193
if(SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE)

src/pal/pal_windows.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifdef _WIN32
77
# ifndef _MSC_VER
88
# include <cstdio>
9+
# include <utility>
910
# endif
1011
# define WIN32_LEAN_AND_MEAN
1112
# ifndef NOMINMAX

src/test/func/malloc/malloc.cc

+19-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#define SNMALLOC_NAME_MANGLE(a) our_##a
55
#include "../../../override/malloc.cc"
66

7+
#if defined(_WIN32) && !defined(_MSC_VER)
8+
# define ST_FMT "I"
9+
#else
10+
# define ST_FMT "z"
11+
#endif
12+
713
using namespace snmalloc;
814

915
void check_result(size_t size, size_t align, void* p, int err, bool null)
@@ -25,23 +31,25 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
2531
if ((align == 1) && (alloc_size != expected_size))
2632
{
2733
printf(
28-
"Usable size is %zu, but required to be %zu.\n",
34+
"Usable size is %" ST_FMT "u, but required to be %" ST_FMT "u.\n",
2935
alloc_size,
3036
expected_size);
3137
abort();
3238
}
3339
if ((align != 1) && (alloc_size < expected_size))
3440
{
3541
printf(
36-
"Usable size is %zu, but required to be at least %zu.\n",
42+
"Usable size is %" ST_FMT "u, but required to be at least %" ST_FMT
43+
"u.\n",
3744
alloc_size,
3845
expected_size);
3946
abort();
4047
}
4148
if (static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0)
4249
{
4350
printf(
44-
"Address is 0x%zx, but required to be aligned to 0x%zx.\n",
51+
"Address is 0x%" ST_FMT "x, but required to be aligned to 0x%" ST_FMT
52+
"x.\n",
4553
reinterpret_cast<uintptr_t>(p),
4654
align);
4755
abort();
@@ -52,7 +60,7 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
5260

5361
void test_calloc(size_t nmemb, size_t size, int err, bool null)
5462
{
55-
fprintf(stderr, "calloc(%zu, %zu)\n", nmemb, size);
63+
fprintf(stderr, "calloc(%" ST_FMT "u, %" ST_FMT "u)\n", nmemb, size);
5664
errno = 0;
5765
void* p = our_calloc(nmemb, size);
5866

@@ -73,7 +81,8 @@ void test_realloc(void* p, size_t size, int err, bool null)
7381
if (p != nullptr)
7482
old_size = our_malloc_usable_size(p);
7583

76-
fprintf(stderr, "realloc(%p(%zu), %zu)\n", p, old_size, size);
84+
fprintf(
85+
stderr, "realloc(%p(%" ST_FMT "u), %" ST_FMT "u)\n", p, old_size, size);
7786
errno = 0;
7887
auto new_p = our_realloc(p, size);
7988
// Realloc failure case, deallocate original block
@@ -84,15 +93,16 @@ void test_realloc(void* p, size_t size, int err, bool null)
8493

8594
void test_posix_memalign(size_t size, size_t align, int err, bool null)
8695
{
87-
fprintf(stderr, "posix_memalign(&p, %zu, %zu)\n", align, size);
96+
fprintf(
97+
stderr, "posix_memalign(&p, %" ST_FMT "u, %" ST_FMT "u)\n", align, size);
8898
void* p = nullptr;
8999
errno = our_posix_memalign(&p, align, size);
90100
check_result(size, align, p, err, null);
91101
}
92102

93103
void test_memalign(size_t size, size_t align, int err, bool null)
94104
{
95-
fprintf(stderr, "memalign(%zu, %zu)\n", align, size);
105+
fprintf(stderr, "memalign(%" ST_FMT "u, %" ST_FMT "u)\n", align, size);
96106
errno = 0;
97107
void* p = our_memalign(align, size);
98108
check_result(size, align, p, err, null);
@@ -112,7 +122,7 @@ int main(int argc, char** argv)
112122
for (sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
113123
{
114124
const size_t size = 1ULL << sc;
115-
printf("malloc: %zu\n", size);
125+
printf("malloc: %" ST_FMT "u\n", size);
116126
check_result(size, 1, our_malloc(size), SUCCESS, false);
117127
check_result(size + 1, 1, our_malloc(size + 1), SUCCESS, false);
118128
}
@@ -160,7 +170,7 @@ int main(int argc, char** argv)
160170
for (sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++)
161171
{
162172
const size_t size2 = 1ULL << sc2;
163-
printf("size1: %zu, size2:%zu\n", size, size2);
173+
printf("size1: %" ST_FMT "u, size2:%" ST_FMT "u\n", size, size2);
164174
test_realloc(our_malloc(size), size2, SUCCESS, false);
165175
test_realloc(our_malloc(size + 1), size2, SUCCESS, false);
166176
}

0 commit comments

Comments
 (0)