[PATCH v2 3/5] mem-pool: fill out functionality

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Adds the following functionality to memory pools:

 - Lifecycle management functions (init, discard)

 - Test whether a memory location is part of the managed pool

 - Function to combine 2 pools

This also adds logic to track all memory allocations made by a memory
pool.

These functions will be used in a future commit in this commit series.

Signed-off-by: Jameson Miller <jamill@xxxxxxxxxxxxx>
---
 mem-pool.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 mem-pool.h |  32 +++++++++++++++++
 2 files changed, 142 insertions(+), 4 deletions(-)

diff --git a/mem-pool.c b/mem-pool.c
index 389d7af447..a495885c4b 100644
--- a/mem-pool.c
+++ b/mem-pool.c
@@ -5,6 +5,8 @@
 #include "cache.h"
 #include "mem-pool.h"
 
+#define BLOCK_GROWTH_SIZE 1024*1024 - sizeof(struct mp_block);
+
 static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc)
 {
 	struct mp_block *p;
@@ -19,6 +21,60 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b
 	return p;
 }
 
+static void *mem_pool_alloc_custom(struct mem_pool *mem_pool, size_t block_alloc)
+{
+	char *p;
+	ALLOC_GROW(mem_pool->custom, mem_pool->nr + 1, mem_pool->alloc);
+	ALLOC_GROW(mem_pool->custom_end, mem_pool->nr_end + 1, mem_pool->alloc_end);
+
+	p = xmalloc(block_alloc);
+	mem_pool->custom[mem_pool->nr++] = p;
+	mem_pool->custom_end[mem_pool->nr_end++] = p + block_alloc;
+
+	mem_pool->pool_alloc += block_alloc;
+
+	return mem_pool->custom[mem_pool->nr];
+}
+
+void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size)
+{
+	if (!(*mem_pool))
+	{
+		*mem_pool = xmalloc(sizeof(struct mem_pool));
+		(*mem_pool)->pool_alloc = 0;
+		(*mem_pool)->mp_block = NULL;
+		(*mem_pool)->block_alloc = BLOCK_GROWTH_SIZE;
+		(*mem_pool)->custom = NULL;
+		(*mem_pool)->nr = 0;
+		(*mem_pool)->alloc = 0;
+		(*mem_pool)->custom_end = NULL;
+		(*mem_pool)->nr_end = 0;
+		(*mem_pool)->alloc_end = 0;
+
+		if (initial_size > 0)
+			mem_pool_alloc_block(*mem_pool, initial_size);
+	}
+}
+
+void mem_pool_discard(struct mem_pool *mem_pool)
+{
+	int i;
+	struct mp_block *block, *block_to_free;
+	for (block = mem_pool->mp_block; block;)
+	{
+		block_to_free = block;
+		block = block->next_block;
+		free(block_to_free);
+	}
+
+	for (i = 0; i < mem_pool->nr; i++)
+		free(mem_pool->custom[i]);
+
+	free(mem_pool->custom);
+	free(mem_pool->custom_end);
+	free(mem_pool);
+}
+
 void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
 {
 	struct mp_block *p;
@@ -33,10 +89,8 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
 			break;
 
 	if (!p) {
-		if (len >= (mem_pool->block_alloc / 2)) {
-			mem_pool->pool_alloc += len;
-			return xmalloc(len);
-		}
+		if (len >= (mem_pool->block_alloc / 2))
+			return mem_pool_alloc_custom(mem_pool, len);
 
 		p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc);
 	}
@@ -53,3 +107,55 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
 	memset(r, 0, len);
 	return r;
 }
+
+int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
+{
+	int i;
+	struct mp_block *p;
+
+	/* Check if memory is allocated in a block */
+	for (p = mem_pool->mp_block; p; p = p->next_block)
+		if ((mem >= ((void *)p->space)) &&
+		    (mem < ((void *)p->end)))
+			return 1;
+
+	/* Check if memory is allocated in custom block */
+	for (i = 0; i < mem_pool->nr; i++)
+		if ((mem >= mem_pool->custom[i]) &&
+		    (mem < mem_pool->custom_end[i]))
+			return 1;
+
+	return 0;
+}
+
+void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
+{
+	int i;
+	struct mp_block **tail = &dst->mp_block;
+
+	/* Find pointer of dst's last block (if any) */
+	while (*tail)
+		tail = &(*tail)->next_block;
+
+	/* Append the blocks from src to dst */
+	*tail = src->mp_block;
+
+	/* Combine custom allocations */
+	ALLOC_GROW(dst->custom, dst->nr + src->nr, dst->alloc);
+	ALLOC_GROW(dst->custom_end, dst->nr_end + src->nr_end, dst->alloc_end);
+
+	for (i = 0; i < src->nr; i++) {
+		dst->custom[dst->nr++] = src->custom[i];
+		dst->custom_end[dst->nr_end++] = src->custom_end[i];
+	}
+
+	dst->pool_alloc += src->pool_alloc;
+	src->pool_alloc = 0;
+	src->mp_block = NULL;
+	src->custom = NULL;
+	src->nr = 0;
+	src->alloc = 0;
+	src->custom_end = NULL;
+	src->nr_end = 0;
+	src->alloc_end = 0;
+}
diff --git a/mem-pool.h b/mem-pool.h
index 829ad58ecf..34df4fa709 100644
--- a/mem-pool.h
+++ b/mem-pool.h
@@ -19,8 +19,27 @@ struct mem_pool {
 
 	/* The total amount of memory allocated by the pool. */
 	size_t pool_alloc;
+
+	/*
+	 * Array of pointers to "custom size" memory allocations.
+	 * This is used for "large" memory allocations.
+	 * The *_end variables are used to track the range of memory
+	 * allocated.
+	 */
+	void **custom, **custom_end;
+	int nr, nr_end, alloc, alloc_end;
 };
 
+/*
+ * Initialize mem_pool specified initial.
+ */
+void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size);
+
+/*
+ * Discard a memory pool and free all the memory it is responsible for.
+ */
+void mem_pool_discard(struct mem_pool *mem_pool);
+
 /*
  * Alloc memory from the mem_pool.
  */
@@ -31,4 +50,17 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
  */
 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
 
+/*
+ * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
+ * pool will be empty and not contain any memory. It still needs to be free'd
+ * with a call to `mem_pool_discard`.
+ */
+void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src);
+
+/*
+ * Check if a memory pointed at by 'mem' is part of the range of
+ * memory managed by the specified mem_pool.
+ */
+int mem_pool_contains(struct mem_pool *mem_pool, void *mem);
+
 #endif
-- 
2.14.3





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux