From 9f61a819fcac987239e9a64f0545db4f82621398 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Sun, 21 Feb 2021 21:59:53 +0000 Subject: [PATCH] Add zfree --- include/common/zfree.h | 8 ++++++++ src/common/meson.build | 1 + src/common/zfree.c | 11 +++++++++++ src/config/rcxml.c | 22 +++++++--------------- 4 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 include/common/zfree.h create mode 100644 src/common/zfree.c diff --git a/include/common/zfree.h b/include/common/zfree.h new file mode 100644 index 00000000..e791b68b --- /dev/null +++ b/include/common/zfree.h @@ -0,0 +1,8 @@ +#ifndef __LABWC_ZFREE_H +#define __LABWC_ZFREE_H + +void __zfree(void **ptr); + +#define zfree(ptr) __zfree((void **)&(ptr)) + +#endif /* __LABWC_ZFREE_H */ diff --git a/src/common/meson.build b/src/common/meson.build index 3efd27b4..21044fdb 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -7,4 +7,5 @@ labwc_sources += files( 'nodename.c', 'spawn.c', 'string-helpers.c', + 'zfree.c', ) diff --git a/src/common/zfree.c b/src/common/zfree.c new file mode 100644 index 00000000..106a6285 --- /dev/null +++ b/src/common/zfree.c @@ -0,0 +1,11 @@ +#include +#include "common/zfree.h" + +void __zfree(void **ptr) +{ + if (!ptr || !*ptr) { + return; + } + free(*ptr); + *ptr = NULL; +} diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 835b24ae..c308f97a 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -16,6 +16,7 @@ #include "common/log.h" #include "common/nodename.h" #include "common/string-helpers.h" +#include "common/zfree.h" #include "config/keybind.h" #include "config/rcxml.h" @@ -340,28 +341,19 @@ no_config: post_processing(); } -static void -free_safe(const void *p) -{ - if (p) { - free((void *)p); - } - p = NULL; -} - void rcxml_finish(void) { - free_safe(rc.font_name_activewindow); - free_safe(rc.theme_name); + zfree(rc.font_name_activewindow); + zfree(rc.theme_name); struct keybind *k, *k_tmp; wl_list_for_each_safe (k, k_tmp, &rc.keybinds, link) { wl_list_remove(&k->link); - free_safe(k->command); - free_safe(k->action); - free_safe(k->keysyms); - free_safe(k); + zfree(k->command); + zfree(k->action); + zfree(k->keysyms); + zfree(k); } }