diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-05-11 20:09:58 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-05-11 22:10:36 +0200 |
commit | 4b64536ee7fd0438c93d1784824098f826cd642c (patch) | |
tree | d976127a01a80146d8dc04fb576063824195b4a8 | |
parent | 27023c0ef5701d31ae2a985d986674282d465b0f (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.
-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 c5c1b0ccbf..de891447e7 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -5682,6 +5682,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) @@ -5692,7 +5695,7 @@ int free_and_strdup(char **p, const char *s) { free(*p); *p = t; - return 0; + return 1; } int sethostname_idempotent(const char *s) { |