1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | In-memory cache
|
---|
4 | Copyright (C) Volker Lendecke 2007
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "memcache.h"
|
---|
21 | #include "rbtree.h"
|
---|
22 |
|
---|
23 | static struct memcache *global_cache;
|
---|
24 |
|
---|
25 | struct memcache_element {
|
---|
26 | struct rb_node rb_node;
|
---|
27 | struct memcache_element *prev, *next;
|
---|
28 | size_t keylength, valuelength;
|
---|
29 | uint8 n; /* This is really an enum, but save memory */
|
---|
30 | char data[1]; /* placeholder for offsetof */
|
---|
31 | };
|
---|
32 |
|
---|
33 | struct memcache {
|
---|
34 | struct memcache_element *mru, *lru;
|
---|
35 | struct rb_root tree;
|
---|
36 | size_t size;
|
---|
37 | size_t max_size;
|
---|
38 | };
|
---|
39 |
|
---|
40 | static void memcache_element_parse(struct memcache_element *e,
|
---|
41 | DATA_BLOB *key, DATA_BLOB *value);
|
---|
42 |
|
---|
43 | static bool memcache_is_talloc(enum memcache_number n)
|
---|
44 | {
|
---|
45 | bool result;
|
---|
46 |
|
---|
47 | switch (n) {
|
---|
48 | case GETPWNAM_CACHE:
|
---|
49 | case PDB_GETPWSID_CACHE:
|
---|
50 | case SINGLETON_CACHE_TALLOC:
|
---|
51 | result = true;
|
---|
52 | break;
|
---|
53 | default:
|
---|
54 | result = false;
|
---|
55 | break;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static int memcache_destructor(struct memcache *cache) {
|
---|
62 | struct memcache_element *e, *next;
|
---|
63 |
|
---|
64 | for (e = cache->mru; e != NULL; e = next) {
|
---|
65 | next = e->next;
|
---|
66 | if (memcache_is_talloc((enum memcache_number)e->n)
|
---|
67 | && (e->valuelength == sizeof(void *))) {
|
---|
68 | DATA_BLOB key, value;
|
---|
69 | void *ptr;
|
---|
70 | memcache_element_parse(e, &key, &value);
|
---|
71 | memcpy(&ptr, value.data, sizeof(ptr));
|
---|
72 | TALLOC_FREE(ptr);
|
---|
73 | }
|
---|
74 | SAFE_FREE(e);
|
---|
75 | }
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
|
---|
80 | {
|
---|
81 | struct memcache *result;
|
---|
82 |
|
---|
83 | result = TALLOC_ZERO_P(mem_ctx, struct memcache);
|
---|
84 | if (result == NULL) {
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | result->max_size = max_size;
|
---|
88 | talloc_set_destructor(result, memcache_destructor);
|
---|
89 | return result;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void memcache_set_global(struct memcache *cache)
|
---|
93 | {
|
---|
94 | TALLOC_FREE(global_cache);
|
---|
95 | global_cache = cache;
|
---|
96 | }
|
---|
97 |
|
---|
98 | static struct memcache_element *memcache_node2elem(struct rb_node *node)
|
---|
99 | {
|
---|
100 | return (struct memcache_element *)
|
---|
101 | ((char *)node - offsetof(struct memcache_element, rb_node));
|
---|
102 | }
|
---|
103 |
|
---|
104 | static void memcache_element_parse(struct memcache_element *e,
|
---|
105 | DATA_BLOB *key, DATA_BLOB *value)
|
---|
106 | {
|
---|
107 | key->data = ((uint8 *)e) + offsetof(struct memcache_element, data);
|
---|
108 | key->length = e->keylength;
|
---|
109 | value->data = key->data + e->keylength;
|
---|
110 | value->length = e->valuelength;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static size_t memcache_element_size(size_t key_length, size_t value_length)
|
---|
114 | {
|
---|
115 | return sizeof(struct memcache_element) - 1 + key_length + value_length;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static int memcache_compare(struct memcache_element *e, enum memcache_number n,
|
---|
119 | DATA_BLOB key)
|
---|
120 | {
|
---|
121 | DATA_BLOB this_key, this_value;
|
---|
122 |
|
---|
123 | if ((int)e->n < (int)n) return 1;
|
---|
124 | if ((int)e->n > (int)n) return -1;
|
---|
125 |
|
---|
126 | if (e->keylength < key.length) return 1;
|
---|
127 | if (e->keylength > key.length) return -1;
|
---|
128 |
|
---|
129 | memcache_element_parse(e, &this_key, &this_value);
|
---|
130 | return memcmp(this_key.data, key.data, key.length);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static struct memcache_element *memcache_find(
|
---|
134 | struct memcache *cache, enum memcache_number n, DATA_BLOB key)
|
---|
135 | {
|
---|
136 | struct rb_node *node;
|
---|
137 |
|
---|
138 | node = cache->tree.rb_node;
|
---|
139 |
|
---|
140 | while (node != NULL) {
|
---|
141 | struct memcache_element *elem = memcache_node2elem(node);
|
---|
142 | int cmp;
|
---|
143 |
|
---|
144 | cmp = memcache_compare(elem, n, key);
|
---|
145 | if (cmp == 0) {
|
---|
146 | return elem;
|
---|
147 | }
|
---|
148 | node = (cmp < 0) ? node->rb_left : node->rb_right;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | bool memcache_lookup(struct memcache *cache, enum memcache_number n,
|
---|
155 | DATA_BLOB key, DATA_BLOB *value)
|
---|
156 | {
|
---|
157 | struct memcache_element *e;
|
---|
158 |
|
---|
159 | if (cache == NULL) {
|
---|
160 | cache = global_cache;
|
---|
161 | }
|
---|
162 | if (cache == NULL) {
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | e = memcache_find(cache, n, key);
|
---|
167 | if (e == NULL) {
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (cache->size != 0) {
|
---|
172 | /*
|
---|
173 | * Do LRU promotion only when we will ever shrink
|
---|
174 | */
|
---|
175 | if (e == cache->lru) {
|
---|
176 | cache->lru = e->prev;
|
---|
177 | }
|
---|
178 | DLIST_PROMOTE(cache->mru, e);
|
---|
179 | if (cache->mru == NULL) {
|
---|
180 | cache->mru = e;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | memcache_element_parse(e, &key, value);
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
|
---|
189 | DATA_BLOB key)
|
---|
190 | {
|
---|
191 | DATA_BLOB value;
|
---|
192 | void *result;
|
---|
193 |
|
---|
194 | if (!memcache_lookup(cache, n, key, &value)) {
|
---|
195 | return NULL;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (value.length != sizeof(result)) {
|
---|
199 | return NULL;
|
---|
200 | }
|
---|
201 |
|
---|
202 | memcpy(&result, value.data, sizeof(result));
|
---|
203 |
|
---|
204 | return result;
|
---|
205 | }
|
---|
206 |
|
---|
207 | static void memcache_delete_element(struct memcache *cache,
|
---|
208 | struct memcache_element *e)
|
---|
209 | {
|
---|
210 | rb_erase(&e->rb_node, &cache->tree);
|
---|
211 |
|
---|
212 | if (e == cache->lru) {
|
---|
213 | cache->lru = e->prev;
|
---|
214 | }
|
---|
215 | DLIST_REMOVE(cache->mru, e);
|
---|
216 |
|
---|
217 | if (memcache_is_talloc(e->n)) {
|
---|
218 | DATA_BLOB cache_key, cache_value;
|
---|
219 | void *ptr;
|
---|
220 |
|
---|
221 | memcache_element_parse(e, &cache_key, &cache_value);
|
---|
222 | SMB_ASSERT(cache_value.length == sizeof(ptr));
|
---|
223 | memcpy(&ptr, cache_value.data, sizeof(ptr));
|
---|
224 | TALLOC_FREE(ptr);
|
---|
225 | }
|
---|
226 |
|
---|
227 | cache->size -= memcache_element_size(e->keylength, e->valuelength);
|
---|
228 |
|
---|
229 | SAFE_FREE(e);
|
---|
230 | }
|
---|
231 |
|
---|
232 | static void memcache_trim(struct memcache *cache)
|
---|
233 | {
|
---|
234 | if (cache->max_size == 0) {
|
---|
235 | return;
|
---|
236 | }
|
---|
237 |
|
---|
238 | while ((cache->size > cache->max_size) && (cache->lru != NULL)) {
|
---|
239 | memcache_delete_element(cache, cache->lru);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void memcache_delete(struct memcache *cache, enum memcache_number n,
|
---|
244 | DATA_BLOB key)
|
---|
245 | {
|
---|
246 | struct memcache_element *e;
|
---|
247 |
|
---|
248 | if (cache == NULL) {
|
---|
249 | cache = global_cache;
|
---|
250 | }
|
---|
251 | if (cache == NULL) {
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | e = memcache_find(cache, n, key);
|
---|
256 | if (e == NULL) {
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | memcache_delete_element(cache, e);
|
---|
261 | }
|
---|
262 |
|
---|
263 | void memcache_add(struct memcache *cache, enum memcache_number n,
|
---|
264 | DATA_BLOB key, DATA_BLOB value)
|
---|
265 | {
|
---|
266 | struct memcache_element *e;
|
---|
267 | struct rb_node **p;
|
---|
268 | struct rb_node *parent;
|
---|
269 | DATA_BLOB cache_key, cache_value;
|
---|
270 | size_t element_size;
|
---|
271 |
|
---|
272 | if (cache == NULL) {
|
---|
273 | cache = global_cache;
|
---|
274 | }
|
---|
275 | if (cache == NULL) {
|
---|
276 | return;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (key.length == 0) {
|
---|
280 | return;
|
---|
281 | }
|
---|
282 |
|
---|
283 | e = memcache_find(cache, n, key);
|
---|
284 |
|
---|
285 | if (e != NULL) {
|
---|
286 | memcache_element_parse(e, &cache_key, &cache_value);
|
---|
287 |
|
---|
288 | if (value.length <= cache_value.length) {
|
---|
289 | if (memcache_is_talloc(e->n)) {
|
---|
290 | void *ptr;
|
---|
291 | SMB_ASSERT(cache_value.length == sizeof(ptr));
|
---|
292 | memcpy(&ptr, cache_value.data, sizeof(ptr));
|
---|
293 | TALLOC_FREE(ptr);
|
---|
294 | }
|
---|
295 | /*
|
---|
296 | * We can reuse the existing record
|
---|
297 | */
|
---|
298 | memcpy(cache_value.data, value.data, value.length);
|
---|
299 | e->valuelength = value.length;
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | memcache_delete_element(cache, e);
|
---|
304 | }
|
---|
305 |
|
---|
306 | element_size = memcache_element_size(key.length, value.length);
|
---|
307 |
|
---|
308 |
|
---|
309 | e = (struct memcache_element *)SMB_MALLOC(element_size);
|
---|
310 |
|
---|
311 | if (e == NULL) {
|
---|
312 | DEBUG(0, ("malloc failed\n"));
|
---|
313 | return;
|
---|
314 | }
|
---|
315 |
|
---|
316 | e->n = n;
|
---|
317 | e->keylength = key.length;
|
---|
318 | e->valuelength = value.length;
|
---|
319 |
|
---|
320 | memcache_element_parse(e, &cache_key, &cache_value);
|
---|
321 | memcpy(cache_key.data, key.data, key.length);
|
---|
322 | memcpy(cache_value.data, value.data, value.length);
|
---|
323 |
|
---|
324 | parent = NULL;
|
---|
325 | p = &cache->tree.rb_node;
|
---|
326 |
|
---|
327 | while (*p) {
|
---|
328 | struct memcache_element *elem = memcache_node2elem(*p);
|
---|
329 | int cmp;
|
---|
330 |
|
---|
331 | parent = (*p);
|
---|
332 |
|
---|
333 | cmp = memcache_compare(elem, n, key);
|
---|
334 |
|
---|
335 | p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
|
---|
336 | }
|
---|
337 |
|
---|
338 | rb_link_node(&e->rb_node, parent, p);
|
---|
339 | rb_insert_color(&e->rb_node, &cache->tree);
|
---|
340 |
|
---|
341 | DLIST_ADD(cache->mru, e);
|
---|
342 | if (cache->lru == NULL) {
|
---|
343 | cache->lru = e;
|
---|
344 | }
|
---|
345 |
|
---|
346 | cache->size += element_size;
|
---|
347 | memcache_trim(cache);
|
---|
348 | }
|
---|
349 |
|
---|
350 | void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
|
---|
351 | DATA_BLOB key, void *pptr)
|
---|
352 | {
|
---|
353 | void **ptr = (void **)pptr;
|
---|
354 | void *p;
|
---|
355 |
|
---|
356 | if (cache == NULL) {
|
---|
357 | cache = global_cache;
|
---|
358 | }
|
---|
359 | if (cache == NULL) {
|
---|
360 | return;
|
---|
361 | }
|
---|
362 |
|
---|
363 | p = talloc_move(cache, ptr);
|
---|
364 | memcache_add(cache, n, key, data_blob_const(&p, sizeof(p)));
|
---|
365 | }
|
---|
366 |
|
---|
367 | void memcache_flush(struct memcache *cache, enum memcache_number n)
|
---|
368 | {
|
---|
369 | struct rb_node *node;
|
---|
370 |
|
---|
371 | if (cache == NULL) {
|
---|
372 | cache = global_cache;
|
---|
373 | }
|
---|
374 | if (cache == NULL) {
|
---|
375 | return;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * Find the smallest element of number n
|
---|
380 | */
|
---|
381 |
|
---|
382 | node = cache->tree.rb_node;
|
---|
383 | if (node == NULL) {
|
---|
384 | return;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * First, find *any* element of number n
|
---|
389 | */
|
---|
390 |
|
---|
391 | while (true) {
|
---|
392 | struct memcache_element *elem = memcache_node2elem(node);
|
---|
393 | struct rb_node *next;
|
---|
394 |
|
---|
395 | if ((int)elem->n == (int)n) {
|
---|
396 | break;
|
---|
397 | }
|
---|
398 |
|
---|
399 | if ((int)elem->n < (int)n) {
|
---|
400 | next = node->rb_right;
|
---|
401 | }
|
---|
402 | else {
|
---|
403 | next = node->rb_left;
|
---|
404 | }
|
---|
405 | if (next == NULL) {
|
---|
406 | break;
|
---|
407 | }
|
---|
408 | node = next;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (node == NULL) {
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * Then, find the leftmost element with number n
|
---|
417 | */
|
---|
418 |
|
---|
419 | while (true) {
|
---|
420 | struct rb_node *prev = rb_prev(node);
|
---|
421 | struct memcache_element *elem;
|
---|
422 |
|
---|
423 | if (prev == NULL) {
|
---|
424 | break;
|
---|
425 | }
|
---|
426 | elem = memcache_node2elem(prev);
|
---|
427 | if ((int)elem->n != (int)n) {
|
---|
428 | break;
|
---|
429 | }
|
---|
430 | node = prev;
|
---|
431 | }
|
---|
432 |
|
---|
433 | while (node != NULL) {
|
---|
434 | struct memcache_element *e = memcache_node2elem(node);
|
---|
435 | struct rb_node *next = rb_next(node);
|
---|
436 |
|
---|
437 | if (e->n != n) {
|
---|
438 | break;
|
---|
439 | }
|
---|
440 |
|
---|
441 | memcache_delete_element(cache, e);
|
---|
442 | node = next;
|
---|
443 | }
|
---|
444 | }
|
---|