@matheusbsbs / matheus-bagliano / commits / b449f4cfc9
Fork 0

Rework strbuf API and semantics.

The gory details are explained in strbuf.h. The change of semantics this patch enforces is that the embeded buffer has always a '\0' character after its last byte, to always make it a C-string. The offs-by-one changes are all related to that very change. A strbuf can be used to store byte arrays, or as an extended string library. The `buf' member can be passed to any C legacy string function, because strbuf operations always ensure there is a terminating \0 at the end of the buffer, not accounted in the `len' field of the structure. A strbuf can be used to generate a string/buffer whose final size is not really known, and then "strbuf_detach" can be used to get the built buffer, and keep the wrapping "strbuf" structure usable for further work again. Other interesting feature: strbuf_grow(sb, size) ensure that there is enough allocated space in `sb' to put `size' new octets of data in the buffer. It helps avoiding reallocating data for nothing when the problem the strbuf helps to solve has a known typical size. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 6, 2007 at 13:20 UTC b449f4cfc972929b638b90d375b8960c37790618
5 files changed +180 -28
archive-tar.c
+1 -1
@@ -166,7 +166,7 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
166 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
167 } else {
168 if (verbose)
169 - fprintf(stderr, "%.*s\n", path->len, path->buf);
169 + fprintf(stderr, "%.*s\n", (int)path->len, path->buf);
170 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
171 *header.typeflag = TYPEFLAG_DIR;
172 mode = (mode | 0777) & ~tar_umask;
fast-import.c
+7 -8
@@ -1595,7 +1595,7 @@ static void read_next_command(void)
1595 } else {
1596 struct recent_command *rc;
1597
1598 - command_buf.buf = NULL;
1598 + strbuf_detach(&command_buf);
1599 read_line(&command_buf, stdin, '\n');
1600 if (command_buf.eof)
1601 return;
@@ -1649,7 +1649,6 @@ static void *cmd_data (size_t *size)
1649 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1650 length = 0;
1651 buffer = xmalloc(sz);
1652 - command_buf.buf = NULL;
1652 for (;;) {
1653 read_line(&command_buf, stdin, '\n');
1654 if (command_buf.eof)
@@ -1657,11 +1656,11 @@ static void *cmd_data (size_t *size)
1656 if (term_len == command_buf.len
1657 && !strcmp(term, command_buf.buf))
1658 break;
1660 - ALLOC_GROW(buffer, length + command_buf.len, sz);
1659 + ALLOC_GROW(buffer, length + command_buf.len + 1, sz);
1660 memcpy(buffer + length,
1661 command_buf.buf,
1663 - command_buf.len - 1);
1664 - length += command_buf.len - 1;
1662 + command_buf.len);
1663 + length += command_buf.len;
1664 buffer[length++] = '\n';
1665 }
1666 free(term);
@@ -2101,7 +2100,7 @@ static void cmd_new_commit(void)
2100 }
2101
2102 /* file_change* */
2104 - while (!command_buf.eof && command_buf.len > 1) {
2103 + while (!command_buf.eof && command_buf.len > 0) {
2104 if (!prefixcmp(command_buf.buf, "M "))
2105 file_change_m(b);
2106 else if (!prefixcmp(command_buf.buf, "D "))
@@ -2256,7 +2255,7 @@ static void cmd_reset_branch(void)
2255 else
2256 b = new_branch(sp);
2257 read_next_command();
2259 - if (!cmd_from(b) && command_buf.len > 1)
2258 + if (!cmd_from(b) && command_buf.len > 0)
2259 unread_command_buf = 1;
2260 }
2261
@@ -2273,7 +2272,7 @@ static void cmd_checkpoint(void)
2272
2273 static void cmd_progress(void)
2274 {
2276 - fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
2275 + fwrite(command_buf.buf, 1, command_buf.len, stdout);
2276 fputc('\n', stdout);
2277 fflush(stdout);
2278 skip_optional_lf();
mktree.c
+1 -3
@@ -92,7 +92,6 @@ int main(int ac, char **av)
92
93 strbuf_init(&sb);
94 while (1) {
95 - int len;
95 char *ptr, *ntr;
96 unsigned mode;
97 enum object_type type;
@@ -101,7 +100,6 @@ int main(int ac, char **av)
100 read_line(&sb, stdin, line_termination);
101 if (sb.eof)
102 break;
104 - len = sb.len;
103 ptr = sb.buf;
104 /* Input is non-recursive ls-tree output format
105 * mode SP type SP sha1 TAB name
@@ -111,7 +109,7 @@ int main(int ac, char **av)
109 die("input format error: %s", sb.buf);
110 ptr = ntr + 1; /* type */
111 ntr = strchr(ptr, ' ');
114 - if (!ntr || sb.buf + len <= ntr + 41 ||
112 + if (!ntr || sb.buf + sb.len <= ntr + 40 ||
113 ntr[41] != '\t' ||
114 get_sha1_hex(ntr + 1, sha1))
115 die("input format error: %s", sb.buf);
strbuf.c
+87 -14
@@ -2,40 +2,113 @@
2 #include "strbuf.h"
3
4 void strbuf_init(struct strbuf *sb) {
5 - sb->buf = NULL;
6 - sb->eof = sb->alloc = sb->len = 0;
5 + memset(sb, 0, sizeof(*sb));
6 }
7
9 -static void strbuf_begin(struct strbuf *sb) {
8 +void strbuf_release(struct strbuf *sb) {
9 free(sb->buf);
10 + memset(sb, 0, sizeof(*sb));
11 +}
12 +
13 +void strbuf_reset(struct strbuf *sb) {
14 + if (sb->len)
15 + strbuf_setlen(sb, 0);
16 + sb->eof = 0;
17 +}
18 +
19 +char *strbuf_detach(struct strbuf *sb) {
20 + char *res = sb->buf;
21 strbuf_init(sb);
22 + return res;
23 +}
24 +
25 +void strbuf_grow(struct strbuf *sb, size_t extra) {
26 + if (sb->len + extra + 1 <= sb->len)
27 + die("you want to use way too much memory");
28 + ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
29 +}
30 +
31 +void strbuf_add(struct strbuf *sb, const void *data, size_t len) {
32 + strbuf_grow(sb, len);
33 + memcpy(sb->buf + sb->len, data, len);
34 + strbuf_setlen(sb, sb->len + len);
35 +}
36 +
37 +void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
38 + int len;
39 + va_list ap;
40 +
41 + va_start(ap, fmt);
42 + len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
43 + va_end(ap);
44 + if (len < 0) {
45 + len = 0;
46 + }
47 + if (len >= strbuf_avail(sb)) {
48 + strbuf_grow(sb, len);
49 + va_start(ap, fmt);
50 + len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
51 + va_end(ap);
52 + if (len >= strbuf_avail(sb)) {
53 + die("this should not happen, your snprintf is broken");
54 + }
55 + }
56 + strbuf_setlen(sb, sb->len + len);
57 }
58
14 -static void inline strbuf_add(struct strbuf *sb, int ch) {
15 - if (sb->alloc <= sb->len) {
16 - sb->alloc = sb->alloc * 3 / 2 + 16;
17 - sb->buf = xrealloc(sb->buf, sb->alloc);
59 +size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) {
60 + size_t res;
61 +
62 + strbuf_grow(sb, size);
63 + res = fread(sb->buf + sb->len, 1, size, f);
64 + if (res > 0) {
65 + strbuf_setlen(sb, sb->len + res);
66 }
19 - sb->buf[sb->len++] = ch;
67 + return res;
68 }
69
22 -static void strbuf_end(struct strbuf *sb) {
23 - strbuf_add(sb, 0);
70 +ssize_t strbuf_read(struct strbuf *sb, int fd)
71 +{
72 + size_t oldlen = sb->len;
73 +
74 + for (;;) {
75 + ssize_t cnt;
76 +
77 + strbuf_grow(sb, 8192);
78 + cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
79 + if (cnt < 0) {
80 + strbuf_setlen(sb, oldlen);
81 + return -1;
82 + }
83 + if (!cnt)
84 + break;
85 + sb->len += cnt;
86 + }
87 +
88 + sb->buf[sb->len] = '\0';
89 + return sb->len - oldlen;
90 }
91
92 void read_line(struct strbuf *sb, FILE *fp, int term) {
93 int ch;
28 - strbuf_begin(sb);
94 if (feof(fp)) {
95 + strbuf_release(sb);
96 sb->eof = 1;
97 return;
98 }
99 +
100 + strbuf_reset(sb);
101 while ((ch = fgetc(fp)) != EOF) {
102 if (ch == term)
103 break;
36 - strbuf_add(sb, ch);
104 + strbuf_grow(sb, 1);
105 + sb->buf[sb->len++] = ch;
106 }
38 - if (ch == EOF && sb->len == 0)
107 + if (ch == EOF && sb->len == 0) {
108 + strbuf_release(sb);
109 sb->eof = 1;
40 - strbuf_end(sb);
110 + }
111 +
112 + strbuf_grow(sb, 1);
113 + sb->buf[sb->len] = '\0';
114 }
strbuf.h
+84 -2
@@ -1,13 +1,95 @@
1 #ifndef STRBUF_H
2 #define STRBUF_H
3 +
4 +/*
5 + * Strbuf's can be use in many ways: as a byte array, or to store arbitrary
6 + * long, overflow safe strings.
7 + *
8 + * Strbufs has some invariants that are very important to keep in mind:
9 + *
10 + * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
11 + * build complex strings/buffers whose final size isn't easily known.
12 + *
13 + * It is legal to copy the ->buf pointer away. Though if you want to reuse
14 + * the strbuf after that, setting ->buf to NULL isn't legal.
15 + * `strbuf_detach' is the operation that detachs a buffer from its shell
16 + * while keeping the shell valid wrt its invariants.
17 + *
18 + * 2. the ->buf member is a byte array that has at least ->len + 1 bytes
19 + * allocated. The extra byte is used to store a '\0', allowing the ->buf
20 + * member to be a valid C-string. Every strbuf function ensure this
21 + * invariant is preserved.
22 + *
23 + * Note that it is OK to "play" with the buffer directly if you work it
24 + * that way:
25 + *
26 + * strbuf_grow(sb, SOME_SIZE);
27 + * // ... here the memory areay starting at sb->buf, and of length
28 + * // sb_avail(sb) is all yours, and you are sure that sb_avail(sb) is at
29 + * // least SOME_SIZE
30 + * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
31 + *
32 + * Of course, SOME_OTHER_SIZE must be smaller or equal to sb_avail(sb).
33 + *
34 + * Doing so is safe, though if it has to be done in many places, adding the
35 + * missing API to the strbuf module is the way to go.
36 + *
37 + * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
38 + * even if it's true in the current implementation. Alloc is somehow a
39 + * "private" member that should not be messed with.
40 + */
41 +
42 +#include <assert.h>
43 +
44 struct strbuf {
4 - int alloc;
5 - int len;
45 + size_t alloc;
46 + size_t len;
47 int eof;
48 char *buf;
49 };
50
51 +#define STRBUF_INIT { 0, 0, 0, NULL }
52 +
53 +/*----- strbuf life cycle -----*/
54 extern void strbuf_init(struct strbuf *);
55 +extern void strbuf_release(struct strbuf *);
56 +extern void strbuf_reset(struct strbuf *);
57 +extern char *strbuf_detach(struct strbuf *);
58 +
59 +/*----- strbuf size related -----*/
60 +static inline size_t strbuf_avail(struct strbuf *sb) {
61 + return sb->alloc ? sb->alloc - sb->len - 1 : 0;
62 +}
63 +static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
64 + assert (len < sb->alloc);
65 + sb->len = len;
66 + sb->buf[len] = '\0';
67 +}
68 +
69 +extern void strbuf_grow(struct strbuf *, size_t);
70 +
71 +/*----- add data in your buffer -----*/
72 +static inline void strbuf_addch(struct strbuf *sb, int c) {
73 + strbuf_grow(sb, 1);
74 + sb->buf[sb->len++] = c;
75 + sb->buf[sb->len] = '\0';
76 +}
77 +
78 +extern void strbuf_add(struct strbuf *, const void *, size_t);
79 +static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
80 + strbuf_add(sb, s, strlen(s));
81 +}
82 +static inline void strbuf_addbuf(struct strbuf *sb, struct strbuf *sb2) {
83 + strbuf_add(sb, sb2->buf, sb2->len);
84 +}
85 +
86 +__attribute__((format(printf,2,3)))
87 +extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
88 +
89 +extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
90 +/* XXX: if read fails, any partial read is undone */
91 +extern ssize_t strbuf_read(struct strbuf *, int fd);
92 +
93 extern void read_line(struct strbuf *, FILE *, int);
94
95 #endif /* STRBUF_H */