u_cache.c File Reference

Simple cache implementation. More...

Include dependency graph for u_cache.c:

Go to the source code of this file.

Data Structures

struct  util_cache_entry
struct  util_cache

Functions

struct util_cacheutil_cache_create (uint32_t(*hash)(const void *key), int(*compare)(const void *key1, const void *key2), void(*destroy)(void *key, void *value), uint32_t size)
 Create a cache.
static struct util_cache_entryutil_cache_entry_get (struct util_cache *cache, const void *key)
static void util_cache_entry_destroy (struct util_cache *cache, struct util_cache_entry *entry)
void util_cache_set (struct util_cache *cache, void *key, void *value)
void * util_cache_get (struct util_cache *cache, const void *key)
void util_cache_clear (struct util_cache *cache)
void util_cache_destroy (struct util_cache *cache)


Detailed Description

Simple cache implementation.

We simply have fixed size array, and destroy previous values on collision.

Author:
Jose Fonseca <jfonseca@vmware.com>

Definition in file u_cache.c.


Function Documentation

void util_cache_clear ( struct util_cache cache  ) 

Definition at line 174 of file u_cache.c.

References assert, util_cache::entries, util_cache::size, and util_cache_entry_destroy().

00175 {
00176    uint32_t i;
00177 
00178    assert(cache);
00179    
00180    for(i = 0; i < cache->size; ++i)
00181       util_cache_entry_destroy(cache, &cache->entries[i]);
00182 }

struct util_cache* util_cache_create ( uint32_t(*)(const void *key)  hash,
int(*)(const void *key1, const void *key2)  compare,
void(*)(void *key, void *value)  destroy,
uint32_t  size 
) [read]

Create a cache.

Parameters:
hash hash function
compare should return 0 for two equal keys
destroy destruction callback (optional)
size maximum number of entries

Definition at line 79 of file u_cache.c.

References CALLOC, CALLOC_STRUCT, util_cache::compare, util_cache::destroy, util_cache::entries, FREE, util_cache::hash, and util_cache::size.

00083 {
00084    struct util_cache *cache;
00085    
00086    cache = CALLOC_STRUCT(util_cache);
00087    if(!cache)
00088       return NULL;
00089    
00090    cache->hash = hash;
00091    cache->compare = compare;
00092    cache->destroy = destroy;
00093    cache->size = size;
00094    
00095    cache->entries = CALLOC(size, sizeof(struct util_cache_entry));
00096    if(!cache->entries) {
00097       FREE(cache);
00098       return NULL;
00099    }
00100    
00101    return cache;
00102 }

void util_cache_destroy ( struct util_cache cache  ) 

Definition at line 186 of file u_cache.c.

References assert, util_cache::entries, FREE, util_cache::size, and util_cache_clear().

00187 {
00188    assert(cache);
00189 
00190 #ifdef DEBUG
00191    if(cache->count >= 20*cache->size) {
00192       /* Normal approximation of the Poisson distribution */
00193       double mean = (double)cache->count/(double)cache->size;
00194       double stddev = sqrt(mean);
00195       unsigned i;
00196       for(i = 0; i < cache->size; ++i) {
00197          double z = fabs(cache->count - mean)/stddev;
00198          /* This assert should not fail 99.9999998027% of the times, unless 
00199           * the hash function is a poor one */
00200          assert(z <= 6.0);
00201       }
00202    }
00203 #endif
00204       
00205    util_cache_clear(cache);
00206    
00207    FREE(cache->entries);
00208    FREE(cache);
00209 }

static void util_cache_entry_destroy ( struct util_cache cache,
struct util_cache_entry entry 
) [static]

Definition at line 117 of file u_cache.c.

References util_cache::destroy, util_cache_entry::key, and util_cache_entry::value.

00119 {
00120    void *key = entry->key;
00121    void *value = entry->value;
00122    
00123    entry->key = NULL;
00124    entry->value = NULL;
00125    
00126    if(key || value)
00127       if(cache->destroy)
00128          cache->destroy(key, value);
00129 }

static struct util_cache_entry* util_cache_entry_get ( struct util_cache cache,
const void *  key 
) [static, read]

Definition at line 106 of file u_cache.c.

References util_cache::entries, util_cache::hash, and util_cache::size.

00108 {
00109    uint32_t hash;
00110    
00111    hash = cache->hash(key);
00112 
00113    return &cache->entries[hash % cache->size];
00114 }

void* util_cache_get ( struct util_cache cache,
const void *  key 
)

Definition at line 155 of file u_cache.c.

References assert, util_cache::compare, util_cache_entry::key, util_cache_entry_get(), and util_cache_entry::value.

00157 {
00158    struct util_cache_entry *entry;
00159 
00160    assert(cache);
00161 
00162    entry = util_cache_entry_get(cache, key);
00163    if(!entry->key && !entry->value)
00164       return NULL;
00165    
00166    if(cache->compare(key, entry->key) != 0)
00167       return NULL;
00168    
00169    return entry->value;
00170 }

void util_cache_set ( struct util_cache cache,
void *  key,
void *  value 
)

Definition at line 133 of file u_cache.c.

References assert, util_cache_entry::key, util_cache_entry_destroy(), util_cache_entry_get(), and util_cache_entry::value.

00136 {
00137    struct util_cache_entry *entry;
00138 
00139    assert(cache);
00140 
00141    entry = util_cache_entry_get(cache, key);
00142    util_cache_entry_destroy(cache, entry);
00143    
00144 #ifdef DEBUG
00145    ++entry->count;
00146    ++cache->count;
00147 #endif
00148    
00149    entry->key = key;
00150    entry->value = value;
00151 }


Generated on Tue Sep 29 06:25:27 2009 for Gallium3D by  doxygen 1.5.4