mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-26 00:40:08 +01:00
glib-aux: don't use GPtrArray in nm_utils_strsplit_quoted()
GPtrArray requires an additional heap allocation for the GPtrArray. Utterly useless in the majority of cases. Anyway. Allocating (and exponentially grown) a buffer is not too hard, just slightly more cumbersome. Since nm_utils_strsplit_quoted() is heavily unit tested and entirely self-contained, let's opt for the more complicated implementation and avoid the extra allocation.
This commit is contained in:
parent
e122df6005
commit
4dd4d5b759
1 changed files with 24 additions and 9 deletions
|
|
@ -2288,9 +2288,11 @@ nm_utils_escaped_tokens_options_split(char *str, const char **out_key, const cha
|
|||
char **
|
||||
nm_utils_strsplit_quoted(const char *str)
|
||||
{
|
||||
gs_unref_ptrarray GPtrArray *arr = NULL;
|
||||
gs_free char *str_out = NULL;
|
||||
CharLookupTable ch_lookup;
|
||||
char **arr = NULL;
|
||||
gsize arr_len = 0;
|
||||
gsize arr_alloc = 0;
|
||||
gs_free char *str_out = NULL;
|
||||
CharLookupTable ch_lookup;
|
||||
|
||||
nm_assert(str);
|
||||
|
||||
|
|
@ -2347,19 +2349,32 @@ nm_utils_strsplit_quoted(const char *str)
|
|||
str++;
|
||||
}
|
||||
|
||||
if (!arr)
|
||||
arr = g_ptr_array_new();
|
||||
g_ptr_array_add(arr, g_strndup(str_out, j));
|
||||
if (arr_len >= arr_alloc) {
|
||||
if (arr_alloc == 0)
|
||||
arr_alloc = 4;
|
||||
else
|
||||
arr_alloc *= 2;
|
||||
arr = g_realloc(arr, sizeof(char *) * arr_alloc);
|
||||
}
|
||||
|
||||
arr[arr_len++] = g_strndup(str_out, j);
|
||||
}
|
||||
|
||||
if (!arr)
|
||||
return g_new0(char *, 1);
|
||||
|
||||
g_ptr_array_add(arr, NULL);
|
||||
|
||||
/* We want to return an optimally sized strv array, with no excess
|
||||
* memory allocated. Hence, clone once more. */
|
||||
return nm_memdup(arr->pdata, sizeof(char *) * arr->len);
|
||||
|
||||
if (arr_len + 1u != arr_alloc) {
|
||||
gs_free char **arr_old = arr;
|
||||
|
||||
arr = g_new(char *, arr_len + 1u);
|
||||
memcpy(arr, arr_old, sizeof(char *) * arr_len);
|
||||
}
|
||||
|
||||
arr[arr_len] = NULL;
|
||||
return arr;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue