00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #ifndef __CACHE_H__
00010 #define __CACHE_H__
00011 
00012 #include "helper.h"
00013 
00020 typedef long cache_value_t;
00021 
00022 
00026 struct cache_entry_t {
00028     cache_value_t id;
00030     cache_value_t hash_data;
00032     int len;
00033 #if 0
00034 
00035     short accessed;
00036 #endif
00037 
00038     char data[1];
00039 };
00040 
00041 
00042 #define CACHE_DEFAULT (4)
00043 
00052 struct cache_t {
00054     int max;
00056     int used;
00078     int lru;
00079 
00081   struct cache_entry_t *entries[CACHE_DEFAULT+1];
00082 };
00083 
00084 
00088 int cch__add(struct cache_t *cache, 
00089         cache_value_t id, const char *data, int len,
00090         char **copy);
00091 
00093 int cch__find(struct cache_t *cache, cache_value_t id, 
00094         int *index, char **data, int *len);
00095 
00097 inline int cch__entry_set(struct cache_entry_t **cache, 
00098         cache_value_t id, const char *data, int len,
00099         int copy_old_data,
00100         char **copy);
00101     
00104 int cch__set_by_id(struct cache_t *cache, 
00105         cache_value_t id, const char *data, int len,
00106         int copy_old_data,
00107         char **copy);
00108 
00110 void cch__set_active(struct cache_t *cache, int index);
00111 
00112 
00139 __attribute__((gnu_inline, always_inline)) static inline int cch__new_cache(struct cache_t **cache, int max)
00140 {
00141     int status, len;
00142 
00143     status=0;
00144     if (!*cache)
00145     {
00146         len= sizeof(struct cache_entry_t*)*(max-CACHE_DEFAULT)+
00147             sizeof(struct cache_t);
00148 
00149         STOPIF( hlp__alloc( cache, len), NULL);
00150 
00151         memset(*cache, 0, len);
00152         (*cache)->max=max;
00153     }
00154 
00155 ex:
00156     return status;
00157 }
00158 
00159 
00162 int cch__hash_find(struct cache_t *cache, 
00163         const char *key, cache_value_t *data);
00166 int cch__hash_add(struct cache_t *cache, 
00167         const char *key, cache_value_t value);
00168 
00169 #endif
00170