[PATCH weston v2 2/4] Moved helper macro to a discrete include file.

Jon A. Cruz jonc at osg.samsung.com
Mon Jun 15 15:37:08 PDT 2015


To help reduce code duplication and also 'kitchen-sink' includes
the ARRAY_LENGTH macro was moved to a stand-alone file and
referenced from the sources consuming it. Other macros will be
added in subsequent passes.

Signed-off-by: Jon A. Cruz <jonc at osg.samsung.com>
---
 Makefile.am                        |  1 +
 clients/calibrator.c               |  3 +--
 clients/dnd.c                      |  1 +
 clients/editor.c                   |  1 +
 clients/eventdemo.c                |  1 +
 clients/ivi-shell-user-interface.c |  2 +-
 clients/nested.c                   |  1 +
 clients/presentation-shm.c         |  3 +--
 clients/simple-touch.c             |  3 +--
 clients/stacking.c                 |  1 +
 clients/subsurfaces.c              |  1 +
 clients/terminal.c                 |  1 +
 clients/weston-info.c              |  3 +--
 clients/window.c                   |  1 +
 clients/window.h                   |  2 --
 clients/wscreensaver.c             |  1 +
 ivi-shell/ivi-shell.c              |  1 +
 shared/cairo-util.c                |  3 +--
 shared/helpers.h                   | 48 ++++++++++++++++++++++++++++++++++++++
 shared/image-loader.c              |  3 +--
 src/compositor-drm.c               |  1 +
 src/compositor-fbdev.c             |  1 +
 src/compositor-headless.c          |  1 +
 src/compositor-rdp.c               |  1 +
 src/compositor-rpi.c               |  1 +
 src/compositor-wayland.c           |  1 +
 src/compositor-x11.c               |  1 +
 src/compositor.c                   |  1 +
 src/compositor.h                   |  2 --
 src/gl-renderer.c                  |  1 +
 src/input.c                        |  1 +
 tests/ivi_layout-test.c            |  1 +
 tests/presentation-test.c          |  1 +
 tests/vertex-clip-test.c           |  1 +
 tests/weston-test-runner.h         |  4 ++--
 xwayland/selection.c               |  1 +
 xwayland/window-manager.c          |  1 +
 37 files changed, 83 insertions(+), 19 deletions(-)
 create mode 100644 shared/helpers.h

diff --git a/Makefile.am b/Makefile.am
index 5819b19..261798c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -189,6 +189,7 @@ westoninclude_HEADERS =				\
 	src/timeline-object.h			\
 	shared/matrix.h				\
 	shared/config-parser.h			\
+	shared/helpers.h			\
 	shared/zalloc.h				\
 	shared/platform.h
 
diff --git a/clients/calibrator.c b/clients/calibrator.c
index c5ecbe9..04c1cfc 100644
--- a/clients/calibrator.c
+++ b/clients/calibrator.c
@@ -35,10 +35,9 @@
 #include <wayland-client.h>
 
 #include "window.h"
+#include "shared/helpers.h"
 #include "shared/matrix.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 /* Our points for the calibration must be not be on a line */
 static const struct {
 	float x_ratio, y_ratio;
diff --git a/clients/dnd.c b/clients/dnd.c
index 1ab2033..e6a0c39 100644
--- a/clients/dnd.c
+++ b/clients/dnd.c
@@ -41,6 +41,7 @@
 
 #include "window.h"
 #include "shared/cairo-util.h"
+#include "shared/helpers.h"
 
 struct dnd_drag;
 
diff --git a/clients/editor.c b/clients/editor.c
index a86c590..d0b7fee 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -36,6 +36,7 @@
 
 #include <pango/pangocairo.h>
 
+#include "shared/helpers.h"
 #include "window.h"
 #include "text-client-protocol.h"
 
diff --git a/clients/eventdemo.c b/clients/eventdemo.c
index 0f98869..dc69fd6 100644
--- a/clients/eventdemo.c
+++ b/clients/eventdemo.c
@@ -38,6 +38,7 @@
 
 #include <cairo.h>
 
+#include "shared/helpers.h"
 #include "window.h"
 
 /** window title */
diff --git a/clients/ivi-shell-user-interface.c b/clients/ivi-shell-user-interface.c
index cff7664..0e99d25 100644
--- a/clients/ivi-shell-user-interface.c
+++ b/clients/ivi-shell-user-interface.c
@@ -35,6 +35,7 @@
 #include <wayland-cursor.h>
 #include "shared/cairo-util.h"
 #include "shared/config-parser.h"
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "ivi-application-client-protocol.h"
 #include "ivi-hmi-controller-client-protocol.h"
@@ -175,7 +176,6 @@ mem_alloc(size_t size, char *file, int32_t line)
 }
 
 #define MEM_ALLOC(s) mem_alloc((s),__FILE__,__LINE__)
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
 
 /*****************************************************************************
  *  Event Handler
diff --git a/clients/nested.c b/clients/nested.c
index 92720f4..f758fdc 100644
--- a/clients/nested.c
+++ b/clients/nested.c
@@ -46,6 +46,7 @@
 #define WL_HIDE_DEPRECATED
 #include <wayland-server.h>
 
+#include "shared/helpers.h"
 #include "window.h"
 
 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
diff --git a/clients/presentation-shm.c b/clients/presentation-shm.c
index e81f5f4..cf4517c 100644
--- a/clients/presentation-shm.c
+++ b/clients/presentation-shm.c
@@ -36,11 +36,10 @@
 #include <time.h>
 
 #include <wayland-client.h>
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "presentation_timing-client-protocol.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 enum run_mode {
 	RUN_MODE_FEEDBACK,
 	RUN_MODE_FEEDBACK_IDLE,
diff --git a/clients/simple-touch.c b/clients/simple-touch.c
index 7864394..446f2ca 100644
--- a/clients/simple-touch.c
+++ b/clients/simple-touch.c
@@ -33,10 +33,9 @@
 #include <sys/mman.h>
 
 #include <wayland-client.h>
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 struct seat {
 	struct touch *touch;
 	struct wl_seat *seat;
diff --git a/clients/stacking.c b/clients/stacking.c
index fc8f8fb..ae9c332 100644
--- a/clients/stacking.c
+++ b/clients/stacking.c
@@ -32,6 +32,7 @@
 #include <linux/input.h>
 #include <cairo.h>
 
+#include "shared/helpers.h"
 #include "window.h"
 
 struct stacking {
diff --git a/clients/subsurfaces.c b/clients/subsurfaces.c
index 749e9bb..32179db 100644
--- a/clients/subsurfaces.c
+++ b/clients/subsurfaces.c
@@ -41,6 +41,7 @@
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
 
+#include "shared/helpers.h"
 #include "window.h"
 
 #if 0
diff --git a/clients/terminal.c b/clients/terminal.c
index 67e8d1d..ad68a00 100644
--- a/clients/terminal.c
+++ b/clients/terminal.c
@@ -44,6 +44,7 @@
 #include <wayland-client.h>
 
 #include "shared/config-parser.h"
+#include "shared/helpers.h"
 #include "window.h"
 
 static int option_fullscreen;
diff --git a/clients/weston-info.c b/clients/weston-info.c
index 0eb589d..58e8e83 100644
--- a/clients/weston-info.c
+++ b/clients/weston-info.c
@@ -32,11 +32,10 @@
 
 #include <wayland-client.h>
 
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "presentation_timing-client-protocol.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
 
 typedef void (*print_info_t)(void *info);
diff --git a/clients/window.c b/clients/window.c
index 67afabd..c5e6f5a 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -67,6 +67,7 @@ typedef void *EGLContext;
 #include <linux/input.h>
 #include <wayland-client.h>
 #include "shared/cairo-util.h"
+#include "shared/helpers.h"
 #include "xdg-shell-client-protocol.h"
 #include "text-cursor-position-client-protocol.h"
 #include "workspaces-client-protocol.h"
diff --git a/clients/window.h b/clients/window.h
index dc089a2..c47aa51 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -33,8 +33,6 @@
 #include "shared/zalloc.h"
 #include "shared/platform.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 #define container_of(ptr, type, member) ({				\
 	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
 	(type *)( (char *)__mptr - offsetof(type,member) );})
diff --git a/clients/wscreensaver.c b/clients/wscreensaver.c
index 8d96963..804ab32 100644
--- a/clients/wscreensaver.c
+++ b/clients/wscreensaver.c
@@ -37,6 +37,7 @@
 
 #include "desktop-shell-client-protocol.h"
 #include "window.h"
+#include "shared/helpers.h"
 
 extern struct wscreensaver_plugin glmatrix_screensaver;
 
diff --git a/ivi-shell/ivi-shell.c b/ivi-shell/ivi-shell.c
index ec3a52b..f9c2583 100644
--- a/ivi-shell/ivi-shell.c
+++ b/ivi-shell/ivi-shell.c
@@ -45,6 +45,7 @@
 #include "ivi-application-server-protocol.h"
 #include "ivi-layout-export.h"
 #include "ivi-layout-private.h"
+#include "shared/helpers.h"
 
 /* Representation of ivi_surface protocol object. */
 struct ivi_shell_surface
diff --git a/shared/cairo-util.c b/shared/cairo-util.c
index f6d02a8..21fcbea 100644
--- a/shared/cairo-util.c
+++ b/shared/cairo-util.c
@@ -35,11 +35,10 @@
 #include <cairo.h>
 #include "cairo-util.h"
 
+#include "shared/helpers.h"
 #include "image-loader.h"
 #include "config-parser.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 void
 surface_flush_device(cairo_surface_t *surface)
 {
diff --git a/shared/helpers.h b/shared/helpers.h
new file mode 100644
index 0000000..7502ff3
--- /dev/null
+++ b/shared/helpers.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright © 2015 Samsung Electronics Co., Ltd
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+  *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_HELPERS_H
+#define WESTON_HELPERS_H
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file
+ * Simple misc helper macros.
+ */
+
+/**
+ * Compile-time computation of number of items in a hardcoded array.
+ *
+ * @param a the array being measured.
+ * @return the number of items hardcoded into the array.
+ */
+#ifndef ARRAY_LENGTH
+#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
+#endif
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* WESTON_HELPERS_H */
diff --git a/shared/image-loader.c b/shared/image-loader.c
index b01266c..ec75bd4 100644
--- a/shared/image-loader.c
+++ b/shared/image-loader.c
@@ -34,10 +34,9 @@
 #include <png.h>
 #include <pixman.h>
 
+#include "shared/helpers.h"
 #include "image-loader.h"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 #ifdef HAVE_WEBP
 #include <webp/decode.h>
 #endif
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index 33e2af6..6d8684d 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -46,6 +46,7 @@
 #include <gbm.h>
 #include <libudev.h>
 
+#include "shared/helpers.h"
 #include "libbacklight.h"
 #include "compositor.h"
 #include "gl-renderer.h"
diff --git a/src/compositor-fbdev.c b/src/compositor-fbdev.c
index 04fb718..a6fd823 100644
--- a/src/compositor-fbdev.c
+++ b/src/compositor-fbdev.c
@@ -42,6 +42,7 @@
 
 #include <libudev.h>
 
+#include "shared/helpers.h"
 #include "compositor.h"
 #include "launcher-util.h"
 #include "pixman-renderer.h"
diff --git a/src/compositor-headless.c b/src/compositor-headless.c
index 598eb33..c0e35ce 100644
--- a/src/compositor-headless.c
+++ b/src/compositor-headless.c
@@ -31,6 +31,7 @@
 #include <sys/time.h>
 #include <stdbool.h>
 
+#include "shared/helpers.h"
 #include "compositor.h"
 #include "pixman-renderer.h"
 #include "presentation_timing-server-protocol.h"
diff --git a/src/compositor-rdp.c b/src/compositor-rdp.c
index d155507..6d4d1e4 100644
--- a/src/compositor-rdp.c
+++ b/src/compositor-rdp.c
@@ -65,6 +65,7 @@
 #include <freerdp/locale/keyboard.h>
 #include <winpr/input.h>
 
+#include "shared/helpers.h"
 #include "compositor.h"
 #include "pixman-renderer.h"
 
diff --git a/src/compositor-rpi.c b/src/compositor-rpi.c
index 722cf9c..8012461 100644
--- a/src/compositor-rpi.c
+++ b/src/compositor-rpi.c
@@ -46,6 +46,7 @@
 #  include "rpi-bcm-stubs.h"
 #endif
 
+#include "shared/helpers.h"
 #include "compositor.h"
 #include "rpi-renderer.h"
 #include "launcher-util.h"
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index 4610552..ab73853 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -42,6 +42,7 @@
 #include "compositor.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
+#include "shared/helpers.h"
 #include "shared/image-loader.h"
 #include "shared/os-compatibility.h"
 #include "shared/cairo-util.h"
diff --git a/src/compositor-x11.c b/src/compositor-x11.c
index c5dcfb9..b85b134 100644
--- a/src/compositor-x11.c
+++ b/src/compositor-x11.c
@@ -53,6 +53,7 @@
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
 #include "shared/config-parser.h"
+#include "shared/helpers.h"
 #include "shared/image-loader.h"
 #include "presentation_timing-server-protocol.h"
 
diff --git a/src/compositor.c b/src/compositor.c
index 2f7864f..2f89b39 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -61,6 +61,7 @@
 #include "compositor.h"
 #include "scaler-server-protocol.h"
 #include "presentation_timing-server-protocol.h"
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "git-version.h"
 #include "version.h"
diff --git a/src/compositor.h b/src/compositor.h
index 1e4ca27..685767e 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -49,8 +49,6 @@ extern "C" {
 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
 #endif
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 #define container_of(ptr, type, member) ({				\
 	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
 	(type *)( (char *)__mptr - offsetof(type,member) );})
diff --git a/src/gl-renderer.c b/src/gl-renderer.c
index ad7012a..62eb490 100644
--- a/src/gl-renderer.c
+++ b/src/gl-renderer.c
@@ -40,6 +40,7 @@
 #include "gl-renderer.h"
 #include "vertex-clipping.h"
 
+#include "shared/helpers.h"
 #include "weston-egl-ext.h"
 
 struct gl_shader {
diff --git a/src/input.c b/src/input.c
index 65d2d09..568985f 100644
--- a/src/input.c
+++ b/src/input.c
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 #include <limits.h>
 
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "compositor.h"
 
diff --git a/tests/ivi_layout-test.c b/tests/ivi_layout-test.c
index 90dee29..d6401c4 100644
--- a/tests/ivi_layout-test.c
+++ b/tests/ivi_layout-test.c
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "shared/helpers.h"
 #include "weston-test-client-helper.h"
 #include "ivi-application-client-protocol.h"
 #include "ivi-test.h"
diff --git a/tests/presentation-test.c b/tests/presentation-test.c
index f3152f5..27f33da 100644
--- a/tests/presentation-test.c
+++ b/tests/presentation-test.c
@@ -31,6 +31,7 @@
 #include <assert.h>
 #include <time.h>
 
+#include "shared/helpers.h"
 #include "weston-test-client-helper.h"
 #include "presentation_timing-client-protocol.h"
 
diff --git a/tests/vertex-clip-test.c b/tests/vertex-clip-test.c
index 2a5ec10..8a4e5a6 100644
--- a/tests/vertex-clip-test.c
+++ b/tests/vertex-clip-test.c
@@ -30,6 +30,7 @@
 
 #include "weston-test-runner.h"
 
+#include "shared/helpers.h"
 #include "src/vertex-clipping.h"
 
 #define BOUNDING_BOX_TOP_Y 100.0f
diff --git a/tests/weston-test-runner.h b/tests/weston-test-runner.h
index 0ed131c..a443691 100644
--- a/tests/weston-test-runner.h
+++ b/tests/weston-test-runner.h
@@ -31,6 +31,8 @@
 
 #include <stdlib.h>
 
+#include "shared/helpers.h"
+
 #ifdef NDEBUG
 #error "Tests must not be built with NDEBUG defined, they rely on assert()."
 #endif
@@ -44,8 +46,6 @@ struct weston_test {
 	int must_fail;
 } __attribute__ ((aligned (32)));
 
-#define ARRAY_LENGTH(a) ((int) (sizeof (a) / sizeof (a)[0]))
-
 #define TEST_BEGIN(name, arg)					\
 	static void name(arg)
 
diff --git a/xwayland/selection.c b/xwayland/selection.c
index 82771c4..572a0b5 100644
--- a/xwayland/selection.c
+++ b/xwayland/selection.c
@@ -31,6 +31,7 @@
 #include <fcntl.h>
 
 #include "xwayland.h"
+#include "shared/helpers.h"
 
 static int
 writable_callback(int fd, uint32_t mask, void *data)
diff --git a/xwayland/window-manager.c b/xwayland/window-manager.c
index 9b4e088..c3a57c1 100644
--- a/xwayland/window-manager.c
+++ b/xwayland/window-manager.c
@@ -42,6 +42,7 @@
 #include "cairo-util.h"
 #include "compositor.h"
 #include "hash.h"
+#include "shared/helpers.h"
 
 struct wm_size_hints {
 	uint32_t flags;
-- 
2.1.0



More information about the wayland-devel mailing list