Commit f7108d4bab3f109ddc871c66257d547fc144d214
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.
Showing
9 changed files
with
25 additions
and
15 deletions
... | ... | @@ -42,7 +42,7 @@ void * TR_hashGet(TR_Hash, const char *, size_t); |
42 | 42 | void * TR_hashGetFirst(TR_Hash); |
43 | 43 | void * TR_hashDeleteByVal(TR_Hash, unsigned long); |
44 | 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 | 46 | void TR_hashCleanup(TR_Hash); |
47 | 47 | |
48 | 48 | #endif // __TR_HASH_H__ | ... | ... |
... | ... | @@ -39,12 +39,12 @@ TR_INSTANCE_INIT(TR_Tree); |
39 | 39 | TR_CLASSVARS_DECL(TR_Tree) {}; |
40 | 40 | |
41 | 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 | 44 | void * TR_treeFind(TR_Tree, const void *, TR_TreeComp); |
45 | 45 | void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp); |
46 | 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 | 48 | void TR_treeDestroy(TR_Tree *, TR_TreeAction); |
49 | 49 | |
50 | 50 | #endif // __TR_TREE_H__ | ... | ... |
... | ... | @@ -25,22 +25,25 @@ |
25 | 25 | #include "tr/hash.h" |
26 | 26 | #include "tr/tree.h" |
27 | 27 | |
28 | -static void (*cb)(const void*); | |
28 | +static void (*cb)(const void*, const void*); | |
29 | 29 | |
30 | 30 | static |
31 | 31 | inline |
32 | 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 | 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 | 44 | cb = callback; |
42 | 45 | |
43 | - TR_treeWalk(this->root, walk); | |
46 | + TR_treeWalk(this->root, data, walk); | |
44 | 47 | } |
45 | 48 | |
46 | 49 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -29,6 +29,10 @@ static |
29 | 29 | int |
30 | 30 | queueCtor(void * _this, va_list * params) |
31 | 31 | { |
32 | + TR_Queue this = _this; | |
33 | + | |
34 | + this->free_msgs = 1; | |
35 | + | |
32 | 36 | return 0; |
33 | 37 | } |
34 | 38 | |
... | ... | @@ -41,7 +45,9 @@ queueDtor(void * _this) |
41 | 45 | |
42 | 46 | while (NULL != node) { |
43 | 47 | TR_Queue next = node->next; |
44 | - TR_delete(node->msg); | |
48 | + if (this->free_msgs) { | |
49 | + TR_delete(node->msg); | |
50 | + } | |
45 | 51 | TR_delete(node); |
46 | 52 | node = next; |
47 | 53 | } | ... | ... |
... | ... | @@ -23,7 +23,7 @@ |
23 | 23 | #include "tr/tree.h" |
24 | 24 | |
25 | 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 | 28 | TR_Tree previous = this; |
29 | 29 | TR_Tree node = this; |
... | ... | @@ -39,7 +39,7 @@ TR_treeWalk(TR_Tree this, TR_TreeAction action) |
39 | 39 | |
40 | 40 | if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) { |
41 | 41 | if (action) { |
42 | - action(node->data, depth); | |
42 | + action(node->data, data, depth); | |
43 | 43 | } |
44 | 44 | previous = node; |
45 | 45 | ... | ... |
Please
register
or
login
to post a comment