Add zfree

This commit is contained in:
Johan Malm 2021-02-21 21:59:53 +00:00
parent 1b263e1f67
commit 9f61a819fc
4 changed files with 27 additions and 15 deletions

8
include/common/zfree.h Normal file
View file

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

View file

@ -7,4 +7,5 @@ labwc_sources += files(
'nodename.c', 'nodename.c',
'spawn.c', 'spawn.c',
'string-helpers.c', 'string-helpers.c',
'zfree.c',
) )

11
src/common/zfree.c Normal file
View file

@ -0,0 +1,11 @@
#include <stdlib.h>
#include "common/zfree.h"
void __zfree(void **ptr)
{
if (!ptr || !*ptr) {
return;
}
free(*ptr);
*ptr = NULL;
}

View file

@ -16,6 +16,7 @@
#include "common/log.h" #include "common/log.h"
#include "common/nodename.h" #include "common/nodename.h"
#include "common/string-helpers.h" #include "common/string-helpers.h"
#include "common/zfree.h"
#include "config/keybind.h" #include "config/keybind.h"
#include "config/rcxml.h" #include "config/rcxml.h"
@ -340,28 +341,19 @@ no_config:
post_processing(); post_processing();
} }
static void
free_safe(const void *p)
{
if (p) {
free((void *)p);
}
p = NULL;
}
void void
rcxml_finish(void) rcxml_finish(void)
{ {
free_safe(rc.font_name_activewindow); zfree(rc.font_name_activewindow);
free_safe(rc.theme_name); zfree(rc.theme_name);
struct keybind *k, *k_tmp; struct keybind *k, *k_tmp;
wl_list_for_each_safe (k, k_tmp, &rc.keybinds, link) { wl_list_for_each_safe (k, k_tmp, &rc.keybinds, link) {
wl_list_remove(&k->link); wl_list_remove(&k->link);
free_safe(k->command); zfree(k->command);
free_safe(k->action); zfree(k->action);
free_safe(k->keysyms); zfree(k->keysyms);
free_safe(k); zfree(k);
} }
} }