1 /*
2  * Copyright (C) 1996-2021 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 #undef CPU_COUNT
19 #undef CPU_AND
20 
21 #if !HAVE_CPU_AFFINITY
22 /* failing replacements to minimize the number of if-HAVE_CPU_AFFINITYs */
23 #if !HAVE_CPU_SET_T
24 typedef struct {
25     int bits;
26 } cpu_set_t;
27 #endif
sched_setaffinity(int,size_t,cpu_set_t *)28 inline int sched_setaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
sched_getaffinity(int,size_t,cpu_set_t *)29 inline int sched_getaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
30 #endif /* HAVE_CPU_AFFINITY */
31 
32 #if !defined(CPU_SETSIZE)
33 #define CPU_SETSIZE 0
34 #endif
35 
36 #if !defined(CPU_ZERO)
37 #define CPU_ZERO(set) (void)0
38 #endif
39 
40 #if !defined(CPU_SET)
41 #define CPU_SET(cpu, set) (void)0
42 #endif
43 
44 #if !defined(CPU_CLR)
45 #define CPU_CLR(cpu, set) (void)0
46 #endif
47 
48 #if !defined(CPU_ISSET)
49 #define CPU_ISSET(cpu, set) false
50 #endif
51 
52 // glibc prior to 2.6 lacks CPU_COUNT
53 #ifndef CPU_COUNT
54 #define CPU_COUNT(set) CpuCount(set)
55 /// CPU_COUNT replacement
56 inline int
CpuCount(const cpu_set_t * set)57 CpuCount(const cpu_set_t *set)
58 {
59     int count = 0;
60     for (int i = 0; i < CPU_SETSIZE; ++i) {
61         if (CPU_ISSET(i, set))
62             ++count;
63     }
64     return count;
65 }
66 #endif /* CPU_COUNT */
67 
68 // glibc prior to 2.7 lacks CPU_AND
69 #ifndef CPU_AND
70 #define CPU_AND(destset, srcset1, srcset2) CpuAnd((destset), (srcset1), (srcset2))
71 /// CPU_AND replacement
72 inline void
CpuAnd(cpu_set_t * destset,const cpu_set_t * srcset1,const cpu_set_t * srcset2)73 CpuAnd(cpu_set_t *destset, const cpu_set_t *srcset1, const cpu_set_t *srcset2)
74 {
75     for (int i = 0; i < CPU_SETSIZE; ++i) {
76         if (CPU_ISSET(i, srcset1) && CPU_ISSET(i, srcset2))
77             CPU_SET(i, destset);
78         else
79             CPU_CLR(i, destset);
80     }
81 }
82 #endif /* CPU_AND */
83 
84 #endif /* SQUID_COMPAT_CPU_H */
85 
86