aboutsummaryrefslogtreecommitdiffstats
path: root/epan/wmem
diff options
context:
space:
mode:
authorMichael Mann <mmann78@netscape.net>2017-02-07 16:52:42 -0500
committerMichael Mann <mmann78@netscape.net>2017-03-04 21:53:25 +0000
commit9b4f325132091c053131b352df914f378286809a (patch)
tree4b2418f6d53694cbcf8262ac3c6363f8cffc5928 /epan/wmem
parent3cc1d1cf5aa6a5c05aa5d03b5e4c9fd844ced5a2 (diff)
Add wmem_tree_count.
There are cases where wmem_tree needs to know its number of nodes. Change-Id: I6411cf4275fd4d85a1d76382e1922d236be3b176 Reviewed-on: https://code.wireshark.org/review/20005 Petri-Dish: Michael Mann <mmann78@netscape.net> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Michael Mann <mmann78@netscape.net>
Diffstat (limited to 'epan/wmem')
-rw-r--r--epan/wmem/wmem_test.c4
-rw-r--r--epan/wmem/wmem_tree.c21
-rw-r--r--epan/wmem/wmem_tree.h5
3 files changed, 30 insertions, 0 deletions
diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c
index fb46a83501..067e32c808 100644
--- a/epan/wmem/wmem_test.c
+++ b/epan/wmem/wmem_test.c
@@ -1147,6 +1147,7 @@ wmem_test_tree(void)
g_assert(wmem_tree_lookup32(tree, i) == GINT_TO_POINTER(i));
g_assert(!wmem_tree_is_empty(tree));
}
+ g_assert(wmem_tree_count(tree) == CONTAINER_ITERS);
wmem_free_all(allocator);
tree = wmem_tree_new(allocator);
@@ -1155,6 +1156,7 @@ wmem_test_tree(void)
wmem_tree_insert32(tree, rand_int, GINT_TO_POINTER(i));
g_assert(wmem_tree_lookup32(tree, rand_int) == GINT_TO_POINTER(i));
}
+ g_assert(wmem_tree_count(tree) == CONTAINER_ITERS);
wmem_free_all(allocator);
/* test auto-reset functionality */
@@ -1164,7 +1166,9 @@ wmem_test_tree(void)
wmem_tree_insert32(tree, i, GINT_TO_POINTER(i));
g_assert(wmem_tree_lookup32(tree, i) == GINT_TO_POINTER(i));
}
+ g_assert(wmem_tree_count(tree) == CONTAINER_ITERS);
wmem_free_all(extra_allocator);
+ g_assert(wmem_tree_count(tree) == 0);
for (i=0; i<CONTAINER_ITERS; i++) {
g_assert(wmem_tree_lookup32(tree, i) == NULL);
g_assert(wmem_tree_lookup32_le(tree, i) == NULL);
diff --git a/epan/wmem/wmem_tree.c b/epan/wmem/wmem_tree.c
index c5ef912f03..9a76c02982 100644
--- a/epan/wmem/wmem_tree.c
+++ b/epan/wmem/wmem_tree.c
@@ -271,6 +271,27 @@ wmem_tree_is_empty(wmem_tree_t *tree)
return tree->root == NULL;
}
+static gboolean
+count_nodes(const void *key _U_, void *value _U_, void *userdata)
+{
+ guint* count = (guint*)userdata;
+ (*count)++;
+ return FALSE;
+}
+
+guint
+wmem_tree_count(wmem_tree_t* tree)
+{
+ guint count = 0;
+
+ /* Recursing through the tree counting each node is the simplest approach.
+ We don't keep track of the count within the tree because it can get
+ complicated with subtrees within the tree */
+ wmem_tree_foreach(tree, count_nodes, &count);
+
+ return count;
+}
+
static wmem_tree_node_t *
create_node(wmem_allocator_t *allocator, wmem_tree_node_t *parent, const void *key,
void *data, wmem_node_color_t color, gboolean is_subtree)
diff --git a/epan/wmem/wmem_tree.h b/epan/wmem/wmem_tree.h
index 788af2413e..bcb7fdf783 100644
--- a/epan/wmem/wmem_tree.h
+++ b/epan/wmem/wmem_tree.h
@@ -77,6 +77,11 @@ WS_DLL_PUBLIC
gboolean
wmem_tree_is_empty(wmem_tree_t *tree);
+/** Returns number of nodes in tree */
+WS_DLL_PUBLIC
+guint
+wmem_tree_count(wmem_tree_t* tree);
+
/** Insert a node indexed by a guint32 key value.
*
* Data is a pointer to the structure you want to be able to retrieve by