summaryrefslogtreecommitdiff
path: root/wsutil/buffer.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2014-07-15 16:40:46 -0700
committerGuy Harris <guy@alum.mit.edu>2014-07-15 23:43:32 +0000
commitd4dab16a3fed4dc399083e454226675894b42871 (patch)
tree24be814f0f9d36acf67a4df4bc59ae11e8fc9576 /wsutil/buffer.c
parent61ac8156812f5f9ef277f303a5e22aac375ad357 (diff)
downloadwireshark-d4dab16a3fed4dc399083e454226675894b42871.tar.gz
Only one buffer.c, please.
Otherwise, if you link with both libwiretap and libfiletap, it's anybody's guess which one you get. That means you're wasting memory with two copies of its routines if they're identical, and means surprising behavior if they're not (which showed up when I was debugging a double-free crash - fixing libwiretap's buffer_free() didn't fix the problem, because Wireshark happened to be calling libfiletap' unfixed buffer_free()). There's nothing *tap-specific about Buffers, anyway, so it really belongs in wsutil. Change-Id: I91537e46917e91277981f8f3365a2c0873152870 Reviewed-on: https://code.wireshark.org/review/3066 Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wsutil/buffer.c')
-rw-r--r--wsutil/buffer.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/wsutil/buffer.c b/wsutil/buffer.c
new file mode 100644
index 0000000000..e12c8de5f6
--- /dev/null
+++ b/wsutil/buffer.c
@@ -0,0 +1,162 @@
+/* buffer.c
+ *
+ * Wiretap Library
+ * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+
+#include "buffer.h"
+
+/* Initializes a buffer with a certain amount of allocated space */
+void
+buffer_init(Buffer* buffer, gsize space)
+{
+ buffer->data = (guint8*)g_malloc(space);
+ buffer->allocated = space;
+ buffer->start = 0;
+ buffer->first_free = 0;
+}
+
+/* Frees the memory used by a buffer */
+void
+buffer_free(Buffer* buffer)
+{
+ g_free(buffer->data);
+ buffer->data = NULL;
+}
+
+/* Assures that there are 'space' bytes at the end of the used space
+ so that another routine can copy directly into the buffer space. After
+ doing that, the routine will also want to run
+ buffer_increase_length(). */
+void
+buffer_assure_space(Buffer* buffer, gsize space)
+{
+ gsize available_at_end = buffer->allocated - buffer->first_free;
+ gsize space_used;
+ gboolean space_at_beginning;
+
+ /* If we've got the space already, good! */
+ if (space <= available_at_end) {
+ return;
+ }
+
+ /* Maybe we don't have the space available at the end, but we would
+ if we moved the used space back to the beginning of the
+ allocation. The buffer could have become fragmented through lots
+ of calls to buffer_remove_start(). I'm using buffer->start as the
+ same as 'available_at_start' in this comparison. */
+
+ /* or maybe there's just no more room. */
+
+ space_at_beginning = buffer->start >= space;
+ if (space_at_beginning || buffer->start > 0) {
+ space_used = buffer->first_free - buffer->start;
+ /* this memory copy better be safe for overlapping memory regions! */
+ memmove(buffer->data, buffer->data + buffer->start, space_used);
+ buffer->start = 0;
+ buffer->first_free = space_used;
+ }
+ /*if (buffer->start >= space) {*/
+ if (space_at_beginning) {
+ return;
+ }
+
+ /* We'll allocate more space */
+ buffer->allocated += space + 1024;
+ buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
+}
+
+void
+buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
+{
+ buffer_assure_space(buffer, bytes);
+ memcpy(buffer->data + buffer->first_free, from, bytes);
+ buffer->first_free += bytes;
+}
+
+void
+buffer_remove_start(Buffer* buffer, gsize bytes)
+{
+ if (buffer->start + bytes > buffer->first_free) {
+ g_error("buffer_remove_start trying to remove %" G_GINT64_MODIFIER "u bytes. s=%" G_GINT64_MODIFIER "u ff=%" G_GINT64_MODIFIER "u!\n",
+ (guint64)bytes, (guint64)buffer->start,
+ (guint64)buffer->first_free);
+ /** g_error() does an abort() and thus never returns **/
+ }
+ buffer->start += bytes;
+
+ if (buffer->start == buffer->first_free) {
+ buffer->start = 0;
+ buffer->first_free = 0;
+ }
+}
+
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+void
+buffer_clean(Buffer* buffer)
+{
+ buffer_remove_start(buffer, buffer_length(buffer));
+}
+#endif
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+void
+buffer_increase_length(Buffer* buffer, gsize bytes)
+{
+ buffer->first_free += bytes;
+}
+#endif
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+gsize
+buffer_length(Buffer* buffer)
+{
+ return buffer->first_free - buffer->start;
+}
+#endif
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+guint8 *
+buffer_start_ptr(Buffer* buffer)
+{
+ return buffer->data + buffer->start;
+}
+#endif
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+guint8 *
+buffer_end_ptr(Buffer* buffer)
+{
+ return buffer->data + buffer->first_free;
+}
+#endif
+
+#ifndef SOME_FUNCTIONS_ARE_DEFINES
+void
+buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
+{
+ buffer_append(buffer, buffer_start_ptr(src_buffer), buffer_length(src_buffer));
+}
+#endif