From 11c9bf6681af0e33802cf2e946cd8cb82f5ef59a Mon Sep 17 00:00:00 2001 From: Andrei Alexeyev Date: Sun, 8 Jan 2023 23:54:01 +0100 Subject: [PATCH] compat: add a few more attribute macros --- meson.build | 12 ++++++++++++ src/util/compat.h | 24 ++++++++++++++++++++---- src/util/meson.build | 6 ------ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/meson.build b/meson.build index 5dbd7134..d0511999 100644 --- a/meson.build +++ b/meson.build @@ -297,6 +297,18 @@ endif config.set('TAISEI_BUILDCONF_HAVE_ZIP_COMPRESSION_METHOD_SUPPORTED', have_zip_compression_method_supported) +config.set('TAISEI_BUILDCONF_HAVE_ATTR_DESIGNATED_INIT', cc.compiles( + 'struct { int dummy; } __attribute__((designated_init)) x;', + name : '__attribute__((designated_init))', + args : ['-Wattributes', '-Werror'] +)) + +config.set('TAISEI_BUILDCONF_HAVE_ATTR_MALLOC_WITH_ARGS', cc.compiles( + 'void myfree(void *p); void *myalloc() __attribute__((malloc(myfree, 1)));', + name : '__attribute__((malloc(deallocator, index)))', + args : ['-Wattributes', '-Werror'] +)) + prefer_relpath_systems = [ 'windows', ] diff --git a/src/util/compat.h b/src/util/compat.h index c25066fa..1ac0cc83 100644 --- a/src/util/compat.h +++ b/src/util/compat.h @@ -272,7 +272,7 @@ typedef _Complex double cmplx; attr_returns_nonnull attr_returns_max_aligned attr_nodiscard // Structure must not be initialized with an implicit (non-designated) initializer. -#if __has_attribute(designated_init) && defined(TAISEI_BUILDCONF_USE_DESIGNATED_INIT) +#if __has_attribute(designated_init) && defined(TAISEI_BUILDCONF_HAVE_ATTR_DESIGNATED_INIT) #define attr_designated_init \ __attribute__ ((designated_init)) #else @@ -284,9 +284,25 @@ typedef _Complex double cmplx; #define attr_malloc \ __attribute__ ((malloc)) -// Function returns a pointer to object whose size is specified by the 'size_arg_index'th function argument. -#define attr_alloc_size(size_arg_index) \ - __attribute__ ((alloc_size(size_arg_index))) +// Function returns a pointer that must be 'freed' with the specified deallocator function +#ifdef TAISEI_BUILDCONF_HAVE_ATTR_MALLOC_WITH_ARGS + #define attr_dealloc(deallocator, arg_index) \ + __attribute__ ((malloc(deallocator, arg_index))) +#else + #define attr_dealloc(deallocator, arg_index) +#endif + +// With one argument n: function returns a pointer to object whose size is specified by the +// nth argument. +// With two arguments n, m: function returns a pointer to object whose size is specified by the +// product of nth and mth arguments. +#define attr_alloc_size(...) \ + __attribute__ ((alloc_size(__VA_ARGS__))) + +// Function returns a pointer aligned to a byte boundary specified by nth argument +#define attr_alloc_align(arg_index) \ + __attribute__ ((alloc_align(arg_index))) + #define INLINE static inline attr_must_inline __attribute__((gnu_inline)) diff --git a/src/util/meson.build b/src/util/meson.build index a3688bb7..6fe8aa53 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -23,12 +23,6 @@ if is_developer_build util_src += files('debug.c') endif -config.set('TAISEI_BUILDCONF_USE_DESIGNATED_INIT', cc.compiles( - 'struct { int dummy; } __attribute__((designated_init)) x;', - name : '__attribute__((designated_init)) with -Werror', - args : ['-Wattributes', '-Werror'] -)) - if dep_crypto.found() util_src += files('sha256_openssl.c') else