summaryrefslogtreecommitdiff
path: root/src/util/hash_table.h
diff options
context:
space:
mode:
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>2021-01-12 14:49:48 -0500
committerMarge Bot <eric+marge@anholt.net>2021-04-07 22:57:27 +0000
commit31e546a762b7ed4c73d7207868ace87fcc01f063 (patch)
tree3d7b07f0298e7b65ce7c847aa9bd94c239ee818d /src/util/hash_table.h
parent759cc914501fa13d0093cfe8a9b0fc32e36bfb5c (diff)
util/hash_table: add macro for destructively iterating entries
a common usage for hash tables is for tracking exactly one instance of a pointer for a given period of time, after which the table's entries are purged and it is reused this macro enables the purge phase of such usage to reset the table to a pristine state, avoiding future rehashing due to ballooning of deleted entries Reviewed-by: Adam Jackson <ajax@redhat.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8498>
Diffstat (limited to 'src/util/hash_table.h')
-rw-r--r--src/util/hash_table.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/util/hash_table.h b/src/util/hash_table.h
index 9281b917318..ff3b3bef576 100644
--- a/src/util/hash_table.h
+++ b/src/util/hash_table.h
@@ -103,6 +103,8 @@ void _mesa_hash_table_remove_key(struct hash_table *ht,
struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
struct hash_entry *entry);
+struct hash_entry *_mesa_hash_table_next_entry_unsafe(const struct hash_table *ht,
+ struct hash_entry *entry);
struct hash_entry *
_mesa_hash_table_random_entry(struct hash_table *ht,
bool (*predicate)(struct hash_entry *entry));
@@ -136,6 +138,15 @@ _mesa_hash_table_reserve(struct hash_table *ht, unsigned size);
for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL); \
entry != NULL; \
entry = _mesa_hash_table_next_entry(ht, entry))
+/**
+ * This foreach function destroys the table as it iterates.
+ * It is not safe to use when inserting or removing entries.
+ */
+#define hash_table_foreach_remove(ht, entry) \
+ for (struct hash_entry *entry = _mesa_hash_table_next_entry_unsafe(ht, NULL); \
+ (ht)->entries; \
+ entry->hash = 0, entry->key = (void*)NULL, entry->data = NULL, \
+ (ht)->entries--, entry = _mesa_hash_table_next_entry_unsafe(ht, entry))
static inline void
hash_table_call_foreach(struct hash_table *ht,