Mesa (glsl2): Add hash table helper functions for using pointers as hash keys

Ian Romanick idr at kemper.freedesktop.org
Tue Jul 6 22:05:00 UTC 2010


Module: Mesa
Branch: glsl2
Commit: d1a1ee583e7e8338243b3e9768d2fc5312a1145d
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=d1a1ee583e7e8338243b3e9768d2fc5312a1145d

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Tue Jul  6 14:49:14 2010 -0700

Add hash table helper functions for using pointers as hash keys

---

 src/glsl/ir_function_inlining.cpp |   14 +-------------
 src/glsl/ir_validate.cpp          |   15 ++-------------
 src/mesa/shader/hash_table.c      |   14 ++++++++++++++
 src/mesa/shader/hash_table.h      |   24 ++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/src/glsl/ir_function_inlining.cpp b/src/glsl/ir_function_inlining.cpp
index 1adf678..b3d1f1d 100644
--- a/src/glsl/ir_function_inlining.cpp
+++ b/src/glsl/ir_function_inlining.cpp
@@ -33,9 +33,7 @@
 #include "ir_function_inlining.h"
 #include "ir_expression_flattening.h"
 #include "glsl_types.h"
-extern "C" {
 #include "hash_table.h"
-}
 
 class ir_function_inlining_visitor : public ir_hierarchical_visitor {
 public:
@@ -60,16 +58,6 @@ public:
 };
 
 
-unsigned int hash_func(const void *key)
-{
-   return (unsigned int)(uintptr_t)key;
-}
-
-int hash_compare_func(const void *key1, const void *key2)
-{
-   return key1 == key2 ? 0 : 1;
-}
-
 bool
 automatic_inlining_predicate(ir_instruction *ir)
 {
@@ -124,7 +112,7 @@ ir_call::generate_inline(ir_instruction *next_ir)
    ir_variable *retval = NULL;
    struct hash_table *ht;
 
-   ht = hash_table_ctor(0, hash_func, hash_compare_func);
+   ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
 
    num_parameters = 0;
    foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
diff --git a/src/glsl/ir_validate.cpp b/src/glsl/ir_validate.cpp
index 1953852..7582d57 100644
--- a/src/glsl/ir_validate.cpp
+++ b/src/glsl/ir_validate.cpp
@@ -36,26 +36,15 @@
 #include <inttypes.h>
 #include "ir.h"
 #include "ir_hierarchical_visitor.h"
-extern "C" {
 #include "hash_table.h"
-}
-
-static unsigned int hash_func(const void *key)
-{
-   return (unsigned int)(uintptr_t)key;
-}
-
-static int hash_compare_func(const void *key1, const void *key2)
-{
-   return key1 == key2 ? 0 : 1;
-}
 
 
 class ir_validate : public ir_hierarchical_visitor {
 public:
    ir_validate()
    {
-      this->ht = hash_table_ctor(0, hash_func, hash_compare_func);
+      this->ht = hash_table_ctor(0, hash_table_pointer_hash,
+				 hash_table_pointer_compare);
 
       this->callback = ir_validate::validate_ir;
       this->data = ht;
diff --git a/src/mesa/shader/hash_table.c b/src/mesa/shader/hash_table.c
index fa6ba2b..933e300 100644
--- a/src/mesa/shader/hash_table.c
+++ b/src/mesa/shader/hash_table.c
@@ -157,3 +157,17 @@ hash_table_string_hash(const void *key)
 
     return hash;
 }
+
+
+unsigned
+hash_table_pointer_hash(const void *key)
+{
+   return (unsigned)((uintptr_t) key / sizeof(void *));
+}
+
+
+int
+hash_table_pointer_compare(const void *key1, const void *key2)
+{
+   return key1 == key2 ? 0 : 1;
+}
diff --git a/src/mesa/shader/hash_table.h b/src/mesa/shader/hash_table.h
index 881e756..0552691 100644
--- a/src/mesa/shader/hash_table.h
+++ b/src/mesa/shader/hash_table.h
@@ -118,6 +118,30 @@ extern unsigned hash_table_string_hash(const void *key);
  */
 #define hash_table_string_compare ((hash_compare_func_t) strcmp)
 
+
+/**
+ * Compute hash value of a pointer
+ *
+ * \param key  Pointer to be used as a hash key
+ *
+ * \note
+ * The memory pointed to by \c key is \b never accessed.  The value of \c key
+ * itself is used as the hash key
+ *
+ * \sa hash_table_pointer_compare
+ */
+unsigned
+hash_table_pointer_hash(const void *key);
+
+
+/**
+ * Compare two pointers used as keys
+ *
+ * \sa hash_table_pointer_hash
+ */
+int
+hash_table_pointer_compare(const void *key1, const void *key2);
+
 #ifdef __cplusplus
 };
 #endif




More information about the mesa-commit mailing list