Skip to content

Commit

Permalink
fix mingw
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Sep 19, 2020
1 parent e615c33 commit 8359523
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
18 changes: 13 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ()
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifdef _WIN32
# ifndef _MSC_VER
# include <cstdio>
# include <utility>
# endif
# define WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
Expand Down
28 changes: 19 additions & 9 deletions src/test/func/malloc/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,23 +31,25 @@ 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();
}
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();
}
if (static_cast<size_t>(reinterpret_cast<uintptr_t>(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<uintptr_t>(p),
align);
abort();
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -84,15 +93,16 @@ 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);
}

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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 8359523

Please sign in to comment.