root/source3/lib/memcache.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. memcache_is_talloc
  2. memcache_destructor
  3. memcache_init
  4. memcache_set_global
  5. memcache_node2elem
  6. memcache_element_parse
  7. memcache_element_size
  8. memcache_compare
  9. memcache_find
  10. memcache_lookup
  11. memcache_lookup_talloc
  12. memcache_delete_element
  13. memcache_trim
  14. memcache_delete
  15. memcache_add
  16. memcache_add_talloc
  17. memcache_flush

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

/* [<][>][^][v][top][bottom][index][help] */