[PATCH weston v2 4/4] Unified multiple definitions of container_of() macro.

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


Removed duplicate definitions of the container_of() macro and
refactored sources to use the single implementation.

Signed-off-by: Jon A. Cruz <jonc at osg.samsung.com>
---
 clients/clickdot.c                  |  1 +
 clients/desktop-shell.c             |  1 +
 clients/window.h                    |  4 ----
 desktop-shell/exposay.c             |  1 +
 desktop-shell/input-panel.c         |  1 +
 fullscreen-shell/fullscreen-shell.c |  1 +
 ivi-shell/hmi-controller.c          |  1 +
 ivi-shell/input-panel-ivi.c         |  1 +
 ivi-shell/ivi-layout.c              |  1 +
 shared/config-parser.c              |  5 +----
 shared/helpers.h                    | 37 +++++++++++++++++++++++++++++++++++++
 src/animation.c                     |  1 +
 src/bindings.c                      |  1 +
 src/clipboard.c                     |  1 +
 src/cms-colord.c                    |  1 +
 src/cms-static.c                    |  1 +
 src/compositor.h                    |  4 ----
 src/data-device.c                   |  1 +
 src/libinput-device.c               |  1 +
 src/libinput-seat.c                 |  1 +
 src/pixman-renderer.c               |  1 +
 src/rpi-renderer.c                  |  1 +
 src/screen-share.c                  |  1 +
 src/screenshooter.c                 |  1 +
 src/text-backend.c                  |  1 +
 src/zoom.c                          |  1 +
 tests/ivi_layout-test-plugin.c      |  1 +
 tests/weston-test.c                 |  2 ++
 xwayland/launcher.c                 |  1 +
 29 files changed, 64 insertions(+), 12 deletions(-)

diff --git a/clients/clickdot.c b/clients/clickdot.c
index b522974..776f8da 100644
--- a/clients/clickdot.c
+++ b/clients/clickdot.c
@@ -40,6 +40,7 @@
 #include <wayland-client.h>
 
 #include "window.h"
+#include "shared/helpers.h"
 
 struct clickdot {
 	struct display *display;
diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 03b1b19..9cf3d68 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -45,6 +45,7 @@
 #include "window.h"
 #include "shared/cairo-util.h"
 #include "shared/config-parser.h"
+#include "shared/helpers.h"
 
 #include "desktop-shell-client-protocol.h"
 
diff --git a/clients/window.h b/clients/window.h
index c47aa51..b61a62a 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -33,10 +33,6 @@
 #include "shared/zalloc.h"
 #include "shared/platform.h"
 
-#define container_of(ptr, type, member) ({				\
-	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
-
 struct window;
 struct widget;
 struct display;
diff --git a/desktop-shell/exposay.c b/desktop-shell/exposay.c
index 622aa5a..b641340 100644
--- a/desktop-shell/exposay.c
+++ b/desktop-shell/exposay.c
@@ -28,6 +28,7 @@
 #include <linux/input.h>
 
 #include "shell.h"
+#include "shared/helpers.h"
 
 struct exposay_surface {
 	struct desktop_shell *shell;
diff --git a/desktop-shell/input-panel.c b/desktop-shell/input-panel.c
index aa95b9b..1028df1 100644
--- a/desktop-shell/input-panel.c
+++ b/desktop-shell/input-panel.c
@@ -32,6 +32,7 @@
 #include "shell.h"
 #include "desktop-shell-server-protocol.h"
 #include "input-method-server-protocol.h"
+#include "shared/helpers.h"
 
 struct input_panel_surface {
 	struct wl_resource *resource;
diff --git a/fullscreen-shell/fullscreen-shell.c b/fullscreen-shell/fullscreen-shell.c
index 9b81212..ab9c420 100644
--- a/fullscreen-shell/fullscreen-shell.c
+++ b/fullscreen-shell/fullscreen-shell.c
@@ -34,6 +34,7 @@
 
 #include "compositor.h"
 #include "fullscreen-shell-server-protocol.h"
+#include "shared/helpers.h"
 
 struct fullscreen_shell {
 	struct wl_client *client;
diff --git a/ivi-shell/hmi-controller.c b/ivi-shell/hmi-controller.c
index 34a7b24..eae346a 100644
--- a/ivi-shell/hmi-controller.c
+++ b/ivi-shell/hmi-controller.c
@@ -60,6 +60,7 @@
 
 #include "ivi-layout-export.h"
 #include "ivi-hmi-controller-server-protocol.h"
+#include "shared/helpers.h"
 
 /*****************************************************************************
  *  structure, globals
diff --git a/ivi-shell/input-panel-ivi.c b/ivi-shell/input-panel-ivi.c
index abe7a1d..c624ef3 100644
--- a/ivi-shell/input-panel-ivi.c
+++ b/ivi-shell/input-panel-ivi.c
@@ -34,6 +34,7 @@
 #include "ivi-shell.h"
 #include "input-method-server-protocol.h"
 #include "ivi-layout-private.h"
+#include "shared/helpers.h"
 
 struct input_panel_surface {
 	struct wl_resource *resource;
diff --git a/ivi-shell/ivi-layout.c b/ivi-shell/ivi-layout.c
index 6186821..4aed4c8 100644
--- a/ivi-shell/ivi-layout.c
+++ b/ivi-shell/ivi-layout.c
@@ -63,6 +63,7 @@
 #include "ivi-layout-export.h"
 #include "ivi-layout-private.h"
 
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 
 struct link_layer {
diff --git a/shared/config-parser.c b/shared/config-parser.c
index 4174836..a50773b 100644
--- a/shared/config-parser.c
+++ b/shared/config-parser.c
@@ -39,10 +39,7 @@
 
 #include <wayland-util.h>
 #include "config-parser.h"
-
-#define container_of(ptr, type, member) ({				\
-	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
+#include "helpers.h"
 
 struct weston_config_entry {
 	char *key;
diff --git a/shared/helpers.h b/shared/helpers.h
index 83f79b1..1d1e458 100644
--- a/shared/helpers.h
+++ b/shared/helpers.h
@@ -52,6 +52,43 @@ extern "C" {
 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
 #endif
 
+/**
+ * Returns a pointer the the containing struct of a given member item.
+ *
+ * To demonstrate, the following example retrieves a pointer to
+ * `example_container` given only its `destroy_listener` member:
+ *
+ * @code
+ * struct example_container {
+ *     struct wl_listener destroy_listener;
+ *     // other members...
+ * };
+ *
+ * void example_container_destroy(struct wl_listener *listener, void *data)
+ * {
+ *     struct example_container *ctr;
+ *
+ *     ctr = wl_container_of(listener, ctr, destroy_listener);
+ *     // destroy ctr...
+ * }
+ * @endcode
+ *
+ * @param ptr A valid pointer to the contained item.
+ *
+ * @param type A pointer to the type of content that the list item
+ * stores. Type does not need be a valid pointer; a null or
+ * an uninitialised pointer will suffice.
+ *
+ * @param member The named location of ptr within the sample type.
+ *
+ * @return The container for the specified pointer.
+ */
+#ifndef container_of
+#define container_of(ptr, type, member) ({				\
+	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
+	(type *)( (char *)__mptr - offsetof(type,member) );})
+#endif
+
 #ifdef  __cplusplus
 }
 #endif
diff --git a/src/animation.c b/src/animation.c
index 4b69bdd..cc7482d 100644
--- a/src/animation.c
+++ b/src/animation.c
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 
 #include "compositor.h"
+#include "shared/helpers.h"
 
 WL_EXPORT void
 weston_spring_init(struct weston_spring *spring,
diff --git a/src/bindings.c b/src/bindings.c
index 5aa8b6e..e339454 100644
--- a/src/bindings.c
+++ b/src/bindings.c
@@ -29,6 +29,7 @@
 #include <linux/input.h>
 
 #include "compositor.h"
+#include "shared/helpers.h"
 
 struct weston_binding {
 	uint32_t key;
diff --git a/src/clipboard.c b/src/clipboard.c
index 023cd41..da7dbb6 100644
--- a/src/clipboard.c
+++ b/src/clipboard.c
@@ -33,6 +33,7 @@
 #include <sys/uio.h>
 
 #include "compositor.h"
+#include "shared/helpers.h"
 
 struct clipboard_source {
 	struct weston_data_source base;
diff --git a/src/cms-colord.c b/src/cms-colord.c
index b318e4b..2adc886 100644
--- a/src/cms-colord.c
+++ b/src/cms-colord.c
@@ -36,6 +36,7 @@
 
 #include "compositor.h"
 #include "cms-helper.h"
+#include "shared/helpers.h"
 
 struct cms_colord {
 	struct weston_compositor	*ec;
diff --git a/src/cms-static.c b/src/cms-static.c
index 2a3ca75..7166f57 100644
--- a/src/cms-static.c
+++ b/src/cms-static.c
@@ -30,6 +30,7 @@
 
 #include "compositor.h"
 #include "cms-helper.h"
+#include "shared/helpers.h"
 
 struct cms_static {
 	struct weston_compositor	*ec;
diff --git a/src/compositor.h b/src/compositor.h
index b7b2d22..3586f5b 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -45,10 +45,6 @@ extern "C" {
 #include "zalloc.h"
 #include "timeline-object.h"
 
-#define container_of(ptr, type, member) ({				\
-	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
-
 struct weston_transform {
 	struct weston_matrix matrix;
 	struct wl_list link;
diff --git a/src/data-device.c b/src/data-device.c
index 8cdf223..825b734 100644
--- a/src/data-device.c
+++ b/src/data-device.c
@@ -32,6 +32,7 @@
 #include <assert.h>
 
 #include "compositor.h"
+#include "shared/helpers.h"
 
 struct weston_drag {
 	struct wl_client *client;
diff --git a/src/libinput-device.c b/src/libinput-device.c
index 9fba25e..2cbfb88 100644
--- a/src/libinput-device.c
+++ b/src/libinput-device.c
@@ -38,6 +38,7 @@
 
 #include "compositor.h"
 #include "libinput-device.h"
+#include "shared/helpers.h"
 
 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
 
diff --git a/src/libinput-seat.c b/src/libinput-seat.c
index 7757170..ce79d34 100644
--- a/src/libinput-seat.c
+++ b/src/libinput-seat.c
@@ -37,6 +37,7 @@
 #include "launcher-util.h"
 #include "libinput-seat.h"
 #include "libinput-device.h"
+#include "shared/helpers.h"
 
 static const char default_seat[] = "seat0";
 static const char default_seat_name[] = "default";
diff --git a/src/pixman-renderer.c b/src/pixman-renderer.c
index 23315b1..b19875d 100644
--- a/src/pixman-renderer.c
+++ b/src/pixman-renderer.c
@@ -32,6 +32,7 @@
 #include <assert.h>
 
 #include "pixman-renderer.h"
+#include "shared/helpers.h"
 
 #include <linux/input.h>
 
diff --git a/src/rpi-renderer.c b/src/rpi-renderer.c
index d218532..204802e 100644
--- a/src/rpi-renderer.c
+++ b/src/rpi-renderer.c
@@ -37,6 +37,7 @@
 
 #include "compositor.h"
 #include "rpi-renderer.h"
+#include "shared/helpers.h"
 
 #ifdef ENABLE_EGL
 #include <EGL/egl.h>
diff --git a/src/screen-share.c b/src/screen-share.c
index 92c4e02..6b1b34c 100644
--- a/src/screen-share.c
+++ b/src/screen-share.c
@@ -40,6 +40,7 @@
 #include <wayland-client.h>
 
 #include "compositor.h"
+#include "shared/helpers.h"
 #include "shared/os-compatibility.h"
 #include "fullscreen-shell-client-protocol.h"
 
diff --git a/src/screenshooter.c b/src/screenshooter.c
index ecf2fdd..b7b8dce 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -35,6 +35,7 @@
 
 #include "compositor.h"
 #include "screenshooter-server-protocol.h"
+#include "shared/helpers.h"
 
 #include "wcap/wcap-decode.h"
 
diff --git a/src/text-backend.c b/src/text-backend.c
index 4ebbbdd..d33f703 100644
--- a/src/text-backend.c
+++ b/src/text-backend.c
@@ -35,6 +35,7 @@
 #include "compositor.h"
 #include "text-server-protocol.h"
 #include "input-method-server-protocol.h"
+#include "shared/helpers.h"
 
 struct text_input_manager;
 struct input_method;
diff --git a/src/zoom.c b/src/zoom.c
index 880df68..4216607 100644
--- a/src/zoom.c
+++ b/src/zoom.c
@@ -29,6 +29,7 @@
 
 #include "compositor.h"
 #include "text-cursor-position-server-protocol.h"
+#include "shared/helpers.h"
 
 static void
 weston_zoom_frame_z(struct weston_animation *animation,
diff --git a/tests/ivi_layout-test-plugin.c b/tests/ivi_layout-test-plugin.c
index c39a714..b4abfbf 100644
--- a/tests/ivi_layout-test-plugin.c
+++ b/tests/ivi_layout-test-plugin.c
@@ -35,6 +35,7 @@
 #include "weston-test-server-protocol.h"
 #include "ivi-test.h"
 #include "ivi-shell/ivi-layout-export.h"
+#include "shared/helpers.h"
 
 struct test_context;
 
diff --git a/tests/weston-test.c b/tests/weston-test.c
index f4fa5ce..df9a139 100644
--- a/tests/weston-test.c
+++ b/tests/weston-test.c
@@ -40,6 +40,8 @@
 #include "src/weston-egl-ext.h"
 #endif /* ENABLE_EGL */
 
+#include "shared/helpers.h"
+
 struct weston_test {
 	struct weston_compositor *compositor;
 	struct weston_layer layer;
diff --git a/xwayland/launcher.c b/xwayland/launcher.c
index 8338958..db5e1d0 100644
--- a/xwayland/launcher.c
+++ b/xwayland/launcher.c
@@ -36,6 +36,7 @@
 #include <signal.h>
 
 #include "xwayland.h"
+#include "shared/helpers.h"
 
 
 static int
-- 
2.1.0



More information about the wayland-devel mailing list