[Mesa-dev] [PATCH 1/5] mesa: Move string_to_uint_map's implementation to its own c++ header file.

Eric Anholt eric at anholt.net
Thu Oct 25 09:13:01 PDT 2012


I want to replace the hash_table.[ch] implementation, but this was getting in
the way.
---
 src/glsl/link_uniforms.cpp              |    2 +-
 src/glsl/linker.cpp                     |    1 +
 src/mesa/main/shader_query.cpp          |    2 +-
 src/mesa/main/shaderobj.c               |    2 +-
 src/mesa/main/uniform_query.cpp         |    2 +-
 src/mesa/program/hash_table.h           |   95 -----------------------
 src/mesa/program/ir_to_mesa.cpp         |    2 +-
 src/mesa/program/sampler.cpp            |    2 +-
 src/mesa/program/string_to_uint_map.cpp |    4 +-
 src/mesa/program/string_to_uint_map.h   |  128 +++++++++++++++++++++++++++++++
 10 files changed, 137 insertions(+), 103 deletions(-)
 create mode 100644 src/mesa/program/string_to_uint_map.h

diff --git a/src/glsl/link_uniforms.cpp b/src/glsl/link_uniforms.cpp
index aa8a8b3..930b65b 100644
--- a/src/glsl/link_uniforms.cpp
+++ b/src/glsl/link_uniforms.cpp
@@ -26,7 +26,7 @@
 #include "linker.h"
 #include "ir_uniform.h"
 #include "glsl_symbol_table.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "program.h"
 
 static inline unsigned int
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
index 34ce133..b0d2e3a 100644
--- a/src/glsl/linker.cpp
+++ b/src/glsl/linker.cpp
@@ -69,6 +69,7 @@
 #include "ir.h"
 #include "program.h"
 #include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "linker.h"
 #include "ir_optimization.h"
 
diff --git a/src/mesa/main/shader_query.cpp b/src/mesa/main/shader_query.cpp
index 02a48ba..fa19d0f 100644
--- a/src/mesa/main/shader_query.cpp
+++ b/src/mesa/main/shader_query.cpp
@@ -32,7 +32,7 @@
 #include "glsl_symbol_table.h"
 #include "ir.h"
 #include "shaderobj.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "../glsl/program.h"
 
 extern "C" {
diff --git a/src/mesa/main/shaderobj.c b/src/mesa/main/shaderobj.c
index 1706cac..ba0fc7d 100644
--- a/src/mesa/main/shaderobj.c
+++ b/src/mesa/main/shaderobj.c
@@ -38,7 +38,7 @@
 #include "main/uniforms.h"
 #include "program/program.h"
 #include "program/prog_parameter.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "ralloc.h"
 
 /**********************************************************************/
diff --git a/src/mesa/main/uniform_query.cpp b/src/mesa/main/uniform_query.cpp
index bddb8f9..5f05ca4 100644
--- a/src/mesa/main/uniform_query.cpp
+++ b/src/mesa/main/uniform_query.cpp
@@ -29,7 +29,7 @@
 #include "main/context.h"
 #include "ir.h"
 #include "ir_uniform.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "../glsl/program.h"
 #include "../glsl/ir_uniform.h"
 #include "main/shaderapi.h"
diff --git a/src/mesa/program/hash_table.h b/src/mesa/program/hash_table.h
index e95fc49..6c96bfd 100644
--- a/src/mesa/program/hash_table.h
+++ b/src/mesa/program/hash_table.h
@@ -38,8 +38,6 @@
 #include <limits.h>
 #include <assert.h>
 
-struct string_to_uint_map;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -188,101 +186,8 @@ hash_table_call_foreach(struct hash_table *ht,
 					 void *closure),
 			void *closure);
 
-struct string_to_uint_map *
-string_to_uint_map_ctor();
-
-void
-string_to_uint_map_dtor(struct string_to_uint_map *);
-
 
 #ifdef __cplusplus
 }
-
-/**
- * Map from a string (name) to an unsigned integer value
- *
- * \note
- * Because of the way this class interacts with the \c hash_table
- * implementation, values of \c UINT_MAX cannot be stored in the map.
- */
-struct string_to_uint_map {
-public:
-   string_to_uint_map()
-   {
-      this->ht = hash_table_ctor(0, hash_table_string_hash,
-				 hash_table_string_compare);
-   }
-
-   ~string_to_uint_map()
-   {
-      hash_table_call_foreach(this->ht, delete_key, NULL);
-      hash_table_dtor(this->ht);
-   }
-
-   /**
-    * Remove all mappings from this map.
-    */
-   void clear()
-   {
-      hash_table_call_foreach(this->ht, delete_key, NULL);
-      hash_table_clear(this->ht);
-   }
-
-   /**
-    * Get the value associated with a particular key
-    *
-    * \return
-    * If \c key is found in the map, \c true is returned.  Otherwise \c false
-    * is returned.
-    *
-    * \note
-    * If \c key is not found in the table, \c value is not modified.
-    */
-   bool get(unsigned &value, const char *key)
-   {
-      const intptr_t v =
-	 (intptr_t) hash_table_find(this->ht, (const void *) key);
-
-      if (v == 0)
-	 return false;
-
-      value = (unsigned)(v - 1);
-      return true;
-   }
-
-   void put(unsigned value, const char *key)
-   {
-      /* The low-level hash table structure returns NULL if key is not in the
-       * hash table.  However, users of this map might want to store zero as a
-       * valid value in the table.  Bias the value by +1 so that a
-       * user-specified zero is stored as 1.  This enables ::get to tell the
-       * difference between a user-specified zero (returned as 1 by
-       * hash_table_find) and the key not in the table (returned as 0 by
-       * hash_table_find).
-       *
-       * The net effect is that we can't store UINT_MAX in the table.  This is
-       * because UINT_MAX+1 = 0.
-       */
-      assert(value != UINT_MAX);
-      char *dup_key = strdup(key);
-      bool result = hash_table_replace(this->ht,
-				       (void *) (intptr_t) (value + 1),
-				       dup_key);
-      if (result)
-	 free(dup_key);
-   }
-
-private:
-   static void delete_key(const void *key, void *data, void *closure)
-   {
-      (void) data;
-      (void) closure;
-
-      free((char *)key);
-   }
-
-   struct hash_table *ht;
-};
-
 #endif /* __cplusplus */
 #endif /* HASH_TABLE_H */
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index d5b9683..e8974cc 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -45,7 +45,7 @@
 
 #include "main/mtypes.h"
 #include "main/shaderobj.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 
 extern "C" {
 #include "main/shaderapi.h"
diff --git a/src/mesa/program/sampler.cpp b/src/mesa/program/sampler.cpp
index e3641aa..8949d85 100644
--- a/src/mesa/program/sampler.cpp
+++ b/src/mesa/program/sampler.cpp
@@ -27,7 +27,7 @@
 #include "glsl_types.h"
 #include "ir_visitor.h"
 #include "../glsl/program.h"
-#include "program/hash_table.h"
+#include "program/string_to_uint_map.h"
 #include "ir_uniform.h"
 
 extern "C" {
diff --git a/src/mesa/program/string_to_uint_map.cpp b/src/mesa/program/string_to_uint_map.cpp
index cfa73ab..b6a08c2 100644
--- a/src/mesa/program/string_to_uint_map.cpp
+++ b/src/mesa/program/string_to_uint_map.cpp
@@ -23,11 +23,11 @@
 
 /**
  * \file string_to_uint_map.cpp
- * \brief Dumb wrapprs so that C code can create and destroy maps.
- *
+` *
  * \author Ian Romanick <ian.d.romanick at intel.com>
  */
 #include "hash_table.h"
+#include "string_to_uint_map.h"
 
 extern "C" struct string_to_uint_map *
 string_to_uint_map_ctor()
diff --git a/src/mesa/program/string_to_uint_map.h b/src/mesa/program/string_to_uint_map.h
new file mode 100644
index 0000000..8921a6e
--- /dev/null
+++ b/src/mesa/program/string_to_uint_map.h
@@ -0,0 +1,128 @@
+/*
+ * Copyright © 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "hash_table.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct string_to_uint_map;
+
+struct string_to_uint_map *
+string_to_uint_map_ctor();
+
+void
+string_to_uint_map_dtor(struct string_to_uint_map *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef __cplusplus
+/**
+ * Map from a string (name) to an unsigned integer value
+ *
+ * \note
+ * Because of the way this class interacts with the \c hash_table
+ * implementation, values of \c UINT_MAX cannot be stored in the map.
+ */
+struct string_to_uint_map {
+public:
+   string_to_uint_map()
+   {
+      this->ht = hash_table_ctor(0, hash_table_string_hash,
+				 hash_table_string_compare);
+   }
+
+   ~string_to_uint_map()
+   {
+      hash_table_call_foreach(this->ht, delete_key, NULL);
+      hash_table_dtor(this->ht);
+   }
+
+   /**
+    * Remove all mappings from this map.
+    */
+   void clear()
+   {
+      hash_table_call_foreach(this->ht, delete_key, NULL);
+      hash_table_clear(this->ht);
+   }
+
+   /**
+    * Get the value associated with a particular key
+    *
+    * \return
+    * If \c key is found in the map, \c true is returned.  Otherwise \c false
+    * is returned.
+    *
+    * \note
+    * If \c key is not found in the table, \c value is not modified.
+    */
+   bool get(unsigned &value, const char *key)
+   {
+      const intptr_t v =
+	 (intptr_t) hash_table_find(this->ht, (const void *) key);
+
+      if (v == 0)
+	 return false;
+
+      value = (unsigned)(v - 1);
+      return true;
+   }
+
+   void put(unsigned value, const char *key)
+   {
+      /* The low-level hash table structure returns NULL if key is not in the
+       * hash table.  However, users of this map might want to store zero as a
+       * valid value in the table.  Bias the value by +1 so that a
+       * user-specified zero is stored as 1.  This enables ::get to tell the
+       * difference between a user-specified zero (returned as 1 by
+       * hash_table_find) and the key not in the table (returned as 0 by
+       * hash_table_find).
+       *
+       * The net effect is that we can't store UINT_MAX in the table.  This is
+       * because UINT_MAX+1 = 0.
+       */
+      assert(value != UINT_MAX);
+      char *dup_key = strdup(key);
+      bool result = hash_table_replace(this->ht,
+				       (void *) (intptr_t) (value + 1),
+				       dup_key);
+      if (result)
+	 free(dup_key);
+   }
+
+private:
+   static void delete_key(const void *key, void *data, void *closure)
+   {
+      (void) data;
+      (void) closure;
+
+      free((char *)key);
+   }
+
+   struct hash_table *ht;
+};
+#endif /* __cplusplus */
-- 
1.7.10.4



More information about the mesa-dev mailing list