1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-27 20:44:56 +00:00

cpp-ify prioqueue.c and use free_and_zero in [p-r]*.cc where appropriate

This commit is contained in:
Nikolas Garofil 2010-02-24 02:46:45 +01:00
parent 6092d063c9
commit 4afe96b9ce
6 changed files with 29 additions and 46 deletions

View File

@ -37,7 +37,7 @@ endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc
temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.c proc.cc temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc
user.cc) user.cc)
# add timed thread library # add timed thread library

View File

@ -32,6 +32,7 @@
* *
*/ */
#include "conky.h"
#include <limits.h> /* INT_MAX */ #include <limits.h> /* INT_MAX */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -69,7 +70,7 @@ struct prio_queue *init_prio_queue(void)
{ {
struct prio_queue *retval; struct prio_queue *retval;
retval = malloc(sizeof(struct prio_queue)); retval = (struct prio_queue *) malloc(sizeof(struct prio_queue));
memset(retval, 0, sizeof(struct prio_queue)); memset(retval, 0, sizeof(struct prio_queue));
/* use pq_free_nop by default */ /* use pq_free_nop by default */
@ -110,7 +111,7 @@ static struct prio_elem *init_prio_elem(void *data)
{ {
struct prio_elem *retval; struct prio_elem *retval;
retval = malloc(sizeof(struct prio_elem)); retval = (struct prio_elem *) malloc(sizeof(struct prio_elem));
memset(retval, 0, sizeof(struct prio_elem)); memset(retval, 0, sizeof(struct prio_elem));
retval->data = data; retval->data = data;
@ -171,8 +172,7 @@ check_cur_size:
queue->cur_size--; queue->cur_size--;
queue->tail = queue->tail->prev; queue->tail = queue->tail->prev;
(*queue->free)(queue->tail->next->data); (*queue->free)(queue->tail->next->data);
free(queue->tail->next); free_and_zero(queue->tail->next);
queue->tail->next = NULL;
} }
} }

View File

@ -25,9 +25,6 @@
#ifndef _PRIOQUEUE_H #ifndef _PRIOQUEUE_H
#define _PRIOQUEUE_H #define _PRIOQUEUE_H
#ifdef __cplusplus
extern "C" {
#endif
/* forward-define for private data */ /* forward-define for private data */
struct prio_queue; struct prio_queue;
@ -66,8 +63,4 @@ void *pop_prio_elem(prio_queue_t);
/* clear and free the given queue */ /* clear and free the given queue */
void free_prio_queue(prio_queue_t); void free_prio_queue(prio_queue_t);
#ifdef __cplusplus
}
#endif
#endif /* _PRIOQUEUE_H */ #endif /* _PRIOQUEUE_H */

View File

@ -49,19 +49,20 @@ void free_rss_items(PRSS *data)
{ {
int i; int i;
for (i = 0; i < data->item_count; i++) { if(data->items) {
#define CLEAR(a) if (data->items[i].a) { free(data->items[i].a); data->items[i].a = 0; } for (i = 0; i < data->item_count; i++) {
CLEAR(title); #define CLEAR(a) free_and_zero(data->items[i].a);
CLEAR(link); CLEAR(title);
CLEAR(description); CLEAR(link);
CLEAR(category); CLEAR(description);
CLEAR(pubDate); CLEAR(category);
CLEAR(guid); CLEAR(pubDate);
CLEAR(guid);
#undef CLEAR #undef CLEAR
}
free_and_zero(data->items);
data->item_count = 0;
} }
free(data->items);
data->item_count = 0;
data->items = 0;
} }
void prss_free(PRSS *data) void prss_free(PRSS *data)
@ -69,16 +70,10 @@ void prss_free(PRSS *data)
if (!data) { if (!data) {
return; return;
} }
if (data->version) { free_and_zero(data->version);
free(data->version); free_rss_items(data);
data->version = 0;
}
if (data->items) {
free_rss_items(data);
}
data->version = 0; data->version = 0;
data->items = 0; #define CLEAR(a) free_and_zero(data->a);
#define CLEAR(a) if (data->a) { free(data->a); data->a = 0; }
CLEAR(title); CLEAR(title);
CLEAR(link); CLEAR(link);
CLEAR(description); CLEAR(description);
@ -116,7 +111,7 @@ static inline void read_item(PRSS_Item *res, xmlNodePtr data)
} }
#define ASSIGN(a) if (strcasecmp((const char*)data->name, #a) == EQUAL) { \ #define ASSIGN(a) if (strcasecmp((const char*)data->name, #a) == EQUAL) { \
if (res->a) free(res->a); \ free_and_zero(res->a); \
res->a = strdup((const char*)child->content); \ res->a = strdup((const char*)child->content); \
continue; \ continue; \
} }
@ -143,7 +138,7 @@ static inline void read_element(PRSS *res, xmlNodePtr n)
} }
#define ASSIGN(a) if (strcasecmp((const char*)n->name, #a) == EQUAL) { \ #define ASSIGN(a) if (strcasecmp((const char*)n->name, #a) == EQUAL) { \
if (res->a) free(res->a); \ free_and_zero(res->a); \
res->a = strdup((const char*)child->content); \ res->a = strdup((const char*)child->content); \
return; \ return; \
} }
@ -188,9 +183,9 @@ static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
} }
} }
if (res->version) free(res->version); free_and_zero(res->version);
res->version = strndup("2.0", text_buffer_size); res->version = strndup("2.0", text_buffer_size);
if (res->items) free_rss_items(res); free_rss_items(res);
res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item)); res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item));
res->item_count = 0; res->item_count = 0;
@ -221,9 +216,9 @@ static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
} }
} }
if (res->version) free(res->version); free_and_zero(res->version);
res->version = strndup("1.0", text_buffer_size); res->version = strndup("1.0", text_buffer_size);
if (res->items) free_rss_items(res); free_rss_items(res);
res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item)); res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item));
res->item_count = 0; res->item_count = 0;

View File

@ -109,8 +109,6 @@ void free_read_tcp(struct text_object *obj)
if (!rtd) if (!rtd)
return; return;
if (rtd->host) free_and_zero(rtd->host);
free(rtd->host); free_and_zero(obj->data.opaque);
free(obj->data.opaque);
obj->data.opaque = NULL;
} }

View File

@ -183,8 +183,5 @@ void rss_print_info(struct text_object *obj, char *p, int p_max_size)
void rss_free_obj_info(struct text_object *obj) void rss_free_obj_info(struct text_object *obj)
{ {
if (obj->data.opaque) { free_and_zero(obj->data.opaque);
free(obj->data.opaque);
obj->data.opaque = NULL;
}
} }