wmem: add foreach function to wmem_map.

Makes wmem_map more similar to g_hash.

Change-Id: Ia17a19ab0be8e07fbb64801d54db2ba8217a7fea
Reviewed-on: https://code.wireshark.org/review/15020
Petri-Dish: Anders Broman <a.broman58@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Dario Lombardo 2016-04-20 18:33:50 +02:00 committed by Anders Broman
parent 9abbf8b69d
commit fe75c87575
3 changed files with 43 additions and 2 deletions

View File

@ -214,6 +214,21 @@ wmem_map_remove(wmem_map_t *map, const void *key)
return NULL;
}
void
wmem_map_foreach(wmem_map_t *map, GHFunc foreach_func, gpointer user_data)
{
wmem_map_item_t *cur;
unsigned i;
for (i = 0; i < CAPACITY(map); i++) {
cur = map->table[i];
while (cur) {
foreach_func((gpointer)cur->key, (gpointer)cur->value, user_data);
cur = cur->next;
}
}
}
/* Borrowed from Perl 5.18. This is based on Bob Jenkin's one-at-a-time
* algorithm with some additional randomness seeded in. It is believed to be
* generally secure against collision attacks. See

View File

@ -102,6 +102,17 @@ WS_DLL_PUBLIC
void *
wmem_map_remove(wmem_map_t *map, const void *key);
/** Run a function against all key/value pairs in the map. The order
* of the calls is unpredictable, since it is based on the internal
* storage of data.
*
* @param map The map to use
* @param foreach_func the function to call for each key/value pair
* @param user_data user data to pass to the function
*/
WS_DLL_PUBLIC
void
wmem_map_foreach(wmem_map_t *map, GHFunc foreach_func, gpointer user_data);
/** Compute a strong hash value for an arbitrary sequence of bytes. Use of this
* hash value should be secure against algorithmic complexity attacks, even for

View File

@ -549,7 +549,7 @@ wmem_test_array(void)
}
static void
checkval(gpointer val, gpointer val_to_check)
check_val_list(gpointer val, gpointer val_to_check)
{
g_assert(val == val_to_check);
}
@ -646,10 +646,16 @@ wmem_test_list(void)
for (i=0; i<CONTAINER_ITERS; i++) {
wmem_list_append(list, GINT_TO_POINTER(1));
}
wmem_list_foreach(list, checkval, GINT_TO_POINTER(1));
wmem_list_foreach(list, check_val_list, GINT_TO_POINTER(1));
wmem_destroy_list(list);
}
void
check_val_map(gpointer key _U_, gpointer val, gpointer user_data)
{
g_assert(val == user_data);
}
static void
wmem_test_map(void)
{
@ -696,6 +702,15 @@ wmem_test_map(void)
g_assert(ret == GINT_TO_POINTER(i));
}
/* test foreach */
map = wmem_map_new(allocator, wmem_str_hash, g_str_equal);
g_assert(map);
for (i=0; i<CONTAINER_ITERS; i++) {
str_key = wmem_test_rand_string(allocator, 1, 64);
wmem_map_insert(map, str_key, GINT_TO_POINTER(2));
}
wmem_map_foreach(map, check_val_map, GINT_TO_POINTER(2));
wmem_destroy_allocator(allocator);
}