From 1fa465efb6b941c9a16b204584d35ae4c66c4f93 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 01:51:02 +0100 Subject: [PATCH] Use free_and_zero in l*.cc where appropriate --- src/libmpdclient.cc | 67 ++++++++++++--------------------------------- src/linux.cc | 14 ++++------ src/llua.cc | 24 ++++------------ 3 files changed, 28 insertions(+), 77 deletions(-) diff --git a/src/libmpdclient.cc b/src/libmpdclient.cc index 92156a13..ecc5ee42 100644 --- a/src/libmpdclient.cc +++ b/src/libmpdclient.cc @@ -485,12 +485,8 @@ void mpd_clearError(mpd_Connection *connection) void mpd_closeConnection(mpd_Connection *connection) { closesocket(connection->sock); - if (connection->returnElement) { - free(connection->returnElement); - } - if (connection->request) { - free(connection->request); - } + free_and_zero(connection->returnElement); + free_and_zero(connection->request); free(connection); WSACleanup(); } @@ -856,9 +852,7 @@ mpd_Status *mpd_getStatus(mpd_Connection *connection) void mpd_freeStatus(mpd_Status *status) { - if (status->error) { - free(status->error); - } + free_and_zero(status->error); free(status); } @@ -1011,39 +1005,17 @@ static void mpd_initSong(mpd_Song *song) static void mpd_finishSong(mpd_Song *song) { - if (song->file) { - free(song->file); - } - if (song->artist) { - free(song->artist); - } - if (song->album) { - free(song->album); - } - if (song->title) { - free(song->title); - } - if (song->track) { - free(song->track); - } - if (song->name) { - free(song->name); - } - if (song->date) { - free(song->date); - } - if (song->genre) { - free(song->genre); - } - if (song->composer) { - free(song->composer); - } - if (song->disc) { - free(song->disc); - } - if (song->comment) { - free(song->comment); - } + free_and_zero(song->file); + free_and_zero(song->artist); + free_and_zero(song->album); + free_and_zero(song->title); + free_and_zero(song->track); + free_and_zero(song->name); + free_and_zero(song->date); + free_and_zero(song->genre); + free_and_zero(song->composer); + free_and_zero(song->disc); + free_and_zero(song->comment); } mpd_Song *mpd_newSong(void) @@ -1112,9 +1084,7 @@ static void mpd_initDirectory(mpd_Directory *directory) static void mpd_finishDirectory(mpd_Directory *directory) { - if (directory->path) { - free(directory->path); - } + free_and_zero(directory->path); } mpd_Directory *mpd_newDirectory(void) @@ -1151,9 +1121,7 @@ static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist) static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist) { - if (playlist->path) { - free(playlist->path); - } + free_and_zero(playlist->path); } mpd_PlaylistFile *mpd_newPlaylistFile(void) @@ -2084,8 +2052,7 @@ void mpd_commitSearch(mpd_Connection *connection) connection->request[len - 1] = '\0'; mpd_sendInfoCommand(connection, connection->request); - free(connection->request); - connection->request = NULL; + free_and_zero(connection->request); } /** diff --git a/src/linux.cc b/src/linux.cc index c2272ea0..8327345e 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -248,7 +248,6 @@ static struct { int count; } gw_info; -#define COND_FREE(x) if(x) free(x); x = 0 #define SAVE_SET_STRING(x, y) \ if (x && strcmp((char *)x, (char *)y)) { \ free(x); \ @@ -279,8 +278,8 @@ void update_gateway_info(void) unsigned long dest, gate, mask; unsigned int flags; - COND_FREE(gw_info.iface); - COND_FREE(gw_info.ip); + free_and_zero(gw_info.iface); + free_and_zero(gw_info.ip); gw_info.count = 0; if ((fp = fopen("/proc/net/route", "r")) == NULL) { @@ -315,10 +314,8 @@ void free_gateway_info(struct text_object *obj) { (void)obj; - if (gw_info.iface) - free(gw_info.iface); - if (gw_info.ip) - free(gw_info.ip); + free_and_zero(gw_info.iface); + free_and_zero(gw_info.ip); memset(&gw_info, 0, sizeof(gw_info)); } @@ -1116,8 +1113,7 @@ void free_sysfs_sensor(struct text_object *obj) return; close(sf->fd); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(obj->data.opaque); } #define CPUFREQ_PREFIX "/sys/devices/system/cpu" diff --git a/src/llua.cc b/src/llua.cc index 1ba00351..6cab830d 100644 --- a/src/llua.cc +++ b/src/llua.cc @@ -299,22 +299,10 @@ void llua_close(void) #ifdef HAVE_SYS_INOTIFY_H llua_rm_notifies(); #endif /* HAVE_SYS_INOTIFY_H */ - if (draw_pre_hook) { - free(draw_pre_hook); - draw_pre_hook = 0; - } - if (draw_post_hook) { - free(draw_post_hook); - draw_post_hook = 0; - } - if (startup_hook) { - free(startup_hook); - startup_hook = 0; - } - if (shutdown_hook) { - free(shutdown_hook); - shutdown_hook = 0; - } + free_and_zero(draw_pre_hook); + free_and_zero(draw_post_hook); + free_and_zero(startup_hook); + free_and_zero(shutdown_hook); if(!lua_L) return; lua_close(lua_L); lua_L = NULL; @@ -410,13 +398,13 @@ void llua_set_number(const char *key, double value) void llua_set_startup_hook(const char *args) { - if (startup_hook) free(startup_hook); + free_and_zero(startup_hook); startup_hook = strdup(args); } void llua_set_shutdown_hook(const char *args) { - if (shutdown_hook) free(shutdown_hook); + free_and_zero(shutdown_hook); shutdown_hook = strdup(args); }