libctf: add hash traversal helpers
authorNick Alcock <nick.alcock@oracle.com>
Thu, 27 Jun 2019 12:30:22 +0000 (13:30 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Mon, 1 Jul 2019 10:05:59 +0000 (11:05 +0100)
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.

libctf/ChangeLog
libctf/ctf-hash.c
libctf/ctf-impl.h

index dd3113b316cb53cc6ccc174f01a3170a4198ace8..21b62e1db3fafae7f26cddd5fe432638503111ea 100644 (file)
@@ -1,3 +1,15 @@
+2019-06-28  Nick Alcock <nick.alcock@oracle.com>
+
+       * 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.
+
 2019-06-28  Nick Alcock <nick.alcock@oracle.com>
 
        * ctf-hash.c (ctf_dynhash_remove): Call with a mocked-up element.
index 03a398e06c95e4d8a5ee75f9d105dba597ee6a47..12bd6ef9b921ac077d05f25e4917c042fe2bae13 100644 (file)
@@ -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)
 {
index 44f049316f504f167bf9ea71a4156880c9faad3a..2601e1b082b90b139263edf62018550b97a1d6eb 100644 (file)
@@ -295,6 +295,9 @@ extern int ctf_hash_eq_string (const void *, const void *);
 
 typedef void (*ctf_hash_free_fun) (void *);
 
+typedef void (*ctf_hash_iter_f) (void *key, void *value, void *arg);
+typedef int (*ctf_hash_iter_remove_f) (void *key, void *value, void *arg);
+
 extern ctf_hash_t *ctf_hash_create (unsigned long, ctf_hash_fun, ctf_hash_eq_fun);
 extern int ctf_hash_insert_type (ctf_hash_t *, ctf_file_t *, uint32_t, uint32_t);
 extern int ctf_hash_define_type (ctf_hash_t *, ctf_file_t *, uint32_t, uint32_t);
@@ -308,6 +311,9 @@ extern int ctf_dynhash_insert (ctf_dynhash_t *, void *, void *);
 extern void ctf_dynhash_remove (ctf_dynhash_t *, const void *);
 extern void *ctf_dynhash_lookup (ctf_dynhash_t *, const void *);
 extern void ctf_dynhash_destroy (ctf_dynhash_t *);
+extern void ctf_dynhash_iter (ctf_dynhash_t *, ctf_hash_iter_f, void *);
+extern void ctf_dynhash_iter_remove (ctf_dynhash_t *, ctf_hash_iter_remove_f,
+                                    void *);
 
 #define        ctf_list_prev(elem)     ((void *)(((ctf_list_t *)(elem))->l_prev))
 #define        ctf_list_next(elem)     ((void *)(((ctf_list_t *)(elem))->l_next))