From 8d3b15576bfe1a6940d5726a51af6f444c1a705e Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Sat, 16 Sep 2023 22:25:41 +0100 Subject: [PATCH] Add ARRAY_SIZE() macro --- include/common/array-size.h | 19 +++++++++++++++++++ include/ssd-internal.h | 3 ++- scripts/checkpatch.pl | 1 - src/cursor.c | 3 +-- src/layers.c | 4 ++-- src/output.c | 7 +++---- src/ssd/ssd_titlebar.c | 2 +- src/theme.c | 3 ++- 8 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 include/common/array-size.h diff --git a/include/common/array-size.h b/include/common/array-size.h new file mode 100644 index 00000000..67443943 --- /dev/null +++ b/include/common/array-size.h @@ -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 */ diff --git a/include/ssd-internal.h b/include/ssd-internal.h index 148337ed..9fe0ebfb 100644 --- a/include/ssd-internal.h +++ b/include/ssd-internal.h @@ -3,13 +3,14 @@ #define LABWC_SSD_INTERNAL_H #include +#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 } diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 72b1fba4..e6b701f1 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -62,7 +62,6 @@ my @ignore = ( "OPEN_ENDED_LINE", "MACRO_ARG_REUSE", "PREFER_FALLTHROUGH", - "ARRAY_SIZE", "INITIALISED_STATIC", "UNNECESSARY_ELSE", ); diff --git a/src/cursor.c b/src/cursor.c index f6f8b1d7..ce102502 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -7,6 +7,7 @@ #include #include #include "action.h" +#include "common/array-size.h" #include "common/mem.h" #include "common/scene-helpers.h" #include "config/mousebind.h" @@ -51,14 +52,12 @@ static const char * const cursors_x11[] = { "left_side" }; -#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) static_assert( ARRAY_SIZE(cursors_xdg) == LAB_CURSOR_COUNT, "XDG cursor names are out of sync"); static_assert( ARRAY_SIZE(cursors_x11) == LAB_CURSOR_COUNT, "X11 cursor names are out of sync"); -#undef ARRAY_SIZE enum lab_cursors cursor_get_from_edge(uint32_t resize_edges) diff --git a/src/layers.c b/src/layers.c index 2d750f22..b6ae4bb0 100644 --- a/src/layers.c +++ b/src/layers.c @@ -14,6 +14,7 @@ #include #include #include +#include "common/array-size.h" #include "common/list.h" #include "common/mem.h" #include "config/rcxml.h" @@ -76,8 +77,7 @@ layers_arrange(struct output *output) return; } - int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]); - for (int i = 0; i < nr_layers; i++) { + for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) { struct wlr_scene_tree *layer = output->layer_tree[i]; /* diff --git a/src/output.c b/src/output.c index 5db45963..1677995b 100644 --- a/src/output.c +++ b/src/output.c @@ -16,6 +16,7 @@ #include #include #include +#include "common/array-size.h" #include "common/mem.h" #include "labwc.h" #include "layers.h" @@ -48,8 +49,7 @@ output_destroy_notify(struct wl_listener *listener, void *data) wl_list_remove(&output->frame.link); wl_list_remove(&output->destroy.link); - int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]); - for (int i = 0; i < nr_layers; i++) { + for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) { wlr_scene_node_destroy(&output->layer_tree[i]->node); } wlr_scene_node_destroy(&output->layer_popup_tree->node); @@ -186,8 +186,7 @@ new_output_notify(struct wl_listener *listener, void *data) * Create layer-trees (background, bottom, top and overlay) and * a layer-popup-tree. */ - int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]); - for (int i = 0; i < nr_layers; i++) { + for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) { output->layer_tree[i] = wlr_scene_tree_create(&server->scene->tree); node_descriptor_create(&output->layer_tree[i]->node, diff --git a/src/ssd/ssd_titlebar.c b/src/ssd/ssd_titlebar.c index e66949fd..b0aaa2d0 100644 --- a/src/ssd/ssd_titlebar.c +++ b/src/ssd/ssd_titlebar.c @@ -105,7 +105,7 @@ set_squared_corners(struct ssd *ssd, bool enable) enum ssd_part_type ssd_type[2] = { LAB_SSD_BUTTON_WINDOW_MENU, LAB_SSD_BUTTON_CLOSE }; FOR_EACH_STATE(ssd, subtree) { - for (size_t i = 0; i < sizeof(ssd_type) / sizeof(ssd_type[0]); i++) { + for (size_t i = 0; i < ARRAY_SIZE(ssd_type); i++) { part = ssd_get_part(&subtree->parts, ssd_type[i]); struct ssd_button *button = node_ssd_button_from_node(part->node); diff --git a/src/theme.c b/src/theme.c index fe2c754b..37dc8037 100644 --- a/src/theme.c +++ b/src/theme.c @@ -19,6 +19,7 @@ #include #include #include +#include "common/array-size.h" #include "common/dir.h" #include "common/font.h" #include "common/graphic-helpers.h" @@ -109,7 +110,7 @@ load_buttons(struct theme *theme) }; char filename[4096] = {0}; - for (size_t i = 0; i < sizeof(buttons) / sizeof(buttons[0]); ++i) { + for (size_t i = 0; i < ARRAY_SIZE(buttons); ++i) { struct button *b = &buttons[i]; drop(b->active.buffer);