50e59fc7040719b3491bba3e0deff4ca2e608257
[sfrench/samba-autobuild/.git] / lib / util / memcache.c
1 /*
2    Unix SMB/CIFS implementation.
3    In-memory cache
4    Copyright (C) Volker Lendecke 2007
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include <talloc.h>
22 #include "../lib/util/samba_util.h"
23 #include "../lib/util/debug.h"
24 #include "../lib/util/dlinklist.h"
25 #include "../lib/util/rbtree.h"
26 #include "memcache.h"
27
28 static struct memcache *global_cache;
29
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 */
36 };
37
38 struct memcache {
39         struct memcache_element *mru;
40         struct rb_root tree;
41         size_t size;
42         size_t max_size;
43 };
44
45 static void memcache_element_parse(struct memcache_element *e,
46                                    DATA_BLOB *key, DATA_BLOB *value);
47
48 static bool memcache_is_talloc(enum memcache_number n)
49 {
50         bool result;
51
52         switch (n) {
53         case GETPWNAM_CACHE:
54         case PDB_GETPWSID_CACHE:
55         case SINGLETON_CACHE_TALLOC:
56                 result = true;
57                 break;
58         default:
59                 result = false;
60                 break;
61         }
62
63         return result;
64 }
65
66 static int memcache_destructor(struct memcache *cache) {
67         struct memcache_element *e, *next;
68
69         for (e = cache->mru; e != NULL; e = next) {
70                 next = e->next;
71                 TALLOC_FREE(e);
72         }
73         return 0;
74 }
75
76 struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
77 {
78         struct memcache *result;
79
80         result = talloc_zero(mem_ctx, struct memcache);
81         if (result == NULL) {
82                 return NULL;
83         }
84         result->max_size = max_size;
85         talloc_set_destructor(result, memcache_destructor);
86         return result;
87 }
88
89 void memcache_set_global(struct memcache *cache)
90 {
91         TALLOC_FREE(global_cache);
92         global_cache = cache;
93 }
94
95 static struct memcache_element *memcache_node2elem(struct rb_node *node)
96 {
97         return (struct memcache_element *)
98                 ((char *)node - offsetof(struct memcache_element, rb_node));
99 }
100
101 static void memcache_element_parse(struct memcache_element *e,
102                                    DATA_BLOB *key, DATA_BLOB *value)
103 {
104         key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
105         key->length = e->keylength;
106         value->data = key->data + e->keylength;
107         value->length = e->valuelength;
108 }
109
110 static size_t memcache_element_size(size_t key_length, size_t value_length)
111 {
112         return sizeof(struct memcache_element) - 1 + key_length + value_length;
113 }
114
115 static int memcache_compare(struct memcache_element *e, enum memcache_number n,
116                             DATA_BLOB key)
117 {
118         DATA_BLOB this_key, this_value;
119
120         if ((int)e->n < (int)n) return 1;
121         if ((int)e->n > (int)n) return -1;
122
123         if (e->keylength < key.length) return 1;
124         if (e->keylength > key.length) return -1;
125
126         memcache_element_parse(e, &this_key, &this_value);
127         return memcmp(this_key.data, key.data, key.length);
128 }
129
130 static struct memcache_element *memcache_find(
131         struct memcache *cache, enum memcache_number n, DATA_BLOB key)
132 {
133         struct rb_node *node;
134
135         node = cache->tree.rb_node;
136
137         while (node != NULL) {
138                 struct memcache_element *elem = memcache_node2elem(node);
139                 int cmp;
140
141                 cmp = memcache_compare(elem, n, key);
142                 if (cmp == 0) {
143                         return elem;
144                 }
145                 node = (cmp < 0) ? node->rb_left : node->rb_right;
146         }
147
148         return NULL;
149 }
150
151 bool memcache_lookup(struct memcache *cache, enum memcache_number n,
152                      DATA_BLOB key, DATA_BLOB *value)
153 {
154         struct memcache_element *e;
155
156         if (cache == NULL) {
157                 cache = global_cache;
158         }
159         if (cache == NULL) {
160                 return false;
161         }
162
163         e = memcache_find(cache, n, key);
164         if (e == NULL) {
165                 return false;
166         }
167
168         if (cache->size != 0) {
169                 DLIST_PROMOTE(cache->mru, e);
170         }
171
172         memcache_element_parse(e, &key, value);
173         return true;
174 }
175
176 void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
177                              DATA_BLOB key)
178 {
179         DATA_BLOB value;
180         void *result;
181
182         if (!memcache_lookup(cache, n, key, &value)) {
183                 return NULL;
184         }
185
186         if (value.length != sizeof(result)) {
187                 return NULL;
188         }
189
190         memcpy(&result, value.data, sizeof(result));
191
192         return result;
193 }
194
195 static void memcache_delete_element(struct memcache *cache,
196                                     struct memcache_element *e)
197 {
198         rb_erase(&e->rb_node, &cache->tree);
199
200         DLIST_REMOVE(cache->mru, e);
201
202         if (memcache_is_talloc(e->n)) {
203                 DATA_BLOB cache_key, cache_value;
204                 void *ptr;
205
206                 memcache_element_parse(e, &cache_key, &cache_value);
207                 SMB_ASSERT(cache_value.length == sizeof(ptr));
208                 memcpy(&ptr, cache_value.data, sizeof(ptr));
209                 TALLOC_FREE(ptr);
210         }
211
212         cache->size -= memcache_element_size(e->keylength, e->valuelength);
213
214         TALLOC_FREE(e);
215 }
216
217 static void memcache_trim(struct memcache *cache)
218 {
219         if (cache->max_size == 0) {
220                 return;
221         }
222
223         while ((cache->size > cache->max_size) && DLIST_TAIL(cache->mru)) {
224                 memcache_delete_element(cache, DLIST_TAIL(cache->mru));
225         }
226 }
227
228 void memcache_delete(struct memcache *cache, enum memcache_number n,
229                      DATA_BLOB key)
230 {
231         struct memcache_element *e;
232
233         if (cache == NULL) {
234                 cache = global_cache;
235         }
236         if (cache == NULL) {
237                 return;
238         }
239
240         e = memcache_find(cache, n, key);
241         if (e == NULL) {
242                 return;
243         }
244
245         memcache_delete_element(cache, e);
246 }
247
248 void memcache_add(struct memcache *cache, enum memcache_number n,
249                   DATA_BLOB key, DATA_BLOB value)
250 {
251         struct memcache_element *e;
252         struct rb_node **p;
253         struct rb_node *parent;
254         DATA_BLOB cache_key, cache_value;
255         size_t element_size;
256
257         if (cache == NULL) {
258                 cache = global_cache;
259         }
260         if (cache == NULL) {
261                 return;
262         }
263
264         if (key.length == 0) {
265                 return;
266         }
267
268         e = memcache_find(cache, n, key);
269
270         if (e != NULL) {
271                 memcache_element_parse(e, &cache_key, &cache_value);
272
273                 if (value.length <= cache_value.length) {
274                         if (memcache_is_talloc(e->n)) {
275                                 void *ptr;
276                                 SMB_ASSERT(cache_value.length == sizeof(ptr));
277                                 memcpy(&ptr, cache_value.data, sizeof(ptr));
278                                 TALLOC_FREE(ptr);
279                         }
280                         /*
281                          * We can reuse the existing record
282                          */
283                         memcpy(cache_value.data, value.data, value.length);
284                         e->valuelength = value.length;
285                         return;
286                 }
287
288                 memcache_delete_element(cache, e);
289         }
290
291         element_size = memcache_element_size(key.length, value.length);
292
293         e = talloc_size(cache, element_size);
294         if (e == NULL) {
295                 DEBUG(0, ("talloc failed\n"));
296                 return;
297         }
298         talloc_set_type(e, struct memcache_element);
299
300         e->n = n;
301         e->keylength = key.length;
302         e->valuelength = value.length;
303
304         memcache_element_parse(e, &cache_key, &cache_value);
305         memcpy(cache_key.data, key.data, key.length);
306         memcpy(cache_value.data, value.data, value.length);
307
308         parent = NULL;
309         p = &cache->tree.rb_node;
310
311         while (*p) {
312                 struct memcache_element *elem = memcache_node2elem(*p);
313                 int cmp;
314
315                 parent = (*p);
316
317                 cmp = memcache_compare(elem, n, key);
318
319                 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
320         }
321
322         rb_link_node(&e->rb_node, parent, p);
323         rb_insert_color(&e->rb_node, &cache->tree);
324
325         DLIST_ADD(cache->mru, e);
326
327         cache->size += element_size;
328         memcache_trim(cache);
329 }
330
331 void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
332                          DATA_BLOB key, void *pptr)
333 {
334         void **ptr = (void **)pptr;
335         void *p;
336
337         if (cache == NULL) {
338                 cache = global_cache;
339         }
340         if (cache == NULL) {
341                 return;
342         }
343
344         p = talloc_move(cache, ptr);
345         memcache_add(cache, n, key, data_blob_const(&p, sizeof(p)));
346 }
347
348 void memcache_flush(struct memcache *cache, enum memcache_number n)
349 {
350         struct rb_node *node;
351
352         if (cache == NULL) {
353                 cache = global_cache;
354         }
355         if (cache == NULL) {
356                 return;
357         }
358
359         /*
360          * Find the smallest element of number n
361          */
362
363         node = cache->tree.rb_node;
364         if (node == NULL) {
365                 return;
366         }
367
368         /*
369          * First, find *any* element of number n
370          */
371
372         while (true) {
373                 struct memcache_element *elem = memcache_node2elem(node);
374                 struct rb_node *next;
375
376                 if ((int)elem->n == (int)n) {
377                         break;
378                 }
379
380                 if ((int)elem->n < (int)n) {
381                         next = node->rb_right;
382                 }
383                 else {
384                         next = node->rb_left;
385                 }
386                 if (next == NULL) {
387                         break;
388                 }
389                 node = next;
390         }
391
392         /*
393          * Then, find the leftmost element with number n
394          */
395
396         while (true) {
397                 struct rb_node *prev = rb_prev(node);
398                 struct memcache_element *elem;
399
400                 if (prev == NULL) {
401                         break;
402                 }
403                 elem = memcache_node2elem(prev);
404                 if ((int)elem->n != (int)n) {
405                         break;
406                 }
407                 node = prev;
408         }
409
410         while (node != NULL) {
411                 struct memcache_element *e = memcache_node2elem(node);
412                 struct rb_node *next = rb_next(node);
413
414                 if (e->n != n) {
415                         break;
416                 }
417
418                 memcache_delete_element(cache, e);
419                 node = next;
420         }
421 }