Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-4654

aligned_alloc is undefined on Android

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.24.0, 1.23.5
    • Affects Version/s: 1.23.4
    • Component/s: BSON
    • Labels:
      None

      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;
      }

      bson-memory.c.patch

       

            Assignee:
            ezra.chung@mongodb.com Ezra Chung
            Reporter:
            jeanmarc.leroux@gmail.com Jean-Marc Le Roux
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: