diff options
author | Thomas Haller <thaller@redhat.com> | 2019-09-21 15:47:18 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-12-04 18:46:10 +0100 |
commit | 333e734ea4f0ef013c2bc6ae303b375cf0530edf (patch) | |
tree | 9b46c24a10980750d98614403c559039111c36f5 | |
parent | e27cadd9185caf2ecb4bf12cd7296b9ff569ed37 (diff) | |
download | NetworkManager-th/initrd-parse-cleanup.tar.gz |
initrd: use nm_utils_strsplit_set_with_empty() to split strings in nmi_dt_reader_parse()th/initrd-parse-cleanup
strsplit() can be done in O(n) runtime and one memory allocation.
g_strsplit() creates a deepcopied strv array, requiring allocate
memory for each token separately. Also, it's implemented by constructing
the list with a tempory GSList, so it actually needs two allocations
for each tokens.
I think we should not use such wasteful API, if we don't need a clone
of the tokens.
-rw-r--r-- | src/initrd/nmi-dt-reader.c | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/initrd/nmi-dt-reader.c b/src/initrd/nmi-dt-reader.c index a8677ed9c3..5ea04c4da6 100644 --- a/src/initrd/nmi-dt-reader.c +++ b/src/initrd/nmi-dt-reader.c @@ -115,8 +115,9 @@ nmi_dt_reader_parse (const char *sysfs_dir) gs_unref_object NMConnection *connection = NULL; gs_free char *base = NULL; gs_free char *bootpath = NULL; - gs_strfreev char **tokens = NULL; - char *path = NULL; + gs_free const char **tokens_free = NULL; + const char *const*tokens; + const char *bootpath_tokens; gboolean bootp = FALSE; const char *s_ipaddr = NULL; const char *s_netmask = NULL; @@ -147,10 +148,9 @@ nmi_dt_reader_parse (const char *sysfs_dir) c = strchr (bootpath, ':'); if (c) { *c = '\0'; - path = c + 1; - } else { - path = ""; - } + bootpath_tokens = &c[1]; + } else + bootpath_tokens = NULL; dt_get_property (base, "chosen", "client-name", &hostname, NULL); @@ -159,7 +159,9 @@ nmi_dt_reader_parse (const char *sysfs_dir) if (g_strcmp0 (local_hwaddr, hwaddr) == 0) g_clear_pointer (&local_hwaddr, g_free); - tokens = g_strsplit (path, ",", 0); + tokens_free = nm_utils_strsplit_set_with_empty (bootpath_tokens, ","); + tokens = tokens_free + ?: NM_PTRARRAY_EMPTY (const char *); /* * Ethernet device settings. Defined by "Open Firmware, |