From 867d50eea7fc8be4e286ae751b75dcb4f27df99e Mon Sep 17 00:00:00 2001 From: Jon Cruz Date: Mon, 15 Jun 2015 15:37:10 -0700 Subject: [PATCH] Unified multiple definitions of container_of() macro. Removed duplicate definitions of the container_of() macro and refactored sources to use the single implementation. Signed-off-by: Jon A. Cruz Reviewed-by: Peter Hutterer Reviewed-by: Bryce Harrington --- 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 b5229742..776f8da8 100644 --- a/clients/clickdot.c +++ b/clients/clickdot.c @@ -40,6 +40,7 @@ #include #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 03b1b193..9cf3d686 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 c47aa515..b61a62a1 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 622aa5a8..b6413405 100644 --- a/desktop-shell/exposay.c +++ b/desktop-shell/exposay.c @@ -28,6 +28,7 @@ #include #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 aa95b9bb..1028df13 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 9b81212d..ab9c420b 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 34a7b24b..eae346af 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 abe7a1d0..c624ef37 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 61868215..4aed4c87 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 4174836d..a50773ba 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -39,10 +39,7 @@ #include #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 83f79b11..1d1e4589 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 4b69bddd..cc7482dc 100644 --- a/src/animation.c +++ b/src/animation.c @@ -34,6 +34,7 @@ #include #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 5aa8b6e4..e339454c 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -29,6 +29,7 @@ #include #include "compositor.h" +#include "shared/helpers.h" struct weston_binding { uint32_t key; diff --git a/src/clipboard.c b/src/clipboard.c index 023cd411..da7dbb60 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -33,6 +33,7 @@ #include #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 b318e4bc..2adc886b 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 2a3ca75b..7166f572 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 b7b2d220..3586f5bf 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 8cdf2238..825b734c 100644 --- a/src/data-device.c +++ b/src/data-device.c @@ -32,6 +32,7 @@ #include #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 9fba25e4..2cbfb88d 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 77571701..ce79d34e 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 23315b12..b19875d5 100644 --- a/src/pixman-renderer.c +++ b/src/pixman-renderer.c @@ -32,6 +32,7 @@ #include #include "pixman-renderer.h" +#include "shared/helpers.h" #include diff --git a/src/rpi-renderer.c b/src/rpi-renderer.c index d2185327..204802ed 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 diff --git a/src/screen-share.c b/src/screen-share.c index 92c4e028..6b1b34cc 100644 --- a/src/screen-share.c +++ b/src/screen-share.c @@ -40,6 +40,7 @@ #include #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 ecf2fddd..b7b8dce3 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 4ebbbddb..d33f7038 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 880df68c..4216607e 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 c39a7144..b4abfbf2 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 f4fa5ce8..df9a1391 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 83389580..db5e1d03 100644 --- a/xwayland/launcher.c +++ b/xwayland/launcher.c @@ -36,6 +36,7 @@ #include #include "xwayland.h" +#include "shared/helpers.h" static int