From: Cristian Rodríguez Date: Mon, 24 Nov 2025 20:04:08 +0000 (-0300) Subject: include: implement ARRAY_SIZE with compiler _Countof if supported X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6cc285a9d07d53099ce2e318466a56bb6b1e0606;p=thirdparty%2Futil-linux.git include: implement ARRAY_SIZE with compiler _Countof if supported C2Y has _Countof operator for this. GCC has an stdcountof.h hedaer and a countof definition Clang implements _Countof and needs __has_extension check --- diff --git a/include/c.h b/include/c.h index f7cd08fd8..0126591f2 100644 --- a/include/c.h +++ b/include/c.h @@ -133,6 +133,10 @@ C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough #define __has_feature(x) 0 #endif +#ifndef __has_extension + #define __has_extension __has_feature +#endif + /* * Function attributes */ @@ -173,6 +177,15 @@ C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); })) #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) +#if __has_include() +#include +#define ARRAY_SIZE(arr) countof(arr) +#endif + +#if !defined(ARRAY_SIZE) && __has_extension(c_countof) +#define ARRAY_SIZE(arr) _Countof(arr) +#endif + #ifndef ARRAY_SIZE # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) #endif