messages_ctdb: Use message_hdr_[get/put]
[Samba.git] / lib / util / memcache.c
blob9e9a20802e6c0e04736c237343863a6cd8fe0d6f
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 result = true;
58 break;
59 default:
60 result = false;
61 break;
64 return result;
67 static int memcache_destructor(struct memcache *cache) {
68 struct memcache_element *e, *next;
70 for (e = cache->mru; e != NULL; e = next) {
71 next = e->next;
72 TALLOC_FREE(e);
74 return 0;
77 struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
79 struct memcache *result;
81 result = talloc_zero(mem_ctx, struct memcache);
82 if (result == NULL) {
83 return NULL;
85 result->max_size = max_size;
86 talloc_set_destructor(result, memcache_destructor);
87 return result;
90 void memcache_set_global(struct memcache *cache)
92 TALLOC_FREE(global_cache);
93 global_cache = cache;
96 static struct memcache_element *memcache_node2elem(struct rb_node *node)
98 return (struct memcache_element *)
99 ((char *)node - offsetof(struct memcache_element, rb_node));
102 static void memcache_element_parse(struct memcache_element *e,
103 DATA_BLOB *key, DATA_BLOB *value)
105 key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
106 key->length = e->keylength;
107 value->data = key->data + e->keylength;
108 value->length = e->valuelength;
111 static size_t memcache_element_size(size_t key_length, size_t value_length)
113 return sizeof(struct memcache_element) - 1 + key_length + value_length;
116 static int memcache_compare(struct memcache_element *e, enum memcache_number n,
117 DATA_BLOB key)
119 DATA_BLOB this_key, this_value;
121 if ((int)e->n < (int)n) return 1;
122 if ((int)e->n > (int)n) return -1;
124 if (e->keylength < key.length) return 1;
125 if (e->keylength > key.length) return -1;
127 memcache_element_parse(e, &this_key, &this_value);
128 return memcmp(this_key.data, key.data, key.length);
131 static struct memcache_element *memcache_find(
132 struct memcache *cache, enum memcache_number n, DATA_BLOB key)
134 struct rb_node *node;
136 node = cache->tree.rb_node;
138 while (node != NULL) {
139 struct memcache_element *elem = memcache_node2elem(node);
140 int cmp;
142 cmp = memcache_compare(elem, n, key);
143 if (cmp == 0) {
144 return elem;
146 node = (cmp < 0) ? node->rb_left : node->rb_right;
149 return NULL;
152 bool memcache_lookup(struct memcache *cache, enum memcache_number n,
153 DATA_BLOB key, DATA_BLOB *value)
155 struct memcache_element *e;
157 if (cache == NULL) {
158 cache = global_cache;
160 if (cache == NULL) {
161 return false;
164 e = memcache_find(cache, n, key);
165 if (e == NULL) {
166 return false;
169 if (cache->size != 0) {
170 DLIST_PROMOTE(cache->mru, e);
173 memcache_element_parse(e, &key, value);
174 return true;
177 void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
178 DATA_BLOB key)
180 DATA_BLOB value;
181 void *result;
183 if (!memcache_lookup(cache, n, key, &value)) {
184 return NULL;
187 if (value.length != sizeof(result)) {
188 return NULL;
191 memcpy(&result, value.data, sizeof(result));
193 return result;
196 static void memcache_delete_element(struct memcache *cache,
197 struct memcache_element *e)
199 rb_erase(&e->rb_node, &cache->tree);
201 DLIST_REMOVE(cache->mru, e);
203 if (memcache_is_talloc(e->n)) {
204 DATA_BLOB cache_key, cache_value;
205 void *ptr;
207 memcache_element_parse(e, &cache_key, &cache_value);
208 SMB_ASSERT(cache_value.length == sizeof(ptr));
209 memcpy(&ptr, cache_value.data, sizeof(ptr));
210 TALLOC_FREE(ptr);
213 cache->size -= memcache_element_size(e->keylength, e->valuelength);
215 TALLOC_FREE(e);
218 static void memcache_trim(struct memcache *cache)
220 if (cache->max_size == 0) {
221 return;
224 while ((cache->size > cache->max_size) && DLIST_TAIL(cache->mru)) {
225 memcache_delete_element(cache, DLIST_TAIL(cache->mru));
229 void memcache_delete(struct memcache *cache, enum memcache_number n,
230 DATA_BLOB key)
232 struct memcache_element *e;
234 if (cache == NULL) {
235 cache = global_cache;
237 if (cache == NULL) {
238 return;
241 e = memcache_find(cache, n, key);
242 if (e == NULL) {
243 return;
246 memcache_delete_element(cache, e);
249 void memcache_add(struct memcache *cache, enum memcache_number n,
250 DATA_BLOB key, DATA_BLOB value)
252 struct memcache_element *e;
253 struct rb_node **p;
254 struct rb_node *parent;
255 DATA_BLOB cache_key, cache_value;
256 size_t element_size;
258 if (cache == NULL) {
259 cache = global_cache;
261 if (cache == NULL) {
262 return;
265 if (key.length == 0) {
266 return;
269 e = memcache_find(cache, n, key);
271 if (e != NULL) {
272 memcache_element_parse(e, &cache_key, &cache_value);
274 if (value.length <= cache_value.length) {
275 if (memcache_is_talloc(e->n)) {
276 void *ptr;
277 SMB_ASSERT(cache_value.length == sizeof(ptr));
278 memcpy(&ptr, cache_value.data, sizeof(ptr));
279 TALLOC_FREE(ptr);
282 * We can reuse the existing record
284 memcpy(cache_value.data, value.data, value.length);
285 e->valuelength = value.length;
286 return;
289 memcache_delete_element(cache, e);
292 element_size = memcache_element_size(key.length, value.length);
294 e = talloc_size(cache, element_size);
295 if (e == NULL) {
296 DEBUG(0, ("talloc failed\n"));
297 return;
299 talloc_set_type(e, struct memcache_element);
301 e->n = n;
302 e->keylength = key.length;
303 e->valuelength = value.length;
305 memcache_element_parse(e, &cache_key, &cache_value);
306 memcpy(cache_key.data, key.data, key.length);
307 memcpy(cache_value.data, value.data, value.length);
309 parent = NULL;
310 p = &cache->tree.rb_node;
312 while (*p) {
313 struct memcache_element *elem = memcache_node2elem(*p);
314 int cmp;
316 parent = (*p);
318 cmp = memcache_compare(elem, n, key);
320 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
323 rb_link_node(&e->rb_node, parent, p);
324 rb_insert_color(&e->rb_node, &cache->tree);
326 DLIST_ADD(cache->mru, e);
328 cache->size += element_size;
329 memcache_trim(cache);
332 void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
333 DATA_BLOB key, void *pptr)
335 void **ptr = (void **)pptr;
336 void *p;
338 if (cache == NULL) {
339 cache = global_cache;
341 if (cache == NULL) {
342 return;
345 p = talloc_move(cache, ptr);
346 memcache_add(cache, n, key, data_blob_const(&p, sizeof(p)));
349 void memcache_flush(struct memcache *cache, enum memcache_number n)
351 struct rb_node *node;
353 if (cache == NULL) {
354 cache = global_cache;
356 if (cache == NULL) {
357 return;
361 * Find the smallest element of number n
364 node = cache->tree.rb_node;
365 if (node == NULL) {
366 return;
370 * First, find *any* element of number n
373 while (true) {
374 struct memcache_element *elem = memcache_node2elem(node);
375 struct rb_node *next;
377 if ((int)elem->n == (int)n) {
378 break;
381 if ((int)elem->n < (int)n) {
382 next = node->rb_right;
384 else {
385 next = node->rb_left;
387 if (next == NULL) {
388 break;
390 node = next;
394 * Then, find the leftmost element with number n
397 while (true) {
398 struct rb_node *prev = rb_prev(node);
399 struct memcache_element *elem;
401 if (prev == NULL) {
402 break;
404 elem = memcache_node2elem(prev);
405 if ((int)elem->n != (int)n) {
406 break;
408 node = prev;
411 while (node != NULL) {
412 struct memcache_element *e = memcache_node2elem(node);
413 struct rb_node *next = rb_next(node);
415 if (e->n != n) {
416 break;
419 memcache_delete_element(cache, e);
420 node = next;