aboutsummaryrefslogtreecommitdiff
path: root/src/lib/memorypool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/memorypool.c')
-rw-r--r--src/lib/memorypool.c118
1 files changed, 59 insertions, 59 deletions
diff --git a/src/lib/memorypool.c b/src/lib/memorypool.c
index 8ef1e2d1..9f7472e1 100644
--- a/src/lib/memorypool.c
+++ b/src/lib/memorypool.c
@@ -25,11 +25,11 @@
25#include "memorypool.h" 25#include "memorypool.h"
26 26
27/* define MAP_ANONYMOUS for Mac OS X */ 27/* define MAP_ANONYMOUS for Mac OS X */
28#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) 28#if defined(MAP_ANON) && ! defined(MAP_ANONYMOUS)
29#define MAP_ANONYMOUS MAP_ANON 29#define MAP_ANONYMOUS MAP_ANON
30#endif 30#endif
31#ifndef MAP_FAILED 31#ifndef MAP_FAILED
32#define MAP_FAILED ((void*)-1) 32#define MAP_FAILED ((void*) -1)
33#endif 33#endif
34 34
35/** 35/**
@@ -40,7 +40,7 @@
40/** 40/**
41 * Round up 'n' to a multiple of ALIGN_SIZE. 41 * Round up 'n' to a multiple of ALIGN_SIZE.
42 */ 42 */
43#define ROUND_TO_ALIGN(n) ((n+(ALIGN_SIZE-1)) & (~(ALIGN_SIZE-1))) 43#define ROUND_TO_ALIGN(n) ((n + (ALIGN_SIZE - 1)) & (~(ALIGN_SIZE - 1)))
44 44
45 45
46/** 46/**
@@ -110,11 +110,11 @@ MHD_pool_create (size_t max)
110 if (max <= 32 * 1024) 110 if (max <= 32 * 1024)
111 pool->memory = MAP_FAILED; 111 pool->memory = MAP_FAILED;
112 else 112 else
113#if defined(MAP_ANONYMOUS) && !defined(_WIN32) 113#if defined(MAP_ANONYMOUS) && ! defined(_WIN32)
114 pool->memory = mmap (NULL, 114 pool->memory = mmap (NULL,
115 max, 115 max,
116 PROT_READ | PROT_WRITE, 116 PROT_READ | PROT_WRITE,
117 MAP_PRIVATE | MAP_ANONYMOUS, 117 MAP_PRIVATE | MAP_ANONYMOUS,
118 -1, 118 -1,
119 0); 119 0);
120#elif defined(_WIN32) 120#elif defined(_WIN32)
@@ -128,19 +128,19 @@ MHD_pool_create (size_t max)
128#endif 128#endif
129 if ( (MAP_FAILED == pool->memory) || 129 if ( (MAP_FAILED == pool->memory) ||
130 (NULL == pool->memory)) 130 (NULL == pool->memory))
131 {
132 pool->memory = malloc (max);
133 if (NULL == pool->memory)
131 { 134 {
132 pool->memory = malloc (max); 135 free (pool);
133 if (NULL == pool->memory) 136 return NULL;
134 {
135 free (pool);
136 return NULL;
137 }
138 pool->is_mmap = false;
139 } 137 }
138 pool->is_mmap = false;
139 }
140 else 140 else
141 { 141 {
142 pool->is_mmap = true; 142 pool->is_mmap = true;
143 } 143 }
144 pool->pos = 0; 144 pool->pos = 0;
145 pool->end = max; 145 pool->end = max;
146 pool->size = max; 146 pool->size = max;
@@ -161,7 +161,7 @@ MHD_pool_destroy (struct MemoryPool *pool)
161 if (! pool->is_mmap) 161 if (! pool->is_mmap)
162 free (pool->memory); 162 free (pool->memory);
163 else 163 else
164#if defined(MAP_ANONYMOUS) && !defined(_WIN32) 164#if defined(MAP_ANONYMOUS) && ! defined(_WIN32)
165 munmap (pool->memory, 165 munmap (pool->memory,
166 pool->size); 166 pool->size);
167#elif defined(_WIN32) 167#elif defined(_WIN32)
@@ -201,7 +201,7 @@ MHD_pool_get_free (struct MemoryPool *pool)
201 */ 201 */
202void * 202void *
203MHD_pool_allocate (struct MemoryPool *pool, 203MHD_pool_allocate (struct MemoryPool *pool,
204 size_t size, 204 size_t size,
205 int from_end) 205 int from_end)
206{ 206{
207 void *ret; 207 void *ret;
@@ -214,15 +214,15 @@ MHD_pool_allocate (struct MemoryPool *pool,
214 (pool->pos + asize < pool->pos)) 214 (pool->pos + asize < pool->pos))
215 return NULL; 215 return NULL;
216 if (from_end == MHD_YES) 216 if (from_end == MHD_YES)
217 { 217 {
218 ret = &pool->memory[pool->end - asize]; 218 ret = &pool->memory[pool->end - asize];
219 pool->end -= asize; 219 pool->end -= asize;
220 } 220 }
221 else 221 else
222 { 222 {
223 ret = &pool->memory[pool->pos]; 223 ret = &pool->memory[pool->pos];
224 pool->pos += asize; 224 pool->pos += asize;
225 } 225 }
226 return ret; 226 return ret;
227} 227}
228 228
@@ -247,8 +247,8 @@ MHD_pool_allocate (struct MemoryPool *pool,
247void * 247void *
248MHD_pool_reallocate (struct MemoryPool *pool, 248MHD_pool_reallocate (struct MemoryPool *pool,
249 void *old, 249 void *old,
250 size_t old_size, 250 size_t old_size,
251 size_t new_size) 251 size_t new_size)
252{ 252{
253 void *ret; 253 void *ret;
254 size_t asize; 254 size_t asize;
@@ -263,35 +263,35 @@ MHD_pool_reallocate (struct MemoryPool *pool,
263 263
264 if ( (pool->pos >= old_size) && 264 if ( (pool->pos >= old_size) &&
265 (&pool->memory[pool->pos - old_size] == old) ) 265 (&pool->memory[pool->pos - old_size] == old) )
266 {
267 /* was the previous allocation - optimize! */
268 if (pool->pos + asize - old_size <= pool->end)
266 { 269 {
267 /* was the previous allocation - optimize! */ 270 /* fits */
268 if (pool->pos + asize - old_size <= pool->end) 271 pool->pos += asize - old_size;
269 { 272 if (asize < old_size) /* shrinking - zero again! */
270 /* fits */ 273 memset (&pool->memory[pool->pos],
271 pool->pos += asize - old_size; 274 0,
272 if (asize < old_size) /* shrinking - zero again! */ 275 old_size - asize);
273 memset (&pool->memory[pool->pos], 276 return old;
274 0,
275 old_size - asize);
276 return old;
277 }
278 /* does not fit */
279 return NULL;
280 } 277 }
278 /* does not fit */
279 return NULL;
280 }
281 if (asize <= old_size) 281 if (asize <= old_size)
282 return old; /* cannot shrink, no need to move */ 282 return old; /* cannot shrink, no need to move */
283 if ((pool->pos + asize >= pool->pos) && 283 if ((pool->pos + asize >= pool->pos) &&
284 (pool->pos + asize <= pool->end)) 284 (pool->pos + asize <= pool->end))
285 { 285 {
286 /* fits */ 286 /* fits */
287 ret = &pool->memory[pool->pos]; 287 ret = &pool->memory[pool->pos];
288 if (0 != old_size) 288 if (0 != old_size)
289 memmove (ret, 289 memmove (ret,
290 old, 290 old,
291 old_size); 291 old_size);
292 pool->pos += asize; 292 pool->pos += asize;
293 return ret; 293 return ret;
294 } 294 }
295 /* does not fit */ 295 /* does not fit */
296 return NULL; 296 return NULL;
297} 297}
@@ -312,19 +312,19 @@ MHD_pool_reallocate (struct MemoryPool *pool,
312 */ 312 */
313void * 313void *
314MHD_pool_reset (struct MemoryPool *pool, 314MHD_pool_reset (struct MemoryPool *pool,
315 void *keep, 315 void *keep,
316 size_t copy_bytes, 316 size_t copy_bytes,
317 size_t new_size) 317 size_t new_size)
318{ 318{
319 if ( (NULL != keep) && 319 if ( (NULL != keep) &&
320 (keep != pool->memory) ) 320 (keep != pool->memory) )
321 { 321 {
322 if (0 != copy_bytes) 322 if (0 != copy_bytes)
323 memmove (pool->memory, 323 memmove (pool->memory,
324 keep, 324 keep,
325 copy_bytes); 325 copy_bytes);
326 keep = pool->memory; 326 keep = pool->memory;
327 } 327 }
328 pool->end = pool->size; 328 pool->end = pool->size;
329 /* technically not needed, but safer to zero out */ 329 /* technically not needed, but safer to zero out */
330 if (pool->size > copy_bytes) 330 if (pool->size > copy_bytes)