Skip to content

Commit 81eddda

Browse files
pks-tttaylorr
authored andcommitted
reftable/basics: provide new reftable_buf interface
Implement a new `reftable_buf` interface that will replace Git's own `strbuf` interface. This is done due to three reasons: - The `strbuf` interfaces do not handle memory allocation failures and instead causes us to die. This is okay in the context of Git, but is not in the context of the reftable library, which is supposed to be usable by third-party applications. - The `strbuf` interface is quite deeply tied into Git, which makes it hard to use the reftable library as a standalone library. Any dependent would have to carefully extract the relevant parts of it to make things work, which is not all that sensible. - The `strbuf` interface does not use the pluggable allocators that can be set up via `reftable_set_alloc()`. So we have good reasons to use our own type, and the implementation is rather trivial. Implement our own type. Conversion of the reftable library will be handled in subsequent commits. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 7fa7e14 commit 81eddda

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

reftable/basics.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ license that can be found in the LICENSE file or at
99
#define REFTABLE_ALLOW_BANNED_ALLOCATORS
1010
#include "basics.h"
1111
#include "reftable-basics.h"
12+
#include "reftable-error.h"
1213

1314
static void *(*reftable_malloc_ptr)(size_t sz);
1415
static void *(*reftable_realloc_ptr)(void *, size_t);
@@ -69,6 +70,79 @@ void reftable_set_alloc(void *(*malloc)(size_t),
6970
reftable_free_ptr = free;
7071
}
7172

73+
void reftable_buf_init(struct reftable_buf *buf)
74+
{
75+
struct reftable_buf empty = REFTABLE_BUF_INIT;
76+
*buf = empty;
77+
}
78+
79+
void reftable_buf_release(struct reftable_buf *buf)
80+
{
81+
reftable_free(buf->buf);
82+
reftable_buf_init(buf);
83+
}
84+
85+
void reftable_buf_reset(struct reftable_buf *buf)
86+
{
87+
if (buf->alloc) {
88+
buf->len = 0;
89+
buf->buf[0] = '\0';
90+
}
91+
}
92+
93+
int reftable_buf_setlen(struct reftable_buf *buf, size_t len)
94+
{
95+
if (len > buf->len)
96+
return -1;
97+
if (len == buf->len)
98+
return 0;
99+
buf->buf[len] = '\0';
100+
buf->len = len;
101+
return 0;
102+
}
103+
104+
int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b)
105+
{
106+
size_t len = a->len < b->len ? a->len : b->len;
107+
if (len) {
108+
int cmp = memcmp(a->buf, b->buf, len);
109+
if (cmp)
110+
return cmp;
111+
}
112+
return a->len < b->len ? -1 : a->len != b->len;
113+
}
114+
115+
int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len)
116+
{
117+
size_t newlen = buf->len + len;
118+
119+
if (newlen + 1 > buf->alloc) {
120+
char *reallocated = buf->buf;
121+
REFTABLE_ALLOC_GROW(reallocated, newlen + 1, buf->alloc);
122+
if (!reallocated)
123+
return REFTABLE_OUT_OF_MEMORY_ERROR;
124+
buf->buf = reallocated;
125+
}
126+
127+
memcpy(buf->buf + buf->len, data, len);
128+
buf->buf[newlen] = '\0';
129+
buf->len = newlen;
130+
131+
return 0;
132+
}
133+
134+
int reftable_buf_addstr(struct reftable_buf *buf, const char *s)
135+
{
136+
return reftable_buf_add(buf, s, strlen(s));
137+
}
138+
139+
char *reftable_buf_detach(struct reftable_buf *buf)
140+
{
141+
char *result = buf->buf;
142+
reftable_buf_init(buf);
143+
return result;
144+
}
145+
72146
void put_be24(uint8_t *out, uint32_t i)
73147
{
74148
out[0] = (uint8_t)((i >> 16) & 0xff);

reftable/basics.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,62 @@ license that can be found in the LICENSE file or at
1616
#include "system.h"
1717
#include "reftable-basics.h"
1818

19+
struct reftable_buf {
20+
size_t alloc;
21+
size_t len;
22+
char *buf;
23+
};
24+
#define REFTABLE_BUF_INIT { 0 }
25+
26+
/*
27+
* Initialize the buffer such that it is ready for use. This is equivalent to
28+
* using REFTABLE_BUF_INIT for stack-allocated variables.
29+
*/
30+
void reftable_buf_init(struct reftable_buf *buf);
31+
32+
/*
33+
* Release memory associated with the buffer. The buffer is reinitialized such
34+
* that it can be reused for subsequent operations.
35+
*/
36+
void reftable_buf_release(struct reftable_buf *buf);
37+
38+
/*
39+
* Reset the buffer such that it is effectively empty, without releasing the
40+
* memory that this structure holds on to. This is equivalent to calling
41+
* `reftable_buf_setlen(buf, 0)`.
42+
*/
43+
void reftable_buf_reset(struct reftable_buf *buf);
44+
45+
/*
46+
* Trim the buffer to a shorter length by updating the `len` member and writing
47+
* a NUL byte to `buf[len]`. Returns 0 on success, -1 when `len` points outside
48+
* of the array.
49+
*/
50+
int reftable_buf_setlen(struct reftable_buf *buf, size_t len);
51+
52+
/*
53+
* Lexicographically compare the two buffers. Returns 0 when both buffers have
54+
* the same contents, -1 when `a` is lexicographically smaller than `b`, and 1
55+
* otherwise.
56+
*/
57+
int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b);
58+
59+
/*
60+
* Add the given bytes to the buffer. Returns 0 on success,
61+
* REFTABLE_OUT_OF_MEMORY_ERROR on allocation failure.
62+
*/
63+
int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len);
64+
65+
/* Equivalent to `reftable_buf_add(buf, s, strlen(s))`. */
66+
int reftable_buf_addstr(struct reftable_buf *buf, const char *s);
67+
68+
/*
69+
* Detach the buffer from the structure such that the underlying memory is now
70+
* owned by the caller. The buffer is reinitialized such that it can be reused
71+
* for subsequent operations.
72+
*/
73+
char *reftable_buf_detach(struct reftable_buf *buf);
74+
1975
/* Bigendian en/decoding of integers */
2076

2177
void put_be24(uint8_t *out, uint32_t i);

0 commit comments

Comments
 (0)