From b5022cbd812d67089164ce3dd2a095eed838909a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 16 May 2024 20:17:15 +0530 Subject: [PATCH] DRYer --- glfw/dbus_glfw.c | 6 ++---- glfw/dbus_glfw.h | 3 +++ glfw/linux_desktop_settings.c | 7 +++---- glfw/linux_notify.c | 2 -- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/glfw/dbus_glfw.c b/glfw/dbus_glfw.c index 252e8ac69..7313bcc85 100644 --- a/glfw/dbus_glfw.c +++ b/glfw/dbus_glfw.c @@ -224,13 +224,12 @@ format_message_error(DBusError *err) { static void method_reply_received(DBusPendingCall *pending, void *user_data) { MethodResponse *res = (MethodResponse*)user_data; - DBusMessage *msg = dbus_pending_call_steal_reply(pending); + RAII_MSG(msg, dbus_pending_call_steal_reply(pending)); if (msg) { DBusError err; dbus_error_init(&err); if (dbus_set_error_from_message(&err, msg)) res->callback(NULL, format_message_error(&err), res->user_data); else res->callback(msg, NULL, res->user_data); - dbus_message_unref(msg); } } @@ -264,7 +263,7 @@ call_method_with_msg(DBusConnection *conn, DBusMessage *msg, int timeout, dbus_p static bool call_method(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, int timeout, dbus_pending_callback callback, void *user_data, va_list ap) { if (!conn || !path) return false; - DBusMessage *msg = dbus_message_new_method_call(node, path, interface, method); + RAII_MSG(msg, dbus_message_new_method_call(node, path, interface, method)); if (!msg) return false; bool retval = false; @@ -274,7 +273,6 @@ call_method(DBusConnection *conn, const char *node, const char *path, const char } else { _glfwInputError(GLFW_PLATFORM_ERROR, "Failed to call DBUS method: %s on node: %s and interface: %s could not add arguments", method, node, interface); } - dbus_message_unref(msg); return retval; } diff --git a/glfw/dbus_glfw.h b/glfw/dbus_glfw.h index fe6f7b3e3..a491778a6 100644 --- a/glfw/dbus_glfw.h +++ b/glfw/dbus_glfw.h @@ -30,6 +30,9 @@ #include #include "backend_utils.h" +static inline void cleanup_msg(void *p) { DBusMessage *m = *(DBusMessage**)p; if (m) dbus_message_unref(m); m = NULL; } +#define RAII_MSG(name, initializer) __attribute__((cleanup(cleanup_msg))) DBusMessage *name = initializer + typedef void(*dbus_pending_callback)(DBusMessage *msg, const char* err, void* data); typedef struct { diff --git a/glfw/linux_desktop_settings.c b/glfw/linux_desktop_settings.c index b089ff2b1..8bc96a48f 100644 --- a/glfw/linux_desktop_settings.c +++ b/glfw/linux_desktop_settings.c @@ -118,14 +118,13 @@ HANDLER(process_desktop_settings) static bool read_desktop_settings(DBusConnection *session_bus) { - DBusMessage *msg = dbus_message_new_method_call(DESKTOP_SERVICE, DESKTOP_PATH, DESKTOP_INTERFACE, "ReadAll"); + RAII_MSG(msg, dbus_message_new_method_call(DESKTOP_SERVICE, DESKTOP_PATH, DESKTOP_INTERFACE, "ReadAll")); if (!msg) return false; DBusMessageIter iter, array_iter; dbus_message_iter_init_append(msg, &iter); - if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &array_iter)) { dbus_message_unref(msg); return false; } - if (!dbus_message_iter_close_container(&iter, &array_iter)) { dbus_message_unref(msg); return false; } + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &array_iter)) { return false; } + if (!dbus_message_iter_close_container(&iter, &array_iter)) { return false; } bool ok = call_method_with_msg(session_bus, msg, DBUS_TIMEOUT_USE_DEFAULT, process_desktop_settings, NULL); - dbus_message_unref(msg); return ok; } diff --git a/glfw/linux_notify.c b/glfw/linux_notify.c index a87044e80..f52c00169 100644 --- a/glfw/linux_notify.c +++ b/glfw/linux_notify.c @@ -15,8 +15,6 @@ static inline void cleanup_free(void *p) { free(*(void**)p); } #define RAII_ALLOC(type, name, initializer) __attribute__((cleanup(cleanup_free))) type *name = initializer -static inline void cleanup_msg(void *p) { dbus_message_unref(*(DBusMessage**)p); *(DBusMessage**)p = NULL; } -#define RAII_MSG(name, initializer) __attribute__((cleanup(cleanup_msg))) DBusMessage *name = initializer static notification_id_type notification_id = 0;