FictionTools/z88dk/ext/uthash/tests/lru_cache/cache.c
/** Checks if a given key is in the cache

    @param cache
    The cache object

    @param key
    The key to look-up

    @param result
    Where to store the result if key is found.

    A warning: Even though result is just a pointer,
    you have to call this function with a **ptr,
    otherwise this will blow up in your face.

    @return EINVAL if cache is NULL, 0 otherwise
*/
Checks if a given key is in the cache
Checks if a given key is in the cache
int foo_cache_lookup(struct foo_cache *cache, char *key, void *result)
{
	int rv;
	struct foo_cache_entry *tmp = NULL;
	char **dirty_hack = result;
	if (!cache || !key || !result)
		return EINVAL;
	rv = pthread_rwlock_wrlock(&(cache->cache_lock));
	if (rv)
		return rv;
	HASH_FIND_STR(cache->entries, key, tmp);
	if (tmp) {
		size_t key_len = strnlen(tmp->key, KEY_MAX_LENGTH);
		HASH_DELETE(hh, cache->entries, tmp);
		HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp);
		*dirty_hack = tmp->data;
	} else {
		*dirty_hack = result = NULL;
	}
	rv = pthread_rwlock_unlock(&(cache->cache_lock));
	return rv;
}
int foo_cache_lookup ( struct foo_cache * cache , char * key , void * result ) { int rv ; struct foo_cache_entry * tmp = NULL ; char * * dirty_hack = result ; if ( ! cache || ! key || ! result ) return EINVAL ; rv = pthread_rwlock_wrlock ( & ( cache -> cache_lock ) ) ; if ( rv ) return rv ; HASH_FIND_STR ( cache -> entries , key , tmp ) ; if ( tmp ) { size_t key_len = strnlen ( tmp -> key , KEY_MAX_LENGTH ) ; HASH_DELETE ( hh , cache -> entries , tmp ) ; HASH_ADD_KEYPTR ( hh , cache -> entries , tmp -> key , key_len , tmp ) ; * dirty_hack = tmp -> data ; } else { * dirty_hack = result = NULL ; } rv = pthread_rwlock_unlock ( & ( cache -> cache_lock ) ) ; return rv ; }
Checks if a given key is in the cache
Checks if a given key is in the cache