]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/cpu.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / compat / cpu.h
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_COMPAT_CPU_H
10 #define SQUID_COMPAT_CPU_H
11
12 #if HAVE_ERRNO_H
13 #include <errno.h> /* for ENOTSUP */
14 #endif
15 #if HAVE_SCHED_H
16 #include <sched.h>
17 #endif
18
19 #if !HAVE_CPU_AFFINITY
20 /* failing replacements to minimize the number of if-HAVE_CPU_AFFINITYs */
21 #if !HAVE_CPU_SET_T
22 typedef struct {
23 int bits;
24 } cpu_set_t;
25 #endif
26 inline int sched_setaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
27 inline int sched_getaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
28 #endif /* HAVE_CPU_AFFINITY */
29
30 #if !defined(CPU_SETSIZE)
31 #define CPU_SETSIZE 0
32 #endif
33
34 #if !defined(CPU_ZERO)
35 #define CPU_ZERO(set) (void)0
36 #endif
37
38 #if !defined(CPU_SET)
39 #define CPU_SET(cpu, set) (void)0
40 #endif
41
42 #if !defined(CPU_CLR)
43 #define CPU_CLR(cpu, set) (void)0
44 #endif
45
46 #if !defined(CPU_ISSET)
47 #define CPU_ISSET(cpu, set) false
48 #endif
49
50 // glibc prior to 2.6 lacks CPU_COUNT
51 #ifndef CPU_COUNT
52 #define CPU_COUNT(set) CpuCount(set)
53 /// CPU_COUNT replacement
54 inline int
55 CpuCount(const cpu_set_t *set)
56 {
57 int count = 0;
58 for (int i = 0; i < CPU_SETSIZE; ++i) {
59 if (CPU_ISSET(i, set))
60 ++count;
61 }
62 return count;
63 }
64 #endif /* CPU_COUNT */
65
66 // glibc prior to 2.7 lacks CPU_AND
67 #ifndef CPU_AND
68 #define CPU_AND(destset, srcset1, srcset2) CpuAnd((destset), (srcset1), (srcset2))
69 /// CPU_AND replacement
70 inline void
71 CpuAnd(cpu_set_t *destset, const cpu_set_t *srcset1, const cpu_set_t *srcset2)
72 {
73 for (int i = 0; i < CPU_SETSIZE; ++i) {
74 if (CPU_ISSET(i, srcset1) && CPU_ISSET(i, srcset2))
75 CPU_SET(i, destset);
76 else
77 CPU_CLR(i, destset);
78 }
79 }
80 #endif /* CPU_AND */
81
82 #endif /* SQUID_COMPAT_CPU_H */
83