aboutsummaryrefslogtreecommitdiff
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 @@
26 26
27#include "memorypool.h" 27#include "memorypool.h"
28 28
29struct MemoryPool { 29struct MemoryPool
30{
30 31
31 /** 32 /**
32 * Pointer to the pool's memory 33 * Pointer to the pool's memory
33 */ 34 */
34 char * memory; 35 char *memory;
35 36
36 /** 37 /**
37 * Size of the pool. 38 * Size of the pool.
@@ -59,25 +60,30 @@ struct MemoryPool {
59 * 60 *
60 * @param max maximum size of the pool 61 * @param max maximum size of the pool
61 */ 62 */
62struct MemoryPool * MHD_pool_create(unsigned int max) { 63struct MemoryPool *
63 struct MemoryPool * pool; 64MHD_pool_create (unsigned int max)
65{
66 struct MemoryPool *pool;
64 67
65 pool = malloc(sizeof(struct MemoryPool)); 68 pool = malloc (sizeof (struct MemoryPool));
66 if (pool == NULL) 69 if (pool == NULL)
67 return NULL; 70 return NULL;
68 pool->memory = MMAP(NULL, max, PROT_READ | PROT_WRITE, 71 pool->memory = MMAP (NULL, max, PROT_READ | PROT_WRITE,
69 MAP_ANONYMOUS, -1, 0); 72 MAP_ANONYMOUS, -1, 0);
70 if ( (pool->memory == MAP_FAILED) || 73 if ((pool->memory == MAP_FAILED) || (pool->memory == NULL))
71 (pool->memory == NULL) ) { 74 {
72 pool->memory = malloc(max); 75 pool->memory = malloc (max);
73 if (pool->memory == NULL) { 76 if (pool->memory == NULL)
74 free(pool); 77 {
75 return NULL; 78 free (pool);
79 return NULL;
80 }
81 pool->is_mmap = 0;
82 }
83 else
84 {
85 pool->is_mmap = 1;
76 } 86 }
77 pool->is_mmap = 0;
78 } else {
79 pool->is_mmap = 1;
80 }
81 pool->pos = 0; 87 pool->pos = 0;
82 pool->end = max; 88 pool->end = max;
83 pool->size = max; 89 pool->size = max;
@@ -87,14 +93,16 @@ struct MemoryPool * MHD_pool_create(unsigned int max) {
87/** 93/**
88 * Destroy a memory pool. 94 * Destroy a memory pool.
89 */ 95 */
90void MHD_pool_destroy(struct MemoryPool * pool) { 96void
97MHD_pool_destroy (struct MemoryPool *pool)
98{
91 if (pool == NULL) 99 if (pool == NULL)
92 return; 100 return;
93 if (pool->is_mmap == 0) 101 if (pool->is_mmap == 0)
94 free(pool->memory); 102 free (pool->memory);
95 else 103 else
96 MUNMAP(pool->memory, pool->size); 104 MUNMAP (pool->memory, pool->size);
97 free(pool); 105 free (pool);
98} 106}
99 107
100/** 108/**
@@ -102,21 +110,23 @@ void MHD_pool_destroy(struct MemoryPool * pool) {
102 * @return NULL if the pool cannot support size more 110 * @return NULL if the pool cannot support size more
103 * bytes 111 * bytes
104 */ 112 */
105void * MHD_pool_allocate(struct MemoryPool * pool, 113void *
106 unsigned int size, 114MHD_pool_allocate (struct MemoryPool *pool, unsigned int size, int from_end)
107 int from_end) { 115{
108 void * ret; 116 void *ret;
109 117
110 if ( (pool->pos + size > pool->end) || 118 if ((pool->pos + size > pool->end) || (pool->pos + size < pool->pos))
111 (pool->pos + size < pool->pos) )
112 return NULL; 119 return NULL;
113 if (from_end == MHD_YES) { 120 if (from_end == MHD_YES)
114 ret = &pool->memory[pool->end - size]; 121 {
115 pool->end -= size; 122 ret = &pool->memory[pool->end - size];
116 } else { 123 pool->end -= size;
117 ret = &pool->memory[pool->pos]; 124 }
118 pool->pos += size; 125 else
119 } 126 {
127 ret = &pool->memory[pool->pos];
128 pool->pos += size;
129 }
120 return ret; 130 return ret;
121} 131}
122 132
@@ -136,43 +146,40 @@ void * MHD_pool_allocate(struct MemoryPool * pool,
136 * NULL if the pool cannot support new_size 146 * NULL if the pool cannot support new_size
137 * bytes (old continues to be valid for old_size) 147 * bytes (old continues to be valid for old_size)
138 */ 148 */
139void * MHD_pool_reallocate(struct MemoryPool * pool, 149void *
140 void * old, 150MHD_pool_reallocate (struct MemoryPool *pool,
141 unsigned int old_size, 151 void *old, unsigned int old_size, unsigned int new_size)
142 unsigned int new_size) { 152{
143 void * ret; 153 void *ret;
144 154
145 if ( (pool->end < old_size) || 155 if ((pool->end < old_size) || (pool->end < new_size))
146 (pool->end < new_size) ) 156 return NULL; /* unsatisfiable or bogus request */
147 return NULL; /* unsatisfiable or bogus request */ 157
148 158 if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old))
149 if ( (pool->pos >= old_size) && 159 {
150 (&pool->memory[pool->pos - old_size] == old) ) { 160 /* was the previous allocation - optimize! */
151 /* was the previous allocation - optimize! */ 161 if (pool->pos + new_size - old_size <= pool->end)
152 if (pool->pos + new_size - old_size <= pool->end) { 162 {
153 /* fits */ 163 /* fits */
154 pool->pos += new_size - old_size; 164 pool->pos += new_size - old_size;
155 if (new_size < old_size) /* shrinking - zero again! */ 165 if (new_size < old_size) /* shrinking - zero again! */
156 memset(&pool->memory[pool->pos], 166 memset (&pool->memory[pool->pos], 0, old_size - new_size);
157 0, 167 return old;
158 old_size - new_size); 168 }
159 return old; 169 /* does not fit */
170 return NULL;
160 } 171 }
161 /* does not fit */
162 return NULL;
163 }
164 if (new_size <= old_size) 172 if (new_size <= old_size)
165 return old; /* cannot shrink, no need to move */ 173 return old; /* cannot shrink, no need to move */
166 if ( (pool->pos + new_size >= pool->pos) && 174 if ((pool->pos + new_size >= pool->pos) &&
167 (pool->pos + new_size <= pool->end) ) { 175 (pool->pos + new_size <= pool->end))
168 /* fits */ 176 {
169 ret = &pool->memory[pool->pos]; 177 /* fits */
170 memcpy(ret, 178 ret = &pool->memory[pool->pos];
171 old, 179 memcpy (ret, old, old_size);
172 old_size); 180 pool->pos += new_size;
173 pool->pos += new_size; 181 return ret;
174 return ret; 182 }
175 }
176 /* does not fit */ 183 /* does not fit */
177 return NULL; 184 return NULL;
178} 185}