From 7a1c7b7e4e195a7eb2ab87aa6015679a03bde948 Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Mon, 13 Apr 2020 14:08:27 +0800 Subject: [PATCH 1/5] adjust for android --- src/override/malloc.cc | 8 ++++++++ src/pal/pal_posix.h | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 975bd4102..039929165 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -43,10 +43,18 @@ extern "C" return ThreadAlloc::get_noncachable()->alloc(sz); } +#ifndef __ANDROID__ SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(void* ptr) { return Alloc::alloc_size(ptr); } +#else + SNMALLOC_EXPORT + size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) + { + return Alloc::alloc_size(const_cast(ptr)); + } +#endif SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(realloc)(void* ptr, size_t size) { diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 0289e001f..0f7eb8565 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -2,8 +2,10 @@ #include "../ds/address.h" #include "../mem/allocconfig.h" - -#include +#if __has_include() +# define SNMALLOC_HAS_BACKTRACE 1 +# include +#endif #include #include #include @@ -40,6 +42,7 @@ namespace snmalloc static void print_stack_trace() { +#ifdef SNMALLOC_HAS_BACKTRACE constexpr int SIZE = 1024; void* buffer[SIZE]; auto nptrs = backtrace(buffer, SIZE); @@ -47,6 +50,7 @@ namespace snmalloc backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO); puts(""); fflush(stdout); +#endif } /** From 244ab188c13296b98318652e4f98499e6d3c0f0a Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Wed, 15 Apr 2020 10:59:11 +0800 Subject: [PATCH 2/5] update docs --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 2b126c9e1..7f5be764c 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,16 @@ your toolchain: LD_PRELOAD=/usr/local/lib/libsnmallocshim.so ninja ``` +## Cross Compile for Android +Android support is out-of-the-box. + +To cross-compile the library for arm android, you can simply invoke CMake with the toolchain file and the andorid api settings (for more infomation, check this [document](https://developer.android.com/ndk/guides/cmake)). + +For example, you can cross-compile for `arm64-v8a` with the following command: +``` +cmake /path/to/snmalloc -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a +``` + # CMake Feature Flags These can be added to your cmake command line. From 8591aa4a77c11fbd8e14fe12189a93e40d47150f Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Wed, 15 Apr 2020 20:18:57 +0800 Subject: [PATCH 3/5] add const qualifier to unify the path --- src/ds/address.h | 2 +- src/mem/alloc.h | 2 +- src/mem/mediumslab.h | 2 +- src/mem/metaslab.h | 2 +- src/mem/superslab.h | 2 +- src/override/malloc.cc | 10 +--------- 6 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/ds/address.h b/src/ds/address.h index a92ef36a6..e8ecb402f 100644 --- a/src/ds/address.h +++ b/src/ds/address.h @@ -70,7 +70,7 @@ namespace snmalloc * power of two. */ template - SNMALLOC_FAST_PATH T* pointer_align_down(void* p) + SNMALLOC_FAST_PATH T* pointer_align_down(const void* p) { static_assert(alignment > 0); static_assert(bits::next_pow2_const(alignment) == alignment); diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 8cd2dbb43..c58015b1a 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -457,7 +457,7 @@ namespace snmalloc return pointer_cast(external_address(p)); } - static size_t alloc_size(void* p) + static size_t alloc_size(const void* p) { // This must be called on an external pointer. size_t size = ChunkMap::get(address_cast(p)); diff --git a/src/mem/mediumslab.h b/src/mem/mediumslab.h index dcabf8e11..20200247b 100644 --- a/src/mem/mediumslab.h +++ b/src/mem/mediumslab.h @@ -39,7 +39,7 @@ namespace snmalloc return OS_PAGE_SIZE; } - static Mediumslab* get(void* p) + static Mediumslab* get(const void* p) { return pointer_align_down(p); } diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 4d53b77f2..77367f197 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -140,7 +140,7 @@ namespace snmalloc return (slab_end - allocation_start) % size == 0; } - static Slab* get_slab(void* p) + static Slab* get_slab(const void* p) { return pointer_align_down(p); } diff --git a/src/mem/superslab.h b/src/mem/superslab.h index 443e33f80..6cf27180a 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -65,7 +65,7 @@ namespace snmalloc StatusChange = 2 }; - static Superslab* get(void* p) + static Superslab* get(const void* p) { return pointer_align_down(p); } diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 039929165..f8888956f 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -43,18 +43,10 @@ extern "C" return ThreadAlloc::get_noncachable()->alloc(sz); } -#ifndef __ANDROID__ - SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(void* ptr) + SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) { return Alloc::alloc_size(ptr); } -#else - SNMALLOC_EXPORT - size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) - { - return Alloc::alloc_size(const_cast(ptr)); - } -#endif SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(realloc)(void* ptr, size_t size) { From 7cfb413a99b1e9aa1f970d7bdf670e4c48b9ae4f Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Wed, 15 Apr 2020 21:22:29 +0800 Subject: [PATCH 4/5] move const qualifer out of the alignment cast --- src/ds/address.h | 2 +- src/mem/mediumslab.h | 3 ++- src/mem/metaslab.h | 2 +- src/mem/superslab.h | 3 ++- src/override/malloc.cc | 3 ++- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ds/address.h b/src/ds/address.h index e8ecb402f..a92ef36a6 100644 --- a/src/ds/address.h +++ b/src/ds/address.h @@ -70,7 +70,7 @@ namespace snmalloc * power of two. */ template - SNMALLOC_FAST_PATH T* pointer_align_down(const void* p) + SNMALLOC_FAST_PATH T* pointer_align_down(void* p) { static_assert(alignment > 0); static_assert(bits::next_pow2_const(alignment) == alignment); diff --git a/src/mem/mediumslab.h b/src/mem/mediumslab.h index 20200247b..d7632d594 100644 --- a/src/mem/mediumslab.h +++ b/src/mem/mediumslab.h @@ -41,7 +41,8 @@ namespace snmalloc static Mediumslab* get(const void* p) { - return pointer_align_down(p); + return pointer_align_down( + const_cast(p)); } void init(RemoteAllocator* alloc, sizeclass_t sc, size_t rsize) diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 77367f197..ea4200f6d 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -142,7 +142,7 @@ namespace snmalloc static Slab* get_slab(const void* p) { - return pointer_align_down(p); + return pointer_align_down(const_cast(p)); } static bool is_short(Slab* p) diff --git a/src/mem/superslab.h b/src/mem/superslab.h index 6cf27180a..c528e652b 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -67,7 +67,8 @@ namespace snmalloc static Superslab* get(const void* p) { - return pointer_align_down(p); + return pointer_align_down( + const_cast(p)); } static bool is_short_sizeclass(sizeclass_t sizeclass) diff --git a/src/override/malloc.cc b/src/override/malloc.cc index f8888956f..7c87a8d5c 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -43,7 +43,8 @@ extern "C" return ThreadAlloc::get_noncachable()->alloc(sz); } - SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) + SNMALLOC_EXPORT + size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) { return Alloc::alloc_size(ptr); } From 1483cff3df4231aa6dd4df58196195879023285e Mon Sep 17 00:00:00 2001 From: SchrodingerZhu Date: Thu, 16 Apr 2020 14:57:13 +0800 Subject: [PATCH 5/5] check const qualifier in cmake --- CMakeLists.txt | 11 +++++++++++ src/override/malloc.cc | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 13887a6ae..d91019938 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.8) project(snmalloc C CXX) include(CheckCXXCompilerFlag) +include(CheckCSourceCompiles) option(USE_SNMALLOC_STATS "Track allocation stats" OFF) option(SNMALLOC_CI_BUILD "Disable features not sensible for CI" OFF) @@ -12,6 +13,12 @@ option(SNMALLOC_RUST_SUPPORT "Build static library for rust" OFF) option(SNMALLOC_QEMU_WORKAROUND "Disable using madvise(DONT_NEED) to zero memory on Linux" Off) set(CACHE_FRIENDLY_OFFSET OFF CACHE STRING "Base offset to place linked-list nodes.") +CHECK_C_SOURCE_COMPILES(" +#include +size_t malloc_usable_size(const void* ptr) { return 0; } +int main() { return 0; } +" CONST_QUALIFIED_MALLOC_USABLE_SIZE) + 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 () @@ -135,6 +142,10 @@ if(USE_POSIX_COMMIT_CHECKS) target_compile_definitions(snmalloc_lib INTERFACE -DUSE_POSIX_COMMIT_CHECKS) endif() +if(CONST_QUALIFIED_MALLOC_USABLE_SIZE) + target_compile_definitions(snmalloc_lib INTERFACE -DMALLOC_USABLE_SIZE_QUALIFIER=const) +endif() + # To build with just the header library target define SNMALLOC_ONLY_HEADER_LIBRARY # in containing Cmake file. diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 7c87a8d5c..daaa04fd7 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -14,6 +14,10 @@ using namespace snmalloc; # define SNMALLOC_NAME_MANGLE(a) a #endif +#ifndef MALLOC_USABLE_SIZE_QUALIFIER +# define MALLOC_USABLE_SIZE_QUALIFIER +#endif + extern "C" { SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(__malloc_end_pointer)(void* ptr) @@ -44,7 +48,8 @@ extern "C" } SNMALLOC_EXPORT - size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) + size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)( + MALLOC_USABLE_SIZE_QUALIFIER void* ptr) { return Alloc::alloc_size(ptr); }