summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-06-29 13:52:39 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-29 13:52:39 -0400
commit4ad724391daeff5b86ad420f1e1a8b35d65fd7e0 (patch)
treec94344799850c28ce4d425a0b4fe2a00e2eb64d8 /src
parentec50b98d5584b0bb291a463a0c39905ce05acfe7 (diff)
downloadbfs-4ad724391daeff5b86ad420f1e1a8b35d65fd7e0.tar.xz
dstring: Allow dstreserve(NULL, n)
Diffstat (limited to 'src')
-rw-r--r--src/dstring.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/dstring.c b/src/dstring.c
index 7ca74d0..f019ba9 100644
--- a/src/dstring.c
+++ b/src/dstring.c
@@ -3,6 +3,7 @@
#include "dstring.h"
#include "alloc.h"
+#include "bit.h"
#include "diag.h"
#include <stdarg.h>
#include <stdio.h>
@@ -67,10 +68,15 @@ size_t dstrlen(const char *dstr) {
}
int dstreserve(char **dstr, size_t capacity) {
+ if (!*dstr) {
+ *dstr = dstralloc(capacity);
+ return *dstr ? 0 : -1;
+ }
+
struct dstring *header = dstrheader(*dstr);
if (capacity > header->capacity) {
- capacity *= 2;
+ capacity = bit_ceil(capacity + 1) - 1;
header = realloc(header, dstrsize(capacity));
if (!header) {