Mesa (asm-shader-rework-1): Add destructor for hash_table

Ian Romanick idr at kemper.freedesktop.org
Mon Jul 27 19:23:55 UTC 2009


Module: Mesa
Branch: asm-shader-rework-1
Commit: 0044d3ba94f9041492ea90cf8961fd8b55daefda
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=0044d3ba94f9041492ea90cf8961fd8b55daefda

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Mon Jul 27 12:17:06 2009 -0700

Add destructor for hash_table

---

 src/mesa/shader/hash_table.c |   15 ++++++++++++---
 src/mesa/shader/hash_table.h |    9 +++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/mesa/shader/hash_table.c b/src/mesa/shader/hash_table.c
index 9b8f251..3ff603b 100644
--- a/src/mesa/shader/hash_table.c
+++ b/src/mesa/shader/hash_table.c
@@ -68,7 +68,8 @@ hash_table_ctor(unsigned num_buckets, hash_func_t hash,
         num_buckets = 16;
     }
 
-    ht = malloc(sizeof(*ht) + ((num_buckets - 1) * sizeof(ht->buckets[0])));
+    ht = _mesa_malloc(sizeof(*ht) + ((num_buckets - 1) 
+				     * sizeof(ht->buckets[0])));
     if (ht != NULL) {
         ht->hash = hash;
         ht->compare = compare;
@@ -84,6 +85,14 @@ hash_table_ctor(unsigned num_buckets, hash_func_t hash,
 
 
 void
+hash_table_dtor(struct hash_table *ht)
+{
+   hash_table_clear(ht);
+   _mesa_free(ht);
+}
+
+
+void
 hash_table_clear(struct hash_table *ht)
 {
    struct node *node;
@@ -94,7 +103,7 @@ hash_table_clear(struct hash_table *ht)
    for (i = 0; i < ht->num_buckets; i++) {
       foreach_s(node, temp, & ht->buckets[i]) {
 	 remove_from_list(node);
-	 free(node);
+	 _mesa_free(node);
       }
 
       assert(is_empty_list(& ht->buckets[i]));
@@ -128,7 +137,7 @@ hash_table_insert(struct hash_table *ht, void *data, const void *key)
     const unsigned bucket = hash_value % ht->num_buckets;
     struct hash_node *node;
 
-    node = calloc(1, sizeof(*node));
+    node = _mesa_calloc(1, sizeof(*node));
 
     node->data = data;
     node->key = key;
diff --git a/src/mesa/shader/hash_table.h b/src/mesa/shader/hash_table.h
index 83ae7f0..7b302f5 100644
--- a/src/mesa/shader/hash_table.h
+++ b/src/mesa/shader/hash_table.h
@@ -54,6 +54,15 @@ extern struct hash_table *hash_table_ctor(unsigned num_buckets,
 
 
 /**
+ * Release all memory associated with a hash table
+ *
+ * \warning
+ * This function cannot release memory occupied either by keys or data.
+ */
+extern void hash_table_dtor(struct hash_table *ht);
+
+
+/**
  * Flush all entries from a hash table
  *
  * \param ht  Table to be cleared of its entries.




More information about the mesa-commit mailing list