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 }

View file

@ -62,7 +62,6 @@ my @ignore = (
"OPEN_ENDED_LINE",
"MACRO_ARG_REUSE",
"PREFER_FALLTHROUGH",
"ARRAY_SIZE",
"INITIALISED_STATIC",
"UNNECESSARY_ELSE",
);

View file

@ -7,6 +7,7 @@
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/region.h>
#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)

View file

@ -14,6 +14,7 @@
#include <wayland-server.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/util/log.h>
#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];
/*

View file

@ -16,6 +16,7 @@
#include <wlr/types/wlr_scene.h>
#include <wlr/util/region.h>
#include <wlr/util/log.h>
#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,

View file

@ -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);

View file

@ -19,6 +19,7 @@
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include <strings.h>
#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);