changeset 5026:d7e064f77f79 HEAD

Don't keep the memory pool virtual functions in the pool structure itself, rather access them via a pointer to a shared virtual function struct. Reduces memory usage a bit.
author Timo Sirainen <tss@iki.fi>
date Thu, 18 Jan 2007 17:33:36 +0200
parents 1f4216e1d060
children 787ead060fe3
files src/lib/mempool-alloconly.c src/lib/mempool-datastack.c src/lib/mempool-system.c src/lib/mempool-unsafe-datastack.c src/lib/mempool.h
diffstat 5 files changed, 47 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/mempool-alloconly.c	Thu Jan 18 17:31:20 2007 +0200
+++ b/src/lib/mempool-alloconly.c	Thu Jan 18 17:33:36 2007 +0200
@@ -56,7 +56,7 @@
 
 static void block_alloc(struct alloconly_pool *pool, size_t size);
 
-static struct pool static_alloconly_pool = {
+static const struct pool_vfuncs static_alloconly_pool_vfuncs = {
 	pool_alloconly_get_name,
 
 	pool_alloconly_ref,
@@ -68,10 +68,14 @@
 	pool_alloconly_realloc,
 
 	pool_alloconly_clear,
-	pool_alloconly_get_max_easy_alloc_size,
+	pool_alloconly_get_max_easy_alloc_size
+};
 
-	TRUE,
-	FALSE
+static const struct pool static_alloconly_pool = {
+	MEMBER(v) &static_alloconly_pool_vfuncs,
+
+	MEMBER(alloconly_pool) TRUE,
+	MEMBER(datastack_pool) FALSE
 };
 
 #ifdef DEBUG
--- a/src/lib/mempool-datastack.c	Thu Jan 18 17:31:20 2007 +0200
+++ b/src/lib/mempool-datastack.c	Thu Jan 18 17:33:36 2007 +0200
@@ -15,7 +15,7 @@
 static void pool_data_stack_clear(pool_t pool);
 static size_t pool_data_stack_get_max_easy_alloc_size(pool_t pool);
 
-static struct pool static_data_stack_pool = {
+static struct pool_vfuncs static_data_stack_pool_vfuncs = {
 	pool_data_stack_get_name,
 
 	pool_data_stack_ref,
@@ -27,10 +27,14 @@
 	pool_data_stack_realloc,
 
 	pool_data_stack_clear,
-	pool_data_stack_get_max_easy_alloc_size,
+	pool_data_stack_get_max_easy_alloc_size
+};
 
-	TRUE,
-	TRUE
+static const struct pool static_data_stack_pool = {
+	MEMBER(v) &static_data_stack_pool_vfuncs,
+
+	MEMBER(alloconly_pool) TRUE,
+	MEMBER(datastack_pool) TRUE
 };
 
 struct datastack_pool {
--- a/src/lib/mempool-system.c	Thu Jan 18 17:31:20 2007 +0200
+++ b/src/lib/mempool-system.c	Thu Jan 18 17:33:36 2007 +0200
@@ -23,7 +23,7 @@
 static void pool_system_clear(pool_t pool);
 static size_t pool_system_get_max_easy_alloc_size(pool_t pool);
 
-static struct pool static_system_pool = {
+static struct pool_vfuncs static_system_pool_vfuncs = {
 	pool_system_get_name,
 
 	pool_system_ref,
@@ -35,12 +35,17 @@
 	pool_system_realloc,
 
 	pool_system_clear,
-	pool_system_get_max_easy_alloc_size,
+	pool_system_get_max_easy_alloc_size
+};
 
-	FALSE,
-	FALSE
+static struct pool static_system_pool = {
+	MEMBER(v) &static_system_pool_vfuncs,
+
+	MEMBER(alloconly_pool) FALSE,
+	MEMBER(datastack_pool) FALSE
 };
 
+
 pool_t system_pool = &static_system_pool;
 
 static const char *pool_system_get_name(pool_t pool __attr_unused__)
--- a/src/lib/mempool-unsafe-datastack.c	Thu Jan 18 17:31:20 2007 +0200
+++ b/src/lib/mempool-unsafe-datastack.c	Thu Jan 18 17:33:36 2007 +0200
@@ -15,7 +15,7 @@
 static void pool_unsafe_data_stack_clear(pool_t pool);
 static size_t pool_unsafe_data_stack_get_max_easy_alloc_size(pool_t pool);
 
-static struct pool static_unsafe_data_stack_pool = {
+static struct pool_vfuncs static_unsafe_data_stack_pool_vfuncs = {
 	pool_unsafe_data_stack_get_name,
 
 	pool_unsafe_data_stack_ref,
@@ -27,10 +27,14 @@
 	pool_unsafe_data_stack_realloc,
 
 	pool_unsafe_data_stack_clear,
-	pool_unsafe_data_stack_get_max_easy_alloc_size,
+	pool_unsafe_data_stack_get_max_easy_alloc_size
+};
 
-	TRUE,
-	TRUE
+static struct pool static_unsafe_data_stack_pool = {
+	MEMBER(v) &static_unsafe_data_stack_pool_vfuncs,
+
+	MEMBER(alloconly_pool) TRUE,
+	MEMBER(datastack_pool) TRUE
 };
 
 pool_t unsafe_data_stack_pool = &static_unsafe_data_stack_pool;
--- a/src/lib/mempool.h	Thu Jan 18 17:31:20 2007 +0200
+++ b/src/lib/mempool.h	Thu Jan 18 17:33:36 2007 +0200
@@ -16,7 +16,7 @@
 
 typedef struct pool *pool_t;
 
-struct pool {
+struct pool_vfuncs {
 	const char *(*get_name)(pool_t pool);
 
 	void (*ref)(pool_t pool);
@@ -37,6 +37,10 @@
 	/* Returns the maximum amount of bytes that can be allocated with
 	   minimal trouble. If there's no such concept, always returns 0. */
 	size_t (*get_max_easy_alloc_size)(pool_t pool);
+};
+
+struct pool {
+	const struct pool_vfuncs *v;
 
 	unsigned int alloconly_pool:1;
 	unsigned int datastack_pool:1;
@@ -69,16 +73,16 @@
 size_t pool_get_exp_grown_size(pool_t pool, size_t old_size, size_t min_size);
 
 /* Pools should be used through these macros: */
-#define pool_get_name(pool) (pool)->get_name(pool)
-#define pool_ref(pool) (pool)->ref(pool)
-#define pool_unref(pool) (pool)->unref(&(pool))
+#define pool_get_name(pool) (pool)->v->get_name(pool)
+#define pool_ref(pool) (pool)->v->ref(pool)
+#define pool_unref(pool) (pool)->v->unref(&(pool))
 
 #define p_new(pool, type, count) \
 	((type *) p_malloc(pool, sizeof(type) * (count)))
 
-#define p_malloc(pool, size) (pool)->malloc(pool, size)
+#define p_malloc(pool, size) (pool)->v->malloc(pool, size)
 #define p_realloc(pool, mem, old_size, new_size) \
-	(pool)->realloc(pool, mem, old_size, new_size)
+	(pool)->v->realloc(pool, mem, old_size, new_size)
 
 /* Free the memory. Currently it also sets memory to NULL, but that shouldn't
    be relied on as it's only an extra safety check. It might as well be later
@@ -86,15 +90,16 @@
    in some "optimization".. */
 #define p_free(pool, mem) \
 	STMT_START { \
-          (pool)->free(pool, mem); \
+          (pool)->v->free(pool, mem); \
           (mem) = NULL; \
 	} STMT_END
 
 /* A macro that's guaranteed to set mem = NULL. */
 #define p_free_and_null(pool, mem) p_free(pool, mem)
 
-#define p_clear(pool) (pool)->clear(pool)
+#define p_clear(pool) (pool)->v->clear(pool)
 
-#define p_get_max_easy_alloc_size(pool) (pool)->get_max_easy_alloc_size(pool)
+#define p_get_max_easy_alloc_size(pool) \
+	(pool)->v->get_max_easy_alloc_size(pool)
 
 #endif