[systemd-devel] [PATCH] core: avoid needless strdup by working with strings directly
Shawn Landden
shawnlandden at gmail.com
Thu Aug 16 10:14:42 PDT 2012
---
src/core/main.c | 52 ++++++++++++++++++++++------------------------------
1 file changed, 22 insertions(+), 30 deletions(-)
diff --git a/src/core/main.c b/src/core/main.c
index cdd77c1..4e12793 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -237,7 +237,7 @@ static int set_default_unit(const char *u) {
return 0;
}
-static int parse_proc_cmdline_word(const char *word) {
+static int parse_proc_cmdline_word(char *word) {
static const char * const rlmap[] = {
"emergency", SPECIAL_EMERGENCY_TARGET,
@@ -342,9 +342,7 @@ static int parse_proc_cmdline_word(const char *word) {
char *cenv, *eq;
int r;
- cenv = strdup(word + 15);
- if (!cenv)
- return -ENOMEM;
+ cenv = word + 15;
eq = strchr(cenv, '=');
if (!eq) {
@@ -352,12 +350,12 @@ static int parse_proc_cmdline_word(const char *word) {
if (r < 0)
log_warning("unsetenv failed %m. Ignoring.");
} else {
- *eq = 0;
+ *eq = '\0';
r = setenv(cenv, eq + 1, 1);
+ *eq = '=';
if (r < 0)
log_warning("setenv failed %m. Ignoring.");
}
- free(cenv);
} else if (startswith(word, "systemd.") ||
(in_initrd() && startswith(word, "rd.systemd."))) {
@@ -490,15 +488,14 @@ static int config_parse_cpu_affinity2(
assert(rvalue);
FOREACH_WORD_QUOTED(w, l, rvalue, state) {
- char *t;
int r;
unsigned cpu;
+ char ch;
- if (!(t = strndup(w, l)))
- return log_oom();
-
- r = safe_atou(t, &cpu);
- free(t);
+ ch = w[l+1];
+ w[l+1] = '\0';
+ r = safe_atou(w, &cpu);
+ w[l+1] = ch;
if (!c)
if (!(c = cpu_set_malloc(&ncpus)))
@@ -549,7 +546,7 @@ static int config_parse_join_controllers(
const char *section,
const char *lvalue,
int ltype,
- const char *rvalue,
+ char *rvalue,
void *data,
void *userdata) {
@@ -564,14 +561,12 @@ static int config_parse_join_controllers(
free_join_controllers();
FOREACH_WORD_QUOTED(w, length, rvalue, state) {
- char *s, **l;
-
- s = strndup(w, length);
- if (!s)
- return log_oom();
+ char **l, ch;
- l = strv_split(s, ",");
- free(s);
+ ch = w[length+1];
+ w[length+1] = '\0';
+ l = strv_split(w, ",");
+ w[length+1] = ch;
strv_uniq(l);
@@ -704,7 +699,7 @@ static int parse_config_file(void) {
}
static int parse_proc_cmdline(void) {
- char *line, *w, *state;
+ char *line, *word, *state;
int r;
size_t l;
@@ -718,19 +713,16 @@ static int parse_proc_cmdline(void) {
return 0;
}
- FOREACH_WORD_QUOTED(w, l, line, state) {
- char *word;
-
- if (!(word = strndup(w, l))) {
- r = -ENOMEM;
- goto finish;
- }
+ FOREACH_WORD_QUOTED(word, l, line, state) {
+ char ch;
+ ch = word[l+1];
+ word[l+1] = '\0';
r = parse_proc_cmdline_word(word);
- free(word);
+ word[l+1] = ch;
if (r < 0) {
- log_error("Failed on cmdline argument %s: %s", word, strerror(-r));
+ log_error("Failed on cmdline argument %.*s: %s", (int)l, word, strerror(-r));
goto finish;
}
}
--
1.7.9.5
More information about the systemd-devel
mailing list