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>
This commit is contained in:
Michael Mann 2017-02-07 16:52:42 -05:00
parent 3cc1d1cf5a
commit 9b4f325132
3 changed files with 30 additions and 0 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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