From ab2c98e41c7c37872ec285330559e5f6e20a9247 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Mon, 13 Apr 2020 23:36:56 +0300 Subject: [PATCH] mdbx: support for Android/Bionic. Change-Id: Ia6a4d8a7848ffe3e488b4a92c9ec53c61c78a9bb --- CMakeLists.dist-minimal | 6 ++++++ src/CMakeLists.txt | 4 ++++ src/elements/core.c | 12 ++++++++---- src/elements/internals.h | 14 ++++++++++++-- src/elements/osal.c | 24 ++++++++++++++++++++---- src/elements/osal.h | 9 ++++++++- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/CMakeLists.dist-minimal b/CMakeLists.dist-minimal index 36c9ab51..849c305d 100644 --- a/CMakeLists.dist-minimal +++ b/CMakeLists.dist-minimal @@ -257,6 +257,9 @@ if(MDBX_BUILD_SHARED_LIBRARY) if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris") target_link_libraries(mdbx PRIVATE kstat) endif() + if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") + target_link_libraries(mdbx PRIVATE log) + endif() list(APPEND MDBX_BUILD_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) endif() @@ -276,6 +279,9 @@ endif() if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris") target_link_libraries(mdbx-static INTERFACE kstat) endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") + target_link_libraries(mdbx-static INTERFACE log) +endif() # mdbx-tools if(MDBX_BUILD_TOOLS) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c2fe7853..39bb3c32 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -100,6 +100,10 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solar target_link_libraries(mdbx ${MDBX_LIBDEP_MODE} kstat) endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") + target_link_libraries(mdbx ${MDBX_LIBDEP_MODE} log) +endif() + set_target_properties(mdbx PROPERTIES INTERPROCEDURAL_OPTIMIZATION $ C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON diff --git a/src/elements/core.c b/src/elements/core.c index 29205e1c..8db231eb 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -4830,7 +4830,9 @@ __cold static int mdbx_wipe_steady(MDBX_env *env, const txnid_t last_steady) { if (unlikely(err != MDBX_SUCCESS)) return err; } 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), SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER)) 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); for (size_t offset = meta_bytes; rc == MDBX_SUCCESS && offset < used_size;) { 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; const intptr_t written = sendfile(fd, env->me_lazy_fd, &in_offset, used_size - offset); @@ -18086,8 +18090,8 @@ __dll_export #ifdef MDBX_BUILD_TARGET MDBX_BUILD_TARGET #else - #if defined(__ANDROID__) - "Android" + #if defined(__ANDROID_API__) + "Android" STRINGIFY(__ANDROID_API__) #elif defined(__linux__) || defined(__gnu_linux__) "Linux" #elif defined(EMSCRIPTEN) || defined(__EMSCRIPTEN__) diff --git a/src/elements/internals.h b/src/elements/internals.h index 1946476f..215a49ba 100644 --- a/src/elements/internals.h +++ b/src/elements/internals.h @@ -991,8 +991,6 @@ MDBX_INTERNAL_FUNC void mdbx_debug_log(int type, const char *function, int line, const char *fmt, ...) __printf_args(4, 5); -MDBX_INTERNAL_FUNC void mdbx_panic(const char *fmt, ...) __printf_args(1, 2); - #if MDBX_DEBUG #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 */ +#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, const char *func, int line); +#endif #define mdbx_debug_extra(fmt, ...) \ do { \ diff --git a/src/elements/osal.c b/src/elements/osal.c index 1a4447a0..1f38928a 100644 --- a/src/elements/osal.c +++ b/src/elements/osal.c @@ -197,6 +197,8 @@ __extern_C void __assert(const char *function, const char *file, int line, #endif /* __assert_fail */ +#if !defined(__ANDROID_API__) || MDBX_DEBUG + MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env, const char *msg, const char *func, int line) { @@ -221,6 +223,8 @@ MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env, OutputDebugStringA(message); if (IsDebuggerPresent()) DebugBreak(); +#elif defined(__ANDROID_API__) + __android_log_assert(msg, "mdbx", "%s:%u", func, line); #else __assert_fail(msg, "mdbx", line, func); #endif @@ -233,6 +237,10 @@ MDBX_INTERNAL_FUNC void __cold mdbx_assert_fail(const MDBX_env *env, #endif } +#endif /* __ANDROID_API__ || MDBX_DEBUG */ + +#if !defined(__ANDROID_API__) + MDBX_INTERNAL_FUNC __cold void mdbx_panic(const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -256,6 +264,8 @@ MDBX_INTERNAL_FUNC __cold void mdbx_panic(const char *fmt, ...) { #endif } +#endif /* ! __ANDROID_API__ */ + /*----------------------------------------------------------------------------*/ #ifndef mdbx_vasprintf @@ -315,7 +325,8 @@ MDBX_INTERNAL_FUNC int mdbx_memalign_alloc(size_t alignment, size_t bytes, #elif defined(_ISOC11_SOURCE) *result = aligned_alloc(alignment, ceil_powerof2(bytes, alignment)); return *result ? MDBX_SUCCESS : errno; -#elif _POSIX_VERSION >= 200112L +#elif _POSIX_VERSION >= 200112L && \ + (!defined(__ANDROID_API__) || __ANDROID_API__ >= 17) *result = nullptr; return posix_memalign(result, alignment, bytes); #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, 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; for (int i = 0; i < iovcnt; ++i) { 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); return rc; } + #else 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) const char *const name = 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 const char *name = ""; @@ -1165,7 +1181,7 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) { mounted = setmntent("/etc/mtab", "r"); if (mounted) { 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)) struct mntent entbuf; const bool should_copy = false; @@ -1191,7 +1207,7 @@ static int mdbx_check_fs_local(mdbx_filehandle_t handle, int flags) { } endmntent(mounted); } -#endif /* !xBSD */ +#endif /* !xBSD && !Android/Bionic */ #endif if (name_len) { diff --git a/src/elements/osal.h b/src/elements/osal.h index 70be5a63..7ea5913f 100644 --- a/src/elements/osal.h +++ b/src/elements/osal.h @@ -240,6 +240,13 @@ typedef pthread_mutex_t mdbx_fastmutex_t; #define malloc_usable_size(ptr) _msize(ptr) #endif /* malloc_usable_size */ +#ifdef __ANDROID_API__ +#include +#if __ANDROID_API__ >= 21 +#include +#endif +#endif /* Android */ + /* *INDENT-OFF* */ /* clang-format off */ #if defined(HAVE_SYS_STAT_H) || __has_include() @@ -340,7 +347,7 @@ typedef pthread_mutex_t mdbx_fastmutex_t; /* *INDENT-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() #include #elif defined(__APPLE__) || defined(__MACH__) || defined(__OpenBSD__) || \