/
cache.c
217 lines (179 loc) · 5.32 KB
/
cache.c
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
/*
* \brief MTK cache handling module
*
* This module provides the needed functionality
* to handle caches. Although the caches can be
* used for every kind of data the primary use
* of this module is the management of image and
* font caches.
*/
/*
* Copyright (C) 2002-2008 Norman Feske <norman.feske@genode-labs.com>
* Genode Labs, Feske & Helmuth Systementwicklung GbR
*
* This file is part of the MTK package, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#include <stdlib.h>
#include "mtkstd.h"
#include "cache.h"
struct cache_elem {
void *data; /* cached data block */
s32 size; /* size of data block */
s32 ident; /* data block identifier */
void (*destroy)(void *); /* data block destroy function */
};
struct cache {
s32 max_entries; /* size of ring buffer */
s32 max_size; /* max amount of cached data */
s32 curr_size; /* current amount of cached data */
s32 idxtokill; /* 'oldest' ring buffer index */
s32 idxtoadd; /* ring buffer index for new element */
struct cache_elem *elem; /* pointer to ring buffer */
};
int init_cache(struct mtk_services *d);
/***********************
** Service functions **
***********************/
/**
* Create new cache
*/
static CACHE *create(s32 max_entries,s32 max_size)
{
struct cache *c;
/* get memory for cache struct and the ring buffer */
c = (struct cache *)zalloc(sizeof(struct cache)
+ sizeof(struct cache_elem)*max_entries);
if (!c) {
INFO(printf("Cache(create): out of memory\n");)
return NULL;
}
/* set values in cache struct */
c->max_entries = max_entries;
c->max_size = max_size;
c->elem = (struct cache_elem *)((int)c + sizeof(struct cache));
return c;
}
/**
* Remove element from cache
*/
static void remove_elem(CACHE *cache,s32 index)
{
struct cache_elem *e;
if (!cache) {
INFO(printf("Cache(remove_elem): cache == NULL\n");)
return;
}
if (index >= cache->max_entries) {
INFO(printf("Cache(remove_elem): index out of range\n");)
return;
}
e=cache->elem + index;
INFO(printf("Cache(remove_element): removing element %u\n",index);)
if (e->data) {
if (e->destroy) e->destroy(e->data);
else free(e->data);
cache->curr_size -= e->size;
e->data=NULL;
}
}
/**
* Destroy cache
*/
static void destroy(CACHE *cache)
{
s32 i;
if (!cache) return;
/* free cache entries */
for (i=0;i<cache->max_entries;i++) remove_elem(cache,i);
/* free cache struct itself */
free(cache);
}
static void reduce_cachesize(CACHE *cache,int needed_size)
{
if (!cache) return;
INFO(printf("Cache(reduce_cachesize): old size is %u\n",cache->curr_size);)
while (cache->curr_size > needed_size) {
remove_elem(cache,cache->idxtokill);
cache->idxtokill = (cache->idxtokill + 1)%(cache->max_entries);
}
INFO(printf("Cache(reduce_cachesize): new size is %u\n",cache->curr_size);)
}
/**
* Insert element into cache
*
* \param elem data block to add to cache
* \param elemsize size of data block (needed to determine when element must
* be removed)
* \param ident identifier for this element (needed to determine if the
* element did not got overwritten by another
* \param destroy function that is called when an element needs to be
* destroyed
* \return index to cache element. The cached element can be accessed
* later using the function get_element with this index and
* the identifier as args.
*/
static s32 add_elem(CACHE *cache,void *elem,s32 elemsize,s32 ident,void (*destroy)(void *))
{
struct cache_elem *e;
s32 new_idx;
if (!cache) return -1;
/* check if cache grows bigger that its maximum size */
if (cache->curr_size + elemsize > cache->max_size) {
reduce_cachesize(cache,cache->max_size - elemsize);
}
new_idx = cache->idxtoadd;
e=cache->elem + new_idx;
INFO(printf("Cache(add_elem): add element at index %u\n",new_idx);)
/* gets another cache element overwritten? */
if (e->data) {
INFO(printf("Cache(add_elem): old element gets overwritten\n");)
/* remove old element */
remove_elem(cache,new_idx);
cache->idxtokill = (cache->idxtokill + 1)%(cache->max_entries);
}
e->data=elem;
e->size=elemsize;
e->ident=ident;
e->destroy=destroy;
cache->curr_size += elemsize;
cache->idxtoadd = (cache->idxtoadd + 1)%(cache->max_entries);
return new_idx;
}
/**
* Get cached data
*/
static void *get_elem(struct cache *cache,s32 index,s32 ident)
{
if (!cache) {
INFO(printf("Cache(get_elem): cache == NULL\n");)
return NULL;
}
if (index >= cache->max_entries) {
INFO(printf("Cache(get_elem): index %u out of range\n",index);)
return NULL;
}
if ((cache->elem + index)->ident == ident) {
return (cache->elem + index)->data;
}
INFO(printf("Cache(get_elem): element %u is not cached anymore\n",index);)
return NULL;
}
/**************************************
** Service structure of this module **
**************************************/
static struct cache_services services = {
create,
destroy,
add_elem,
get_elem,
remove_elem
};
/************************
** Module entry point **
************************/
int init_cache(struct mtk_services *d)
{
d->register_module("Cache 1.0",&services);
return 1;
}