We'll need the function shortly, so make it public (within kmod).
In the process, re-introduce the test with some changes:
- cosmetics - test function signature, new test macros, DECLARE_*
- do not request an extra byte for \0
- check both ::size and ::bytes
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/451
Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
return true;
}
-static bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
+bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
{
if (n < buf->size - buf->used)
return true;
*/
const char *strbuf_str(struct strbuf *buf);
+/*
+ * Reserve/allocate extra space to buf.
+ *
+ * Check the current buf size and re-allocate, as needed. Therefore follow-up pushes of the
+ * given size (or less) are guaranteed to a) not require re-allocation and b) succeed.
+ *
+ * The terminating \0 byte will be handled internally.
+ */
+bool strbuf_reserve_extra(struct strbuf *buf, size_t n);
+
bool strbuf_pushchar(struct strbuf *buf, char ch);
size_t strbuf_pushmem(struct strbuf *buf, const char *src, size_t sz);
static inline size_t strbuf_pushchars(struct strbuf *buf, const char *str)
}
DEFINE_TEST(test_strbuf_used, .description = "test strbuf_used");
+static int test_strbuf_reserve_extra(void)
+{
+ DECLARE_STRBUF(buf);
+ const char *str;
+ size_t size;
+
+ strbuf_reserve_extra(&buf, strlen(TEXT));
+ size = buf.size;
+ str = buf.bytes;
+ TS_ASSERT(size >= strlen(TEXT) + 1);
+
+ strbuf_pushchars(&buf, TEXT);
+ TS_ASSERT(size == buf.size);
+ TS_ASSERT(str == buf.bytes);
+
+ strbuf_clear(&buf);
+ strbuf_pushchars(&buf, TEXT);
+ TS_ASSERT(size == buf.size);
+ TS_ASSERT(str == buf.bytes);
+
+ return 0;
+}
+DEFINE_TEST(test_strbuf_reserve_extra, .description = "test strbuf_reserve_extra");
+
static int test_strbuf_shrink_to(void)
{
_cleanup_strbuf_ struct strbuf buf;