gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] branch master updated (11a2e012 -> 5ef86b74)


From: gnunet
Subject: [libmicrohttpd] branch master updated (11a2e012 -> 5ef86b74)
Date: Mon, 23 Aug 2021 21:27:17 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 11a2e012 Added new connection state MHD_CONNECTION_START_REPLY
     new 5c8b5aef memory pool: added internal helper function
     new 95d1725c Connection: allocate persistent connection memory with helper
     new 7f33ea76 Stop shrinking memory buffer after preparing reply header or 
footers
     new 5ef86b74 Stop deallocating memory buffer after each hunk or reply part

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/microhttpd/connection.c | 104 ++++++++++++++++++++++++++++++++++----------
 src/microhttpd/memorypool.c |  16 +++++++
 src/microhttpd/memorypool.h |  13 ++++++
 3 files changed, 110 insertions(+), 23 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 5cbb30ed..8ba793f4 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -186,6 +186,77 @@ str_conn_error_ (ssize_t mhd_err_code)
 }
 
 
+/**
+ * Allocate memory from connection's memory pool.
+ * If memory pool doesn't have enough free memory but read of write buffer
+ * have some unused memory, the size of the buffer will be reduced as needed.
+ * @param connection the connection to use
+ * @param size the size of allocated memory area
+ * @return pointer to allocated memory region in the pool or
+ *         NULL if no memory is available
+ */
+static void*
+connection_alloc_memory (struct MHD_Connection *connection,
+                         size_t size)
+{
+  struct MHD_Connection *const c = connection; /* a short alias */
+  struct MemoryPool *const pool = c->pool;     /* a short alias */
+  size_t required_free_size; /**< The required amount of free memory */
+  size_t pool_free; /**< The amount of free memory in the pool */
+  void *res;
+
+  required_free_size = MHD_pool_alloc_size (size);
+  pool_free = MHD_pool_get_free (pool);
+  if (pool_free < required_free_size)
+  {
+    size_t need_to_free = required_free_size - pool_free;
+    mhd_assert (MHD_pool_alloc_size (need_to_free) == need_to_free);
+    if (NULL != c->write_buffer)
+    {
+      /* The connection is in the sending phase */
+      mhd_assert (MHD_CONNECTION_START_REPLY <= c->state);
+      if (c->write_buffer_size - c->write_buffer_append_offset >= need_to_free)
+      {
+        char *buf;
+        buf = MHD_pool_reallocate (pool,
+                                   c->write_buffer,
+                                   c->write_buffer_size,
+                                   c->write_buffer_size - need_to_free);
+        mhd_assert (c->write_buffer == buf);
+#ifdef NDEBUG
+        (void) buf; /* mute compiler warning */
+#endif
+      }
+      else
+        return NULL;
+    }
+    else if (NULL != c->read_buffer)
+    {
+      /* The connection is in the receiving phase */
+      if (c->read_buffer_size - c->read_buffer_offset >= need_to_free)
+      {
+        char *buf;
+        buf = MHD_pool_reallocate (pool,
+                                   c->read_buffer,
+                                   c->read_buffer_size,
+                                   c->read_buffer_size - need_to_free);
+        mhd_assert (c->read_buffer == buf);
+#ifdef NDEBUG
+        (void) buf; /* mute compiler warning */
+#endif
+      }
+      else
+        return NULL;
+    }
+    else
+      return NULL;
+  }
+  res = MHD_pool_allocate (pool, size, true);
+  mhd_assert (NULL != res); /* It has been checked that pool has enough space 
*/
+  return res;
+}
+
+
 #endif /* HAVE_MESSAGES */
 
 /**
@@ -365,9 +436,8 @@ MHD_set_connection_value_n_nocheck_ (struct MHD_Connection 
*connection,
 {
   struct MHD_HTTP_Header *pos;
 
-  pos = MHD_pool_allocate (connection->pool,
-                           sizeof (struct MHD_HTTP_Header),
-                           true);
+  pos = connection_alloc_memory (connection,
+                                 sizeof (struct MHD_HTTP_Header));
   if (NULL == pos)
     return MHD_NO;
   pos->header = (char *) key;
@@ -920,9 +990,8 @@ try_ready_normal_body (struct MHD_Connection *connection)
     if (NULL != connection->resp_iov.iov)
       return MHD_YES;
     copy_size = response->data_iovcnt * sizeof(MHD_iovec_);
-    connection->resp_iov.iov = MHD_pool_allocate (connection->pool,
-                                                  copy_size,
-                                                  true);
+    connection->resp_iov.iov = connection_alloc_memory (connection,
+                                                        copy_size);
     if (NULL == connection->resp_iov.iov)
     {
       MHD_mutex_unlock_chk_ (&response->mutex);
@@ -1477,6 +1546,7 @@ connection_maximize_write_buffer (struct MHD_Connection 
*connection)
 }
 
 
+#if 0 /* disable unused function */
 /**
  * Shrink connection write buffer to the size of unsent data.
  *
@@ -1509,6 +1579,9 @@ connection_shrink_write_buffer (struct MHD_Connection 
*connection)
 }
 
 
+#endif /* unused function */
+
+
 /**
  * Switch connection from recv mode to send mode.
  *
@@ -2030,9 +2103,6 @@ build_header_response (struct MHD_Connection *connection)
   buf[pos++] = '\n';
 
   c->write_buffer_append_offset = pos;
-  /* TODO: remove shrink of the buffer,
-   * handle maximized buffer in other functions */
-  connection_shrink_write_buffer (c);
   return MHD_YES;
 }
 
@@ -2101,8 +2171,6 @@ build_connection_chunked_response_footer (struct 
MHD_Connection *connection)
   c->write_buffer_append_offset += used_size;
   mhd_assert (c->write_buffer_append_offset <= c->write_buffer_size);
 
-  /* TODO: remove shrink */
-  connection_shrink_write_buffer (c);
   return MHD_YES;
 }
 
@@ -2503,9 +2571,8 @@ parse_cookie_header (struct MHD_Connection *connection)
                                                &hdr,
                                                &hdr_len))
     return MHD_YES;
-  cpy = MHD_pool_allocate (connection->pool,
-                           hdr_len + 1,
-                           true);
+  cpy = connection_alloc_memory (connection,
+                                 hdr_len + 1);
   if (NULL == cpy)
   {
 #ifdef HAVE_MESSAGES
@@ -3144,15 +3211,6 @@ check_write_done (struct MHD_Connection *connection,
   connection->write_buffer_append_offset = 0;
   connection->write_buffer_send_offset = 0;
   connection->state = next_state;
-  /* TODO: avoid deallocation of the buffer so
-   * it can be reused for chunked body sending when
-   * header has been sent */
-  MHD_pool_reallocate (connection->pool,
-                       connection->write_buffer,
-                       connection->write_buffer_size,
-                       0);
-  connection->write_buffer = NULL;
-  connection->write_buffer_size = 0;
   return MHD_YES;
 }
 
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c
index d2a0f197..adda0e98 100644
--- a/src/microhttpd/memorypool.c
+++ b/src/microhttpd/memorypool.c
@@ -102,6 +102,22 @@ MHD_init_mem_pools_ (void)
 }
 
 
+/**
+ * Get the real size that would be allocated by the memory pool when
+ * requested to allocate @a size.
+ * @param size the size of memory area that would be rounded up to the
+ *             allocation granularity
+ * @return the size that would be allocated by #MHD_pool_allocate() when
+ *         requested to allocate @a size. It is also minimal size of free
+ *         space in the pool required to #MHD_pool_allocate() succeed.
+ */
+size_t
+MHD_pool_alloc_size (size_t size)
+{
+  return ROUND_TO_ALIGN (size);
+}
+
+
 /**
  * Handle for a memory pool.  Pools are not reentrant and must not be
  * used by multiple threads.
diff --git a/src/microhttpd/memorypool.h b/src/microhttpd/memorypool.h
index 2d1fd119..11355a87 100644
--- a/src/microhttpd/memorypool.h
+++ b/src/microhttpd/memorypool.h
@@ -50,6 +50,19 @@ void
 MHD_init_mem_pools_ (void);
 
 
+/**
+ * Get the real size that would be allocated by the memory pool when
+ * requested to allocate @a size.
+ * @param size the size of memory area that would be rounded up to the
+ *             allocation granularity
+ * @return the size that would be allocated by #MHD_pool_allocate() when
+ *         requested to allocate @a size. It is also minimal size of free
+ *         space in the pool required to #MHD_pool_allocate() succeed.
+ */
+size_t
+MHD_pool_alloc_size (size_t size);
+
+
 /**
  * Create a memory pool.
  *

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]