Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_hash.c
index e65d331a0b2fb3b4452670b4aba4d2967e5f6e64..288cef7b6faea8472a5687dfe36d564cb8c28556 100644 (file)
   *   Zack Rusin <zack@tungstengraphics.com>
   */
 
-#include "cso_hash.h"
+#include "util/u_debug.h"
+#include "util/u_memory.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
+#include "cso_hash.h"
 
 #define MAX(a, b) ((a > b) ? (a) : (b))
 
@@ -98,19 +96,12 @@ struct cso_hash {
 
 static void *cso_data_allocate_node(struct cso_hash_data *hash)
 {
-   return malloc(hash->nodeSize);
+   return MALLOC(hash->nodeSize);
 }
 
-static void cso_data_free_node(struct cso_node *node)
+static void cso_free_node(struct cso_node *node)
 {
-   /* XXX still a leak here.
-    * Need to cast value ptr to original cso type, then free the
-    * driver-specific data hanging off of it.  For example:
-   struct cso_sampler *csamp = (struct cso_sampler *) node->value;
-   free(csamp->data);
-   */
-   free(node->value);
-   free(node);
+   FREE(node);
 }
 
 static struct cso_node *
@@ -119,6 +110,10 @@ cso_hash_create_node(struct cso_hash *hash,
                       struct cso_node **anextNode)
 {
    struct cso_node *node = cso_data_allocate_node(hash->data.d);
+
+   if (!node)
+      return NULL;
+
    node->key = akey;
    node->value = avalue;
 
@@ -149,7 +144,7 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
 
       hash->numBits = (short)hint;
       hash->numBuckets = primeForNumBits(hint);
-      hash->buckets = malloc(sizeof(struct cso_node*) * hash->numBuckets);
+      hash->buckets = MALLOC(sizeof(struct cso_node*) * hash->numBuckets);
       for (i = 0; i < hash->numBuckets; ++i)
          hash->buckets[i] = e;
 
@@ -173,7 +168,7 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
             firstNode = afterLastNode;
          }
       }
-      free(oldBuckets);
+      FREE(oldBuckets);
    }
 }
 
@@ -228,15 +223,30 @@ struct cso_hash_iter cso_hash_insert(struct cso_hash *hash,
    {
       struct cso_node **nextNode = cso_hash_find_node(hash, key);
       struct cso_node *node = cso_hash_create_node(hash, key, data, nextNode);
-      struct cso_hash_iter iter = {hash, node};
-      return iter;
+      if (!node) {
+         struct cso_hash_iter null_iter = {hash, 0};
+         return null_iter;
+      }
+
+      {
+         struct cso_hash_iter iter = {hash, node};
+         return iter;
+      }
    }
 }
 
 struct cso_hash * cso_hash_create(void)
 {
-   struct cso_hash *hash = malloc(sizeof(struct cso_hash));
-   hash->data.d = malloc(sizeof(struct cso_hash_data));
+   struct cso_hash *hash = MALLOC_STRUCT(cso_hash);
+   if (!hash)
+      return NULL;
+
+   hash->data.d = MALLOC_STRUCT(cso_hash_data);
+   if (!hash->data.d) {
+      FREE(hash);
+      return NULL;
+   }
+
    hash->data.d->fakeNext = 0;
    hash->data.d->buckets = 0;
    hash->data.d->size = 0;
@@ -257,13 +267,13 @@ void cso_hash_delete(struct cso_hash *hash)
       struct cso_node *cur = *bucket++;
       while (cur != e_for_x) {
          struct cso_node *next = cur->next;
-         cso_data_free_node(cur);
+         cso_free_node(cur);
          cur = next;
       }
    }
-   free(hash->data.d->buckets);
-   free(hash->data.d);
-   free(hash);
+   FREE(hash->data.d->buckets);
+   FREE(hash->data.d);
+   FREE(hash);
 }
 
 struct cso_hash_iter cso_hash_find(struct cso_hash *hash,
@@ -301,7 +311,7 @@ static struct cso_node *cso_hash_data_next(struct cso_node *node)
 
    a.next = node->next;
    if (!a.next) {
-      fprintf(stderr, "iterating beyond the last element\n");
+      debug_printf("iterating beyond the last element\n");
       return 0;
    }
    if (a.next->next)
@@ -352,7 +362,7 @@ static struct cso_node *cso_hash_data_prev(struct cso_node *node)
       --bucket;
       --start;
    }
-   fprintf(stderr, "iterating backward beyond first element\n");
+   debug_printf("iterating backward beyond first element\n");
    return a.e;
 }
 
@@ -376,7 +386,7 @@ void * cso_hash_take(struct cso_hash *hash,
    if (*node != hash->data.e) {
       void *t = (*node)->value;
       struct cso_node *next = (*node)->next;
-      cso_data_free_node(*node);
+      cso_free_node(*node);
       *node = next;
       --hash->data.d->size;
       cso_data_has_shrunk(hash->data.d);
@@ -397,3 +407,33 @@ struct cso_hash_iter cso_hash_first_node(struct cso_hash *hash)
    struct cso_hash_iter iter = {hash, cso_data_first_node(hash->data.d)};
    return iter;
 }
+
+int cso_hash_size(struct cso_hash *hash)
+{
+   return hash->data.d->size;
+}
+
+struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter iter)
+{
+   struct cso_hash_iter ret = iter;
+   struct cso_node *node = iter.node;
+   struct cso_node **node_ptr;
+
+   if (node == hash->data.e)
+      return iter;
+
+   ret = cso_hash_iter_next(ret);
+   node_ptr = (struct cso_node**)(&hash->data.d->buckets[node->key % hash->data.d->numBuckets]);
+   while (*node_ptr != node)
+      node_ptr = &(*node_ptr)->next;
+   *node_ptr = node->next;
+   cso_free_node(node);
+   --hash->data.d->size;
+   return ret;
+}
+
+boolean cso_hash_contains(struct cso_hash *hash, unsigned key)
+{
+   struct cso_node **node = cso_hash_find_node(hash, key);
+   return (*node != hash->data.e);
+}