summaryrefslogtreecommitdiff
path: root/wsutil
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2016-07-21 17:34:56 -0700
committerAnders Broman <a.broman58@gmail.com>2016-07-22 04:18:08 +0000
commitb2e4a7e21ce7ee8fee857ee71d51ad1ec13feb2e (patch)
tree9676cdf55d35911902b17c3a7135a52f2cd2a4ba /wsutil
parente3a15cfbb24b79c0225d391dc44362ac9cdcb077 (diff)
downloadwireshark-b2e4a7e21ce7ee8fee857ee71d51ad1ec13feb2e.tar.gz
Minimize allocations for frame tvbuffs and Buffers.
Try to minimize the number of times we allocate memory for Buffers and Buffer data. Change-Id: I738fdc64e571772ef4ba6335d49087277dd7b430 Reviewed-on: https://code.wireshark.org/review/16577 Reviewed-by: Gerald Combs <gerald@wireshark.org> Petri-Dish: Gerald Combs <gerald@wireshark.org> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/buffer.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/wsutil/buffer.c b/wsutil/buffer.c
index 5f9daf25f3..7d9d44b1b4 100644
--- a/wsutil/buffer.c
+++ b/wsutil/buffer.c
@@ -25,12 +25,27 @@
#include "buffer.h"
+#define SMALL_BUFFER_SIZE (2 * 1024) /* Everyone still uses 1500 byte frames, right? */
+static GPtrArray *small_buffers = NULL; /* Guaranteed to be at least SMALL_BUFFER_SIZE */
+/* XXX - Add medium and large buffers? */
+
/* Initializes a buffer with a certain amount of allocated space */
void
ws_buffer_init(Buffer* buffer, gsize space)
{
- buffer->data = (guint8*)g_malloc(space);
- buffer->allocated = space;
+ if G_UNLIKELY(!small_buffers) small_buffers = g_ptr_array_sized_new(1024);
+
+ if (space <= SMALL_BUFFER_SIZE) {
+ if (small_buffers->len > 0) {
+ buffer->data = (guint8*) g_ptr_array_remove_index(small_buffers, small_buffers->len - 1);
+ } else {
+ buffer->data = (guint8*)g_malloc(SMALL_BUFFER_SIZE);
+ }
+ buffer->allocated = SMALL_BUFFER_SIZE;
+ } else {
+ buffer->data = (guint8*)g_malloc(space);
+ buffer->allocated = space;
+ }
buffer->start = 0;
buffer->first_free = 0;
}
@@ -39,7 +54,11 @@ ws_buffer_init(Buffer* buffer, gsize space)
void
ws_buffer_free(Buffer* buffer)
{
- g_free(buffer->data);
+ if (buffer->allocated == SMALL_BUFFER_SIZE) {
+ g_ptr_array_add(small_buffers, buffer->data);
+ } else {
+ g_free(buffer->data);
+ }
buffer->data = NULL;
}