aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-11-28 10:05:52 +0000
committerChristian Grothoff <christian@grothoff.org>2013-11-28 10:05:52 +0000
commitba6b6cad9a70cef22f62d5b76cb1180b2aca4e94 (patch)
tree3c897bb1fc03df1bb0dab22355095c78c09b05ab /src
parent2c771abfa30534aaa7435281d817f643548aedf7 (diff)
downloadlibmicrohttpd-ba6b6cad9a70cef22f62d5b76cb1180b2aca4e94.tar.gz
libmicrohttpd-ba6b6cad9a70cef22f62d5b76cb1180b2aca4e94.zip
-handle case that original allocation request was zero
Diffstat (limited to 'src')
-rw-r--r--src/microhttpd/memorypool.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c
index f0115328..57962c17 100644
--- a/src/microhttpd/memorypool.c
+++ b/src/microhttpd/memorypool.c
@@ -89,7 +89,7 @@ MHD_pool_create (size_t max)
89 struct MemoryPool *pool; 89 struct MemoryPool *pool;
90 90
91 pool = malloc (sizeof (struct MemoryPool)); 91 pool = malloc (sizeof (struct MemoryPool));
92 if (pool == NULL) 92 if (NULL == pool)
93 return NULL; 93 return NULL;
94#ifdef MAP_ANONYMOUS 94#ifdef MAP_ANONYMOUS
95 if (max <= 32 * 1024) 95 if (max <= 32 * 1024)
@@ -155,21 +155,22 @@ MHD_pool_allocate (struct MemoryPool *pool,
155 size_t size, int from_end) 155 size_t size, int from_end)
156{ 156{
157 void *ret; 157 void *ret;
158 size_t asize;
158 159
159 size = ROUND_TO_ALIGN (size); 160 asize = ROUND_TO_ALIGN (size);
160 if (0 == size) 161 if ( (0 == asize) && (0 != size) )
161 return NULL; /* size too close to SIZE_MAX */ 162 return NULL; /* size too close to SIZE_MAX */
162 if ((pool->pos + size > pool->end) || (pool->pos + size < pool->pos)) 163 if ((pool->pos + asize > pool->end) || (pool->pos + asize < pool->pos))
163 return NULL; 164 return NULL;
164 if (from_end == MHD_YES) 165 if (from_end == MHD_YES)
165 { 166 {
166 ret = &pool->memory[pool->end - size]; 167 ret = &pool->memory[pool->end - asize];
167 pool->end -= size; 168 pool->end -= asize;
168 } 169 }
169 else 170 else
170 { 171 {
171 ret = &pool->memory[pool->pos]; 172 ret = &pool->memory[pool->pos];
172 pool->pos += size; 173 pool->pos += asize;
173 } 174 }
174 return ret; 175 return ret;
175} 176}
@@ -199,36 +200,37 @@ MHD_pool_reallocate (struct MemoryPool *pool,
199 size_t new_size) 200 size_t new_size)
200{ 201{
201 void *ret; 202 void *ret;
203 size_t asize;
202 204
203 new_size = ROUND_TO_ALIGN (new_size); 205 asize = ROUND_TO_ALIGN (new_size);
204 if (0 == new_size) 206 if ( (0 == asize) && (0 != new_size) )
205 return NULL; /* size too close to SIZE_MAX */ 207 return NULL; /* new_size too close to SIZE_MAX */
206 if ((pool->end < old_size) || (pool->end < new_size)) 208 if ((pool->end < old_size) || (pool->end < asize))
207 return NULL; /* unsatisfiable or bogus request */ 209 return NULL; /* unsatisfiable or bogus request */
208 210
209 if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old)) 211 if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old))
210 { 212 {
211 /* was the previous allocation - optimize! */ 213 /* was the previous allocation - optimize! */
212 if (pool->pos + new_size - old_size <= pool->end) 214 if (pool->pos + asize - old_size <= pool->end)
213 { 215 {
214 /* fits */ 216 /* fits */
215 pool->pos += new_size - old_size; 217 pool->pos += asize - old_size;
216 if (new_size < old_size) /* shrinking - zero again! */ 218 if (asize < old_size) /* shrinking - zero again! */
217 memset (&pool->memory[pool->pos], 0, old_size - new_size); 219 memset (&pool->memory[pool->pos], 0, old_size - asize);
218 return old; 220 return old;
219 } 221 }
220 /* does not fit */ 222 /* does not fit */
221 return NULL; 223 return NULL;
222 } 224 }
223 if (new_size <= old_size) 225 if (asize <= old_size)
224 return old; /* cannot shrink, no need to move */ 226 return old; /* cannot shrink, no need to move */
225 if ((pool->pos + new_size >= pool->pos) && 227 if ((pool->pos + asize >= pool->pos) &&
226 (pool->pos + new_size <= pool->end)) 228 (pool->pos + asize <= pool->end))
227 { 229 {
228 /* fits */ 230 /* fits */
229 ret = &pool->memory[pool->pos]; 231 ret = &pool->memory[pool->pos];
230 memcpy (ret, old, old_size); 232 memcpy (ret, old, old_size);
231 pool->pos += new_size; 233 pool->pos += asize;
232 return ret; 234 return ret;
233 } 235 }
234 /* does not fit */ 236 /* does not fit */