2 Copyright (C) 2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 Simple cache implementation but should be enough for storing LP data.
35 void (*fct_cleanup)(void *);
42 struct entry entries[CAPACITY];
45 static struct cache cache;
47 const void *cache_get(const char *key)
51 for (i = 0; i < cache.size; i++)
52 if (!strcmp(cache.entries[i].key, key)) {
54 printf("DEBUG: cache hit %s\n", key);
56 return cache.entries[i].value;
60 printf("DEBUG: cache miss %s\n", key);
65 void cache_put(const char *key, const void *value,
66 void (*fct_cleanup)(void *))
68 if (cache.size == CAPACITY) {
69 fprintf(stderr, "WARNING: exceed cache capacity\n");
73 cache.entries[cache.size].key = strdup(key);
74 cache.entries[cache.size].value = value;
75 cache.entries[cache.size].fct_cleanup = fct_cleanup;
84 for (i = 0; i < cache.size; i++) {
85 free((char *)cache.entries[i].key);
86 cache.entries[i].fct_cleanup((void *)cache.entries[i].value);