93 #define CTABLE_ENTRY struct ctable_entry
101 #define RING_TO_CTABLE_ENTRY(ring_ptr) \
102 RING_TO_APPL(ring_ptr, CTABLE_ENTRY, ring)
103 #define RING_PTR_OF(x) (&((x)->ring))
115 #define CTABLE_MIN_SIZE 5
123 const char *myname =
"ctable_create";
126 msg_panic(
"%s: bad cache limit: %ld", myname, (
long) limit);
131 cache->create = create;
132 cache->delete =
delete;
134 cache->context = context;
142 const char *myname =
"ctable_locate";
152 if (cache->used >= cache->limit) {
155 msg_info(
"%s: purge entry key %s", myname, entry->key);
157 cache->delete(entry->value, cache->context);
158 htable_delete(cache->table, entry->key, (void (*) (
void *)) 0);
163 entry->value = cache->create(key, cache->context);
167 msg_info(
"%s: install entry key %s", myname, entry->key);
170 msg_info(
"%s: leave existing entry key %s", myname, entry->key);
175 msg_info(
"%s: move existing entry key %s", myname, entry->key);
177 return (entry->value);
184 const char *myname =
"ctable_refresh";
192 cache->delete(entry->value, cache->context);
193 entry->value = cache->create(key, cache->context);
201 msg_info(
"%s: refresh entry key %s", myname, entry->key);
202 return (entry->value);
209 cache->context = context;
212 static CTABLE *ctable_free_cache;
216 static void ctable_free_callback(
void *ptr)
220 ctable_free_cache->delete(entry->value, ctable_free_cache->context);
228 CTABLE *saved_cache = ctable_free_cache;
234 ctable_free_cache = cache;
237 ctable_free_cache = saved_cache;
265 #define STR(x) vstring_str(x)
267 static void *ask(
const char *key,
void *context)
282 static void drop(
void *data,
void *unused_context)
287 int main(
int unused_argc,
char **argv)
void htable_free(HTABLE *table, void(*free_fn)(void *))
void(* CTABLE_DELETE_FN)(void *, void *)
void ring_detach(RING *entry)
char * mystrdup(const char *str)
int vstring_get_nonl(VSTRING *vp, VSTREAM *fp)
NORETURN msg_panic(const char *fmt,...)
void ring_init(RING *ring)
int main(int argc, char **argv)
const void * ctable_locate(CTABLE *cache, const char *key)
#define vstream_longjmp(stream, val)
#define vstream_setjmp(stream)
void ring_append(RING *ring, RING *entry)
HTABLE * htable_create(ssize_t size)
void ctable_newcontext(CTABLE *cache, void *context)
const void * ctable_refresh(CTABLE *cache, const char *key)
VSTREAM * vstream_printf(const char *fmt,...)
void *(* CTABLE_CREATE_FN)(const char *, void *)
CTABLE * ctable_create(ssize_t limit, CTABLE_CREATE_FN create, CTABLE_DELETE_FN delete, void *context)
VSTRING * vstring_alloc(ssize_t len)
void * htable_find(HTABLE *table, const char *key)
int vstream_fflush(VSTREAM *stream)
void ctable_walk(CTABLE *cache, void(*action)(const char *, const void *))
#define CA_VSTREAM_CTL_EXCEPT
VSTRING * vstring_free(VSTRING *vp)
void msg_vstream_init(const char *name, VSTREAM *vp)
#define CA_VSTREAM_CTL_END
void vstream_control(VSTREAM *stream, int name,...)
void htable_delete(HTABLE *table, const char *key, void(*free_fn)(void *))
void ctable_free(CTABLE *cache)
#define RING_TO_CTABLE_ENTRY(ring_ptr)
void * mymalloc(ssize_t len)
HTABLE_INFO * htable_enter(HTABLE *table, const char *key, void *value)
void msg_info(const char *fmt,...)