summaryrefslogtreecommitdiff
path: root/util/cache.c
blob: a80ddac9f06271d5b13e2a7a65fe4eb5ad78075d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*  GMime
 *  Copyright (C) 2000-2014 Jeffrey Stedfast
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "cache.h"


static void
cache_node_free (gpointer node_data)
{
	CacheNode *node = (CacheNode *) node_data;
	Cache *cache = node->cache;
	
	cache->free_node (node);
	g_free (node->key);
	
	g_slice_free1 (cache->node_size, node);
}


Cache *
cache_new (CacheNodeExpireFunc expire, CacheNodeFreeFunc free_node, size_t node_size, size_t max_size)
{
	Cache *cache;
	
	cache = g_new (Cache, 1);
	list_init (&cache->list);
	cache->expire = expire;
	cache->free_node = free_node;
	cache->node_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, cache_node_free);
	cache->node_size = node_size;
	cache->max_size = max_size;
	cache->size = 0;
	
	return cache;
}


void
cache_free (Cache *cache)
{
	g_hash_table_destroy (cache->node_hash);
	g_free (cache);
}


void
cache_expire_unused (Cache *cache)
{
	ListNode *node, *prev;
	
	node = cache->list.tailpred;
	while (node->prev && cache->size > cache->max_size) {
		prev = node->prev;
		if (cache->expire (cache, (CacheNode *) node)) {
			list_unlink (node);
			g_hash_table_remove (cache->node_hash, ((CacheNode *) node)->key);
			cache->size--;
		}
		node = prev;
	}
}

CacheNode *
cache_node_insert (Cache *cache, const char *key)
{
	CacheNode *node;
	
	cache->size++;
	
	if (cache->size > cache->max_size)
		cache_expire_unused (cache);
	
	node = g_slice_alloc (cache->node_size);
	node->key = g_strdup (key);
	node->cache = cache;
	
	g_hash_table_insert (cache->node_hash, node->key, node);
	list_prepend (&cache->list, (ListNode *) node);
	
	return node;
}

CacheNode *
cache_node_lookup (Cache *cache, const char *key, gboolean use)
{
	CacheNode *node;
	
	node = g_hash_table_lookup (cache->node_hash, key);
	if (node && use) {
		list_unlink ((ListNode *) node);
		list_prepend (&cache->list, (ListNode *) node);
	}
	
	return node;
}

void
cache_node_expire (CacheNode *node)
{
	Cache *cache;
	
	cache = node->cache;
	list_unlink ((ListNode *) node);
	g_hash_table_remove (cache->node_hash, node->key);
	cache->size--;
}