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;
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");
/* 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 */
/*
* 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 <sys/param.h>
unsigned final_size;
unsigned total_count;
obj_init_fn init_func;
- obj_clean_fn clean_func;
};
/*
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);
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)
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;
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;
}
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);
}
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;
}
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);
}