implement deleting of driver side cached state in cso's
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_hash.c
index 0338cb3b4748a0883a2664da0e4f7a6b06e8e936..208fc58502b00e6ef73d712997c880c19efb4d4c 100644 (file)
   *   Zack Rusin <zack@tungstengraphics.com>
   */
 
-#include "cso_hash.h"
+#include "pipe/p_debug.h"
+#include "pipe/p_util.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,7 +96,7 @@ 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)
@@ -107,10 +105,10 @@ static void cso_data_free_node(struct cso_node *node)
     * 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(csamp->data);
    */
-   free(node->value);
-   free(node);
+   FREE(node->value);
+   FREE(node);
 }
 
 static struct cso_node *
@@ -134,7 +132,7 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
       hint = countBits(-hint);
       if (hint < MinNumBits)
          hint = MinNumBits;
-      hash->userNumBits = hint;
+      hash->userNumBits = (short)hint;
       while (primeForNumBits(hint) < (hash->size >> 1))
          ++hint;
    } else if (hint < MinNumBits) {
@@ -147,9 +145,9 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
       int oldNumBuckets = hash->numBuckets;
       int  i = 0;
 
-      hash->numBits = 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;
 
@@ -158,11 +156,14 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
          while (firstNode != e) {
             unsigned h = firstNode->key;
             struct cso_node *lastNode = firstNode;
+            struct cso_node *afterLastNode;
+            struct cso_node **beforeFirstNode;
+            
             while (lastNode->next != e && lastNode->next->key == h)
                lastNode = lastNode->next;
 
-            struct cso_node *afterLastNode = lastNode->next;
-            struct cso_node **beforeFirstNode = &hash->buckets[h % hash->numBuckets];
+            afterLastNode = lastNode->next;
+            beforeFirstNode = &hash->buckets[h % hash->numBuckets];
             while (*beforeFirstNode != e)
                beforeFirstNode = &(*beforeFirstNode)->next;
             lastNode->next = *beforeFirstNode;
@@ -170,7 +171,7 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
             firstNode = afterLastNode;
          }
       }
-      free(oldBuckets);
+      FREE(oldBuckets);
    }
 }
 
@@ -222,21 +223,23 @@ struct cso_hash_iter cso_hash_insert(struct cso_hash *hash,
 {
    cso_data_might_grow(hash->data.d);
 
-   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;
+   {
+      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;
+   }
 }
 
 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);
+   hash->data.d = MALLOC_STRUCT(cso_hash_data);
    hash->data.d->fakeNext = 0;
    hash->data.d->buckets = 0;
    hash->data.d->size = 0;
    hash->data.d->nodeSize = sizeof(struct cso_node);
-   hash->data.d->userNumBits = MinNumBits;
+   hash->data.d->userNumBits = (short)MinNumBits;
    hash->data.d->numBits = 0;
    hash->data.d->numBuckets = 0;
 
@@ -256,9 +259,9 @@ void cso_hash_delete(struct cso_hash *hash)
          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,
@@ -290,17 +293,21 @@ static struct cso_node *cso_hash_data_next(struct cso_node *node)
       struct cso_node *e;
       struct cso_hash_data *d;
    } a;
+   int start;
+   struct cso_node **bucket;
+   int n;
+
    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)
       return a.next;
 
-   int start = (node->key % a.d->numBuckets) + 1;
-   struct cso_node **bucket = a.d->buckets + start;
-   int n = a.d->numBuckets - start;
+   start = (node->key % a.d->numBuckets) + 1;
+   bucket = a.d->buckets + start;
+   n = a.d->numBuckets - start;
    while (n--) {
       if (*bucket != a.e)
          return *bucket;
@@ -316,19 +323,21 @@ static struct cso_node *cso_hash_data_prev(struct cso_node *node)
       struct cso_node *e;
       struct cso_hash_data *d;
    } a;
+   int start;
+   struct cso_node *sentinel;
+   struct cso_node **bucket;
 
    a.e = node;
    while (a.e->next)
       a.e = a.e->next;
 
-   int start;
    if (node == a.e)
       start = a.d->numBuckets - 1;
    else
       start = node->key % a.d->numBuckets;
 
-   struct cso_node *sentinel = node;
-   struct cso_node **bucket = a.d->buckets + start;
+   sentinel = node;
+   bucket = a.d->buckets + start;
    while (start >= 0) {
       if (*bucket != sentinel) {
          struct cso_node *prev = *bucket;
@@ -341,7 +350,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;
 }