summaryrefslogtreecommitdiff
path: root/src/daemon/memorypool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/memorypool.c')
-rw-r--r--src/daemon/memorypool.c147
1 files changed, 77 insertions, 70 deletions
diff --git a/src/daemon/memorypool.c b/src/daemon/memorypool.c
index d8e835e8..37cc50c1 100644
--- a/src/daemon/memorypool.c
+++ b/src/daemon/memorypool.c
@@ -26,12 +26,13 @@
#include "memorypool.h"
-struct MemoryPool {
+struct MemoryPool
+{
/**
* Pointer to the pool's memory
- */
- char * memory;
+ */
+ char *memory;
/**
* Size of the pool.
@@ -59,25 +60,30 @@ struct MemoryPool {
*
* @param max maximum size of the pool
*/
-struct MemoryPool * MHD_pool_create(unsigned int max) {
- struct MemoryPool * pool;
+struct MemoryPool *
+MHD_pool_create (unsigned int max)
+{
+ struct MemoryPool *pool;
- pool = malloc(sizeof(struct MemoryPool));
+ pool = malloc (sizeof (struct MemoryPool));
if (pool == NULL)
return NULL;
- pool->memory = MMAP(NULL, max, PROT_READ | PROT_WRITE,
- MAP_ANONYMOUS, -1, 0);
- if ( (pool->memory == MAP_FAILED) ||
- (pool->memory == NULL) ) {
- pool->memory = malloc(max);
- if (pool->memory == NULL) {
- free(pool);
- return NULL;
+ pool->memory = MMAP (NULL, max, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS, -1, 0);
+ if ((pool->memory == MAP_FAILED) || (pool->memory == NULL))
+ {
+ pool->memory = malloc (max);
+ if (pool->memory == NULL)
+ {
+ free (pool);
+ return NULL;
+ }
+ pool->is_mmap = 0;
+ }
+ else
+ {
+ pool->is_mmap = 1;
}
- pool->is_mmap = 0;
- } else {
- pool->is_mmap = 1;
- }
pool->pos = 0;
pool->end = max;
pool->size = max;
@@ -87,14 +93,16 @@ struct MemoryPool * MHD_pool_create(unsigned int max) {
/**
* Destroy a memory pool.
*/
-void MHD_pool_destroy(struct MemoryPool * pool) {
+void
+MHD_pool_destroy (struct MemoryPool *pool)
+{
if (pool == NULL)
return;
if (pool->is_mmap == 0)
- free(pool->memory);
+ free (pool->memory);
else
- MUNMAP(pool->memory, pool->size);
- free(pool);
+ MUNMAP (pool->memory, pool->size);
+ free (pool);
}
/**
@@ -102,21 +110,23 @@ void MHD_pool_destroy(struct MemoryPool * pool) {
* @return NULL if the pool cannot support size more
* bytes
*/
-void * MHD_pool_allocate(struct MemoryPool * pool,
- unsigned int size,
- int from_end) {
- void * ret;
+void *
+MHD_pool_allocate (struct MemoryPool *pool, unsigned int size, int from_end)
+{
+ void *ret;
- if ( (pool->pos + size > pool->end) ||
- (pool->pos + size < pool->pos) )
+ if ((pool->pos + size > pool->end) || (pool->pos + size < pool->pos))
return NULL;
- if (from_end == MHD_YES) {
- ret = &pool->memory[pool->end - size];
- pool->end -= size;
- } else {
- ret = &pool->memory[pool->pos];
- pool->pos += size;
- }
+ if (from_end == MHD_YES)
+ {
+ ret = &pool->memory[pool->end - size];
+ pool->end -= size;
+ }
+ else
+ {
+ ret = &pool->memory[pool->pos];
+ pool->pos += size;
+ }
return ret;
}
@@ -136,43 +146,40 @@ void * MHD_pool_allocate(struct MemoryPool * pool,
* NULL if the pool cannot support new_size
* bytes (old continues to be valid for old_size)
*/
-void * MHD_pool_reallocate(struct MemoryPool * pool,
- void * old,
- unsigned int old_size,
- unsigned int new_size) {
- void * ret;
-
- if ( (pool->end < old_size) ||
- (pool->end < new_size) )
- return NULL; /* unsatisfiable or bogus request */
-
- if ( (pool->pos >= old_size) &&
- (&pool->memory[pool->pos - old_size] == old) ) {
- /* was the previous allocation - optimize! */
- if (pool->pos + new_size - old_size <= pool->end) {
- /* fits */
- pool->pos += new_size - old_size;
- if (new_size < old_size) /* shrinking - zero again! */
- memset(&pool->memory[pool->pos],
- 0,
- old_size - new_size);
- return old;
+void *
+MHD_pool_reallocate (struct MemoryPool *pool,
+ void *old, unsigned int old_size, unsigned int new_size)
+{
+ void *ret;
+
+ if ((pool->end < old_size) || (pool->end < new_size))
+ return NULL; /* unsatisfiable or bogus request */
+
+ if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old))
+ {
+ /* was the previous allocation - optimize! */
+ if (pool->pos + new_size - old_size <= pool->end)
+ {
+ /* fits */
+ pool->pos += new_size - old_size;
+ if (new_size < old_size) /* shrinking - zero again! */
+ memset (&pool->memory[pool->pos], 0, old_size - new_size);
+ return old;
+ }
+ /* does not fit */
+ return NULL;
}
- /* does not fit */
- return NULL;
- }
if (new_size <= old_size)
- return old; /* cannot shrink, no need to move */
- if ( (pool->pos + new_size >= pool->pos) &&
- (pool->pos + new_size <= pool->end) ) {
- /* fits */
- ret = &pool->memory[pool->pos];
- memcpy(ret,
- old,
- old_size);
- pool->pos += new_size;
- return ret;
- }
+ return old; /* cannot shrink, no need to move */
+ if ((pool->pos + new_size >= pool->pos) &&
+ (pool->pos + new_size <= pool->end))
+ {
+ /* fits */
+ ret = &pool->memory[pool->pos];
+ memcpy (ret, old, old_size);
+ pool->pos += new_size;
+ return ret;
+ }
/* does not fit */
return NULL;
}