Commit b913e1d6 authored by Thomas Haller's avatar Thomas Haller

platform: optimize sysctl_set() to use stack allocated buffer

The value written to sysctl is usually a short string. It makes sense
to optimize for this case and avoid allocating a temporary string
on the heap.

An alternative would be to use writev(), which effectively does the same
and also creates a temporary buffer (preferably stack allocated).

https://mail.gnome.org/archives/networkmanager-list/2016-February/msg00070.html
parent f41aebf8
......@@ -2452,8 +2452,11 @@ _log_dbg_sysctl_set_impl (NMPlatform *platform, const char *path, const char *va
static gboolean
sysctl_set (NMPlatform *platform, const char *path, const char *value)
{
int fd, len, nwrote, tries;
int fd, tries;
gssize nwrote;
gsize len;
char *actual;
gs_free char *actual_free = NULL;
g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE);
......@@ -2483,10 +2486,16 @@ sysctl_set (NMPlatform *platform, const char *path, const char *value)
* sysctl support partial writes so the LF must be added to the string we're
* about to write.
*/
actual = g_strdup_printf ("%s\n", value);
len = strlen (value) + 1;
if (len > 512)
actual = actual_free = g_malloc (len + 1);
else
actual = g_alloca (len + 1);
memcpy (actual, value, len - 1);
actual[len - 1] = '\n';
actual[len] = '\0';
/* Try to write the entire value three times if a partial write occurs */
len = strlen (actual);
for (tries = 0, nwrote = 0; tries < 3 && nwrote != len; tries++) {
nwrote = write (fd, actual, len);
if (nwrote == -1) {
......@@ -2505,7 +2514,6 @@ sysctl_set (NMPlatform *platform, const char *path, const char *value)
path, value);
}
g_free (actual);
close (fd);
return (nwrote == len);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment