summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2022-01-18 11:45:58 -0500
committerTavian Barnes <tavianator@tavianator.com>2022-01-18 12:27:29 -0500
commit60dc18c9fcb2550e15a35809818764ee43a178c7 (patch)
treeb2ff70dffc27e9c1f119baab3456d3507a3a5969
parent4cdb17edde114f1907e27dbf8f101b1261f1021f (diff)
downloadbfs-60dc18c9fcb2550e15a35809818764ee43a178c7.tar.xz
dstring: Set a minimum capacity to avoid reallocating for small strings
-rw-r--r--dstring.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/dstring.c b/dstring.c
index 58e74fe..f344d09 100644
--- a/dstring.c
+++ b/dstring.c
@@ -1,6 +1,6 @@
/****************************************************************************
* bfs *
- * Copyright (C) 2016-2020 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2016-2022 Tavian Barnes <tavianator@tavianator.com> *
* *
* Permission to use, copy, modify, and/or distribute this software for any *
* purpose with or without fee is hereby granted. *
@@ -42,6 +42,11 @@ static size_t dstrsize(size_t capacity) {
/** Allocate a dstring with the given contents. */
static char *dstralloc_impl(size_t capacity, size_t length, const char *data) {
+ // Avoid reallocations for small strings
+ if (capacity < 7) {
+ capacity = 7;
+ }
+
struct dstring *header = malloc(dstrsize(capacity));
if (!header) {
return NULL;