[syslog-ng] [PATCH 1/3] scratch-buffers: Scratch buffer implementation.

Gergely Nagy algernon at balabit.hu
Mon Oct 31 12:25:25 CET 2011


Scratch buffers are thread-local GString buffer stacks. The intended
usage is that whenever a piece of code needs a scratch buffer for some
quick task, it can acquire one via scratch_buffer_acquire(), and
whenever it is not needed anymore, it releases it back to the stack.

Buffers are allocated on-demand, and they're freed up at the end of
main_loop_io_worker_job_start(), when all other callbacks have
finished already.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 lib/Makefile.am       |    2 +
 lib/mainloop.c        |    2 +
 lib/scratch-buffers.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/scratch-buffers.h |   41 +++++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 0 deletions(-)
 create mode 100644 lib/scratch-buffers.c
 create mode 100644 lib/scratch-buffers.h

diff --git a/lib/Makefile.am b/lib/Makefile.am
index c4e42b7..7faf241 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -65,6 +65,7 @@ pkginclude_HEADERS = 		\
 	plugin.h		\
 	pragma-parser.h		\
 	rewrite-expr-parser.h	\
+	scratch-buffers.h	\
 	serialize.h		\
 	sgroup.h		\
 	stats.h			\
@@ -134,6 +135,7 @@ libsyslog_ng_la_SOURCES = \
 	plugin.c		\
 	pragma-parser.c		\
 	rewrite-expr-parser.c	\
+	scratch-buffers.c	\
 	serialize.c		\
 	sgroup.c		\
 	stats.c			\
diff --git a/lib/mainloop.c b/lib/mainloop.c
index 92433b7..9b7d1f2 100644
--- a/lib/mainloop.c
+++ b/lib/mainloop.c
@@ -32,6 +32,7 @@
 #include "logqueue.h"
 #include "dnscache.h"
 #include "tls-support.h"
+#include "scratch-buffers.h"
 
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -364,6 +365,7 @@ main_loop_io_worker_job_start(MainLoopIOWorkerJob *self)
       cb->func(cb->user_data);
       list_del_init(&cb->list);
     }
+  scratch_buffers_reset ();
   g_assert(list_empty(&self->finish_callbacks));
   main_loop_current_job = NULL;
 }
diff --git a/lib/scratch-buffers.c b/lib/scratch-buffers.c
new file mode 100644
index 0000000..b254dae
--- /dev/null
+++ b/lib/scratch-buffers.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2011 Gergely Nagy <algernon at balabit.hu>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As an additional exemption you are allowed to compile & link against the
+ * OpenSSL libraries as published by the OpenSSL project. See the file
+ * COPYING for details.
+ *
+ */
+
+#include "tls-support.h"
+#include "scratch-buffers.h"
+
+TLS_BLOCK_START
+{
+  GTrashStack *scratch_buffers;
+}
+TLS_BLOCK_END;
+
+#define local_scratch_buffers	__tls_deref(scratch_buffers)
+
+ScratchBuffer *scratch_buffer_acquire (void)
+{
+  ScratchBuffer *sb;
+
+  sb = g_trash_stack_pop (&local_scratch_buffers);
+  if (!sb)
+    {
+      sb = g_new (ScratchBuffer, 1);
+      sb->s = g_string_new (NULL);
+    }
+  else
+    g_string_set_size (sb->s, 0);
+  return sb;
+}
+
+void scratch_buffer_release (ScratchBuffer *sb)
+{
+  g_trash_stack_push (&local_scratch_buffers, sb);
+}
+
+void scratch_buffers_reset (void)
+{
+  ScratchBuffer *sb;
+
+  while ((sb = g_trash_stack_pop (&local_scratch_buffers)) != NULL)
+    {
+      g_string_free (sb->s, TRUE);
+      g_free (sb);
+    }
+}
diff --git a/lib/scratch-buffers.h b/lib/scratch-buffers.h
new file mode 100644
index 0000000..8ac2413
--- /dev/null
+++ b/lib/scratch-buffers.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2011 Gergely Nagy <algernon at balabit.hu>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As an additional exemption you are allowed to compile & link against the
+ * OpenSSL libraries as published by the OpenSSL project. See the file
+ * COPYING for details.
+ *
+ */
+
+#ifndef SCRATCH_BUFFERS_H_INCLUDED
+#define SCRATCH_BUFFERS_H_INCLUDED 1
+
+#include <glib.h>
+
+typedef struct
+{
+  GTrashStack stackp;
+  GString *s;
+} ScratchBuffer;
+
+ScratchBuffer *scratch_buffer_acquire (void);
+void scratch_buffer_release (ScratchBuffer *sb);
+
+void scratch_buffers_reset (void);
+
+#endif
-- 
1.7.7.1




More information about the syslog-ng mailing list