Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add android support #171

Merged
merged 5 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 <malloc.h>
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 ()
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/mem/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ namespace snmalloc
return pointer_cast<void>(external_address<location>(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));
Expand Down
5 changes: 3 additions & 2 deletions src/mem/mediumslab.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ namespace snmalloc
return OS_PAGE_SIZE;
}

static Mediumslab* get(void* p)
static Mediumslab* get(const void* p)
{
return pointer_align_down<SUPERSLAB_SIZE, Mediumslab>(p);
return pointer_align_down<SUPERSLAB_SIZE, Mediumslab>(
const_cast<void*>(p));
}

void init(RemoteAllocator* alloc, sizeclass_t sc, size_t rsize)
Expand Down
4 changes: 2 additions & 2 deletions src/mem/metaslab.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ 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<SLAB_SIZE, Slab>(p);
return pointer_align_down<SLAB_SIZE, Slab>(const_cast<void*>(p));
}

static bool is_short(Slab* p)
Expand Down
5 changes: 3 additions & 2 deletions src/mem/superslab.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ namespace snmalloc
StatusChange = 2
};

static Superslab* get(void* p)
static Superslab* get(const void* p)
{
return pointer_align_down<SUPERSLAB_SIZE, Superslab>(p);
return pointer_align_down<SUPERSLAB_SIZE, Superslab>(
const_cast<void*>(p));
}

static bool is_short_sizeclass(sizeclass_t sizeclass)
Expand Down
8 changes: 7 additions & 1 deletion src/override/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -43,7 +47,9 @@ extern "C"
return ThreadAlloc::get_noncachable()->alloc<ZeroMem::YesZero>(sz);
}

SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(void* ptr)
SNMALLOC_EXPORT
size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(
MALLOC_USABLE_SIZE_QUALIFIER void* ptr)
{
return Alloc::alloc_size(ptr);
}
Expand Down
8 changes: 6 additions & 2 deletions src/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include "../ds/address.h"
#include "../mem/allocconfig.h"

#include <execinfo.h>
#if __has_include(<execinfo.h>)
# define SNMALLOC_HAS_BACKTRACE 1
# include <execinfo.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -40,13 +42,15 @@ namespace snmalloc

static void print_stack_trace()
{
#ifdef SNMALLOC_HAS_BACKTRACE
constexpr int SIZE = 1024;
void* buffer[SIZE];
auto nptrs = backtrace(buffer, SIZE);
fflush(stdout);
backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO);
puts("");
fflush(stdout);
#endif
}

/**
Expand Down