librpc/ndr: add helper functions to setup and free compression states.
[Samba.git] / lib / util / memcache.c
blobacd663cd4e7d99baedf41a21243560f790e5a349
1 /*
2 Unix SMB/CIFS implementation.
3 In-memory cache
4 Copyright (C) Volker Lendecke 2007
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.
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.
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/>.
20 #include "replace.h"
21 #include <talloc.h>
22 #include "../lib/util/debug.h"
23 #include "../lib/util/samba_util.h"
24 #include "../lib/util/dlinklist.h"
25 #include "../lib/util/rbtree.h"
26 #include "memcache.h"
28 static struct memcache *global_cache;
30 struct memcache_element {
31 struct rb_node rb_node;
32 struct memcache_element *prev, *next;
33 size_t keylength, valuelength;
34 uint8_t n; /* This is really an enum, but save memory */
35 char data[1]; /* placeholder for offsetof */
38 struct memcache {
39 struct memcache_element *mru;
40 struct rb_root tree;
41 size_t size;
42 size_t max_size;
45 static void memcache_element_parse(struct memcache_element *e,
46 DATA_BLOB *key, DATA_BLOB *value);
48 static bool memcache_is_talloc(enum memcache_number n)
50 bool result;
52 switch (n) {
53 case GETPWNAM_CACHE:
54 case PDB_GETPWSID_CACHE:
55 case SINGLETON_CACHE_TALLOC:
56 case SHARE_MODE_LOCK_CACHE:
57 case GETWD_CACHE:
58 result = true;
59 break;
60 default:
61 result = false;
62 break;
65 return result;
68 static int memcache_destructor(struct memcache *cache) {
69 struct memcache_element *e, *next;
71 for (e = cache->mru; e != NULL; e = next) {
72 next = e->next;
73 TALLOC_FREE(e);
75 return 0;
78 struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
80 struct memcache *result;
82 result = talloc_zero(mem_ctx, struct memcache);
83 if (result == NULL) {
84 return NULL;
86 result->max_size = max_size;
87 talloc_set_destructor(result, memcache_destructor);
88 return result;
91 void memcache_set_global(struct memcache *cache)
93 TALLOC_FREE(global_cache);
94 global_cache = cache;
97 static struct memcache_element *memcache_node2elem(struct rb_node *node)
99 return (struct memcache_element *)
100 ((char *)node - offsetof(struct memcache_element, rb_node));
103 static void memcache_element_parse(struct memcache_element *e,
104 DATA_BLOB *key, DATA_BLOB *value)
106 key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
107 key->length = e->keylength;
108 value->data = key->data + e->keylength;
109 value->length = e->valuelength;
112 static size_t memcache_element_size(size_t key_length, size_t value_length)
114 return sizeof(struct memcache_element) - 1 + key_length + value_length;
117 static int memcache_compare(struct memcache_element *e, enum memcache_number n,
118 DATA_BLOB key)
120 DATA_BLOB this_key, this_value;
122 if ((int)e->n < (int)n) return 1;
123 if ((int)e->n > (int)n) return -1;
125 if (e->keylength < key.length) return 1;
126 if (e->keylength > key.length) return -1;
128 memcache_element_parse(e, &this_key, &this_value);
129 return memcmp(this_key.data, key.data, key.length);
132 static struct memcache_element *memcache_find(
133 struct memcache *cache, enum memcache_number n, DATA_BLOB key)
135 struct rb_node *node;
137 node = cache->tree.rb_node;
139 while (node != NULL) {
140 struct memcache_element *elem = memcache_node2elem(node);
141 int cmp;
143 cmp = memcache_compare(elem, n, key);
144 if (cmp == 0) {
145 return elem;
147 node = (cmp < 0) ? node->rb_left : node->rb_right;
150 return NULL;
153 bool memcache_lookup(struct memcache *cache, enum memcache_number n,
154 DATA_BLOB key, DATA_BLOB *value)
156 struct memcache_element *e;
158 if (cache == NULL) {
159 cache = global_cache;
161 if (cache == NULL) {
162 return false;
165 e = memcache_find(cache, n, key);
166 if (e == NULL) {
167 return false;
170 if (cache->size != 0) {
171 DLIST_PROMOTE(cache->mru, e);
174 memcache_element_parse(e, &key, value);
175 return true;
178 void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
179 DATA_BLOB key)
181 DATA_BLOB value;
182 void *result;
184 if (!memcache_lookup(cache, n, key, &value)) {
185 return NULL;
188 if (value.length != sizeof(result)) {
189 return NULL;
192 memcpy(&result, value.data, sizeof(result));
194 return result;
197 static void memcache_delete_element(struct memcache *cache,
198 struct memcache_element *e)
200 rb_erase(&e->rb_node, &cache->tree);
202 DLIST_REMOVE(cache->mru, e);
204 if (memcache_is_talloc(e->n)) {
205 DATA_BLOB cache_key, cache_value;
206 void *ptr;
208 memcache_element_parse(e, &cache_key, &cache_value);
209 SMB_ASSERT(cache_value.length == sizeof(ptr));
210 memcpy(&ptr, cache_value.data, sizeof(ptr));
211 TALLOC_FREE(ptr);
214 cache->size -= memcache_element_size(e->keylength, e->valuelength);
216 TALLOC_FREE(e);
219 static void memcache_trim(struct memcache *cache)
221 if (cache->max_size == 0) {
222 return;
225 while ((cache->size > cache->max_size) && DLIST_TAIL(cache->mru)) {
226 memcache_delete_element(cache, DLIST_TAIL(cache->mru));
230 void memcache_delete(struct memcache *cache, enum memcache_number n,
231 DATA_BLOB key)
233 struct memcache_element *e;
235 if (cache == NULL) {
236 cache = global_cache;
238 if (cache == NULL) {
239 return;
242 e = memcache_find(cache, n, key);
243 if (e == NULL) {
244 return;
247 memcache_delete_element(cache, e);
250 void memcache_add(struct memcache *cache, enum memcache_number n,
251 DATA_BLOB key, DATA_BLOB value)
253 struct memcache_element *e;
254 struct rb_node **p;
255 struct rb_node *parent;
256 DATA_BLOB cache_key, cache_value;
257 size_t element_size;
259 if (cache == NULL) {
260 cache = global_cache;
262 if (cache == NULL) {
263 return;
266 if (key.length == 0) {
267 return;
270 e = memcache_find(cache, n, key);
272 if (e != NULL) {
273 memcache_element_parse(e, &cache_key, &cache_value);
275 if (value.length <= cache_value.length) {
276 if (memcache_is_talloc(e->n)) {
277 void *ptr;
278 SMB_ASSERT(cache_value.length == sizeof(ptr));
279 memcpy(&ptr, cache_value.data, sizeof(ptr));
280 TALLOC_FREE(ptr);
283 * We can reuse the existing record
285 memcpy(cache_value.data, value.data, value.length);
286 e->valuelength = value.length;
287 return;
290 memcache_delete_element(cache, e);
293 element_size = memcache_element_size(key.length, value.length);
295 e = talloc_size(cache, element_size);
296 if (e == NULL) {
297 DEBUG(0, ("talloc failed\n"));
298 return;
300 talloc_set_type(e, struct memcache_element);
302 e->n = n;
303 e->keylength = key.length;
304 e->valuelength = value.length;
306 memcache_element_parse(e, &cache_key, &cache_value);
307 memcpy(cache_key.data, key.data, key.length);
308 memcpy(cache_value.data, value.data, value.length);
310 parent = NULL;
311 p = &cache->tree.rb_node;
313 while (*p) {
314 struct memcache_element *elem = memcache_node2elem(*p);
315 int cmp;
317 parent = (*p);
319 cmp = memcache_compare(elem, n, key);
321 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
324 rb_link_node(&e->rb_node, parent, p);
325 rb_insert_color(&e->rb_node, &cache->tree);
327 DLIST_ADD(cache->mru, e);
329 cache->size += element_size;
330 memcache_trim(cache);
333 void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
334 DATA_BLOB key, void *pptr)
336 void **ptr = (void **)pptr;
337 void *p;
339 if (cache == NULL) {
340 cache = global_cache;
342 if (cache == NULL) {
343 return;
346 p = talloc_move(cache, ptr);
347 memcache_add(cache, n, key, data_blob_const(&p, sizeof(p)));
350 void memcache_flush(struct memcache *cache, enum memcache_number n)
352 struct rb_node *node;
354 if (cache == NULL) {
355 cache = global_cache;
357 if (cache == NULL) {
358 return;
362 * Find the smallest element of number n
365 node = cache->tree.rb_node;
366 if (node == NULL) {
367 return;
371 * First, find *any* element of number n
374 while (true) {
375 struct memcache_element *elem = memcache_node2elem(node);
376 struct rb_node *next;
378 if ((int)elem->n == (int)n) {
379 break;
382 if ((int)elem->n < (int)n) {
383 next = node->rb_right;
385 else {
386 next = node->rb_left;
388 if (next == NULL) {
389 break;
391 node = next;
395 * Then, find the leftmost element with number n
398 while (true) {
399 struct rb_node *prev = rb_prev(node);
400 struct memcache_element *elem;
402 if (prev == NULL) {
403 break;
405 elem = memcache_node2elem(prev);
406 if ((int)elem->n != (int)n) {
407 break;
409 node = prev;
412 while (node != NULL) {
413 struct memcache_element *e = memcache_node2elem(node);
414 struct rb_node *next = rb_next(node);
416 if (e->n != n) {
417 break;
420 memcache_delete_element(cache, e);
421 node = next;