From: Ramkumar Ramachandra <artagnon@gmail.com>
To: "Git Mailing List" <git@vger.kernel.org>
Cc: David Michael Barr <david.barr@cordelta.com>
Subject: [PATCH 3/7] Add buffer pool library
Date: Sun, 23 May 2010 23:40:28 +0200 [thread overview]
Message-ID: <1274650832-7411-4-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1274650832-7411-1-git-send-email-artagnon@gmail.com>
line_buffer creates a couple of static buffers and expose an API for
using them. The idea is to maintain a fixed static memory pool to
avoid constant allocation and de-allocation of memory. Taken directly
from David Michael Barr's svn-dump-fast-export repository.
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
vcs-svn/line_buffer.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++
vcs-svn/line_buffer.h | 14 ++++++
2 files changed, 129 insertions(+), 0 deletions(-)
create mode 100644 vcs-svn/line_buffer.c
create mode 100644 vcs-svn/line_buffer.h
diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c
new file mode 100644
index 0000000..6b47d38
--- /dev/null
+++ b/vcs-svn/line_buffer.c
@@ -0,0 +1,115 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "line_buffer.h"
+
+#define LINE_BUFFER_LEN 10000
+#define COPY_BUFFER_LEN 4096
+
+static char line_buffer[LINE_BUFFER_LEN];
+static char byte_buffer[COPY_BUFFER_LEN];
+static uint32_t line_buffer_len = 0;
+static uint32_t line_len = 0;
+
+char *buffer_read_line(void)
+{
+ char *end;
+ uint32_t n_read;
+
+ if (line_len) {
+ memmove(line_buffer, &line_buffer[line_len],
+ line_buffer_len - line_len);
+ line_buffer_len -= line_len;
+ line_len = 0;
+ }
+
+ end = memchr(line_buffer, '\n', line_buffer_len);
+ while (line_buffer_len < LINE_BUFFER_LEN - 1 &&
+ !feof(stdin) && NULL == end) {
+ n_read =
+ fread(&line_buffer[line_buffer_len], 1,
+ LINE_BUFFER_LEN - 1 - line_buffer_len,
+ stdin);
+ end = memchr(&line_buffer[line_buffer_len], '\n', n_read);
+ line_buffer_len += n_read;
+ }
+
+ if (ferror(stdin))
+ return NULL;
+
+ if (end != NULL) {
+ line_len = end - line_buffer;
+ line_buffer[line_len++] = '\0';
+ } else {
+ line_len = line_buffer_len;
+ line_buffer[line_buffer_len] = '\0';
+ }
+
+ if (line_len == 0)
+ return NULL;
+
+ return line_buffer;
+}
+
+char *buffer_read_string(uint32_t len)
+{
+ char *s = malloc(len + 1);
+ uint32_t offset = 0;
+ if (line_buffer_len > line_len) {
+ offset = line_buffer_len - line_len;
+ if (offset > len)
+ offset = len;
+ memcpy(s, &line_buffer[line_len], offset);
+ line_len += offset;
+ }
+ while (offset < len && !feof(stdin)) {
+ offset += fread(&s[offset], 1, len - offset, stdin);
+ }
+ s[offset] = '\0';
+ return s;
+}
+
+void buffer_copy_bytes(uint32_t len)
+{
+ uint32_t in, out;
+ if (line_buffer_len > line_len) {
+ in = line_buffer_len - line_len;
+ if (in > len)
+ in = len;
+ out = 0;
+ while (out < in && !ferror(stdout)) {
+ out +=
+ fwrite(&line_buffer[line_len + out], 1, in - out, stdout);
+ }
+ len -= in;
+ line_len += in;
+ }
+ while (len > 0 && !feof(stdin)) {
+ in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
+ in = fread(byte_buffer, 1, in, stdin);
+ len -= in;
+ out = 0;
+ while (out < in && !ferror(stdout)) {
+ out += fwrite(&byte_buffer[out], 1, in - out, stdout);
+ }
+ }
+}
+
+void buffer_skip_bytes(uint32_t len)
+{
+ uint32_t in;
+ if (line_buffer_len > line_len) {
+ in = line_buffer_len - line_len;
+ if (in > len)
+ in = len;
+ line_len += in;
+ len -= in;
+ }
+ while (len > 0 && !feof(stdin)) {
+ in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
+ in = fread(byte_buffer, 1, in, stdin);
+ len -= in;
+ }
+}
+
diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h
new file mode 100644
index 0000000..e546f4d
--- /dev/null
+++ b/vcs-svn/line_buffer.h
@@ -0,0 +1,14 @@
+#ifndef LINE_BUFFER_H_
+#define LINE_BUFFER_H_
+
+#include <stdint.h>
+
+char *buffer_read_line(void);
+
+char *buffer_read_string(uint32_t len);
+
+void buffer_copy_bytes(uint32_t len);
+
+void buffer_skip_bytes(uint32_t len);
+
+#endif
--
1.7.1
next prev parent reply other threads:[~2010-05-23 21:39 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-23 21:40 [PATCH 0/7] Import David's SVN exporter Ramkumar Ramachandra
2010-05-23 21:40 ` [WIP PATCH 1/7] Add skeleton remote helper for SVN Ramkumar Ramachandra
2010-05-23 21:40 ` [PATCH 2/7] Add cpp macro implementation of treaps Ramkumar Ramachandra
2010-05-29 7:18 ` Jonathan Nieder
2010-05-30 9:09 ` Ramkumar Ramachandra
2010-05-30 9:31 ` Jonathan Nieder
2010-05-30 9:33 ` Ramkumar Ramachandra
2010-05-23 21:40 ` Ramkumar Ramachandra [this message]
2010-05-24 7:47 ` [PATCH 3/7] Add buffer pool library Peter Baumann
2010-05-24 10:11 ` Ramkumar Ramachandra
2010-05-24 10:37 ` David Michael Barr
2010-05-29 8:51 ` Jonathan Nieder
2010-05-29 10:55 ` David Michael Barr
2010-05-23 21:40 ` [PATCH 4/7] Add a memory " Ramkumar Ramachandra
2010-05-29 9:06 ` Jonathan Nieder
2010-05-30 9:12 ` Ramkumar Ramachandra
2010-05-30 9:55 ` Jonathan Nieder
2010-05-30 10:51 ` Ramkumar Ramachandra
2010-05-23 21:40 ` [PATCH 5/7] Add API for string-specific memory pool Ramkumar Ramachandra
2010-05-29 11:38 ` Jonathan Nieder
2010-05-30 9:38 ` Ramkumar Ramachandra
2010-05-30 10:09 ` Jonathan Nieder
2010-05-30 16:52 ` Ramkumar Ramachandra
2010-05-23 21:40 ` [PATCH 6/7] Add SVN revision parser and exporter Ramkumar Ramachandra
2010-05-29 14:06 ` Jonathan Nieder
2010-05-30 15:58 ` Ramkumar Ramachandra
2010-05-23 21:40 ` [PATCH 7/7] Add handler for SVN dump Ramkumar Ramachandra
2010-05-30 8:59 ` Jonathan Nieder
2010-05-30 10:45 ` Ramkumar Ramachandra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1274650832-7411-4-git-send-email-artagnon@gmail.com \
--to=artagnon@gmail.com \
--cc=david.barr@cordelta.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).