]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/cpu-set-util.c
shared/cpu-set-util: move the part to print cpu-set into a separate function
[thirdparty/systemd.git] / src / shared / cpu-set-util.c
index 9a789ae7565b51e0e5c2afd0268b610ea4273494..aa5c4d110d24b93c710f729e8ad9d6c7097b1095 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <errno.h>
 #include <stddef.h>
+#include <stdio.h>
 #include <syslog.h>
 
 #include "alloc-util.h"
 #include "parse-util.h"
 #include "string-util.h"
 
+char* cpu_set_to_string(const cpu_set_t *set, size_t setsize) {
+        _cleanup_free_ char *str = NULL;
+        size_t allocated = 0, len = 0;
+        int i, r;
+
+        for (i = 0; (size_t) i < setsize * 8; i++) {
+                if (!CPU_ISSET_S(i, setsize, set))
+                        continue;
+
+                if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
+                        return NULL;
+
+                r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
+                assert_se(r > 0);
+                len += r;
+        }
+
+        return TAKE_PTR(str) ?: strdup("");
+}
+
 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
         cpu_set_t *c;
         unsigned n = 1024;