From 4a3102fedb503c8965bb8b7a73afa368b1df42b6 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Tue, 30 Jun 2020 11:01:49 +0100 Subject: [PATCH] Haiku support proposal. (#218) * Haiku support proposal. Basic PAL implementation. This platform does not support TLS modes. * MAP_NORESERVE usage --- CMakeLists.txt | 6 +++- src/pal/pal.h | 3 ++ src/pal/pal_haiku.h | 84 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/pal/pal_haiku.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d174ec7..7014b4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,11 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi") set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG") else() - add_compile_options(-fno-exceptions -fno-rtti -g -ftls-model=initial-exec -fomit-frame-pointer) + add_compile_options(-fno-exceptions -fno-rtti -g -fomit-frame-pointer) + # Static TLS model unsupported on Haiku + if (NOT CMAKE_SYSTEM_NAME MATCHES "Haiku") + add_compile_options(-ftls-model=initial-exec) + endif() 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") diff --git a/src/pal/pal.h b/src/pal/pal.h index 1324c53..64e7f33 100644 --- a/src/pal/pal.h +++ b/src/pal/pal.h @@ -10,6 +10,7 @@ # include "pal_apple.h" # include "pal_freebsd.h" # include "pal_freebsd_kernel.h" +# include "pal_haiku.h" # include "pal_linux.h" # include "pal_netbsd.h" # include "pal_openbsd.h" @@ -31,6 +32,8 @@ namespace snmalloc PALFreeBSDKernel; # elif defined(__FreeBSD__) PALFreeBSD; +# elif defined(__HAIKU__) + PALHaiku; # elif defined(__NetBSD__) PALNetBSD; # elif defined(__OpenBSD__) diff --git a/src/pal/pal_haiku.h b/src/pal/pal_haiku.h new file mode 100644 index 0000000..6c5340f --- /dev/null +++ b/src/pal/pal_haiku.h @@ -0,0 +1,84 @@ +#pragma once + +#if defined(__HAIKU__) +# include "pal_posix.h" + +# include + +namespace snmalloc +{ + /** + * Platform abstraction layer for Haiku. This provides features for this + * system. + */ + class PALHaiku : public PALPOSIX + { + public: + /** + * Bitmap of PalFeatures flags indicating the optional features that this + * PAL supports. + * + */ + static constexpr uint64_t pal_features = PALPOSIX::pal_features; + + /** + * Notify platform that we will not be needing these pages. + * Haiku does not provide madvise call per say only the posix equivalent. + */ + void notify_not_using(void* p, size_t size) noexcept + { + SNMALLOC_ASSERT(is_aligned_block(p, size)); + posix_madvise(p, size, POSIX_MADV_DONTNEED); + } + + /** + * OS specific function for zeroing memory + * using MAP_NORESERVE for explicit over commit appliance. + */ + template + void zero(void* p, size_t size) + { + if (page_aligned || is_aligned_block(p, size)) + { + SNMALLOC_ASSERT(is_aligned_block(p, size)); + void* r = mmap( + p, + size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_NORESERVE, + -1, + 0); + + if (r != MAP_FAILED) + return; + } + + bzero(p, size); + } + + /** + * Reserve memory using MAP_NORESERVE for explicit + * over commit applicance. + */ + std::pair reserve_at_least(size_t size) + { + constexpr size_t min_size = + bits::is64() ? bits::one_at_bit(32) : bits::one_at_bit(28); + auto size_request = bits::max(size, min_size); + + void* p = mmap( + nullptr, + size_request, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, + -1, + 0); + + if (p == MAP_FAILED) + error("Out of memory"); + + return {p, size_request}; + } + }; +} // namespace snmalloc +#endif