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
:
67 static int memcache_destructor(struct memcache
*cache
) {
68 struct memcache_element
*e
, *next
;
70 for (e
= cache
->mru
; e
!= NULL
; e
= next
) {
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
);
85 result
->max_size
= max_size
;
86 talloc_set_destructor(result
, memcache_destructor
);
90 void memcache_set_global(struct memcache
*cache
)
92 TALLOC_FREE(global_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
,
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
);
142 cmp
= memcache_compare(elem
, n
, key
);
146 node
= (cmp
< 0) ? node
->rb_left
: node
->rb_right
;
152 bool memcache_lookup(struct memcache
*cache
, enum memcache_number n
,
153 DATA_BLOB key
, DATA_BLOB
*value
)
155 struct memcache_element
*e
;
158 cache
= global_cache
;
164 e
= memcache_find(cache
, n
, key
);
169 if (cache
->size
!= 0) {
170 DLIST_PROMOTE(cache
->mru
, e
);
173 memcache_element_parse(e
, &key
, value
);
177 void *memcache_lookup_talloc(struct memcache
*cache
, enum memcache_number n
,
183 if (!memcache_lookup(cache
, n
, key
, &value
)) {
187 if (value
.length
!= sizeof(result
)) {
191 memcpy(&result
, value
.data
, sizeof(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
;
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
));
213 cache
->size
-= memcache_element_size(e
->keylength
, e
->valuelength
);
218 static void memcache_trim(struct memcache
*cache
)
220 if (cache
->max_size
== 0) {
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
,
232 struct memcache_element
*e
;
235 cache
= global_cache
;
241 e
= memcache_find(cache
, n
, key
);
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
;
254 struct rb_node
*parent
;
255 DATA_BLOB cache_key
, cache_value
;
259 cache
= global_cache
;
265 if (key
.length
== 0) {
269 e
= memcache_find(cache
, n
, key
);
272 memcache_element_parse(e
, &cache_key
, &cache_value
);
274 if (value
.length
<= cache_value
.length
) {
275 if (memcache_is_talloc(e
->n
)) {
277 SMB_ASSERT(cache_value
.length
== sizeof(ptr
));
278 memcpy(&ptr
, cache_value
.data
, sizeof(ptr
));
282 * We can reuse the existing record
284 memcpy(cache_value
.data
, value
.data
, value
.length
);
285 e
->valuelength
= value
.length
;
289 memcache_delete_element(cache
, e
);
292 element_size
= memcache_element_size(key
.length
, value
.length
);
294 e
= talloc_size(cache
, element_size
);
296 DEBUG(0, ("talloc failed\n"));
299 talloc_set_type(e
, struct memcache_element
);
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
);
310 p
= &cache
->tree
.rb_node
;
313 struct memcache_element
*elem
= memcache_node2elem(*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
;
339 cache
= global_cache
;
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
;
354 cache
= global_cache
;
361 * Find the smallest element of number n
364 node
= cache
->tree
.rb_node
;
370 * First, find *any* element of number n
374 struct memcache_element
*elem
= memcache_node2elem(node
);
375 struct rb_node
*next
;
377 if ((int)elem
->n
== (int)n
) {
381 if ((int)elem
->n
< (int)n
) {
382 next
= node
->rb_right
;
385 next
= node
->rb_left
;
394 * Then, find the leftmost element with number n
398 struct rb_node
*prev
= rb_prev(node
);
399 struct memcache_element
*elem
;
404 elem
= memcache_node2elem(prev
);
405 if ((int)elem
->n
!= (int)n
) {
411 while (node
!= NULL
) {
412 struct memcache_element
*e
= memcache_node2elem(node
);
413 struct rb_node
*next
= rb_next(node
);
419 memcache_delete_element(cache
, e
);