[Mesa-dev] [PATCH] util: Helper to create sets and hashes with pointer keys

Caio Marcelo de Oliveira Filho caio.oliveira at intel.com
Wed Sep 12 17:31:52 UTC 2018


These combinations are common enough and deserve a shortcut.
---
 src/util/hash_table.c | 10 ++++++++++
 src/util/hash_table.h |  3 +++
 src/util/set.c        | 12 ++++++++++++
 src/util/set.h        |  3 +++
 4 files changed, 28 insertions(+)

diff --git a/src/util/hash_table.c b/src/util/hash_table.c
index 7ee9e18a1fc..f8b695eaadc 100644
--- a/src/util/hash_table.c
+++ b/src/util/hash_table.c
@@ -536,6 +536,16 @@ _mesa_key_pointer_equal(const void *a, const void *b)
    return a == b;
 }
 
+/**
+ * Helper to create a hash table with pointer keys.
+ */
+struct hash_table *
+_mesa_pointer_hash_table_create(void *mem_ctx)
+{
+   return _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
+                                  _mesa_key_pointer_equal);
+}
+
 /**
  * Hash table wrapper which supports 64-bit keys.
  *
diff --git a/src/util/hash_table.h b/src/util/hash_table.h
index 40ff041e94b..43845c9a6ab 100644
--- a/src/util/hash_table.h
+++ b/src/util/hash_table.h
@@ -113,6 +113,9 @@ static inline uint32_t _mesa_hash_pointer(const void *pointer)
    return (uint32_t) ((num >> 2) ^ (num >> 6) ^ (num >> 10) ^ (num >> 14));
 }
 
+struct hash_table *
+_mesa_pointer_hash_table_create(void *mem_ctx);
+
 enum {
    _mesa_fnv32_1a_offset_bias = 2166136261u,
 };
diff --git a/src/util/set.c b/src/util/set.c
index feef96d16ea..3ecc5d43d06 100644
--- a/src/util/set.c
+++ b/src/util/set.c
@@ -36,10 +36,12 @@
 #include <assert.h>
 #include <string.h>
 
+#include "hash_table.h"
 #include "macros.h"
 #include "ralloc.h"
 #include "set.h"
 
+
 /*
  * From Knuth -- a good choice for hash/rehash values is p, p-2 where
  * p and p-2 are both prime.  These tables are sized to have an extra 10%
@@ -441,3 +443,13 @@ _mesa_set_random_entry(struct set *ht,
 
    return NULL;
 }
+
+/**
+ * Helper to create a set with pointer keys.
+ */
+struct set *
+_mesa_pointer_set_create(void *mem_ctx)
+{
+   return _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                           _mesa_key_pointer_equal);
+}
diff --git a/src/util/set.h b/src/util/set.h
index ffd19a798bd..1d7edd9eca9 100644
--- a/src/util/set.h
+++ b/src/util/set.h
@@ -91,6 +91,9 @@ struct set_entry *
 _mesa_set_random_entry(struct set *set,
                        int (*predicate)(struct set_entry *entry));
 
+struct set *
+_mesa_pointer_set_create(void *mem_ctx);
+
 /**
  * This foreach function is safe against deletion, but not against
  * insertion (which may rehash the set, making entry a dangling
-- 
2.18.0



More information about the mesa-dev mailing list