From: Yu Watanabe Date: Wed, 2 Aug 2017 04:42:13 +0000 (+0900) Subject: cpu-set-util: add parse_cpu_set() X-Git-Tag: v235~248^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=032cf8e4d2619ec6e51cf334fe4cd3e5315b8145;p=thirdparty%2Fsystemd.git cpu-set-util: add parse_cpu_set() --- diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c index 95ed6928fff..aec0edc3dc7 100644 --- a/src/basic/cpu-set-util.c +++ b/src/basic/cpu-set-util.c @@ -112,3 +112,49 @@ int parse_cpu_set_and_warn( return (int) ncpus; } + +int parse_cpu_set( + const char *rvalue, + cpu_set_t **cpu_set) { + + _cleanup_cpu_free_ cpu_set_t *c = NULL; + unsigned ncpus = 0; + + assert(rvalue); + + for (;;) { + _cleanup_free_ char *word = NULL; + unsigned cpu, cpu_lower, cpu_upper; + int r; + + r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES); + if (r == -ENOMEM) + return r; + if (r <= 0) + break; + + if (!c) { + c = cpu_set_malloc(&ncpus); + if (!c) + return -ENOMEM; + } + + r = parse_range(word, &cpu_lower, &cpu_upper); + if (r < 0) + return r; + if (cpu_lower >= ncpus || cpu_upper >= ncpus) + return -EINVAL; + + if (cpu_lower <= cpu_upper) + for (cpu = cpu_lower; cpu <= cpu_upper; cpu++) + CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); + } + + /* On success, sets *cpu_set and returns ncpus for the system. */ + if (c) { + *cpu_set = c; + c = NULL; + } + + return (int) ncpus; +} diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h index 6f49d9afb00..9b08ba0a672 100644 --- a/src/basic/cpu-set-util.h +++ b/src/basic/cpu-set-util.h @@ -30,3 +30,4 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE); cpu_set_t* cpu_set_malloc(unsigned *ncpus); int parse_cpu_set_and_warn(const char *rvalue, cpu_set_t **cpu_set, const char *unit, const char *filename, unsigned line, const char *lvalue); +int parse_cpu_set(const char *rvalue, cpu_set_t **cpu_set);