2 Unix SMB/CIFS implementation.
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/>.
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"
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 */
39 struct memcache_element
*mru
;
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
)
54 case PDB_GETPWSID_CACHE
:
55 case SINGLETON_CACHE_TALLOC
:
56 case SHARE_MODE_LOCK_CACHE
:
68 static int memcache_destructor(struct memcache
*cache
) {
69 struct memcache_element
*e
, *next
;
71 for (e
= cache
->mru
; e
!= NULL
; e
= next
) {
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
);
86 result
->max_size
= max_size
;
87 talloc_set_destructor(result
, memcache_destructor
);
91 void memcache_set_global(struct memcache
*cache
)
93 TALLOC_FREE(global_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
,
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
);
143 cmp
= memcache_compare(elem
, n
, key
);
147 node
= (cmp
< 0) ? node
->rb_left
: node
->rb_right
;
153 bool memcache_lookup(struct memcache
*cache
, enum memcache_number n
,
154 DATA_BLOB key
, DATA_BLOB
*value
)
156 struct memcache_element
*e
;
159 cache
= global_cache
;
165 e
= memcache_find(cache
, n
, key
);
170 if (cache
->size
!= 0) {
171 DLIST_PROMOTE(cache
->mru
, e
);
174 memcache_element_parse(e
, &key
, value
);
178 void *memcache_lookup_talloc(struct memcache
*cache
, enum memcache_number n
,
184 if (!memcache_lookup(cache
, n
, key
, &value
)) {
188 if (value
.length
!= sizeof(result
)) {
192 memcpy(&result
, value
.data
, sizeof(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
;
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
));
214 cache
->size
-= memcache_element_size(e
->keylength
, e
->valuelength
);
219 static void memcache_trim(struct memcache
*cache
)
221 if (cache
->max_size
== 0) {
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
,
233 struct memcache_element
*e
;
236 cache
= global_cache
;
242 e
= memcache_find(cache
, n
, key
);
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
;
255 struct rb_node
*parent
;
256 DATA_BLOB cache_key
, cache_value
;
260 cache
= global_cache
;
266 if (key
.length
== 0) {
270 e
= memcache_find(cache
, n
, key
);
273 memcache_element_parse(e
, &cache_key
, &cache_value
);
275 if (value
.length
<= cache_value
.length
) {
276 if (memcache_is_talloc(e
->n
)) {
278 SMB_ASSERT(cache_value
.length
== sizeof(ptr
));
279 memcpy(&ptr
, cache_value
.data
, sizeof(ptr
));
283 * We can reuse the existing record
285 memcpy(cache_value
.data
, value
.data
, value
.length
);
286 e
->valuelength
= value
.length
;
290 memcache_delete_element(cache
, e
);
293 element_size
= memcache_element_size(key
.length
, value
.length
);
295 e
= talloc_size(cache
, element_size
);
297 DEBUG(0, ("talloc failed\n"));
300 talloc_set_type(e
, struct memcache_element
);
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
);
311 p
= &cache
->tree
.rb_node
;
314 struct memcache_element
*elem
= memcache_node2elem(*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
;
340 cache
= global_cache
;
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
;
355 cache
= global_cache
;
362 * Find the smallest element of number n
365 node
= cache
->tree
.rb_node
;
371 * First, find *any* element of number n
375 struct memcache_element
*elem
= memcache_node2elem(node
);
376 struct rb_node
*next
;
378 if ((int)elem
->n
== (int)n
) {
382 if ((int)elem
->n
< (int)n
) {
383 next
= node
->rb_right
;
386 next
= node
->rb_left
;
395 * Then, find the leftmost element with number n
399 struct rb_node
*prev
= rb_prev(node
);
400 struct memcache_element
*elem
;
405 elem
= memcache_node2elem(prev
);
406 if ((int)elem
->n
!= (int)n
) {
412 while (node
!= NULL
) {
413 struct memcache_element
*e
= memcache_node2elem(node
);
414 struct rb_node
*next
= rb_next(node
);
420 memcache_delete_element(cache
, e
);