Update to upstream util-linux 2.20.1
[framework/base/util-linux-ng.git] / include / cpuset.h
1 #ifndef UTIL_LINUX_CPUSET_H
2 #define UTIL_LINUX_CPUSET_H
3
4 #include <sched.h>
5
6 /*
7  * Fallback for old or obscure libcs without dynamically allocated cpusets
8  *
9  * The following macros are based on code from glibc.
10  *
11  * The GNU C Library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  */
16 #if !HAVE_DECL_CPU_ALLOC
17
18 # define CPU_ZERO_S(setsize, cpusetp) \
19   do {                                                                        \
20     size_t __i;                                                               \
21     size_t __imax = (setsize) / sizeof (__cpu_mask);                          \
22     __cpu_mask *__bits = (cpusetp)->__bits;                                   \
23     for (__i = 0; __i < __imax; ++__i)                                        \
24       __bits[__i] = 0;                                                        \
25   } while (0)
26
27 # define CPU_SET_S(cpu, setsize, cpusetp) \
28    ({ size_t __cpu = (cpu);                                                   \
29       __cpu < 8 * (setsize)                                                   \
30       ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]               \
31          |= __CPUMASK (__cpu))                                                \
32       : 0; })
33
34 # define CPU_ISSET_S(cpu, setsize, cpusetp) \
35    ({ size_t __cpu = (cpu);                                                   \
36       __cpu < 8 * (setsize)                                                   \
37       ? ((((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]              \
38           & __CPUMASK (__cpu))) != 0                                          \
39       : 0; })
40
41 # define CPU_EQUAL_S(setsize, cpusetp1, cpusetp2) \
42    ({ __cpu_mask *__arr1 = (cpusetp1)->__bits;                                \
43       __cpu_mask *__arr2 = (cpusetp2)->__bits;                                \
44       size_t __imax = (setsize) / sizeof (__cpu_mask);                        \
45       size_t __i;                                                             \
46       for (__i = 0; __i < __imax; ++__i)                                      \
47         if (__arr1[__i] != __arr2[__i])                                       \
48           break;                                                              \
49       __i == __imax; })
50
51 extern int __cpuset_count_s(size_t setsize, const cpu_set_t *set);
52 # define CPU_COUNT_S(setsize, cpusetp)  __cpuset_count_s(setsize, cpusetp)
53
54 # define CPU_ALLOC_SIZE(count) \
55           ((((count) + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask))
56 # define CPU_ALLOC(count)       (malloc(CPU_ALLOC_SIZE(count)))
57 # define CPU_FREE(cpuset)       (free(cpuset))
58
59 #endif /* !HAVE_DECL_CPU_ALLOC */
60
61
62 #define cpuset_nbits(setsize)   (8 * (setsize))
63
64 extern int get_max_number_of_cpus(void);
65
66 extern cpu_set_t *cpuset_alloc(int ncpus, size_t *setsize, size_t *nbits);
67 extern void cpuset_free(cpu_set_t *set);
68
69 extern char *cpulist_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
70 extern int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize);
71
72 extern char *cpumask_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
73 extern int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize);
74
75 #endif /* UTIL_LINUX_CPUSET_H */