From f7108d4bab3f109ddc871c66257d547fc144d214 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 5 Aug 2014 18:51:08 +0100 Subject: [PATCH] add possibility to inject extra data into treeWalk and hashEach and change queue detructor to only conditionally free data within the nodes. --- .gitignore | 2 +- include/tr/hash.h | 2 +- include/tr/queue.h | 3 ++- include/tr/tree.h | 4 ++-- src/hash/cleanup.c | 2 +- src/hash/each.c | 13 ++++++++----- src/queue/queue.c | 8 +++++++- src/tree/destroy.c | 2 +- src/tree/walk.c | 4 ++-- 9 files changed, 25 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index b3338dc..a99e22b 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,4 @@ gmon.out test-driver /assets/html/_documentation.html tags -trdata.h +/trdata.h* diff --git a/include/tr/hash.h b/include/tr/hash.h index 3627581..b9ab3f2 100644 --- a/include/tr/hash.h +++ b/include/tr/hash.h @@ -42,7 +42,7 @@ void * TR_hashGet(TR_Hash, const char *, size_t); void * TR_hashGetFirst(TR_Hash); void * TR_hashDeleteByVal(TR_Hash, unsigned long); void * TR_hashGetByVal(TR_Hash, unsigned long); -void TR_hashEach(TR_Hash, void (*)(const void*)); +void TR_hashEach(TR_Hash, const void *, void (*)(const void *, const void *)); void TR_hashCleanup(TR_Hash); #endif // __TR_HASH_H__ diff --git a/include/tr/queue.h b/include/tr/queue.h index bba1f04..9391835 100644 --- a/include/tr/queue.h +++ b/include/tr/queue.h @@ -45,7 +45,8 @@ TR_CLASS(TR_Queue) { */ TR_Queue first; TR_Queue last; - size_t nmsg; + size_t nmsg; + int free_msgs; }; TR_INSTANCE_INIT(TR_Queue); TR_CLASSVARS_DECL(TR_Queue) {}; diff --git a/include/tr/tree.h b/include/tr/tree.h index febcee6..6ff6040 100644 --- a/include/tr/tree.h +++ b/include/tr/tree.h @@ -39,12 +39,12 @@ TR_INSTANCE_INIT(TR_Tree); TR_CLASSVARS_DECL(TR_Tree) {}; typedef int (*TR_TreeComp)(const void *, const void *); -typedef void (*TR_TreeAction)(const void *, const int); +typedef void (*TR_TreeAction)(const void *, const void *, const int); void * TR_treeFind(TR_Tree, const void *, TR_TreeComp); void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp); void * TR_treeDelete(TR_Tree *, const void *, TR_TreeComp); -void TR_treeWalk(TR_Tree, TR_TreeAction); +void TR_treeWalk(TR_Tree, const void *, TR_TreeAction); void TR_treeDestroy(TR_Tree *, TR_TreeAction); #endif // __TR_TREE_H__ diff --git a/src/hash/cleanup.c b/src/hash/cleanup.c index 4d0d808..2eb0d76 100644 --- a/src/hash/cleanup.c +++ b/src/hash/cleanup.c @@ -27,7 +27,7 @@ static inline void -tDelete(const void * node, const int depth) +tDelete(const void * node, const void * data, const int depth) { TR_delete(node); } diff --git a/src/hash/each.c b/src/hash/each.c index 6138d01..103d737 100644 --- a/src/hash/each.c +++ b/src/hash/each.c @@ -25,22 +25,25 @@ #include "tr/hash.h" #include "tr/tree.h" -static void (*cb)(const void*); +static void (*cb)(const void*, const void*); static inline void -walk(const void * node, const int depth) +walk(const void * node, const void * data, const int depth) { - cb(node); + cb(node, data); } void -TR_hashEach(TR_Hash this, void (*callback)(const void*)) +TR_hashEach( + TR_Hash this, + const void * data, + void (*callback)(const void *, const void *)) { cb = callback; - TR_treeWalk(this->root, walk); + TR_treeWalk(this->root, data, walk); } // vim: set ts=4 sw=4: diff --git a/src/queue/queue.c b/src/queue/queue.c index 95382ec..63584c9 100644 --- a/src/queue/queue.c +++ b/src/queue/queue.c @@ -29,6 +29,10 @@ static int queueCtor(void * _this, va_list * params) { + TR_Queue this = _this; + + this->free_msgs = 1; + return 0; } @@ -41,7 +45,9 @@ queueDtor(void * _this) while (NULL != node) { TR_Queue next = node->next; - TR_delete(node->msg); + if (this->free_msgs) { + TR_delete(node->msg); + } TR_delete(node); node = next; } diff --git a/src/tree/destroy.c b/src/tree/destroy.c index 70c6638..72c472f 100644 --- a/src/tree/destroy.c +++ b/src/tree/destroy.c @@ -47,7 +47,7 @@ TR_treeDestroy(TR_Tree * this, TR_TreeAction action) TR_Tree parent = TR_TREE_PARENT(node); if (action) { - action(node->data, depth); + action(node->data, NULL, depth); } previous = node; diff --git a/src/tree/walk.c b/src/tree/walk.c index 89b10d5..72caff5 100644 --- a/src/tree/walk.c +++ b/src/tree/walk.c @@ -23,7 +23,7 @@ #include "tr/tree.h" void -TR_treeWalk(TR_Tree this, TR_TreeAction action) +TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) { TR_Tree previous = this; TR_Tree node = this; @@ -39,7 +39,7 @@ TR_treeWalk(TR_Tree this, TR_TreeAction action) if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) { if (action) { - action(node->data, depth); + action(node->data, data, depth); } previous = node;