chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
eba20c2
)
util: optimize free_and_strdup() if NOP
author
Lennart Poettering
<lennart@poettering.net>
Mon, 11 May 2015 18:09:58 +0000
(20:09 +0200)
committer
Sven Eden
<yamakuzure@gmx.net>
Tue, 14 Mar 2017 07:54:18 +0000
(08:54 +0100)
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
patch
|
blob
|
history
diff --git
a/src/shared/util.c
b/src/shared/util.c
index 275fdece1e70de1d9534179a925d191f8fa1ae5c..b885a46e439fb3a5a197007b4ff90690fa0c074e 100644
(file)
--- 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) {