diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-05-11 20:09:58 +0200 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-03-14 08:54:18 +0100 |
commit | 249a98a8ff9f9a3da30e977d347c14487088f738 (patch) | |
tree | 39b72cb644107d0c4185aff75e30e0b2c21776f6 /src/shared/util.c | |
parent | eba20c2f8afe24a8bab713946a248b45dd2a80f1 (diff) |
util: optimize free_and_strdup() if NOP
Under the assumption that strcmp() is cheaper than memory allocation,
let's avoid the allocation, if the new value is identical to the old.
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index 275fdece1..b885a46e4 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -5683,6 +5683,9 @@ int free_and_strdup(char **p, const char *s) { /* Replaces a string pointer with an strdup()ed new string, * possibly freeing the old one. */ + if (streq_ptr(*p, s)) + return 0; + if (s) { t = strdup(s); if (!t) @@ -5693,7 +5696,7 @@ int free_and_strdup(char **p, const char *s) { free(*p); *p = t; - return 0; + return 1; } int sethostname_idempotent(const char *s) { |