]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/compat_shared.h
Cleanup: un-wrap C++ header includes
[thirdparty/squid.git] / compat / compat_shared.h
1 #ifndef _SQUID_COMPAT_SHARED_H
2 #define _SQUID_COMPAT_SHARED_H
3
4 /*
5 * This file contains all the compatibility and portability hacks
6 * Which are general-case and shared between all OS and support programs.
7 *
8 * If an OS-specific hack is needed there are per-OS files for that in
9 * the os/ sub-directory here.
10 *
11 * These hacks should be platform and location agnostic.
12 * A quick look-over of the code already here should give you an idea
13 * of the requirements for wrapping your hack for safe portability.
14 */
15
16 #ifdef __cplusplus
17 /*
18 * Define an error display handler override.
19 * If error_notify is set by the linked program it will be used by the local
20 * portability functions. Otherwise perror() will be used.
21 */
22 extern void (*failure_notify) (const char *);
23 #endif
24
25 /*
26 * sys/resource.h and sys/time.h are apparently order-dependant.
27 */
28 #if HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31 #if HAVE_SYS_RESOURCE_H
32 #include <sys/resource.h> /* needs sys/time.h above it */
33 #endif
34
35 /*
36 * DIRENT functionality can apparently come from many places.
37 * With various complaints by different compilers
38 */
39 #if HAVE_DIRENT_H
40 #include <dirent.h>
41 #define NAMLEN(dirent) strlen((dirent)->d_name)
42 #else /* if not HAVE_DIRENT_H */
43 #define dirent direct
44 #define NAMLEN(dirent) (dirent)->d_namlen
45 #if HAVE_SYS_NDIR_H
46 #include <sys/ndir.h>
47 #endif /* HAVE_SYS_NDIR_H */
48 #if HAVE_SYS_DIR_H
49 #include <sys/dir.h>
50 #endif /* HAVE_SYS_DIR_H */
51 #if HAVE_NDIR_H
52 #include <ndir.h>
53 #endif /* HAVE_NDIR_H */
54 #endif /* HAVE_DIRENT_H */
55
56 /* The structure dirent also varies between 64-bit and 32-bit environments.
57 * Define our own dirent_t type for consistent simple internal use.
58 * NP: GCC seems not to care about the type naming differences.
59 */
60 #if defined(__USE_FILE_OFFSET64) && !defined(__GNUC__)
61 #define dirent_t struct dirent64
62 #else
63 #define dirent_t struct dirent
64 #endif
65
66 /*
67 * Filedescriptor limits in the different select loops
68 *
69 * NP: FreeBSD 7 defines FD_SETSIZE as unsigned but Squid needs
70 * it to be signed to compare it with signed values.
71 * Linux and others including FreeBSD <7, define it as signed.
72 * If this causes any issues please contact squid-dev@squid-cache.org
73 */
74 #if defined(USE_SELECT) || defined(USE_SELECT_WIN32)
75 /* Limited by design */
76 # define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
77
78 #elif defined(USE_POLL)
79 /* Limited due to delay pools */
80 # define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
81
82 #elif defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
83 # define SQUID_FDSET_NOUSE 1
84
85 #else
86 # error Unknown select loop model!
87 #endif
88
89 #if !HAVE_STRUCT_RUSAGE
90 /**
91 * If we don't have getrusage() then we create a fake structure
92 * with only the fields Squid cares about. This just makes the
93 * source code cleaner, so we don't need lots of ifdefs in other
94 * places
95 */
96 struct rusage {
97 struct timeval ru_stime;
98 struct timeval ru_utime;
99 int ru_maxrss;
100 int ru_majflt;
101 };
102 #endif /* !HAVE_STRUCT_RUSAGE */
103
104 #ifndef min
105 #ifdef __cplusplus
106 /**
107 * min() comparison may not always be provided.
108 * Squid bundles this template for when its needed.
109 * May be used on any type which provides operator '<'
110 */
111 template<class A>
112 inline A const &
113 min(A const & lhs, A const & rhs)
114 {
115 if (rhs < lhs)
116 return rhs;
117 return lhs;
118 }
119 #else /* !__cplusplus */
120 /* for non-C++ we are stuck with the < and ? operator */
121 #define min(a,b) ((a) < (b) ? (a) : (b))
122 #endif /* __cplusplus */
123 #endif /* min */
124
125 #ifndef max
126 #ifdef __cplusplus
127 /**
128 * max() comparison may not always be provided.
129 * Squid bundles this template for when its needed.
130 * May be used on any type which provides operator '>'
131 */
132 template<class A>
133 inline A const &
134 max(A const & lhs, A const & rhs)
135 {
136 if (rhs > lhs)
137 return rhs;
138 return lhs;
139 }
140 #else /* !__cplusplus */
141 /* for non-C++ we are stuck with the < and ? operator */
142 #define max(a,b) ((a) < (b) ? (b) : (a))
143 #endif /* __cplusplus */
144 #endif /* max */
145
146 /**
147 * Common shared definition of what whitespace consists of for string tests
148 */
149 #define w_space " \t\n\r"
150
151 #ifndef SQUID_NONBLOCK
152 /* REQUIRED for the below logics. If they move this needs to as well */
153 #if HAVE_FCNTL_H
154 #include <fcntl.h>
155 #endif
156 #if defined(O_NONBLOCK)
157 /**
158 * We used to assume O_NONBLOCK was broken on Solaris, but evidence
159 * now indicates that its fine on Solaris 8, and in fact required for
160 * properly detecting EOF on FIFOs. So now we assume that if
161 * its defined, it works correctly on all operating systems.
162 */
163 #define SQUID_NONBLOCK O_NONBLOCK
164 #else
165 /** O_NDELAY is our fallback. */
166 #define SQUID_NONBLOCK O_NDELAY
167 #endif
168 #endif
169
170 /**
171 * Signalling flags are apparently not always provided.
172 * TODO find out if these can be moved into specific OS portability files.
173 */
174 #if defined(__cplusplus)
175 #include <csignal>
176 #else
177 #if HAVE_SIGNAL_H
178 #include <signal.h>
179 #endif
180 #endif
181 #ifndef SA_RESTART
182 #define SA_RESTART 0
183 #endif
184 #ifndef SA_NODEFER
185 #define SA_NODEFER 0
186 #endif
187 #ifndef SA_RESETHAND
188 #define SA_RESETHAND 0
189 #endif
190 #if SA_RESETHAND == 0 && defined(SA_ONESHOT)
191 #undef SA_RESETHAND
192 #define SA_RESETHAND SA_ONESHOT
193 #endif
194
195 /**
196 * com_err.h is a C header and needs explicit shielding, but not
197 * all other system headers including this care to do so.
198 */
199 #ifdef __cplusplus
200 #if HAVE_ET_COM_ERR_H
201 extern "C" {
202 #include <et/com_err.h>
203 }
204 #elif HAVE_COM_ERR_H
205 extern "C" {
206 #include <com_err.h>
207 }
208 #endif
209 #endif
210
211 /*
212 * Several function definitions which we provide for security and code safety.
213 */
214 #include "compat/xalloc.h"
215 #include "compat/xis.h"
216 #include "compat/xstrerror.h"
217 #include "compat/xstring.h"
218 #include "compat/xstrto.h"
219
220 /*
221 * strtoll() is needed. Squid provides a portable definition.
222 */
223 #include "compat/strtoll.h"
224
225 #if !HAVE_MEMCPY
226 #if HAVE_BCOPY
227 #define memcpy(d,s,n) bcopy((s),(d),(n))
228 #elif HAVE_MEMMOVE
229 #define memcpy(d,s,n) memmove((d),(s),(n))
230 #endif
231 #endif
232
233 #if !HAVE_MEMMOVE && HAVE_BCOPY
234 #define memmove(d,s,n) bcopy((s),(d),(n))
235 #endif
236
237 /*
238 * strnstr() is needed. The OS may not provide a working copy.
239 */
240 #if HAVE_STRNSTR
241 /* If strnstr exists and is usable we do so. */
242 #define squid_strnstr(a,b,c) strnstr(a,b,c)
243 #else
244 /* If not we have our own copy imported from FreeBSD */
245 const char * squid_strnstr(const char *s, const char *find, size_t slen);
246 #endif
247
248 #if __GNUC__
249 #if !defined(PRINTF_FORMAT_ARG1)
250 #define PRINTF_FORMAT_ARG1 __attribute__ ((format (printf, 1, 2)))
251 #endif
252 #if !defined(PRINTF_FORMAT_ARG2)
253 #define PRINTF_FORMAT_ARG2 __attribute__ ((format (printf, 2, 3)))
254 #endif
255 #if !defined(PRINTF_FORMAT_ARG3)
256 #define PRINTF_FORMAT_ARG3 __attribute__ ((format (printf, 3, 4)))
257 #endif
258 #else /* !__GNU__ */
259 #define PRINTF_FORMAT_ARG1
260 #define PRINTF_FORMAT_ARG2
261 #define PRINTF_FORMAT_ARG3
262 #endif
263
264 #endif /* _SQUID_COMPAT_SHARED_H */