unit_write_settingf(u, flags, name, "%s=", name);
} else {
_cleanup_free_ char *str = NULL;
- size_t allocated = 0, len = 0, i, ncpus;
+ size_t ncpus;
- ncpus = CPU_SIZE_TO_NUM(n);
-
- for (i = 0; i < ncpus; i++) {
- _cleanup_free_ char *p = NULL;
- size_t add;
-
- if (!CPU_ISSET_S(i, n, (cpu_set_t*) a))
- continue;
-
- r = asprintf(&p, "%zu", i);
- if (r < 0)
- return -ENOMEM;
-
- add = strlen(p);
-
- if (!GREEDY_REALLOC(str, allocated, len + add + 2))
- return -ENOMEM;
-
- strcpy(mempcpy(str + len, p, add), " ");
- len += add + 1;
- }
+ str = cpu_set_to_string(a, n);
+ if (!str)
+ return -ENOMEM;
- if (len != 0)
- str[len - 1] = '\0';
+ ncpus = CPU_SIZE_TO_NUM(n);
if (!c->cpuset || c->cpuset_ncpus < ncpus) {
cpu_set_t *cpuset;
static void test_parse_cpu_set(void) {
cpu_set_t *c = NULL;
+ _cleanup_free_ char *str = NULL;
int ncpus;
int cpu;
assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
+
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* A more interesting range */
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Quoted strings */
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Use commas as separators */
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Commas with spaces (and trailing comma, space) */
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 8; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Ranges */
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Ranges with trailing comma, space */
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Negative range (returns empty cpu_set) */
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
for (cpu = 0; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Mix ranges and individual CPUs */
assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 4; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
+ assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
+ log_info("cpu_set_to_string: %s", str);
+ str = mfree(str);
c = cpu_set_mfree(c);
/* Garbage */