[PATCH 04/12] string: Use conservative allocation growth strategy

Denis Kenzior <[email protected]> Mon, 22 Jul 2024 14:04:21 -0500
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
Instead of always growing exponentially, only request enough pages to
hold the new string.  It is unlikely that ell based applications will be
processing large strings, so be more conservative with memory use.
---
 ell/string.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/ell/string.c b/ell/string.c
index a6d359d20cc9..e8e45beacb92 100644
--- a/ell/string.c
+++ b/ell/string.c
@@ -34,25 +34,18 @@ struct l_string {
 	char *str;
 };
 
-static inline size_t next_power(size_t len)
-{
-	size_t n = 1;
-
-	if (len > SIZE_MAX / 2)
-		return SIZE_MAX;
-
-	while (n < len)
-		n = n << 1;
-
-	return n;
-}
-
 static void grow_string(struct l_string *str, size_t extra)
 {
 	if (str->len + extra < str->max)
 		return;
 
-	str->max = next_power(str->len + extra + 1);
+	str->max = str->len + extra + 1;
+
+	if (str->max < l_util_pagesize())
+		str->max = roundup_pow_of_two(str->max);
+	else
+		str->max = align_len(str->max, l_util_pagesize());
+
 	str->str = l_realloc(str->str, str->max);
 }
 
-- 
2.45.2