aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-hash.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2019-06-27 13:30:22 +0100
committerNick Alcock <nick.alcock@oracle.com>2019-07-01 11:05:59 +0100
commit9658dc39630b8ac4e849fb529a53902da5fc2b1f (patch)
treedd5246f204e11dd0a90c874ec81dc705f23d58db /libctf/ctf-hash.c
parent3e10cffc9872fda4519f76ba487fd108551a179f (diff)
downloadgdb-9658dc39630b8ac4e849fb529a53902da5fc2b1f.zip
gdb-9658dc39630b8ac4e849fb529a53902da5fc2b1f.tar.gz
gdb-9658dc39630b8ac4e849fb529a53902da5fc2b1f.tar.bz2
libctf: add hash traversal helpers
There are two, ctf_dynhash_iter and ctf_dynhash_iter_remove: the latter lets you return a nonzero value to remove the element being iterated over. Used in the next commit. libctf/ * ctf-impl.h (ctf_hash_iter_f): New. (ctf_dynhash_iter): New declaration. (ctf_dynhash_iter_remove): New declaration. * ctf-hash.c (ctf_dynhash_iter): Define. (ctf_dynhash_iter_remove): Likewise. (ctf_hashtab_traverse): New. (ctf_hashtab_traverse_remove): Likewise. (struct ctf_traverse_cb_arg): Likewise. (struct ctf_traverse_remove_cb_arg): Likewise.
Diffstat (limited to 'libctf/ctf-hash.c')
-rw-r--r--libctf/ctf-hash.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/libctf/ctf-hash.c b/libctf/ctf-hash.c
index 03a398e..12bd6ef 100644
--- a/libctf/ctf-hash.c
+++ b/libctf/ctf-hash.c
@@ -193,6 +193,55 @@ ctf_dynhash_lookup (ctf_dynhash_t *hp, const void *key)
return NULL;
}
+typedef struct ctf_traverse_cb_arg
+{
+ ctf_hash_iter_f fun;
+ void *arg;
+} ctf_traverse_cb_arg_t;
+
+static int
+ctf_hashtab_traverse (void **slot, void *arg_)
+{
+ ctf_helem_t *helem = *((ctf_helem_t **) slot);
+ ctf_traverse_cb_arg_t *arg = (ctf_traverse_cb_arg_t *) arg_;
+
+ arg->fun (helem->key, helem->value, arg->arg);
+ return 1;
+}
+
+void
+ctf_dynhash_iter (ctf_dynhash_t *hp, ctf_hash_iter_f fun, void *arg_)
+{
+ ctf_traverse_cb_arg_t arg = { fun, arg_ };
+ htab_traverse (hp->htab, ctf_hashtab_traverse, &arg);
+}
+
+typedef struct ctf_traverse_remove_cb_arg
+{
+ struct htab *htab;
+ ctf_hash_iter_remove_f fun;
+ void *arg;
+} ctf_traverse_remove_cb_arg_t;
+
+static int
+ctf_hashtab_traverse_remove (void **slot, void *arg_)
+{
+ ctf_helem_t *helem = *((ctf_helem_t **) slot);
+ ctf_traverse_remove_cb_arg_t *arg = (ctf_traverse_remove_cb_arg_t *) arg_;
+
+ if (arg->fun (helem->key, helem->value, arg->arg))
+ htab_clear_slot (arg->htab, slot);
+ return 1;
+}
+
+void
+ctf_dynhash_iter_remove (ctf_dynhash_t *hp, ctf_hash_iter_remove_f fun,
+ void *arg_)
+{
+ ctf_traverse_remove_cb_arg_t arg = { hp->htab, fun, arg_ };
+ htab_traverse (hp->htab, ctf_hashtab_traverse_remove, &arg);
+}
+
void
ctf_dynhash_destroy (ctf_dynhash_t *hp)
{