From b9b5faa37d6d2eb715795cebad8647121e3ae6fa Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sat, 13 Sep 2014 07:31:42 +0100 Subject: [PATCH] add queueDestroy and hashEmpty --- include/tr/hash.h | 2 ++ include/tr/queue.h | 1 + src/hash/each.c | 1 - src/queue/Makefile.am | 3 ++- src/queue/destroy.c | 46 +++++++++++++++++++++++++++++++++++++++++++ src/queue/queue.c | 12 +---------- 6 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/queue/destroy.c diff --git a/include/tr/hash.h b/include/tr/hash.h index b9ab3f2..a4328f5 100644 --- a/include/tr/hash.h +++ b/include/tr/hash.h @@ -36,6 +36,8 @@ TR_CLASS(TR_Hash) { TR_INSTANCE_INIT(TR_Hash); TR_CLASSVARS_DECL(TR_Hash) {}; +#define TR_hashEmpty(hash) (NULL == (hash)->root) + void * TR_hashAdd(TR_Hash, void *); void * TR_hashDelete(TR_Hash, const char *, size_t); void * TR_hashGet(TR_Hash, const char *, size_t); diff --git a/include/tr/queue.h b/include/tr/queue.h index 69f9dd8..1240d34 100644 --- a/include/tr/queue.h +++ b/include/tr/queue.h @@ -57,6 +57,7 @@ void * TR_queueGet(TR_Queue); TR_Queue TR_queueFind(TR_Queue, void *); TR_Queue TR_queueFindParent(TR_Queue, void *); void TR_queueDelete(TR_Queue, void *); +void TR_queueDestroy(TR_Queue); #define TR_queueEmpty(this) (0 >= (this)->nmsg) diff --git a/src/hash/each.c b/src/hash/each.c index 103d737..97c3995 100644 --- a/src/hash/each.c +++ b/src/hash/each.c @@ -28,7 +28,6 @@ static void (*cb)(const void*, const void*); static -inline void walk(const void * node, const void * data, const int depth) { diff --git a/src/queue/Makefile.am b/src/queue/Makefile.am index bc42ec8..d3e49c5 100644 --- a/src/queue/Makefile.am +++ b/src/queue/Makefile.am @@ -11,6 +11,7 @@ libqueue_la_SOURCES = queue.c \ put_first.c \ find.c \ find_parent.c \ - delete.c + delete.c \ + destroy.c libqueue_la_CFLAGS = $(AM_CFLAGS) diff --git a/src/queue/destroy.c b/src/queue/destroy.c new file mode 100644 index 0000000..d70ae1f --- /dev/null +++ b/src/queue/destroy.c @@ -0,0 +1,46 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2014 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "tr/queue.h" + +void +TR_queueDestroy(TR_Queue this) +{ + TR_Queue node = this->first; + + while (NULL != node) { + TR_Queue next = node->next; + if (this->free_msgs) { + TR_delete(node->msg); + } + TR_delete(node); + node = next; + } + + this->first = this->next = this->last = NULL; + this->nmsg = 0; +} + +// vim: set ts=4 sw=4: diff --git a/src/queue/queue.c b/src/queue/queue.c index 63584c9..29d0c81 100644 --- a/src/queue/queue.c +++ b/src/queue/queue.c @@ -40,17 +40,7 @@ static void queueDtor(void * _this) { - TR_Queue this = _this; - TR_Queue node = this->first; - - while (NULL != node) { - TR_Queue next = node->next; - if (this->free_msgs) { - TR_delete(node->msg); - } - TR_delete(node); - node = next; - } + TR_queueDestroy((TR_Queue)_this); } TR_INIT_IFACE(TR_Class, queueCtor, queueDtor, NULL);