summaryrefslogtreecommitdiff
path: root/content/cache.c
blob: f9dd4e4f54232f383466368558c7e46684ce528a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * $Id: cache.c,v 1.2 2003/03/04 11:59:35 bursa Exp $
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "netsurf/content/cache.h"
#include "netsurf/utils/utils.h"
#include "netsurf/utils/log.h"

#ifndef TEST
#include "netsurf/desktop/browser.h"
#else
#include <unistd.h>
struct content {
	char *url;
	struct cache_entry *cache;
	unsigned long size;
};
void content_destroy(struct content *c);
#endif


/**
 * internal structures and declarations
 */

struct cache_entry {
	struct content *content;
	unsigned int use_count;
	time_t t;
	struct cache_entry *next, *prev;
};

/* doubly-linked lists using a sentinel */
/* TODO: replace with a structure which can be searched faster */
static struct cache_entry inuse_list_sentinel  = {0, 0, 0, &inuse_list_sentinel,  &inuse_list_sentinel};
static struct cache_entry unused_list_sentinel = {0, 0, 0, &unused_list_sentinel, &unused_list_sentinel};
static struct cache_entry *inuse_list  = &inuse_list_sentinel;
static struct cache_entry *unused_list = &unused_list_sentinel;

static unsigned long max_size = 1024*1024;	/* TODO: make this configurable */
static unsigned long current_size = 0;


/**
 * cache_init -- initialise the cache manager
 */

void cache_init(void)
{
}


/**
 * cache_quit -- terminate the cache manager
 */

void cache_quit(void)
{
}


/**
 * cache_get -- retrieve url from memory cache or disc cache
 */

struct content * cache_get(const char * const url)
{
	struct cache_entry *e;

	/* search inuse_list first */
	for (e = inuse_list->next; e != inuse_list && strcmp(e->content->url, url) != 0; e = e->next)
		;
	if (e != inuse_list) {
		LOG(("'%s' in inuse_list, content %p, use_count %u", url, e->content, e->use_count));
		e->use_count++;
		return e->content;
	}

	/* search unused_list if not found */
	for (e = unused_list->next; e != unused_list && strcmp(e->content->url, url) != 0; e = e->next)
		;
	if (e != unused_list) {
		LOG(("'%s' in unused_list, content %p", url, e->content));
		/* move to inuse_list */
		e->use_count = 1;
		e->prev->next = e->next;
		e->next->prev = e->prev;
		e->prev = inuse_list->prev;
		e->next = inuse_list;
		inuse_list->prev->next = e;
		inuse_list->prev = e;
		return e->content;
	}

	LOG(("'%s' not in cache", url));
	return 0;
}


/**
 * cache_put -- place content in the memory cache
 */

void cache_put(struct content * content)
{
	struct cache_entry * e;
	LOG(("content %p, url '%s'", content, content->url));

	current_size += content->size;
	/* clear old data from the usused_list until the size drops below max_size */
	while (max_size < current_size && unused_list->next != unused_list) {
		e = unused_list->next;
		LOG(("size %lu, removing %p '%s'", current_size, e->content, e->content->url));
		/* TODO: move to disc cache */
		current_size -= e->content->size;
		content_destroy(e->content);
		unused_list->next = e->next;
		e->next->prev = e->prev;
		xfree(e);
	}

	/* add the new content to the inuse_list */
	e = xcalloc(1, sizeof(struct cache_entry));
	e->content = content;
	e->use_count = 1;
	e->prev = inuse_list->prev;
	e->next = inuse_list;
	inuse_list->prev->next = e;
	inuse_list->prev = e;
	content->cache = e;
}


/**
 * cache_free -- free a cache object if it is no longer used
 */

void cache_free(struct content * content)
{
	struct cache_entry * e = content->cache;

	assert(e != 0);
	LOG(("content %p, url '%s', use_count %u", content, content->url, e->use_count));

	assert(e->use_count != 0);
	e->use_count--;
	if (e->use_count == 0) {
		/* move to unused_list or destroy if insufficient space */
		e->use_count = 0;
		e->t = time(0);
		e->prev->next = e->next;
		e->next->prev = e->prev;
		if (max_size < current_size) {
			LOG(("size %lu, removing", current_size));
			/* TODO: move to disc cache */
			current_size -= e->content->size;
			content_destroy(e->content);
			xfree(e);
		} else {
			LOG(("size %lu, moving to unused_list", current_size));
			e->prev = unused_list->prev;
			e->next = unused_list;
			unused_list->prev->next = e;
			unused_list->prev = e;
		}
	}
}


/**
 * cache_dump -- dump contents of cache
 */

void cache_dump(void) {
	struct cache_entry * e;
	LOG(("size %lu", current_size));
	LOG(("inuse_list:"));
	for (e = inuse_list->next; e != inuse_list; e = e->next)
		LOG(("  content %p, url '%s', use_count %u", e->content, e->content->url, e->use_count));
	LOG(("unused_list (time now %lu):", time(0)));
	for (e = unused_list->next; e != unused_list; e = e->next)
		LOG(("  content %p, url '%s', t %lu", e->content, e->content->url, e->t));
	LOG(("end"));
}


/**
 * testing framework
 */

#ifdef TEST
struct content test[] = {
	{"aaa", 0, 200 * 1024},
	{"bbb", 0, 100 * 1024},
	{"ccc", 0, 400 * 1024},
	{"ddd", 0, 600 * 1024},
	{"eee", 0, 300 * 1024},
	{"fff", 0, 500 * 1024},
};

#define TEST_COUNT (sizeof(test) / sizeof(test[0]))

unsigned int test_state[TEST_COUNT];

void content_destroy(struct content *c)
{
}

int main(void)
{
	int i;
	struct content *c;
	for (i = 0; i != TEST_COUNT; i++)
		test_state[i] = 0;

	cache_init();

	for (i = 0; i != 100; i++) {
		int x = rand() % TEST_COUNT;
		switch (rand() % 2) {
			case 0:
				c = cache_get(test[x].url);
				if (c == 0) {
					assert(test_state[x] == 0);
					cache_put(&test[x]);
				} else
					assert(c == &test[x]);
				test_state[x]++;
				break;
			case 1:
				if (test_state[x] != 0) {
					cache_free(&test[x]);
					test_state[x]--;
				}
				break;
		}
	}
	cache_dump();
	return 0;
}
#endif