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/include/memcache.h | 8 ++++++- source3/lib/memcache.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'source3') diff --git a/source3/include/memcache.h b/source3/include/memcache.h index 0ba3bdbb00..36cde98bd3 100644 --- a/source3/include/memcache.h +++ b/source3/include/memcache.h @@ -31,7 +31,7 @@ enum memcache_number { GID_SID_CACHE, SID_GID_CACHE, GETWD_CACHE, - GETPWNAM_CACHE, + GETPWNAM_CACHE, /* talloc */ MANGLE_HASH2_CACHE }; @@ -42,12 +42,18 @@ void memcache_set_global(struct memcache *cache); void memcache_add(struct memcache *cache, enum memcache_number n, DATA_BLOB key, DATA_BLOB value); +void memcache_add_talloc(struct memcache *cache, enum memcache_number n, + DATA_BLOB key, void *ptr); + void memcache_delete(struct memcache *cache, enum memcache_number n, DATA_BLOB key); bool memcache_lookup(struct memcache *cache, enum memcache_number n, DATA_BLOB key, DATA_BLOB *value); +void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n, + DATA_BLOB key); + void memcache_flush(struct memcache *cache, enum memcache_number n); #endif 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