aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/connection.c')
-rw-r--r--src/microhttpd/connection.c51
1 files changed, 24 insertions, 27 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index a9619575..536bb28f 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -204,16 +204,12 @@ connection_alloc_memory (struct MHD_Connection *connection,
204{ 204{
205 struct MHD_Connection *const c = connection; /* a short alias */ 205 struct MHD_Connection *const c = connection; /* a short alias */
206 struct MemoryPool *const pool = c->pool; /* a short alias */ 206 struct MemoryPool *const pool = c->pool; /* a short alias */
207 size_t required_free_size; /**< The required amount of free memory */ 207 size_t need_to_free; /**< The required amount of free memory */
208 size_t pool_free; /**< The amount of free memory in the pool */
209 void *res; 208 void *res;
210 209
211 required_free_size = MHD_pool_alloc_size (size); 210 res = MHD_pool_try_alloc (pool, size, &need_to_free);
212 pool_free = MHD_pool_get_free (pool); 211 if (NULL == res)
213 if (pool_free < required_free_size)
214 { 212 {
215 size_t need_to_free = required_free_size - pool_free;
216 mhd_assert (MHD_pool_alloc_size (need_to_free) == need_to_free);
217 if (NULL != c->write_buffer) 213 if (NULL != c->write_buffer)
218 { 214 {
219 /* The connection is in the sending phase */ 215 /* The connection is in the sending phase */
@@ -227,12 +223,10 @@ connection_alloc_memory (struct MHD_Connection *connection,
227 c->write_buffer_size, 223 c->write_buffer_size,
228 new_buf_size); 224 new_buf_size);
229 mhd_assert (c->write_buffer == buf); 225 mhd_assert (c->write_buffer == buf);
230#ifdef NDEBUG
231 (void) buf; /* mute compiler warning */
232#endif
233 mhd_assert (c->write_buffer_append_offset <= new_buf_size); 226 mhd_assert (c->write_buffer_append_offset <= new_buf_size);
234 mhd_assert (c->write_buffer_send_offset <= new_buf_size); 227 mhd_assert (c->write_buffer_send_offset <= new_buf_size);
235 c->write_buffer_size = new_buf_size; 228 c->write_buffer_size = new_buf_size;
229 c->write_buffer = buf;
236 } 230 }
237 else 231 else
238 return NULL; 232 return NULL;
@@ -249,20 +243,18 @@ connection_alloc_memory (struct MHD_Connection *connection,
249 c->read_buffer_size, 243 c->read_buffer_size,
250 new_buf_size); 244 new_buf_size);
251 mhd_assert (c->read_buffer == buf); 245 mhd_assert (c->read_buffer == buf);
252#ifdef NDEBUG
253 (void) buf; /* mute compiler warning */
254#endif
255 mhd_assert (c->read_buffer_offset <= new_buf_size); 246 mhd_assert (c->read_buffer_offset <= new_buf_size);
256 c->read_buffer_size = new_buf_size; 247 c->read_buffer_size = new_buf_size;
248 c->read_buffer = buf;
257 } 249 }
258 else 250 else
259 return NULL; 251 return NULL;
260 } 252 }
261 else 253 else
262 return NULL; 254 return NULL;
255 res = MHD_pool_allocate (pool, size, true);
256 mhd_assert (NULL != res); /* It has been checked that pool has enough space */
263 } 257 }
264 res = MHD_pool_allocate (pool, size, true);
265 mhd_assert (NULL != res); /* It has been checked that pool has enough space */
266 return res; 258 return res;
267} 259}
268 260
@@ -1486,7 +1478,7 @@ try_grow_read_buffer (struct MHD_Connection *connection,
1486 1478
1487 1479
1488/** 1480/**
1489 * Shrink connection read buffer to the zero of data in the buffer 1481 * Shrink connection read buffer to the zero size of free space in the buffer
1490 * @param connection the connection whose read buffer is being manipulated 1482 * @param connection the connection whose read buffer is being manipulated
1491 */ 1483 */
1492static void 1484static void
@@ -1495,11 +1487,10 @@ connection_shrink_read_buffer (struct MHD_Connection *connection)
1495 struct MHD_Connection *const c = connection; /**< a short alias */ 1487 struct MHD_Connection *const c = connection; /**< a short alias */
1496 void *new_buf; 1488 void *new_buf;
1497 1489
1498 if (NULL == c->read_buffer) 1490 if ((NULL == c->read_buffer) || (0 == c->read_buffer_size))
1499 { 1491 {
1500 mhd_assert (0 == c->read_buffer_size); 1492 mhd_assert (0 == c->read_buffer_size);
1501 mhd_assert (0 == c->read_buffer_offset); 1493 mhd_assert (0 == c->read_buffer_offset);
1502 c->read_buffer = NULL;
1503 return; 1494 return;
1504 } 1495 }
1505 1496
@@ -1507,12 +1498,8 @@ connection_shrink_read_buffer (struct MHD_Connection *connection)
1507 new_buf = MHD_pool_reallocate (c->pool, c->read_buffer, c->read_buffer_size, 1498 new_buf = MHD_pool_reallocate (c->pool, c->read_buffer, c->read_buffer_size,
1508 c->read_buffer_offset); 1499 c->read_buffer_offset);
1509 mhd_assert (c->read_buffer == new_buf); 1500 mhd_assert (c->read_buffer == new_buf);
1510#ifdef NDEBUG 1501 c->read_buffer = new_buf;
1511 (void) new_buf; /* squash compiler warning */
1512#endif /* NDEBUG */
1513 c->read_buffer_size = c->read_buffer_offset; 1502 c->read_buffer_size = c->read_buffer_offset;
1514 if (0 == c->read_buffer_size)
1515 c->read_buffer = NULL;
1516} 1503}
1517 1504
1518 1505
@@ -1582,16 +1569,25 @@ connection_shrink_write_buffer (struct MHD_Connection *connection)
1582 mhd_assert (c->write_buffer_append_offset >= c->write_buffer_send_offset); 1569 mhd_assert (c->write_buffer_append_offset >= c->write_buffer_send_offset);
1583 mhd_assert (c->write_buffer_size >= c->write_buffer_append_offset); 1570 mhd_assert (c->write_buffer_size >= c->write_buffer_append_offset);
1584 1571
1585 if (NULL == c->write_buffer) 1572 if ( (NULL == c->write_buffer) || (0 == c->write_buffer_size))
1573 {
1574 mhd_assert (0 == c->write_buffer_append_offset);
1575 mhd_assert (0 == c->write_buffer_send_offset);
1576 c->write_buffer = NULL;
1586 return; 1577 return;
1578 }
1587 if (c->write_buffer_append_offset == c->write_buffer_size) 1579 if (c->write_buffer_append_offset == c->write_buffer_size)
1588 return; 1580 return;
1589 1581
1590 new_buf = MHD_pool_reallocate (pool, c->write_buffer, c->write_buffer_size, 1582 new_buf = MHD_pool_reallocate (pool, c->write_buffer, c->write_buffer_size,
1591 c->write_buffer_append_offset); 1583 c->write_buffer_append_offset);
1592 mhd_assert (c->write_buffer == new_buf); 1584 mhd_assert ((c->write_buffer == new_buf) || \
1593 (void) new_buf; /* squash compiler warning */ 1585 (0 == c->write_buffer_append_offset));
1594 c->write_buffer_size = c->write_buffer_append_offset; 1586 c->write_buffer_size = c->write_buffer_append_offset;
1587 if (0 == c->write_buffer_size)
1588 c->write_buffer = NULL;
1589 else
1590 c->write_buffer = new_buf;
1595} 1591}
1596 1592
1597 1593
@@ -1987,8 +1983,9 @@ build_header_response (struct MHD_Connection *connection)
1987 buf = c->write_buffer; 1983 buf = c->write_buffer;
1988 pos = c->write_buffer_append_offset; 1984 pos = c->write_buffer_append_offset;
1989 buf_size = c->write_buffer_size; 1985 buf_size = c->write_buffer_size;
1990 if ((NULL == buf) || (0 == buf_size)) 1986 if (0 == buf_size)
1991 return MHD_NO; 1987 return MHD_NO;
1988 mhd_assert (NULL != buf);
1992 1989
1993 /* * The status line * */ 1990 /* * The status line * */
1994 1991