From 4547b4a893bd8a5b42abc7d779b698d934fa996e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 23 Jul 2021 15:41:45 +0200 Subject: [PATCH] std-aux: implement NM_PTRARRAY_LEN() macro via static function We use NM_PTRARRAY_LEN(), and I find it a bit ugly that a macro does so much. Maybe, it's better to have it as a function. But the macro currently lives in "libnm-std-aux/nm-std-aux.h", which is header-only. To add it to a C source file, we would have to move it to another header, but "libnm-std-aux/nm-std-aux.h" is nice because it gets included by default already. Keep it in "libnm-std-aux/nm-std-aux.h", but implement it as an inline function. The macro now only does (as before) some type checking shenanigans to ensure that the argument is a pointer to pointers. In practice, there is probably very little difference compared to the macro before, likely the code will anyway be inlined. --- src/libnm-std-aux/nm-std-aux.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index 0bc4d5b805..bc74a30997 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -316,22 +316,28 @@ nm_mult_clamped_u(unsigned a, unsigned b) /* macro to return strlen() of a compile time string. */ #define NM_STRLEN(str) (sizeof("" str "") - 1u) +static inline size_t +_nm_ptrarray_len_impl(const void *const *array) +{ + size_t n = 0; + + if (array) { + while (array[n]) + n++; + } + return n; +} + /* returns the length of a NULL terminated array of pointers, * like g_strv_length() does. The difference is: * - it operates on arrays of pointers (of any kind, requiring no cast). * - it accepts NULL to return zero. */ -#define NM_PTRARRAY_LEN(array) \ - ({ \ - typeof(*(array)) *const _array = (array); \ - size_t _n = 0; \ - \ - if (_array) { \ - _nm_unused const void *_type_check_is_pointer = _array[0]; \ - \ - while (_array[_n]) \ - _n++; \ - } \ - _n; \ +#define NM_PTRARRAY_LEN(array) \ + ({ \ + typeof(*(array)) *const _array = (array); \ + _nm_unused const void * _type_check_is_pointer = 0 ? _array[0] : NULL; \ + \ + _nm_ptrarray_len_impl((const void *const *) _array); \ }) /*****************************************************************************/