From 34b7992fd6e3894bf6d2229b8aa59cac34bcb1b5 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 13 Jan 2023 22:19:55 -0500 Subject: [PATCH] string: Split g_string_maybe_expand Split off g_string_expand, and inline just the size check. --- glib/gstring.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/glib/gstring.c b/glib/gstring.c index 6abb70b39..e3a491f81 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -75,22 +75,29 @@ */ static void -g_string_maybe_expand (GString *string, - gsize len) +g_string_expand (GString *string, + gsize len) { /* Detect potential overflow */ if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len) g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len); - if (string->len + len >= string->allocated_len) - { - string->allocated_len = g_nearest_pow (string->len + len + 1); - /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough - * memory for this string and don't over-allocate. */ - if (string->allocated_len == 0) - string->allocated_len = string->len + len + 1; - string->str = g_realloc (string->str, string->allocated_len); - } + string->allocated_len = g_nearest_pow (string->len + len + 1); + /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough + * memory for this string and don't over-allocate. + */ + if (string->allocated_len == 0) + string->allocated_len = string->len + len + 1; + + string->str = g_realloc (string->str, string->allocated_len); +} + +static inline void +g_string_maybe_expand (GString *string, + gsize len) +{ + if (G_UNLIKELY (string->len + len >= string->allocated_len)) + g_string_expand (string, len); } /** @@ -113,7 +120,7 @@ g_string_sized_new (gsize dfl_size) string->len = 0; string->str = NULL; - g_string_maybe_expand (string, MAX (dfl_size, 64)); + g_string_expand (string, MAX (dfl_size, 64)); string->str[0] = 0; return string;