cpu-set-util: add parse_cpu_set()

This commit is contained in:
Yu Watanabe 2017-08-02 13:42:13 +09:00
parent 07d46372fe
commit 032cf8e4d2
2 changed files with 47 additions and 0 deletions

View file

@ -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;
}

View file

@ -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);