commit 3ed1e8de9ffabbcbe8c6c6ee5db1802d3f989e71
parent fcb0a20d91344e558b80a487f9d945695b61d66d
Author: Christian Grothoff <christian@grothoff.org>
Date: Fri, 6 Nov 2015 21:56:46 +0000
-fix shrinkage
Diffstat:
5 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -1,3 +1,6 @@
+Fri Nov 6 22:54:38 CET 2015
+ Fixing the buffer shrinkage issue, this time with test. -CG
+
Tue Nov 3 23:24:52 CET 2015
Undoing change from Sun Oct 25 15:29:23 CET 2015
as the original code was counter-intuitive but
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
@@ -130,7 +130,7 @@ typedef intptr_t ssize_t;
* Current version of the library.
* 0x01093001 = 1.9.30-1.
*/
-#define MHD_VERSION 0x00094600
+#define MHD_VERSION 0x00094601
/**
* MHD-internal return code for "YES".
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
@@ -2559,14 +2559,15 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
/* can try to keep-alive */
connection->version = NULL;
connection->state = MHD_CONNECTION_INIT;
- /* read_buffer_size is correct here, as we want to
- preserve the entire read buffer allocation, even
- if in terms of the data we only care to preserve
- up to "read_buffer_offset" */
+ /* Reset the read buffer to the starting size,
+ preserving the bytes we have already read. */
connection->read_buffer
= MHD_pool_reset (connection->pool,
connection->read_buffer,
- connection->read_buffer_size);
+ connection->read_buffer_offset,
+ connection->daemon->pool_size / 2);
+ connection->read_buffer_size
+ = connection->daemon->pool_size / 2;
}
connection->client_aware = MHD_NO;
connection->client_context = NULL;
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c
@@ -219,7 +219,8 @@ MHD_pool_reallocate (struct MemoryPool *pool,
if ((pool->end < old_size) || (pool->end < asize))
return NULL; /* unsatisfiable or bogus request */
- if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old))
+ if ( (pool->pos >= old_size) &&
+ (&pool->memory[pool->pos - old_size] == old) )
{
/* was the previous allocation - optimize! */
if (pool->pos + asize - old_size <= pool->end)
@@ -251,32 +252,40 @@ MHD_pool_reallocate (struct MemoryPool *pool,
/**
* Clear all entries from the memory pool except
- * for @a keep of the given @a size.
+ * for @a keep of the given @a size. The pointer
+ * returned should be a buffer of @a new_size where
+ * the first @a copy_bytes are from @a keep.
*
* @param pool memory pool to use for the operation
* @param keep pointer to the entry to keep (maybe NULL)
- * @param size how many bytes need to be kept at this address
+ * @param copy_bytes how many bytes need to be kept at this address
+ * @param new_size how many bytes should the allocation we return have?
+ * (should be larger or equal to @a copy_bytes)
* @return addr new address of @a keep (if it had to change)
*/
void *
MHD_pool_reset (struct MemoryPool *pool,
void *keep,
- size_t size)
+ size_t copy_bytes,
+ size_t new_size)
{
if (NULL != keep)
{
if (keep != pool->memory)
{
- memmove (pool->memory, keep, size);
+ memmove (pool->memory,
+ keep,
+ copy_bytes);
keep = pool->memory;
}
}
pool->end = pool->size;
- memset (&pool->memory[size],
+ /* technically not needed, but safer to zero out */
+ memset (&pool->memory[copy_bytes],
0,
- pool->size - size);
+ pool->size - copy_bytes);
if (NULL != keep)
- pool->pos = ROUND_TO_ALIGN(size);
+ pool->pos = ROUND_TO_ALIGN (new_size);
return keep;
}
diff --git a/src/microhttpd/memorypool.h b/src/microhttpd/memorypool.h
@@ -99,16 +99,21 @@ MHD_pool_reallocate (struct MemoryPool *pool,
/**
* Clear all entries from the memory pool except
- * for "keep" of the given "size".
+ * for @a keep of the given @a copy_bytes. The pointer
+ * returned should be a buffer of @a new_size where
+ * the first @a copy_bytes are from @a keep.
*
* @param pool memory pool to use for the operation
* @param keep pointer to the entry to keep (maybe NULL)
- * @param size how many bytes need to be kept at this address
- * @return addr new address of "keep" (if it had to change)
+ * @param copy_bytes how many bytes need to be kept at this address
+ * @param new_size how many bytes should the allocation we return have?
+ * (should be larger or equal to @a copy_bytes)
+ * @return addr new address of @a keep (if it had to change)
*/
void *
MHD_pool_reset (struct MemoryPool *pool,
void *keep,
- size_t size);
+ size_t copy_bytes,
+ size_t new_size);
#endif