-
Notifications
You must be signed in to change notification settings - Fork 112
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
add android support #171
Conversation
You can test the compiled binaries with the methods mentioned in https://stackoverflow.com/questions/9868309/how-to-compile-c-into-an-executable-binary-file-and-run-it-in-android-from-andro. works fine on my side. |
@mjp41 is it better to implement a specified |
Does Android have libexecinfo? It would be nice to make the backtrace support conditional on the backtrace symbols existing, with a CMake check. |
@davidchisnall are you proposing a check like we do in Verona for filesystem support: |
I do not know whether this would work or not, will give it a try: https://cmake.org/cmake/help/v3.0/module/CheckLibraryExists.html |
CMakeLists.txt
Outdated
endif() | ||
|
||
check_include_file(execinfo.h HAS_EXECINFO_H) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This macro can help us check the header file directly.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Could you add some documentation about how to use this feature? |
CMakeLists.txt
Outdated
@@ -74,6 +74,11 @@ target_include_directories(snmalloc_lib INTERFACE src/) | |||
|
|||
if(NOT MSVC) | |||
find_package(Threads REQUIRED COMPONENTS snmalloc_lib) | |||
if (SNMALLOC_ARM_ANDROID) | |||
if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") | |||
target_link_libraries(snmalloc_lib INTERFACE log c++_static c++abi m) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with this I can build the library and run it correctly for armv7, but the size is larger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dynamic linking failed to resolve __emutls_get_address
CMakeLists.txt
Outdated
@@ -10,10 +10,10 @@ option(EXPOSE_EXTERNAL_PAGEMAP "Expose the global pagemap" OFF) | |||
option(EXPOSE_EXTERNAL_RESERVE "Expose an interface to reserve memory using the default memory provider" OFF) | |||
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) | |||
option(SNMALLOC_ANDROID "Set cross build target to Android" OFF) | |||
option(SNMALLOC_ARM_ANDROID "Set cross build target to ARM Android" OFF) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why I change this is that android does not always mean arm
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. CMAKE_TOOLCHAIN_FILE
is definitely a good choice, will look into it later.
I set this just because passing CMAKE_SYSTEM_PROCESSOR
via command line is not working.
It should be set by android.toolchain.cmake
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using toolchain file for androideabi
will solve the linking issue too. the linking result of emutls
after changing to the new way:
0002d7e0 t __emutls_get_address
0002d8b8 t __emutls_register_common
0003eff4 r __emutls_t._ZL16g_current_locale
0003e15c r __emutls_t._ZN10__cxxabiv112_GLOBAL__N_15dtorsE
00040dc0 V __emutls_t._ZZN8snmalloc17ThreadAllocCommon13get_referenceEvE5alloc
0002d7c8 t __emutls_unregister_key
00043020 V __emutls_v._ZGVZN8snmalloc27ThreadAllocThreadDestructor16register_cleanupEvE6tidier
00043080 d __emutls_v._ZL16g_current_locale
00043050 d __emutls_v._ZN10__cxxabiv112_GLOBAL__N_111dtors_aliveE
00043060 d __emutls_v._ZN10__cxxabiv112_GLOBAL__N_15dtorsE
00043030 V __emutls_v._ZN8snmalloc17ThreadAllocCommon18destructor_has_runE
00043040 V __emutls_v._ZN8snmalloc19operation_in_flightE
00043000 V __emutls_v._ZZN8snmalloc17ThreadAllocCommon13get_referenceEvE5alloc
00043010 V __emutls_v._ZZN8snmalloc27ThreadAllocThreadDestructor16register_cleanupEvE6tidier
0002d6ec t emutls_alloc
0002d6a8 t emutls_destroy
0002d784 t emutls_init
00046dd8 b emutls_key
00046de0 b emutls_key_created
00046ddc b emutls_mutex
00046de8 b emutls_size
CMakeLists.txt
Outdated
@@ -10,10 +10,10 @@ option(EXPOSE_EXTERNAL_PAGEMAP "Expose the global pagemap" OFF) | |||
option(EXPOSE_EXTERNAL_RESERVE "Expose an interface to reserve memory using the default memory provider" OFF) | |||
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) | |||
option(SNMALLOC_ANDROID "Set cross build target to Android" OFF) | |||
option(SNMALLOC_ARM_ANDROID "Set cross build target to ARM Android" OFF) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
CMakeLists.txt
Outdated
endif() | ||
|
||
check_include_file(execinfo.h HAS_EXECINFO_H) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
src/override/malloc.cc
Outdated
SNMALLOC_EXPORT | ||
size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(const void* ptr) | ||
{ | ||
return Alloc::alloc_size(const_cast<void*>(ptr)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alloc_size
shouldn't need to modify anything through the pointer. I'd prefer to pass a const void*
here and unify both code paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean changing the inner paths within snmalloc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly. I don't think Alloc::alloc_size
should need to take a non-const
pointer, so let's make that one const
.
Thanks, I've added a few comments inline, but they're all minor cleanups. |
using toolchain file however, may make it hard for other language like rust to automatically compile this library. see rust-lang/cmake-rs#80. I think I will adopt a dirty hack to let the user pass a |
Does Rust depend on the NDK at all for Android cross-compilation? I don't have much experience there, but it looks as if the NDK has a fairly standard CMake cross-compile process and is gradually moving in the direction of better alignment with the normal CMake mechanisms where they differ. I'd assume that if you're cross-compiling any CMake project you'd use a toolchain file (every other approach I've tried in the past has resulted in far more pain in the long term), but the cmake-rs issue that you link to seems to imply that they don't want to do that. |
CMakeLists.txt
Outdated
@@ -210,7 +209,7 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY) | |||
add_shim(snmallocshim-1mib SHARED ${SHARED_FILES}) | |||
target_compile_definitions(snmallocshim-1mib PRIVATE IS_ADDRESS_SPACE_CONSTRAINED) | |||
endif() | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only changes in this file are now whitespace. Can you remove them to reduce churn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just rebased away the changes on CMakeLists.txt
Is there a reason for this? We have 32-bit windows on the CI, and I can get 32-bit linux building with a very small change (just a spurious |
@plietar It was the previous discussion that make me write something like this. Perhaps my words were not proper here. Sorry about that. |
src/ds/address.h
Outdated
@@ -70,7 +70,7 @@ namespace snmalloc | |||
* power of two. | |||
*/ | |||
template<size_t alignment, typename T = void> | |||
SNMALLOC_FAST_PATH T* pointer_align_down(void* p) | |||
SNMALLOC_FAST_PATH T* pointer_align_down(const void* p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that this result in an inconsistent qualifier with other alignment cast operations.
src/override/malloc.cc
Outdated
@@ -43,7 +43,8 @@ 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)(const void* ptr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you still need the non-const version for other platforms that declare this in a header that we include here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, it seems to work for other platforms.
I guess the case seems to be this:
- Android somehow includes the
malloc.h
via the headers. - Other platforms does not do this.
- Compiler discards CV-qualifiers so linking stage is not affected for most platforms.
- However, the definitions conflicts on android because the compiler can find the real definitions.
So, do you still suggest me to provide two different versions? if so, I will work on it ASAP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidchisnall How about something like:
include(CheckCSourceCompiles)
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(CONST_QUALIFIED_MALLOC_USABLE_SIZE)
target_compile_definitions(snmalloc_lib INTERFACE -DMALLOC_USABLE_SIZE_QUALIFIER=const)
endif()
And then in the source,
#ifndef MALLOC_USABLE_SIZE_QUALIFIER
# define MALLOC_USABLE_SIZE_QUALIFIER
#endif
// ...
size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(MALLOC_USABLE_SIZE_QUALIFIER void* ptr) {...}
Rebased again to master:
|
@SchrodingerZhu thanks for this. |
No description provided.