diff options
author | Jon Cruz <jonc@osg.samsung.com> | 2015-06-15 15:37:10 -0700 |
---|---|---|
committer | Bryce Harrington <bryce@osg.samsung.com> | 2015-06-15 17:11:58 -0700 |
commit | 867d50eea7fc8be4e286ae751b75dcb4f27df99e (patch) | |
tree | 7e2fcbb6d60eb8fe63893df709b72a88bedfcee5 /shared | |
parent | d618f688d53e1df0ac31a275ae8541dc56f283ed (diff) | |
download | weston-867d50eea7fc8be4e286ae751b75dcb4f27df99e.tar.gz |
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 <jonc@osg.samsung.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Bryce Harrington <bryce@osg.samsung.com>
Diffstat (limited to 'shared')
-rw-r--r-- | shared/config-parser.c | 5 | ||||
-rw-r--r-- | shared/helpers.h | 37 |
2 files changed, 38 insertions, 4 deletions
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 <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 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 |