2 * Copyright (C) 2011-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
21 #define _(String) gettext(String)
33 Simple cache implementation but should be enough for storing LP data.
39 void (*fct_cleanup)(void *);
46 struct entry entries[CAPACITY];
49 static struct cache cache;
51 const void *cache_get(const char *key)
55 for (i = 0; i < cache.size; i++)
56 if (!strcmp(cache.entries[i].key, key)) {
57 log_debug(_("cache hit %s"), key);
59 return cache.entries[i].value;
62 log_debug(_("memory cache miss %s"), key);
67 void cache_put(const char *key, const void *value,
68 void (*fct_cleanup)(void *))
70 if (cache.size == CAPACITY) {
71 log_warn(_("exceed cache capacity"));
75 cache.entries[cache.size].key = strdup(key);
76 cache.entries[cache.size].value = value;
77 cache.entries[cache.size].fct_cleanup = fct_cleanup;
86 for (i = 0; i < cache.size; i++) {
87 free((char *)cache.entries[i].key);
88 cache.entries[i].fct_cleanup((void *)cache.entries[i].value);