]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: optimize free_and_strdup() if NOP
authorLennart Poettering <lennart@poettering.net>
Mon, 11 May 2015 18:09:58 +0000 (20:09 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 11 May 2015 20:10:36 +0000 (22:10 +0200)
Under the assumption that strcmp() is cheaper than memory allocation,
let's avoid the allocation, if the new value is identical to the old.

src/shared/util.c

index c5c1b0ccbfcae04f233e0b3a2c776c445c535823..de891447e7f1292d7b2c3e662c13f2a9ccca4bba 100644 (file)
@@ -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) {