Description
Building libbson for Android fails because `aligned_alloc()` is not defined in [`stdlib.h`](https://android.googlesource.com/platform/development/+/13af1d8/ndk/platforms/android-L/include/stdlib.h):
/embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: error: implicitly declaring library function 'aligned_alloc' with type 'void *(unsigned int, unsigned int)' [-Werror,-Wimplicit-function-declaration] |
return aligned_alloc (alignment, num_bytes); |
^
|
/embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: note: include the header <stdlib.h> or explicitly provide a declaration for 'aligned_alloc' |
1 error generated. |
make[5]: *** [src/libbson/CMakeFiles/bson_static.dir/build.make:244: src/libbson/CMakeFiles/bson_static.dir/src/bson/bson-memory.c.o] Error 1 |
make[5]: *** Waiting for unfinished jobs.... |
/embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: error: implicitly declaring library function 'aligned_alloc' with type 'void *(unsigned int, unsigned int)' [-Werror,-Wimplicit-function-declaration] |
return aligned_alloc (alignment, num_bytes); |
^
|
/embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: note: include the header <stdlib.h> or explicitly provide a declaration for 'aligned_alloc' |
Corresponding Android NDK issue:
https://github.com/android/ndk/issues/1339
Recommended solution:
https://github.com/android/ndk/issues/1339#issuecomment-676821211
Right, use `memalign` or `posix_memalign` instead.
- `memalign` is always available on Android.
- `posix_memalign` was added in API 17 (but is available on API 16 via libandroid_support.a, which is linked by default when targeting API 16).
The test in `bson-memory.c` will most likely have to be changed to something like this:
#if __STDC_VERSION__ >= 201112L && !defined(_WIN32) && !defined(__ANDROID__) |
{
|
return aligned_alloc (alignment, num_bytes); |
}
|
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
|
{
|
void *mem = NULL; |
(void) posix_memalign (&mem, alignment, num_bytes); |
return mem; |
}
|