mdbx: support for Android/Bionic.

Change-Id: Ia6a4d8a7848ffe3e488b4a92c9ec53c61c78a9bb
This commit is contained in:
Leonid Yuriev 2020-04-13 23:36:56 +03:00
parent 595482ca57
commit ab2c98e41c
6 changed files with 58 additions and 11 deletions

View File

@ -257,6 +257,9 @@ if(MDBX_BUILD_SHARED_LIBRARY)
if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris") if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris")
target_link_libraries(mdbx PRIVATE kstat) target_link_libraries(mdbx PRIVATE kstat)
endif() endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
target_link_libraries(mdbx PRIVATE log)
endif()
list(APPEND MDBX_BUILD_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) list(APPEND MDBX_BUILD_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
endif() endif()
@ -276,6 +279,9 @@ endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris") if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris")
target_link_libraries(mdbx-static INTERFACE kstat) target_link_libraries(mdbx-static INTERFACE kstat)
endif() endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
target_link_libraries(mdbx-static INTERFACE log)
endif()
# mdbx-tools # mdbx-tools
if(MDBX_BUILD_TOOLS) if(MDBX_BUILD_TOOLS)

View File

@ -100,6 +100,10 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solar
target_link_libraries(mdbx ${MDBX_LIBDEP_MODE} kstat) target_link_libraries(mdbx ${MDBX_LIBDEP_MODE} kstat)
endif() endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
target_link_libraries(mdbx ${MDBX_LIBDEP_MODE} log)
endif()
set_target_properties(mdbx PROPERTIES set_target_properties(mdbx PROPERTIES
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}> INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>
C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON

View File

@ -4830,7 +4830,9 @@ __cold static int mdbx_wipe_steady(MDBX_env *env, const txnid_t last_steady) {
if (unlikely(err != MDBX_SUCCESS)) if (unlikely(err != MDBX_SUCCESS))
return err; return err;
} else { } else {
#if (defined(__linux__) || defined(__gnu_linux__)) && !defined(MDBX_SAFE4QEMU) #if (defined(__linux__) || defined(__gnu_linux__)) && \
(!defined(__ANDROID_API__) || __ANDROID_API__ >= 26) && \
defined(_GNU_SOURCE) && !defined(MDBX_SAFE4QEMU)
if (sync_file_range(env->me_lazy_fd, 0, pgno2bytes(env, NUM_METAS), if (sync_file_range(env->me_lazy_fd, 0, pgno2bytes(env, NUM_METAS),
SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER)) SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER))
err = errno; err = errno;
@ -15346,7 +15348,9 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, MDBX_txn *read_txn,
buffer + ceil_powerof2(meta_bytes, env->me_os_psize); buffer + ceil_powerof2(meta_bytes, env->me_os_psize);
for (size_t offset = meta_bytes; rc == MDBX_SUCCESS && offset < used_size;) { for (size_t offset = meta_bytes; rc == MDBX_SUCCESS && offset < used_size;) {
if (dest_is_pipe) { if (dest_is_pipe) {
#if defined(__linux__) || defined(__gnu_linux__) && !defined(MDBX_SAFE4QEMU) #if (defined(__linux__) || defined(__gnu_linux__)) && \
(!defined(__ANDROID_API__) || __ANDROID_API__ >= 21) && \
!defined(MDBX_SAFE4QEMU)
off_t in_offset = offset; off_t in_offset = offset;
const intptr_t written = const intptr_t written =
sendfile(fd, env->me_lazy_fd, &in_offset, used_size - offset); sendfile(fd, env->me_lazy_fd, &in_offset, used_size - offset);
@ -18086,8 +18090,8 @@ __dll_export
#ifdef MDBX_BUILD_TARGET #ifdef MDBX_BUILD_TARGET
MDBX_BUILD_TARGET MDBX_BUILD_TARGET
#else #else
#if defined(__ANDROID__) #if defined(__ANDROID_API__)
"Android" "Android" STRINGIFY(__ANDROID_API__)
#elif defined(__linux__) || defined(__gnu_linux__) #elif defined(__linux__) || defined(__gnu_linux__)
"Linux" "Linux"
#elif defined(EMSCRIPTEN) || defined(__EMSCRIPTEN__) #elif defined(EMSCRIPTEN) || defined(__EMSCRIPTEN__)

View File

@ -991,8 +991,6 @@ MDBX_INTERNAL_FUNC void mdbx_debug_log(int type, const char *function, int line,
const char *fmt, ...) const char *fmt, ...)
__printf_args(4, 5); __printf_args(4, 5);
MDBX_INTERNAL_FUNC void mdbx_panic(const char *fmt, ...) __printf_args(1, 2);
#if MDBX_DEBUG #if MDBX_DEBUG
#define mdbx_assert_enabled() unlikely(mdbx_runtime_flags &MDBX_DBG_ASSERT) #define mdbx_assert_enabled() unlikely(mdbx_runtime_flags &MDBX_DBG_ASSERT)
@ -1024,8 +1022,20 @@ MDBX_INTERNAL_FUNC void mdbx_panic(const char *fmt, ...) __printf_args(1, 2);
#endif /* MDBX_DEBUG */ #endif /* MDBX_DEBUG */
#if defined(__ANDROID_API__)
#define mdbx_panic(fmt, ...) \
__android_log_assert("panic", "mdbx", fmt, __VA_ARGS__)
#else
MDBX_INTERNAL_FUNC void mdbx_panic(const char *fmt, ...) __printf_args(1, 2);
#endif
#if !MDBX_DEBUG && defined(__ANDROID_API__)
#define mdbx_assert_fail(env, msg, func, line) \
__android_log_assert(msg, "mdbx", "%s:%u", func, line)
#else
MDBX_INTERNAL_FUNC void mdbx_assert_fail(const MDBX_env *env, const char *msg, MDBX_INTERNAL_FUNC void mdbx_assert_fail(const MDBX_env *env, const char *msg,
const char *func, int line); const char *func, int line);
#endif
#define mdbx_debug_extra(fmt, ...) \ #define mdbx_debug_extra(fmt, ...) \
do { \ do { \

View File

@ -197,6 +197,8 @@ __extern_C void __assert(const char *function, const char *file, int line,
#endif /* __assert_fail */ #endif /* __assert_fail */
#if !defined(__ANDROID_API__) || MDBX_DEBUG
MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env, MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env,
const char *msg, const char *msg,
const char *func, int line) { const char *func, int line) {
@ -221,6 +223,8 @@ MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env,
OutputDebugStringA(message); OutputDebugStringA(message);
if (IsDebuggerPresent()) if (IsDebuggerPresent())
DebugBreak(); DebugBreak();
#elif defined(__ANDROID_API__)
__android_log_assert(msg, "mdbx", "%s:%u", func, line);
#else #else
__assert_fail(msg, "mdbx", line, func); __assert_fail(msg, "mdbx", line, func);
#endif #endif
@ -233,6 +237,10 @@ MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env,
#endif #endif
} }
#endif /* __ANDROID_API__ || MDBX_DEBUG */
#if !defined(__ANDROID_API__)
MDBX_INTERNAL_FUNC __cold void mdbx_panic(const char *fmt, ...) { MDBX_INTERNAL_FUNC __cold void mdbx_panic(const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@ -256,6 +264,8 @@ MDBX_INTERNAL_FUNC __cold void mdbx_panic(const char *fmt, ...) {
#endif #endif
} }
#endif /* ! __ANDROID_API__ */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
#ifndef mdbx_vasprintf #ifndef mdbx_vasprintf
@ -315,7 +325,8 @@ MDBX_INTERNAL_FUNC int mdbx_memalign_alloc(size_t alignment, size_t bytes,
#elif defined(_ISOC11_SOURCE) #elif defined(_ISOC11_SOURCE)
*result = aligned_alloc(alignment, ceil_powerof2(bytes, alignment)); *result = aligned_alloc(alignment, ceil_powerof2(bytes, alignment));
return *result ? MDBX_SUCCESS : errno; return *result ? MDBX_SUCCESS : errno;
#elif _POSIX_VERSION >= 200112L #elif _POSIX_VERSION >= 200112L && \
(!defined(__ANDROID_API__) || __ANDROID_API__ >= 17)
*result = nullptr; *result = nullptr;
return posix_memalign(result, alignment, bytes); return posix_memalign(result, alignment, bytes);
#elif __GLIBC_PREREQ(2, 16) || __STDC_VERSION__ >= 201112L #elif __GLIBC_PREREQ(2, 16) || __STDC_VERSION__ >= 201112L
@ -753,7 +764,8 @@ MDBX_INTERNAL_FUNC int mdbx_write(mdbx_filehandle_t fd, const void *buf,
int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt,
uint64_t offset, size_t expected_written) { uint64_t offset, size_t expected_written) {
#if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) #if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) || \
(defined(__ANDROID_API__) && __ANDROID_API__ < 24)
size_t written = 0; size_t written = 0;
for (int i = 0; i < iovcnt; ++i) { for (int i = 0; i < iovcnt; ++i) {
int rc = mdbx_pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset); int rc = mdbx_pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset);
@ -1095,6 +1107,7 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) {
mdbx_free(PathBuffer); mdbx_free(PathBuffer);
return rc; return rc;
} }
#else #else
struct statvfs statvfs_info; struct statvfs statvfs_info;
@ -1147,6 +1160,9 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) {
defined(MFSTYPENAMELEN) || defined(VFS_NAMELEN) defined(MFSTYPENAMELEN) || defined(VFS_NAMELEN)
const char *const name = statfs_info.f_fstypename; const char *const name = statfs_info.f_fstypename;
const size_t name_len = sizeof(statfs_info.f_fstypename); const size_t name_len = sizeof(statfs_info.f_fstypename);
#elif defined(__ANDROID_API__) && __ANDROID_API__ < 21
const char *const name = "";
const unsigned name_len = 0;
#else #else
const char *name = ""; const char *name = "";
@ -1165,7 +1181,7 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) {
mounted = setmntent("/etc/mtab", "r"); mounted = setmntent("/etc/mtab", "r");
if (mounted) { if (mounted) {
const struct mntent *ent; const struct mntent *ent;
#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \ #if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(__BIONIC__) || \
(defined(_DEFAULT_SOURCE) && __GLIBC_PREREQ(2, 19)) (defined(_DEFAULT_SOURCE) && __GLIBC_PREREQ(2, 19))
struct mntent entbuf; struct mntent entbuf;
const bool should_copy = false; const bool should_copy = false;
@ -1191,7 +1207,7 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) {
} }
endmntent(mounted); endmntent(mounted);
} }
#endif /* !xBSD */ #endif /* !xBSD && !Android/Bionic */
#endif #endif
if (name_len) { if (name_len) {

View File

@ -240,6 +240,13 @@ typedef pthread_mutex_t mdbx_fastmutex_t;
#define malloc_usable_size(ptr) _msize(ptr) #define malloc_usable_size(ptr) _msize(ptr)
#endif /* malloc_usable_size */ #endif /* malloc_usable_size */
#ifdef __ANDROID_API__
#include <android/log.h>
#if __ANDROID_API__ >= 21
#include <sys/sendfile.h>
#endif
#endif /* Android */
/* *INDENT-OFF* */ /* *INDENT-OFF* */
/* clang-format off */ /* clang-format off */
#if defined(HAVE_SYS_STAT_H) || __has_include(<sys/stat.h>) #if defined(HAVE_SYS_STAT_H) || __has_include(<sys/stat.h>)
@ -340,7 +347,7 @@ typedef pthread_mutex_t mdbx_fastmutex_t;
/* *INDENT-OFF* */ /* *INDENT-OFF* */
/* clang-format off */ /* clang-format off */
#if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__) || \ #if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID_API__) || \
defined(HAVE_ENDIAN_H) || __has_include(<endian.h>) defined(HAVE_ENDIAN_H) || __has_include(<endian.h>)
#include <endian.h> #include <endian.h>
#elif defined(__APPLE__) || defined(__MACH__) || defined(__OpenBSD__) || \ #elif defined(__APPLE__) || defined(__MACH__) || defined(__OpenBSD__) || \