From 2076057ba27546a8bd02e2581eaec59629c1e685 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 21 Oct 2020 10:29:00 +1000 Subject: [PATCH] util: add a macro to create unref cleanup functions Slightly less boilerplate than before. Signed-off-by: Peter Hutterer --- src/util-mem.h | 13 +++++++++++++ test/eierpecken.h | 24 ++++++++++++------------ tools/ei-demo-client.c | 14 +++++++------- tools/eis-demo-server.c | 14 +++++++------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/util-mem.h b/src/util-mem.h index adc9464..dc951de 100644 --- a/src/util-mem.h +++ b/src/util-mem.h @@ -51,6 +51,19 @@ static inline void _free_ptr_(void *p) { free(*(void**)p); } } \ struct __useless_struct_to_allow_trailing_semicolon__ +/** + * Define a cleanup function for the struct type foo with a matching + * foo_unref(). Use: + * DEFINE_UNREF_CLEANUP_FUNC(foo) + * _cleanup_(foo_unrefp) struct foo *bar; + */ +#define DEFINE_UNREF_CLEANUP_FUNC(_type) \ + static inline void _type##_unrefp(struct _type **_p) { \ + if (*_p) \ + _type##_unref(*_p); \ + } \ + struct __useless_struct_to_allow_trailing_semicolon__ + static inline void* _steal(void *ptr) { void **original = (void**)ptr; diff --git a/test/eierpecken.h b/test/eierpecken.h index 991e40d..89977a4 100644 --- a/test/eierpecken.h +++ b/test/eierpecken.h @@ -170,7 +170,7 @@ _peck_dispatch_ei(struct peck *peck, int lineno); struct peck * peck_unref(struct peck *peck); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct peck*, peck_unref); +DEFINE_UNREF_CLEANUP_FUNC(peck); #define _cleanup_peck_ _cleanup_(peck_unrefp) /** @@ -236,28 +236,28 @@ const char * peck_eis_event_type_name(enum eis_event_type type); /* Define a bunch of _cleanup_foo_ macros for a struct foo */ -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei *, ei_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei); #define _cleanup_ei_ _cleanup_(ei_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_event *, ei_event_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei_event); #define _cleanup_ei_event_ _cleanup_(ei_event_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_device *, ei_device_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei_device); #define _cleanup_ei_device_ _cleanup_(ei_device_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_touch *, ei_touch_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei_touch); #define _cleanup_ei_touch_ _cleanup_(ei_touch_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_keymap *, ei_keymap_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei_keymap); #define _cleanup_ei_keymap_ _cleanup_(ei_keymap_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis *, eis_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis); #define _cleanup_eis_ _cleanup_(eis_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_client *, eis_client_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis_client); #define _cleanup_eis_client_ _cleanup_(eis_client_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_event *, eis_event_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis_event); #define _cleanup_eis_event_ _cleanup_(eis_event_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_device *, eis_device_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis_device); #define _cleanup_eis_device_ _cleanup_(eis_device_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_keymap *, eis_keymap_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis_keymap); #define _cleanup_eis_keymap_ _cleanup_(eis_keymap_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_seat *, eis_seat_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis_seat); #define _cleanup_eis_seat_ _cleanup_(eis_seat_unrefp) /* Macros intended just for readability to make it more obvious which part diff --git a/tools/ei-demo-client.c b/tools/ei-demo-client.c index f03535b..3f9cfb7 100644 --- a/tools/ei-demo-client.c +++ b/tools/ei-demo-client.c @@ -44,10 +44,10 @@ #include "src/util-color.h" #include "src/util-strings.h" -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei *, ei_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_device *, ei_device_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_keymap *, ei_keymap_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_event *, ei_event_unref); +DEFINE_UNREF_CLEANUP_FUNC(ei); +DEFINE_UNREF_CLEANUP_FUNC(ei_device); +DEFINE_UNREF_CLEANUP_FUNC(ei_keymap); +DEFINE_UNREF_CLEANUP_FUNC(ei_event); static inline void _printf_(1, 2) @@ -66,9 +66,9 @@ colorprint(const char *format, ...) } #if HAVE_LIBXKBCOMMON -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_context*, xkb_context_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_keymap*, xkb_keymap_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_state*, xkb_state_unref); +DEFINE_UNREF_CLEANUP_FUNC(xkb_context); +DEFINE_UNREF_CLEANUP_FUNC(xkb_keymap); +DEFINE_UNREF_CLEANUP_FUNC(xkb_state); #define _cleanup_xkb_context_ _cleanup_(xkb_context_unrefp) #define _cleanup_xkb_keymap_ _cleanup_(xkb_keymap_unrefp) #define _cleanup_xkb_state_ _cleanup_(xkb_state_unrefp) diff --git a/tools/eis-demo-server.c b/tools/eis-demo-server.c index 1cadd90..8565fdf 100644 --- a/tools/eis-demo-server.c +++ b/tools/eis-demo-server.c @@ -49,13 +49,13 @@ static void sighandler(int signal) { stop = true; } -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis *, eis_unref); +DEFINE_UNREF_CLEANUP_FUNC(eis); +DEFINE_UNREF_CLEANUP_FUNC(eis_event); +DEFINE_UNREF_CLEANUP_FUNC(eis_keymap); +DEFINE_UNREF_CLEANUP_FUNC(eis_seat); #define _cleanup_eis_ _cleanup_(eis_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_event *, eis_event_unref); #define _cleanup_eis_event_ _cleanup_(eis_event_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_keymap *, eis_keymap_unref); #define _cleanup_eis_keymap_ _cleanup_(eis_keymap_unrefp) -DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_seat *, eis_seat_unref); #define _cleanup_eis_seat_ _cleanup_(eis_seat_unrefp) static void unlink_free(char **path) { @@ -83,9 +83,9 @@ colorprint(const char *format, ...) } #if HAVE_LIBXKBCOMMON -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_context*, xkb_context_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_keymap*, xkb_keymap_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_state*, xkb_state_unref); +DEFINE_UNREF_CLEANUP_FUNC(xkb_context); +DEFINE_UNREF_CLEANUP_FUNC(xkb_keymap); +DEFINE_UNREF_CLEANUP_FUNC(xkb_state); #define _cleanup_xkb_context_ _cleanup_(xkb_context_unrefp) #define _cleanup_xkb_keymap_ _cleanup_(xkb_keymap_unrefp) #define _cleanup_xkb_state_ _cleanup_(xkb_state_unrefp)