From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 3/3] strbuf.[ch]: make strbuf_fread() take hint, not size
Date: Wed, 7 Jul 2021 12:38:42 +0200 [thread overview]
Message-ID: <patch-3.3-46c65a7ae12-20210707T103712Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-0.3-00000000000-20210707T103712Z-avarab@gmail.com>
Change the strbuf_fread() function to take a "size_t hint" instead of
a "size_t size", for consistency with e.g. strbuf_read(). We can then
change callers that explicitly passed in our default hint of 8192.
The strbuf_fread() has not taken a hint ever since it was added in
b449f4cfc97 (Rework strbuf API and semantics., 2007-09-06), it was
left out when strbuf_read() learned to do it in f1696ee398e (Strbuf
API extensions and fixes., 2007-09-10).
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
builtin/am.c | 4 ++--
strbuf.c | 3 ++-
strbuf.h | 3 ++-
3 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index 0b2d886c81b..295b9c4080c 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -781,7 +781,7 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
}
strbuf_reset(&sb);
- while (strbuf_fread(&sb, 8192, in) > 0) {
+ while (strbuf_fread(&sb, 0, in) > 0) {
fwrite(sb.buf, 1, sb.len, out);
strbuf_reset(&sb);
}
@@ -898,7 +898,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
}
strbuf_reset(&sb);
- while (strbuf_fread(&sb, 8192, in) > 0) {
+ while (strbuf_fread(&sb, 0, in) > 0) {
fwrite(sb.buf, 1, sb.len, out);
strbuf_reset(&sb);
}
diff --git a/strbuf.c b/strbuf.c
index 7e9f5fdc4de..af3af7622d1 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -498,10 +498,11 @@ void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags)
}
}
-size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
+size_t strbuf_fread(struct strbuf *sb, size_t hint, FILE *f)
{
size_t res;
size_t oldalloc = sb->alloc;
+ size_t size = strbuf_hint(hint);
strbuf_grow(sb, size);
res = fread(sb->buf + sb->len, 1, size, f);
diff --git a/strbuf.h b/strbuf.h
index ca3c47966a0..7d178e3c8de 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -442,13 +442,14 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt,
/**
* Read a given size of data from a FILE* pointer to the buffer.
+ * if the size is 0 the default hint is used.
*
* NOTE: The buffer is rewound if the read fails. If -1 is returned,
* `errno` must be consulted, like you would do for `read(3)`.
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
* family of functions have the same behaviour as well.
*/
-size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *file);
+size_t strbuf_fread(struct strbuf *sb, size_t hint, FILE *file);
/**
* Read the contents of a given file descriptor. The third argument can be
--
2.32.0.636.g43e71d69cff
next prev parent reply other threads:[~2021-07-07 10:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-07 10:38 [PATCH 0/3] strbuf.[ch]: add STRBUF_HINT_SIZE, don't hardcode 8192 Ævar Arnfjörð Bjarmason
2021-07-07 10:38 ` [PATCH 1/3] strbuf.[ch]: add STRBUF_HINT_SIZE macro = 8192 Ævar Arnfjörð Bjarmason
2021-07-07 22:33 ` Junio C Hamano
2021-07-07 10:38 ` [PATCH 2/3] strbuf.h API users: don't hardcode 8192, use STRBUF_HINT_SIZE Ævar Arnfjörð Bjarmason
2021-07-07 20:10 ` Eric Sunshine
2021-07-07 22:37 ` Junio C Hamano
2021-07-07 23:09 ` Junio C Hamano
2021-07-07 23:22 ` Randall S. Becker
2021-07-12 17:58 ` Jeff King
2021-07-07 10:38 ` Ævar Arnfjörð Bjarmason [this message]
2021-07-07 11:47 ` [PATCH 3/3] strbuf.[ch]: make strbuf_fread() take hint, not size Han-Wen Nienhuys
2021-07-07 21:27 ` Junio C Hamano
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=patch-3.3-46c65a7ae12-20210707T103712Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/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).