diff options
Diffstat (limited to 'src/microhttpd/memorypool.c')
-rw-r--r-- | src/microhttpd/memorypool.c | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c index c5e5b4fd..fb6c0652 100644 --- a/src/microhttpd/memorypool.c +++ b/src/microhttpd/memorypool.c | |||
@@ -105,22 +105,6 @@ MHD_init_mem_pools_ (void) | |||
105 | 105 | ||
106 | 106 | ||
107 | /** | 107 | /** |
108 | * Get the real size that would be allocated by the memory pool when | ||
109 | * requested to allocate @a size. | ||
110 | * @param size the size of memory area that would be rounded up to the | ||
111 | * allocation granularity | ||
112 | * @return the size that would be allocated by #MHD_pool_allocate() when | ||
113 | * requested to allocate @a size. It is also minimal size of free | ||
114 | * space in the pool required to #MHD_pool_allocate() succeed. | ||
115 | */ | ||
116 | size_t | ||
117 | MHD_pool_alloc_size (size_t size) | ||
118 | { | ||
119 | return ROUND_TO_ALIGN (size); | ||
120 | } | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Handle for a memory pool. Pools are not reentrant and must not be | 108 | * Handle for a memory pool. Pools are not reentrant and must not be |
125 | * used by multiple threads. | 109 | * used by multiple threads. |
126 | */ | 110 | */ |
@@ -312,6 +296,56 @@ MHD_pool_allocate (struct MemoryPool *pool, | |||
312 | 296 | ||
313 | 297 | ||
314 | /** | 298 | /** |
299 | * Try to allocate @a size bytes memory area from the @a pool. | ||
300 | * | ||
301 | * If allocation fails, @a required_bytes is updated with size required to be | ||
302 | * freed in the @a pool from relocatable area to allocate requested number | ||
303 | * of bytes. | ||
304 | * Allocated memory area is always not rellocatable ("from end"). | ||
305 | * | ||
306 | * @param pool memory pool to use for the operation | ||
307 | * @param size the size of memory in bytes to allocate | ||
308 | * @param[out] required_bytes the pointer to variable to be updated with | ||
309 | * the size of the required additional free | ||
310 | * memory area, not updated if function succeed. | ||
311 | * Cannot be NULL. | ||
312 | * @return the pointer to allocated memory area if succeed, | ||
313 | * NULL if the pool doesn't have enough space, required_bytes is updated | ||
314 | * with amount of space needed to be freed in relocatable area or | ||
315 | * set to SIZE_MAX if requested size is too large for the pool. | ||
316 | */ | ||
317 | void * | ||
318 | MHD_pool_try_alloc (struct MemoryPool *pool, | ||
319 | size_t size, | ||
320 | size_t *required_bytes) | ||
321 | { | ||
322 | void *ret; | ||
323 | size_t asize; | ||
324 | |||
325 | mhd_assert (pool->end >= pool->pos); | ||
326 | mhd_assert (pool->size >= pool->end - pool->pos); | ||
327 | asize = ROUND_TO_ALIGN (size); | ||
328 | if ( (0 == asize) && (0 != size) ) | ||
329 | { /* size is too close to SIZE_MAX, very unlikely */ | ||
330 | *required_bytes = SIZE_MAX; | ||
331 | return NULL; | ||
332 | } | ||
333 | if ( (pool->pos + asize > pool->end) || | ||
334 | (pool->pos + asize < pool->pos)) | ||
335 | { | ||
336 | if (asize <= pool->end) | ||
337 | *required_bytes = asize - (pool->end - pool->pos); | ||
338 | else | ||
339 | *required_bytes = SIZE_MAX; | ||
340 | return NULL; | ||
341 | } | ||
342 | ret = &pool->memory[pool->end - asize]; | ||
343 | pool->end -= asize; | ||
344 | return ret; | ||
345 | } | ||
346 | |||
347 | |||
348 | /** | ||
315 | * Reallocate a block of memory obtained from the pool. | 349 | * Reallocate a block of memory obtained from the pool. |
316 | * This is particularly efficient when growing or | 350 | * This is particularly efficient when growing or |
317 | * shrinking the block that was last (re)allocated. | 351 | * shrinking the block that was last (re)allocated. |
@@ -355,7 +389,7 @@ MHD_pool_reallocate (struct MemoryPool *pool, | |||
355 | pool->end); | 389 | pool->end); |
356 | 390 | ||
357 | if (0 != old_size) | 391 | if (0 != old_size) |
358 | { /* Need to save some data */ | 392 | { /* Have previously allocated data */ |
359 | const size_t old_offset = (uint8_t*) old - pool->memory; | 393 | const size_t old_offset = (uint8_t*) old - pool->memory; |
360 | const bool shrinking = (old_size > new_size); | 394 | const bool shrinking = (old_size > new_size); |
361 | /* Try resizing in-place */ | 395 | /* Try resizing in-place */ |