summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-05-30 15:40:46 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-05-30 15:40:46 -0400
commitf61c7c75c3c7ff4a329315700d3efa1d77bafa6d (patch)
tree9fc9bab72b12644aed948a82602638273cc82d62 /libdimension
parent77f880207d4fc37f82299d78d04be13623d9da0a (diff)
downloaddimension-f61c7c75c3c7ff4a329315700d3efa1d77bafa6d.tar.xz
pool: Separate dmnsn_palloc and dmnsn_palloc_tidy() APIs.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/dimension/pool.h23
-rw-r--r--libdimension/pool.c16
-rw-r--r--libdimension/tests/pool.c8
3 files changed, 35 insertions, 12 deletions
diff --git a/libdimension/dimension/pool.h b/libdimension/dimension/pool.h
index 33f1dd7..164bbbc 100644
--- a/libdimension/dimension/pool.h
+++ b/libdimension/dimension/pool.h
@@ -40,18 +40,35 @@ dmnsn_pool *dmnsn_new_pool(void);
* Allocate some memory from a pool.
* @param[in] pool The memory pool to allocate from.
* @param[in] size The size of the memory block to allocate.
- * @param[in] callback An optional callback to invoke before the memory is freed.
* @return The allocated memory area.
*/
-void *dmnsn_palloc(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *callback);
+void *dmnsn_palloc(dmnsn_pool *pool, size_t size);
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] size The size of the memory block to allocate.
+ * @param[in] cleanup_fn A callback to invoke before the memory is freed.
+ * @return The allocated memory area.
+ */
+void *dmnsn_palloc_tidy(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *cleanup_fn);
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] type The type of the memory block to allocate.
+ * @return The allocated memory area.
+ */
+#define DMNSN_PALLOC(pool, type) ((type *)dmnsn_palloc((pool), sizeof(type)))
/**
* Allocate some memory from a pool.
* @param[in] pool The memory pool to allocate from.
* @param[in] type The type of the memory block to allocate.
+ * @param[in] cleanup_fn A callback to invoke before the memory is freed.
* @return The allocated memory area.
*/
-#define DMNSN_PALLOC(pool, type) ((type *)dmnsn_palloc((pool), sizeof(type), NULL))
+#define DMNSN_PALLOC_TIDY(pool, type, cleanup_fn) ((type *)dmnsn_palloc_tidy((pool), sizeof(type), (cleanup_fn)))
/**
* Free a memory pool and all associated allocations.
diff --git a/libdimension/pool.c b/libdimension/pool.c
index a0db161..db6be14 100644
--- a/libdimension/pool.c
+++ b/libdimension/pool.c
@@ -28,7 +28,7 @@
/** A single allocation and associated destructor. */
typedef struct dmnsn_allocation {
void *ptr;
- dmnsn_callback_fn *callback;
+ dmnsn_callback_fn *cleanup_fn;
} dmnsn_allocation;
/** Number of pointers per block, we want a block to fit in a single page. */
@@ -66,7 +66,13 @@ dmnsn_new_pool(void)
}
void *
-dmnsn_palloc(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *callback)
+dmnsn_palloc(dmnsn_pool *pool, size_t size)
+{
+ return dmnsn_palloc_tidy(pool, size, NULL);
+}
+
+void *
+dmnsn_palloc_tidy(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *cleanup_fn)
{
dmnsn_pool_block *old_block = pthread_getspecific(pool->thread_block);
@@ -78,7 +84,7 @@ dmnsn_palloc(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *callback)
dmnsn_allocation *alloc = new_block->allocs + new_block->i;
void *result = alloc->ptr = dmnsn_malloc(size);
- alloc->callback = callback;
+ alloc->cleanup_fn = cleanup_fn;
++new_block->i;
if (dmnsn_unlikely(new_block != old_block)) {
@@ -105,8 +111,8 @@ dmnsn_delete_pool(dmnsn_pool *pool)
/* Free all the allocations in reverse order */
for (size_t i = block->i; i-- > 0;) {
dmnsn_allocation *alloc = block->allocs + i;
- if (alloc->callback) {
- alloc->callback(alloc->ptr);
+ if (alloc->cleanup_fn) {
+ alloc->cleanup_fn(alloc->ptr);
}
dmnsn_free(alloc->ptr);
}
diff --git a/libdimension/tests/pool.c b/libdimension/tests/pool.c
index e135ce4..d12850d 100644
--- a/libdimension/tests/pool.c
+++ b/libdimension/tests/pool.c
@@ -59,10 +59,10 @@ callback(void *ptr)
DMNSN_TEST(pool, callback)
{
- dmnsn_palloc(pool, sizeof(int), NULL);
- dmnsn_palloc(pool, sizeof(int), callback);
- dmnsn_palloc(pool, sizeof(int), callback);
- dmnsn_palloc(pool, sizeof(int), NULL);
+ DMNSN_PALLOC_TIDY(pool, int, NULL);
+ DMNSN_PALLOC_TIDY(pool, int, callback);
+ DMNSN_PALLOC_TIDY(pool, int, callback);
+ DMNSN_PALLOC_TIDY(pool, int, NULL);
dmnsn_delete_pool(pool);
pool = NULL;