Skip to content

CDRIVER-5876 detect aligned_alloc via CMake instead of feature test macros #1851

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

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ endif ()

if (MSVC)
add_definitions (-D_CRT_SECURE_NO_WARNINGS)
list (APPEND CMAKE_REQUIRED_DEFINITIONS -D_CRT_SECURE_NO_WARNINGS)
endif ()

if (ENABLE_MONGOC)
Expand Down
54 changes: 41 additions & 13 deletions src/common/src/common-macros-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@
#define MONGOC_DEBUG_ASSERT(statement) ((void) 0)
#endif

#if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic push")
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("GCC diagnostic pop")
#elif defined(__clang__)
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic push")
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("clang diagnostic pop")
#elif defined(_MSC_VER)
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("warning ( push )")
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("warning ( pop )")
#else
#define MC_PRAGMA_DIAGNOSTIC_PUSH
#define MC_PRAGMA_DIAGNOSTIC_POP
#endif

// `MC_ENABLE_CONVERSION_WARNING_BEGIN` enables -Wconversion to check for potentially unsafe integer conversions.
// The `mcommon_in_range_*` functions can help address these warnings by ensuring a cast is within bounds.
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) // gcc 4.6 added support for "diagnostic push".
#define MC_ENABLE_CONVERSION_WARNING_BEGIN \
_Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic warning \"-Wconversion\"")
#define MC_ENABLE_CONVERSION_WARNING_END _Pragma ("GCC diagnostic pop")
#if defined(__GNUC__)
#define MC_ENABLE_CONVERSION_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic warning \"-Wconversion\"")
#define MC_ENABLE_CONVERSION_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#elif defined(__clang__)
#define MC_ENABLE_CONVERSION_WARNING_BEGIN \
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic warning \"-Wconversion\"")
#define MC_ENABLE_CONVERSION_WARNING_END _Pragma ("clang diagnostic pop")
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic warning \"-Wconversion\"")
#define MC_ENABLE_CONVERSION_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#else
#define MC_ENABLE_CONVERSION_WARNING_BEGIN
#define MC_ENABLE_CONVERSION_WARNING_END
Expand All @@ -50,22 +63,37 @@
#undef MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_BEGIN
#undef MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_BEGIN \
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wcast-function-type-strict\"")
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END _Pragma ("clang diagnostic pop")
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wcast-function-type-strict\"")
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#endif // __has_warning("-Wcast-function-type-strict")
#endif // defined(__clang__)

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#if defined(__GNUC__)
#define BEGIN_IGNORE_DEPRECATIONS \
_Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
#define END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop")
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
#define END_IGNORE_DEPRECATIONS MC_PRAGMA_DIAGNOSTIC_POP
#elif defined(__clang__)
#define BEGIN_IGNORE_DEPRECATIONS \
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"")
#define END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop")
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"")
#define END_IGNORE_DEPRECATIONS MC_PRAGMA_DIAGNOSTIC_PUSH
#else
#define BEGIN_IGNORE_DEPRECATIONS
#define END_IGNORE_DEPRECATIONS
#endif

// Disable the -Wimplicit warning (including -Wimplicit-int and -Wimplicit-function-declaration).
#if defined(__GNUC__)
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic ignored \"-Wimplicit\"")
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#elif defined(__clang__)
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wimplicit\"")
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#elif defined(_MSC_VER)
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("warning (disable : 4013 4431)")
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
#else
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN
#define MC_DISABLE_IMPLICIT_WARNING_END
#endif

#endif /* MONGO_C_DRIVER_COMMON_MACROS_PRIVATE_H */
2 changes: 2 additions & 0 deletions src/libbson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ check_symbol_exists (clock_gettime time.h BSON_HAVE_CLOCK_GETTIME)
mongo_bool01 (BSON_HAVE_CLOCK_GETTIME BSON_HAVE_CLOCK_GETTIME)
check_symbol_exists (strnlen string.h BSON_HAVE_STRNLEN)
mongo_bool01 (BSON_HAVE_STRNLEN BSON_HAVE_STRNLEN)
check_symbol_exists (aligned_alloc stdlib.h BSON_HAVE_ALIGNED_ALLOC)
mongo_bool01 (BSON_HAVE_ALIGNED_ALLOC BSON_HAVE_ALIGNED_ALLOC)
test_big_endian (BSON_BIG_ENDIAN)

if (WIN32)
Expand Down
9 changes: 9 additions & 0 deletions src/libbson/src/bson/bson-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@
# undef BSON_HAVE_STRLCPY
#endif


/*
* Define to 1 if you have aligned_alloc available on your platform.
*/
#define BSON_HAVE_ALIGNED_ALLOC @BSON_HAVE_ALIGNED_ALLOC@
#if BSON_HAVE_ALIGNED_ALLOC != 1
# undef BSON_HAVE_ALIGNED_ALLOC
#endif

#endif /* BSON_CONFIG_H */
7 changes: 5 additions & 2 deletions src/libbson/src/bson/bson-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <bson/bson-config.h>
#include <bson/bson-memory.h>

#include <common-macros-private.h>


// Ensure size of exported structs are stable.
BSON_STATIC_ASSERT2 (bson_mem_vtable_t, sizeof (bson_mem_vtable_t) == sizeof (void *) * 8u);
Expand All @@ -31,10 +33,11 @@ BSON_STATIC_ASSERT2 (bson_mem_vtable_t, sizeof (bson_mem_vtable_t) == sizeof (vo
// For compatibility with C standards prior to C11.
static void *
_aligned_alloc_impl (size_t alignment, size_t num_bytes)
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(_WIN32) && !defined(__ANDROID__) && \
!defined(_AIX)
#if defined(BSON_HAVE_ALIGNED_ALLOC)
{
MC_DISABLE_IMPLICIT_WARNING_BEGIN
return aligned_alloc (alignment, num_bytes);
MC_DISABLE_IMPLICIT_WARNING_END
}
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
{
Expand Down
2 changes: 2 additions & 0 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ if (NOT ENABLE_ZLIB STREQUAL "OFF")
check_include_files ("stdarg.h" HAVE_STDARG_H)
if (HAVE_UNISTD_H)
add_definitions (-DHAVE_UNISTD_H)
list (APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_UNISTD_H)
endif ()
if (HAVE_STDARG_H)
add_definitions (-DHAVE_STDARG_H)
list (APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDARG_H)
endif ()
set (MONGOC_ENABLE_COMPRESSION 1)
set (MONGOC_ENABLE_COMPRESSION_ZLIB 1)
Expand Down