diff options
author | Moshe Kaplan <me@moshekaplan.com> | 2018-02-05 23:31:58 -0500 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2018-02-07 05:21:20 +0000 |
commit | b13678fd3703607953fc4627461d069314f7e990 (patch) | |
tree | 503016fa8cb505b5bf6ff17181f849bd68e15ade /epan/wmem | |
parent | 32ce1fd3ddd4b9d218302c699c9514045f9a4bf5 (diff) |
Added wmem_map_contains and wmem_map_lookup_extended
Change-Id: I2943c67238fb913258f0f1f15df968c17b1ea002
Reviewed-on: https://code.wireshark.org/review/25626
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'epan/wmem')
-rw-r--r-- | epan/wmem/wmem_map.c | 54 | ||||
-rw-r--r-- | epan/wmem/wmem_map.h | 23 | ||||
-rw-r--r-- | epan/wmem/wmem_test.c | 23 |
3 files changed, 100 insertions, 0 deletions
diff --git a/epan/wmem/wmem_map.c b/epan/wmem/wmem_map.c index 4b4a3a4da3..6793e7d104 100644 --- a/epan/wmem/wmem_map.c +++ b/epan/wmem/wmem_map.c @@ -238,6 +238,30 @@ wmem_map_insert(wmem_map_t *map, const void *key, void *value) return NULL; } +gboolean +wmem_map_contains(wmem_map_t *map, const void *key) +{ + wmem_map_item_t *item; + + /* Make sure we have a table */ + if (map->table == NULL) { + return FALSE; + } + + /* find correct slot */ + item = map->table[HASH(map, key)]; + + /* scan list of items in this slot for the correct value */ + while (item) { + if (map->eql_func(key, item->key)) { + return TRUE; + } + item = item->next; + } + + return FALSE; +} + void * wmem_map_lookup(wmem_map_t *map, const void *key) { @@ -262,6 +286,36 @@ wmem_map_lookup(wmem_map_t *map, const void *key) return NULL; } +gboolean +wmem_map_lookup_extended(wmem_map_t *map, const void *key, const void **orig_key, void **value) +{ + wmem_map_item_t *item; + + /* Make sure we have a table */ + if (map->table == NULL) { + return FALSE; + } + + /* find correct slot */ + item = map->table[HASH(map, key)]; + + /* scan list of items in this slot for the correct value */ + while (item) { + if (map->eql_func(key, item->key)) { + if (orig_key) { + *orig_key = item->key; + } + if (value) { + *value = item->value; + } + return TRUE; + } + item = item->next; + } + + return FALSE; +} + void * wmem_map_remove(wmem_map_t *map, const void *key) { diff --git a/epan/wmem/wmem_map.h b/epan/wmem/wmem_map.h index 3a962da811..00d3c03279 100644 --- a/epan/wmem/wmem_map.h +++ b/epan/wmem/wmem_map.h @@ -99,6 +99,16 @@ WS_DLL_PUBLIC void * wmem_map_insert(wmem_map_t *map, const void *key, void *value); +/** Check if a value is in the map. + * + * @param map The map to search in. + * @param key The key to lookup. + * @return true if the key is in the map, otherwise false. + */ +WS_DLL_PUBLIC +gboolean +wmem_map_contains(wmem_map_t *map, const void *key); + /** Lookup a value in the map. * * @param map The map to search in. @@ -109,6 +119,19 @@ WS_DLL_PUBLIC void * wmem_map_lookup(wmem_map_t *map, const void *key); +/** Lookup a value in the map, returning the key, value, and a boolean which + * is true if the key is found. + * + * @param map The map to search in. + * @param key The key to lookup. + * @param orig_key (optional) The key that was determined to be a match, if any. + * @param value (optional) The value stored at the key, if any. + * @return true if the key is in the map, otherwise false. + */ +WS_DLL_PUBLIC +gboolean +wmem_map_lookup_extended(wmem_map_t *map, const void *key, const void **orig_key, void **value); + /** Remove a value from the map. If no value is stored at that key, nothing * happens. * diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c index 067e32c808..7cb3c973ee 100644 --- a/epan/wmem/wmem_test.c +++ b/epan/wmem/wmem_test.c @@ -882,7 +882,10 @@ wmem_test_map(void) wmem_allocator_t *allocator, *extra_allocator; wmem_map_t *map; gchar *str_key; + const void *str_key_ret; unsigned int i; + unsigned int *key_ret; + unsigned int *value_ret; void *ret; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); @@ -903,8 +906,22 @@ wmem_test_map(void) for (i=0; i<CONTAINER_ITERS; i++) { ret = wmem_map_lookup(map, GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(i)); + g_assert(wmem_map_contains(map, GINT_TO_POINTER(i)) == TRUE); + g_assert(wmem_map_lookup_extended(map, GINT_TO_POINTER(i), NULL, NULL)); + key_ret = NULL; + g_assert(wmem_map_lookup_extended(map, GINT_TO_POINTER(i), GINT_TO_POINTER(&key_ret), NULL)); + g_assert(key_ret == GINT_TO_POINTER(i)); + value_ret = NULL; + g_assert(wmem_map_lookup_extended(map, GINT_TO_POINTER(i), NULL, GINT_TO_POINTER(&value_ret))); + g_assert(value_ret == GINT_TO_POINTER(i)); + key_ret = NULL; + value_ret = NULL; + g_assert(wmem_map_lookup_extended(map, GINT_TO_POINTER(i), GINT_TO_POINTER(&key_ret), GINT_TO_POINTER(&value_ret))); + g_assert(key_ret == GINT_TO_POINTER(i)); + g_assert(value_ret == GINT_TO_POINTER(i)); ret = wmem_map_remove(map, GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(i)); + g_assert(wmem_map_contains(map, GINT_TO_POINTER(i)) == FALSE); ret = wmem_map_lookup(map, GINT_TO_POINTER(i)); g_assert(ret == NULL); ret = wmem_map_remove(map, GINT_TO_POINTER(i)); @@ -938,6 +955,12 @@ wmem_test_map(void) wmem_map_insert(map, str_key, GINT_TO_POINTER(i)); ret = wmem_map_lookup(map, str_key); g_assert(ret == GINT_TO_POINTER(i)); + g_assert(wmem_map_contains(map, str_key) == TRUE); + str_key_ret = NULL; + value_ret = NULL; + g_assert(wmem_map_lookup_extended(map, str_key, &str_key_ret, GINT_TO_POINTER(&value_ret)) == TRUE); + g_assert(g_str_equal(str_key_ret, str_key)); + g_assert(value_ret == GINT_TO_POINTER(i)); } /* test foreach */ |