util: Add remove to util_cache
[mesa.git] / src / gallium / auxiliary / util / u_cache.c
index 0a1a64259f1e74d0c70686f4f50b81f8509ce7c2..e88f36dea75d8b14d5b4427b7798fb1d249a5967 100644 (file)
@@ -36,7 +36,7 @@
 
 
 #include "pipe/p_compiler.h"
-#include "pipe/p_debug.h"
+#include "util/u_debug.h"
 
 #include "util/u_math.h"
 #include "util/u_memory.h"
@@ -137,6 +137,8 @@ util_cache_set(struct util_cache *cache,
    struct util_cache_entry *entry;
 
    assert(cache);
+   if (!cache)
+      return;
 
    entry = util_cache_entry_get(cache, key);
    util_cache_entry_destroy(cache, entry);
@@ -158,6 +160,8 @@ util_cache_get(struct util_cache *cache,
    struct util_cache_entry *entry;
 
    assert(cache);
+   if (!cache)
+      return NULL;
 
    entry = util_cache_entry_get(cache, key);
    if(!entry->key && !entry->value)
@@ -176,7 +180,9 @@ util_cache_clear(struct util_cache *cache)
    uint32_t i;
 
    assert(cache);
-   
+   if (!cache)
+      return;
+
    for(i = 0; i < cache->size; ++i)
       util_cache_entry_destroy(cache, &cache->entries[i]);
 }
@@ -186,6 +192,8 @@ void
 util_cache_destroy(struct util_cache *cache)
 {
    assert(cache);
+   if (!cache)
+      return;
 
 #ifdef DEBUG
    if(cache->count >= 20*cache->size) {
@@ -194,7 +202,7 @@ util_cache_destroy(struct util_cache *cache)
       double stddev = sqrt(mean);
       unsigned i;
       for(i = 0; i < cache->size; ++i) {
-         double z = fabs(cache->count - mean)/stddev;
+         double z = fabs(cache->entries[i].count - mean)/stddev;
          /* This assert should not fail 99.9999998027% of the times, unless 
           * the hash function is a poor one */
          assert(z <= 6.0);
@@ -207,3 +215,25 @@ util_cache_destroy(struct util_cache *cache)
    FREE(cache->entries);
    FREE(cache);
 }
+
+
+void
+util_cache_remove(struct util_cache *cache,
+                  const void *key)
+{
+   struct util_cache_entry *entry;
+   uint32_t hash;
+
+   assert(cache);
+   if (!cache)
+      return;
+
+   hash = cache->hash(key);
+
+   entry = util_cache_entry_get(cache, hash, key);
+   if (!entry)
+      return;
+
+   if (entry->state == FILLED)
+      util_cache_entry_destroy(cache, entry);
+}