docs: use the stdarg.option entity in the popt.common.samba entity
[Samba/gebeck_regimport.git] / source3 / lib / memcache.c
blob88453f32dd8d813dd154e7cf570f57a10cdc38e0
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 "memcache.h"
21 #include "../lib/util/rbtree.h"
23 static struct memcache *global_cache;
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 */
33 struct memcache {
34 struct memcache_element *mru;
35 struct rb_root tree;
36 size_t size;
37 size_t max_size;
40 static void memcache_element_parse(struct memcache_element *e,
41 DATA_BLOB *key, DATA_BLOB *value);
43 static bool memcache_is_talloc(enum memcache_number n)
45 bool result;
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;
58 return result;
61 static int memcache_destructor(struct memcache *cache) {
62 struct memcache_element *e, *next;
64 for (e = cache->mru; e != NULL; e = next) {
65 next = e->next;
66 SAFE_FREE(e);
68 return 0;
71 struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
73 struct memcache *result;
75 result = talloc_zero(mem_ctx, struct memcache);
76 if (result == NULL) {
77 return NULL;
79 result->max_size = max_size;
80 talloc_set_destructor(result, memcache_destructor);
81 return result;
84 void memcache_set_global(struct memcache *cache)
86 TALLOC_FREE(global_cache);
87 global_cache = cache;
90 static struct memcache_element *memcache_node2elem(struct rb_node *node)
92 return (struct memcache_element *)
93 ((char *)node - offsetof(struct memcache_element, rb_node));
96 static void memcache_element_parse(struct memcache_element *e,
97 DATA_BLOB *key, DATA_BLOB *value)
99 key->data = ((uint8 *)e) + offsetof(struct memcache_element, data);
100 key->length = e->keylength;
101 value->data = key->data + e->keylength;
102 value->length = e->valuelength;
105 static size_t memcache_element_size(size_t key_length, size_t value_length)
107 return sizeof(struct memcache_element) - 1 + key_length + value_length;
110 static int memcache_compare(struct memcache_element *e, enum memcache_number n,
111 DATA_BLOB key)
113 DATA_BLOB this_key, this_value;
115 if ((int)e->n < (int)n) return 1;
116 if ((int)e->n > (int)n) return -1;
118 if (e->keylength < key.length) return 1;
119 if (e->keylength > key.length) return -1;
121 memcache_element_parse(e, &this_key, &this_value);
122 return memcmp(this_key.data, key.data, key.length);
125 static struct memcache_element *memcache_find(
126 struct memcache *cache, enum memcache_number n, DATA_BLOB key)
128 struct rb_node *node;
130 node = cache->tree.rb_node;
132 while (node != NULL) {
133 struct memcache_element *elem = memcache_node2elem(node);
134 int cmp;
136 cmp = memcache_compare(elem, n, key);
137 if (cmp == 0) {
138 return elem;
140 node = (cmp < 0) ? node->rb_left : node->rb_right;
143 return NULL;
146 bool memcache_lookup(struct memcache *cache, enum memcache_number n,
147 DATA_BLOB key, DATA_BLOB *value)
149 struct memcache_element *e;
151 if (cache == NULL) {
152 cache = global_cache;
154 if (cache == NULL) {
155 return false;
158 e = memcache_find(cache, n, key);
159 if (e == NULL) {
160 return false;
163 if (cache->size != 0) {
164 DLIST_PROMOTE(cache->mru, e);
167 memcache_element_parse(e, &key, value);
168 return true;
171 void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
172 DATA_BLOB key)
174 DATA_BLOB value;
175 void *result;
177 if (!memcache_lookup(cache, n, key, &value)) {
178 return NULL;
181 if (value.length != sizeof(result)) {
182 return NULL;
185 memcpy(&result, value.data, sizeof(result));
187 return result;
190 static void memcache_delete_element(struct memcache *cache,
191 struct memcache_element *e)
193 rb_erase(&e->rb_node, &cache->tree);
195 DLIST_REMOVE(cache->mru, e);
197 if (memcache_is_talloc(e->n)) {
198 DATA_BLOB cache_key, cache_value;
199 void *ptr;
201 memcache_element_parse(e, &cache_key, &cache_value);
202 SMB_ASSERT(cache_value.length == sizeof(ptr));
203 memcpy(&ptr, cache_value.data, sizeof(ptr));
204 TALLOC_FREE(ptr);
207 cache->size -= memcache_element_size(e->keylength, e->valuelength);
209 SAFE_FREE(e);
212 static void memcache_trim(struct memcache *cache)
214 if (cache->max_size == 0) {
215 return;
218 while ((cache->size > cache->max_size) && DLIST_TAIL(cache->mru)) {
219 memcache_delete_element(cache, DLIST_TAIL(cache->mru));
223 void memcache_delete(struct memcache *cache, enum memcache_number n,
224 DATA_BLOB key)
226 struct memcache_element *e;
228 if (cache == NULL) {
229 cache = global_cache;
231 if (cache == NULL) {
232 return;
235 e = memcache_find(cache, n, key);
236 if (e == NULL) {
237 return;
240 memcache_delete_element(cache, e);
243 void memcache_add(struct memcache *cache, enum memcache_number n,
244 DATA_BLOB key, DATA_BLOB value)
246 struct memcache_element *e;
247 struct rb_node **p;
248 struct rb_node *parent;
249 DATA_BLOB cache_key, cache_value;
250 size_t element_size;
252 if (cache == NULL) {
253 cache = global_cache;
255 if (cache == NULL) {
256 return;
259 if (key.length == 0) {
260 return;
263 e = memcache_find(cache, n, key);
265 if (e != NULL) {
266 memcache_element_parse(e, &cache_key, &cache_value);
268 if (value.length <= cache_value.length) {
269 if (memcache_is_talloc(e->n)) {
270 void *ptr;
271 SMB_ASSERT(cache_value.length == sizeof(ptr));
272 memcpy(&ptr, cache_value.data, sizeof(ptr));
273 TALLOC_FREE(ptr);
276 * We can reuse the existing record
278 memcpy(cache_value.data, value.data, value.length);
279 e->valuelength = value.length;
280 return;
283 memcache_delete_element(cache, e);
286 element_size = memcache_element_size(key.length, value.length);
289 e = (struct memcache_element *)SMB_MALLOC(element_size);
291 if (e == NULL) {
292 DEBUG(0, ("malloc failed\n"));
293 return;
296 e->n = n;
297 e->keylength = key.length;
298 e->valuelength = value.length;
300 memcache_element_parse(e, &cache_key, &cache_value);
301 memcpy(cache_key.data, key.data, key.length);
302 memcpy(cache_value.data, value.data, value.length);
304 parent = NULL;
305 p = &cache->tree.rb_node;
307 while (*p) {
308 struct memcache_element *elem = memcache_node2elem(*p);
309 int cmp;
311 parent = (*p);
313 cmp = memcache_compare(elem, n, key);
315 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
318 rb_link_node(&e->rb_node, parent, p);
319 rb_insert_color(&e->rb_node, &cache->tree);
321 DLIST_ADD(cache->mru, e);
323 cache->size += element_size;
324 memcache_trim(cache);
327 void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
328 DATA_BLOB key, void *pptr)
330 void **ptr = (void **)pptr;
331 void *p;
333 if (cache == NULL) {
334 cache = global_cache;
336 if (cache == NULL) {
337 return;
340 p = talloc_move(cache, ptr);
341 memcache_add(cache, n, key, data_blob_const(&p, sizeof(p)));
344 void memcache_flush(struct memcache *cache, enum memcache_number n)
346 struct rb_node *node;
348 if (cache == NULL) {
349 cache = global_cache;
351 if (cache == NULL) {
352 return;
356 * Find the smallest element of number n
359 node = cache->tree.rb_node;
360 if (node == NULL) {
361 return;
365 * First, find *any* element of number n
368 while (true) {
369 struct memcache_element *elem = memcache_node2elem(node);
370 struct rb_node *next;
372 if ((int)elem->n == (int)n) {
373 break;
376 if ((int)elem->n < (int)n) {
377 next = node->rb_right;
379 else {
380 next = node->rb_left;
382 if (next == NULL) {
383 break;
385 node = next;
389 * Then, find the leftmost element with number n
392 while (true) {
393 struct rb_node *prev = rb_prev(node);
394 struct memcache_element *elem;
396 if (prev == NULL) {
397 break;
399 elem = memcache_node2elem(prev);
400 if ((int)elem->n != (int)n) {
401 break;
403 node = prev;
406 while (node != NULL) {
407 struct memcache_element *e = memcache_node2elem(node);
408 struct rb_node *next = rb_next(node);
410 if (e->n != n) {
411 break;
414 memcache_delete_element(cache, e);
415 node = next;