From: Lennart Poettering Date: Mon, 11 May 2015 18:09:58 +0000 (+0200) Subject: util: optimize free_and_strdup() if NOP X-Git-Tag: v226.4~1^2~397 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=249a98a8ff9f9a3da30e977d347c14487088f738 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. --- 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) {