Add ARRAY_SIZE() macro

This commit is contained in:
Johan Malm 2023-09-16 22:25:41 +01:00 committed by Johan Malm
parent ae676c1607
commit 8d3b15576b
8 changed files with 30 additions and 12 deletions

View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_ARRAY_SIZE_H
#define LABWC_ARRAY_SIZE_H
/**
* ARRAY_SIZE() - Get the number of elements in array.
* @arr: array to be sized
*
* This does not work on pointers.
*
* Recent versions of GCC and clang support -Werror=sizeof-pointer-div
* and thus avoids using constructs such as:
*
* #define same_type(a, b) (__builtin_types_compatible_p(typeof(a), typeof(b)) == 1)
* #define ARRAY_SIZE(a) ({ static_assert(!same_type(a, &(a)[0])); sizeof(a) / sizeof(a[0]); })
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif /* LABWC_ARRAY_SIZE_H */

View file

@ -3,13 +3,14 @@
#define LABWC_SSD_INTERNAL_H
#include <wlr/util/box.h>
#include "common/array-size.h"
#include "ssd.h"
#define FOR_EACH(tmp, ...) \
{ \
__typeof__(tmp) _x[] = { __VA_ARGS__, NULL }; \
size_t _i = 0; \
for ((tmp) = _x[_i]; _i < sizeof(_x) / sizeof(_x[0]) - 1; (tmp) = _x[++_i])
for ((tmp) = _x[_i]; _i < ARRAY_SIZE(_x) - 1; (tmp) = _x[++_i])
#define FOR_EACH_END }