From: Jonathan Nieder <jrnieder@gmail.com>
To: Ramkumar Ramachandra <artagnon@gmail.com>
Cc: Git Mailing List <git@vger.kernel.org>,
David Michael Barr <david.barr@cordelta.com>
Subject: Re: [PATCH 3/7] Add buffer pool library
Date: Sat, 29 May 2010 03:51:24 -0500 [thread overview]
Message-ID: <20100529085124.GA6847@progeny.tock> (raw)
In-Reply-To: <1274650832-7411-4-git-send-email-artagnon@gmail.com>
Hi Ram,
Ramkumar Ramachandra wrote:
> line_buffer creates a couple of static buffers and expose an API for
> using them.
So this provides a thread-unsafe fgets() and fread() where the caller
does not have to supply a buffer. Sounds convenient.
> Taken directly
> from David Michael Barr's svn-dump-fast-export repository.
>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Missing From: line and sign-off.
[...]
> +char *buffer_read_line(void)
> +{
> + char *end;
style nitpick: use tabs to indent.
> + 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;
> + }
Why not fgets()?
[...]
> +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);
So if this buffer library is in use, all input needs to pass through
it? I would prefer to avoid that if possible.
> + line_len += offset;
> + }
> + while (offset < len && !feof(stdin)) {
> + offset += fread(&s[offset], 1, len - offset, stdin);
> + }
On error, wouldn’t this be an infinite loop? Maybe:
offset += fread(&s[offset], 1, len - offset, stdin);
if (ferror(stdin)) {
free(s);
return NULL;
}
One iteration should be sufficient, since fread loops internally.
[...]
> +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);
> + }
Likewise.
> + 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);
Likewise.
Why isn’t line_buffer used here?
[...]
> +void buffer_skip_bytes(uint32_t len)
> +{
[...]
> + while (len > 0 && !feof(stdin)) {
> + in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
> + in = fread(byte_buffer, 1, in, stdin);
> + len -= in;
> + }
Likewise.
Too bad stdio does not supply a function for this (fseek is the
closest I can find, and it does not work on unseekable files).
Thanks,
Jonathan
next prev parent reply other threads:[~2010-05-29 8:51 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 ` [PATCH 3/7] Add buffer pool library Ramkumar Ramachandra
2010-05-24 7:47 ` Peter Baumann
2010-05-24 10:11 ` Ramkumar Ramachandra
2010-05-24 10:37 ` David Michael Barr
2010-05-29 8:51 ` Jonathan Nieder [this message]
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=20100529085124.GA6847@progeny.tock \
--to=jrnieder@gmail.com \
--cc=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).