Commit f7108d4bab3f109ddc871c66257d547fc144d214

Authored by Georg Hopp
1 parent da66d5df

add possibility to inject extra data into treeWalk and hashEach and change queue…

… detructor to only conditionally free data within the nodes.
@@ -39,4 +39,4 @@ gmon.out @@ -39,4 +39,4 @@ gmon.out
39 test-driver 39 test-driver
40 /assets/html/_documentation.html 40 /assets/html/_documentation.html
41 tags 41 tags
42 -trdata.h 42 +/trdata.h*
@@ -42,7 +42,7 @@ void * TR_hashGet(TR_Hash, const char *, size_t); @@ -42,7 +42,7 @@ void * TR_hashGet(TR_Hash, const char *, size_t);
42 void * TR_hashGetFirst(TR_Hash); 42 void * TR_hashGetFirst(TR_Hash);
43 void * TR_hashDeleteByVal(TR_Hash, unsigned long); 43 void * TR_hashDeleteByVal(TR_Hash, unsigned long);
44 void * TR_hashGetByVal(TR_Hash, unsigned long); 44 void * TR_hashGetByVal(TR_Hash, unsigned long);
45 -void TR_hashEach(TR_Hash, void (*)(const void*)); 45 +void TR_hashEach(TR_Hash, const void *, void (*)(const void *, const void *));
46 void TR_hashCleanup(TR_Hash); 46 void TR_hashCleanup(TR_Hash);
47 47
48 #endif // __TR_HASH_H__ 48 #endif // __TR_HASH_H__
@@ -45,7 +45,8 @@ TR_CLASS(TR_Queue) { @@ -45,7 +45,8 @@ TR_CLASS(TR_Queue) {
45 */ 45 */
46 TR_Queue first; 46 TR_Queue first;
47 TR_Queue last; 47 TR_Queue last;
48 - size_t nmsg; 48 + size_t nmsg;
  49 + int free_msgs;
49 }; 50 };
50 TR_INSTANCE_INIT(TR_Queue); 51 TR_INSTANCE_INIT(TR_Queue);
51 TR_CLASSVARS_DECL(TR_Queue) {}; 52 TR_CLASSVARS_DECL(TR_Queue) {};
@@ -39,12 +39,12 @@ TR_INSTANCE_INIT(TR_Tree); @@ -39,12 +39,12 @@ TR_INSTANCE_INIT(TR_Tree);
39 TR_CLASSVARS_DECL(TR_Tree) {}; 39 TR_CLASSVARS_DECL(TR_Tree) {};
40 40
41 typedef int (*TR_TreeComp)(const void *, const void *); 41 typedef int (*TR_TreeComp)(const void *, const void *);
42 -typedef void (*TR_TreeAction)(const void *, const int); 42 +typedef void (*TR_TreeAction)(const void *, const void *, const int);
43 43
44 void * TR_treeFind(TR_Tree, const void *, TR_TreeComp); 44 void * TR_treeFind(TR_Tree, const void *, TR_TreeComp);
45 void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp); 45 void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp);
46 void * TR_treeDelete(TR_Tree *, const void *, TR_TreeComp); 46 void * TR_treeDelete(TR_Tree *, const void *, TR_TreeComp);
47 -void TR_treeWalk(TR_Tree, TR_TreeAction); 47 +void TR_treeWalk(TR_Tree, const void *, TR_TreeAction);
48 void TR_treeDestroy(TR_Tree *, TR_TreeAction); 48 void TR_treeDestroy(TR_Tree *, TR_TreeAction);
49 49
50 #endif // __TR_TREE_H__ 50 #endif // __TR_TREE_H__
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 static 27 static
28 inline 28 inline
29 void 29 void
30 -tDelete(const void * node, const int depth) 30 +tDelete(const void * node, const void * data, const int depth)
31 { 31 {
32 TR_delete(node); 32 TR_delete(node);
33 } 33 }
@@ -25,22 +25,25 @@ @@ -25,22 +25,25 @@
25 #include "tr/hash.h" 25 #include "tr/hash.h"
26 #include "tr/tree.h" 26 #include "tr/tree.h"
27 27
28 -static void (*cb)(const void*); 28 +static void (*cb)(const void*, const void*);
29 29
30 static 30 static
31 inline 31 inline
32 void 32 void
33 -walk(const void * node, const int depth) 33 +walk(const void * node, const void * data, const int depth)
34 { 34 {
35 - cb(node); 35 + cb(node, data);
36 } 36 }
37 37
38 void 38 void
39 -TR_hashEach(TR_Hash this, void (*callback)(const void*)) 39 +TR_hashEach(
  40 + TR_Hash this,
  41 + const void * data,
  42 + void (*callback)(const void *, const void *))
40 { 43 {
41 cb = callback; 44 cb = callback;
42 45
43 - TR_treeWalk(this->root, walk); 46 + TR_treeWalk(this->root, data, walk);
44 } 47 }
45 48
46 // vim: set ts=4 sw=4: 49 // vim: set ts=4 sw=4:
@@ -29,6 +29,10 @@ static @@ -29,6 +29,10 @@ static
29 int 29 int
30 queueCtor(void * _this, va_list * params) 30 queueCtor(void * _this, va_list * params)
31 { 31 {
  32 + TR_Queue this = _this;
  33 +
  34 + this->free_msgs = 1;
  35 +
32 return 0; 36 return 0;
33 } 37 }
34 38
@@ -41,7 +45,9 @@ queueDtor(void * _this) @@ -41,7 +45,9 @@ queueDtor(void * _this)
41 45
42 while (NULL != node) { 46 while (NULL != node) {
43 TR_Queue next = node->next; 47 TR_Queue next = node->next;
44 - TR_delete(node->msg); 48 + if (this->free_msgs) {
  49 + TR_delete(node->msg);
  50 + }
45 TR_delete(node); 51 TR_delete(node);
46 node = next; 52 node = next;
47 } 53 }
@@ -47,7 +47,7 @@ TR_treeDestroy(TR_Tree * this, TR_TreeAction action) @@ -47,7 +47,7 @@ TR_treeDestroy(TR_Tree * this, TR_TreeAction action)
47 TR_Tree parent = TR_TREE_PARENT(node); 47 TR_Tree parent = TR_TREE_PARENT(node);
48 48
49 if (action) { 49 if (action) {
50 - action(node->data, depth); 50 + action(node->data, NULL, depth);
51 } 51 }
52 52
53 previous = node; 53 previous = node;
@@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
23 #include "tr/tree.h" 23 #include "tr/tree.h"
24 24
25 void 25 void
26 -TR_treeWalk(TR_Tree this, TR_TreeAction action) 26 +TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action)
27 { 27 {
28 TR_Tree previous = this; 28 TR_Tree previous = this;
29 TR_Tree node = this; 29 TR_Tree node = this;
@@ -39,7 +39,7 @@ TR_treeWalk(TR_Tree this, TR_TreeAction action) @@ -39,7 +39,7 @@ TR_treeWalk(TR_Tree this, TR_TreeAction action)
39 39
40 if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) { 40 if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) {
41 if (action) { 41 if (action) {
42 - action(node->data, depth); 42 + action(node->data, data, depth);
43 } 43 }
44 previous = node; 44 previous = node;
45 45
Please register or login to post a comment