--- /dev/null
+/* 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 */
#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 }
"OPEN_ENDED_LINE",
"MACRO_ARG_REUSE",
"PREFER_FALLTHROUGH",
- "ARRAY_SIZE",
"INITIALISED_STATIC",
"UNNECESSARY_ELSE",
);
#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"
"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)
#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"
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];
/*
#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"
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);
* 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,
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);
#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"
};
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);