From cc48010f41684b5ef8c2e8a5511528cc426d300f Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 20 Dec 2007 10:55:45 +0100 Subject: Add a global cache It hurts, but I think this global variable is necessary for transition, and it has the potential to remove quite a few other global variables without messing with APIs too much. (This used to be commit c131d0dc52ec09c9227eff3d68877369c37aaed5) --- source3/lib/memcache.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'source3/lib/memcache.c') diff --git a/source3/lib/memcache.c b/source3/lib/memcache.c index 17630066ae..38bbd66085 100644 --- a/source3/lib/memcache.c +++ b/source3/lib/memcache.c @@ -20,6 +20,8 @@ #include "memcache.h" #include "rbtree.h" +static struct memcache *global_cache; + struct memcache_element { struct rb_node rb_node; struct memcache_element *prev, *next; @@ -58,6 +60,12 @@ struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size) return result; } +void memcache_set_global(struct memcache *cache) +{ + TALLOC_FREE(global_cache); + global_cache = cache; +} + static struct memcache_element *memcache_node2elem(struct rb_node *node) { return (struct memcache_element *) @@ -119,6 +127,13 @@ bool memcache_lookup(struct memcache *cache, enum memcache_number n, { struct memcache_element *e; + if (cache == NULL) { + cache = global_cache; + } + if (cache == NULL) { + return false; + } + e = memcache_find(cache, n, key); if (e == NULL) { return false; @@ -172,6 +187,13 @@ void memcache_delete(struct memcache *cache, enum memcache_number n, { struct memcache_element *e; + if (cache == NULL) { + cache = global_cache; + } + if (cache == NULL) { + return; + } + e = memcache_find(cache, n, key); if (e == NULL) { return; @@ -189,6 +211,13 @@ void memcache_add(struct memcache *cache, enum memcache_number n, DATA_BLOB cache_key, cache_value; size_t element_size; + if (cache == NULL) { + cache = global_cache; + } + if (cache == NULL) { + return; + } + if (key.length == 0) { return; } @@ -258,6 +287,13 @@ void memcache_flush(struct memcache *cache, enum memcache_number n) { struct rb_node *node; + if (cache == NULL) { + cache = global_cache; + } + if (cache == NULL) { + return; + } + /* * Find the smallest element of number n */ -- cgit From d3d870cc07447cf08a776c714a39f40f9c72da2c Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 20 Dec 2007 14:41:58 +0100 Subject: Add memcache_add_talloc The first memcache API only had blobs, but we have quite a few objects that are more complex talloc'ed structues. The current one I'm looking at is the getpwnam cache, but there are others around. (This used to be commit ea0e5ad9a15c848904dee8cb2d3e392b6a894705) --- source3/lib/memcache.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'source3/lib/memcache.c') diff --git a/source3/lib/memcache.c b/source3/lib/memcache.c index 38bbd66085..192a822cde 100644 --- a/source3/lib/memcache.c +++ b/source3/lib/memcache.c @@ -37,11 +37,38 @@ struct memcache { size_t max_size; }; +static void memcache_element_parse(struct memcache_element *e, + DATA_BLOB *key, DATA_BLOB *value); + +static bool memcache_is_talloc(enum memcache_number n) +{ + bool result; + + switch (n) { + case GETPWNAM_CACHE: + result = true; + break; + default: + result = false; + break; + } + + return result; +} + static int memcache_destructor(struct memcache *cache) { struct memcache_element *e, *next; for (e = cache->mru; e != NULL; e = next) { next = e->next; + if (memcache_is_talloc(e->n) + && (e->valuelength == sizeof(void *))) { + DATA_BLOB key, value; + void *ptr; + memcache_element_parse(e, &key, &value); + memcpy(&ptr, value.data, sizeof(ptr)); + TALLOC_FREE(ptr); + } SAFE_FREE(e); } return 0; @@ -156,6 +183,25 @@ bool memcache_lookup(struct memcache *cache, enum memcache_number n, return true; } +void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n, + DATA_BLOB key) +{ + DATA_BLOB value; + void *result; + + if (!memcache_lookup(cache, n, key, &value)) { + return NULL; + } + + if (value.length != sizeof(result)) { + return NULL; + } + + memcpy(&result, value.data, sizeof(result)); + + return result; +} + static void memcache_delete_element(struct memcache *cache, struct memcache_element *e) { @@ -283,6 +329,12 @@ void memcache_add(struct memcache *cache, enum memcache_number n, memcache_trim(cache); } +void memcache_add_talloc(struct memcache *cache, enum memcache_number n, + DATA_BLOB key, void *ptr) +{ + return memcache_add(cache, n, key, data_blob_const(&ptr, sizeof(ptr))); +} + void memcache_flush(struct memcache *cache, enum memcache_number n) { struct rb_node *node; -- cgit From addf598cde41d17ad4cf497a64b9a2b27e4028c5 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 20 Dec 2007 22:17:16 +0100 Subject: Some C++ warnings (This used to be commit 5ab82d4f574f2a2e2761e9e414c66a70aeffb05d) --- source3/lib/memcache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/memcache.c') diff --git a/source3/lib/memcache.c b/source3/lib/memcache.c index 192a822cde..448b8b2f84 100644 --- a/source3/lib/memcache.c +++ b/source3/lib/memcache.c @@ -61,7 +61,7 @@ static int memcache_destructor(struct memcache *cache) { for (e = cache->mru; e != NULL; e = next) { next = e->next; - if (memcache_is_talloc(e->n) + if (memcache_is_talloc((enum memcache_number)e->n) && (e->valuelength == sizeof(void *))) { DATA_BLOB key, value; void *ptr; -- cgit From 4418b989e7009c63d2206cecdfaa50cc66e1d8da Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 21 Dec 2007 12:53:12 +0100 Subject: Fix the build on Solaris (This used to be commit 5f5e52ba7b3862dc72a16d84e07503e98ccbbf8a) --- source3/lib/memcache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/memcache.c') diff --git a/source3/lib/memcache.c b/source3/lib/memcache.c index 448b8b2f84..457586bd68 100644 --- a/source3/lib/memcache.c +++ b/source3/lib/memcache.c @@ -332,7 +332,7 @@ void memcache_add(struct memcache *cache, enum memcache_number n, void memcache_add_talloc(struct memcache *cache, enum memcache_number n, DATA_BLOB key, void *ptr) { - return memcache_add(cache, n, key, data_blob_const(&ptr, sizeof(ptr))); + memcache_add(cache, n, key, data_blob_const(&ptr, sizeof(ptr))); } void memcache_flush(struct memcache *cache, enum memcache_number n) -- cgit