util: add a macro to create unref cleanup functions

Slightly less boilerplate than before.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-10-21 10:29:00 +10:00
parent 5e24b35ecb
commit 2076057ba2
4 changed files with 39 additions and 26 deletions

View file

@ -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;

View file

@ -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

View file

@ -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)

View file

@ -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)