From 226d642bc767c0c15ac8c386c78be4b5e426c177 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 23 Jan 2008 09:52:10 +0000 Subject: [PATCH] simpler slab cache, with explicit init func --- include/slab.h | 3 +-- src/objects.c | 40 ++++++---------------------------------- src/slab.c | 41 +++++++++++++---------------------------- 3 files changed, 20 insertions(+), 64 deletions(-) diff --git a/include/slab.h b/include/slab.h index 8cc8e0e..f05ee9d 100644 --- a/include/slab.h +++ b/include/slab.h @@ -2,10 +2,9 @@ typedef struct ObjectCache ObjectCache; typedef void (*obj_init_fn)(void *obj); -typedef void (*obj_clean_fn)(void *obj); ObjectCache *objcache_create(const char *name, unsigned obj_size, unsigned align, - obj_init_fn init_func, obj_clean_fn clean_func); + obj_init_fn init_func); void objcache_destroy(ObjectCache *cache); void * obj_alloc(ObjectCache *cache) _MALLOC _MUSTCHECK; diff --git a/src/objects.c b/src/objects.c index c99e5ef..78d9b1c 100644 --- a/src/objects.c +++ b/src/objects.c @@ -63,31 +63,6 @@ int get_active_server_count(void) return objcache_active_count(server_cache); } -/* this should be called on free socket that is put into use */ -static void clean_socket(void *obj) -{ - PgSocket *sk = obj; - - sk->link = NULL; - sk->pool = NULL; - - sk->wait_for_welcome = 0; - sk->ready = 0; - sk->admin_user = 0; - sk->own_user = 0; - sk->suspended = 0; - sk->wait_for_response = 0; - - sk->connect_time = 0; - sk->request_time = 0; - sk->query_start = 0; - - sk->auth_user = NULL; - - varcache_clean(&sk->vars); - sk->setting_vars = 0; -} - static void construct_client(void *obj) { PgSocket *client = obj; @@ -118,9 +93,9 @@ static int user_node_cmp(long userptr, Node *node) void init_objects(void) { tree_init(&user_tree, user_node_cmp, NULL); - user_cache = objcache_create("user_cache", sizeof(PgUser), 0, NULL, NULL); - db_cache = objcache_create("db_cache", sizeof(PgDatabase), 0, NULL, NULL); - pool_cache = objcache_create("pool_cache", sizeof(PgPool), 0, NULL, NULL); + user_cache = objcache_create("user_cache", sizeof(PgUser), 0, NULL); + db_cache = objcache_create("db_cache", sizeof(PgDatabase), 0, NULL); + pool_cache = objcache_create("pool_cache", sizeof(PgPool), 0, NULL); if (!user_cache || !db_cache || !pool_cache) fatal("cannot create initial caches"); @@ -135,12 +110,9 @@ static void do_iobuf_reset(void *arg) /* initialization after config loading */ void init_caches(void) { - server_cache = objcache_create("server_cache", sizeof(PgSocket), 0, - construct_server, clean_socket); - client_cache = objcache_create("client_cache", sizeof(PgSocket), 0, - construct_client, clean_socket); - iobuf_cache = objcache_create("iobuf_cache", IOBUF_SIZE, 0, - do_iobuf_reset, do_iobuf_reset); + server_cache = objcache_create("server_cache", sizeof(PgSocket), 0, construct_server); + client_cache = objcache_create("client_cache", sizeof(PgSocket), 0, construct_client); + iobuf_cache = objcache_create("iobuf_cache", IOBUF_SIZE, 0, do_iobuf_reset); } /* state change means moving between lists */ diff --git a/src/slab.c b/src/slab.c index c40fe02..27d4d8d 100644 --- a/src/slab.c +++ b/src/slab.c @@ -19,17 +19,8 @@ /* * Primitive slab allocator. * - * - On first alloc initializer is called for all objects. - * - On each release, cleaner is called. - * - When giving object out, nothing is done. - * - Writes List struct on obj header, expects it to be overwritten on use. - * - * TODO: fix list head at object: - * - set as policy that real object must have it too - * - or memset() / constructor policy on alloc - * - stop lazy constructor/destructor business as it only complicates, - * call constructor on alloc, destructor on free - * - avoid destructor completely + * - On each alloc initializer is called. + * - if init func is not given, memset() is done */ #include @@ -53,7 +44,6 @@ struct ObjectCache { unsigned final_size; unsigned total_count; obj_init_fn init_func; - obj_clean_fn clean_func; }; /* @@ -74,8 +64,7 @@ static void init_objcache(ObjectCache *cache, const char *name, unsigned obj_size, unsigned align, - obj_init_fn init_func, - obj_clean_fn clean_func) + obj_init_fn init_func) { list_init(&cache->head); statlist_init(&cache->freelist, name); @@ -83,7 +72,6 @@ static void init_objcache(ObjectCache *cache, strlcpy(cache->name, name, sizeof(cache->name)); cache->total_count = 0; cache->init_func = init_func; - cache->clean_func = clean_func; statlist_append(&cache->head, &objcache_list); if (align == 0) @@ -98,8 +86,7 @@ static void init_objcache(ObjectCache *cache, ObjectCache * objcache_create(const char *name, unsigned obj_size, unsigned align, - obj_init_fn init_func, - obj_clean_fn clean_func) + obj_init_fn init_func) { ObjectCache *cache; @@ -109,14 +96,13 @@ ObjectCache * objcache_create(const char *name, if (!objcache_cache) return NULL; init_objcache(objcache_cache, "objcache_cache", - sizeof(ObjectCache), 0, NULL, NULL); + sizeof(ObjectCache), 0, NULL); } /* new cache object */ cache = obj_alloc(objcache_cache); if (cache) - init_objcache(cache, name, obj_size, align, - init_func, clean_func); + init_objcache(cache, name, obj_size, align, init_func); return cache; } @@ -162,9 +148,6 @@ static void grow(ObjectCache *cache) for (i = 0; i < count; i++) { void *obj = area + i * cache->final_size; List *head = (List *)obj; - - if (cache->init_func) - cache->init_func(obj); list_init(head); statlist_append(head, &cache->freelist); } @@ -182,8 +165,12 @@ void *obj_alloc(ObjectCache *cache) grow(cache); item = statlist_pop(&cache->freelist); } - if (item && !cache->init_func) - memset(item, 0, cache->final_size); + if (item) { + if (cache->init_func) + cache->init_func(item); + else + memset(item, 0, cache->final_size); + } return item; } @@ -191,9 +178,7 @@ void *obj_alloc(ObjectCache *cache) void obj_free(ObjectCache *cache, void *obj) { List *item = obj; - if (cache->clean_func) - cache->clean_func(obj); - list_init(item); /* fixme: needs alloc policy too */ + list_init(item); statlist_prepend(item, &cache->freelist); } -- 2.40.0