xref: /lighttpd1.4/src/first.h (revision 94edf7e8)
1 #ifndef LI_FIRST_H
2 #define LI_FIRST_H
3 
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #else
7 # ifndef _GNU_SOURCE
8 #  define _GNU_SOURCE
9 # endif
10 #endif
11 
12 #ifndef _DEFAULT_SOURCE
13 #define _DEFAULT_SOURCE
14 #endif
15 
16 #ifndef __STDC_WANT_LIB_EXT1__
17 #define __STDC_WANT_LIB_EXT1__ 1
18 #endif
19 
20 #ifdef __COVERITY__
21 #define _Float128 long double
22 #define _Float64x long double
23 #define _Float64  double
24 #define _Float32x double
25 #define _Float32  float
26 #endif
27 
28 /* enable glibc Y2038 64-bit time_t (where available on 32-bit systems) */
29 #ifdef _ILP32
30 #ifndef _TIME_BITS
31 #define _TIME_BITS 64
32 #endif
33 #ifndef _FILE_OFFSET_BITS
34 #define _FILE_OFFSET_BITS 64
35 #endif
36 #endif
37 
38 
39 #include <sys/types.h>
40 #include <stddef.h>
41 
42 #if defined HAVE_STDINT_H
43 # include <stdint.h>
44 #elif defined HAVE_INTTYPES_H
45 # include <inttypes.h>
46 #endif
47 
48 
49 /* solaris and NetBSD 1.3.x again */
50 #if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
51 # define uint32_t u_int32_t
52 #endif
53 
54 
55 #include <limits.h>
56 
57 #ifndef SIZE_MAX
58 # ifdef SIZE_T_MAX
59 #  define SIZE_MAX SIZE_T_MAX
60 # else
61 #  define SIZE_MAX (~(size_t)0u)
62 # endif
63 #endif
64 
65 #ifndef SSIZE_MAX
66 # define SSIZE_MAX ((ssize_t)(SIZE_MAX >> 1))
67 #endif
68 
69 
70 /* TODO: would be more accurate to create build-system test for sizeof(time_t)*/
71 #ifndef HAS_TIME_BITS64
72  #if defined(_LP64) || defined(__LP64__) || defined(_WIN64)
73   #define HAS_TIME_BITS64 1
74  #elif defined(__TIMESIZE)
75   #if __TIMESIZE == 64
76    #define HAS_TIME_BITS64 1
77   #elif defined(__USE_TIME_BITS64)
78    #define HAS_TIME_BITS64 1
79   #else
80    #define HAS_TIME_BITS64 0
81   #endif
82  #elif defined(_WIN32)
83   #ifndef _USE_32BIT_TIME_T
84    #define HAS_TIME_BITS64 1
85   #else
86    #define HAS_TIME_BITS64 0
87   #endif
88  #elif defined(_ILP32) \
89    && !defined(__USE_TIME_BITS64) \
90    && !defined(__NetBSD__) && !defined(__OpenBSD__) \
91    && (!defined(__FreeBSD__) || !defined(__i386__)) \
92    && !(defined(__APPLE__) && defined(__MACH__))
93   #define HAS_TIME_BITS64 0
94  #else
95   #define HAS_TIME_BITS64 1
96  #endif
97 #endif
98 
99 /* non-standard types created for lighttpd for Y2038 problem
100  * reference: https://en.wikipedia.org/wiki/Year_2038_problem */
101 #if HAS_TIME_BITS64
102 typedef time_t unix_time64_t;
103 typedef struct timespec unix_timespec64_t;
104 #define TIME64_CAST(t)  (t)
105 #else  /* !HAS_TIME_BITS64 */
106 typedef int64_t unix_time64_t;
107 struct unix_timespec64 {
108   unix_time64_t tv_sec;        /* seconds */
109   long          tv_nsec;       /* nanoseconds */
110 };
111 typedef struct unix_timespec64 unix_timespec64_t;
112 #define TIME64_CAST(t)  ((unix_time64_t)(uint32_t)(t))
113 #endif /* !HAS_TIME_BITS64 */
114 
115 
116 #define UNUSED(x) ( (void)(x) )
117 
118 
119 #ifndef __has_attribute       /* clang */
120 #define __has_attribute(x) 0
121 #endif
122 
123 #ifndef __has_builtin         /* clang */
124 #define __has_builtin(x) 0
125 #endif
126 
127 #ifdef __GNUC__
128 #ifndef __GNUC_PREREQ
129 #  ifdef __GNUC_PREREQ__
130 #    define __GNUC_PREREQ __GNUC_PREREQ__
131 #  elif defined __GNUC__ && defined __GNUC_MINOR__
132 #    define __GNUC_PREREQ(maj, min) \
133        ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
134 #  else
135 #    define __GNUC_PREREQ(maj, min) 0
136 #  endif
137 #endif
138 #else
139 #define __GNUC_PREREQ(maj,min) 0
140 #endif
141 
142 #ifndef __builtin_expect
143 #if !__has_builtin(__builtin_expect) \
144  && !__GNUC_PREREQ(2,96)
145 #define __builtin_expect(x,y) (x)
146 #endif
147 #endif
148 
149 #ifndef __attribute_noinline__
150 #if __has_attribute(noinline) \
151  || __GNUC_PREREQ(3,1)
152 #define __attribute_noinline__  __attribute__((__noinline__))
153 #else
154 #define __attribute_noinline__
155 #endif
156 #endif
157 
158 #ifndef __attribute_cold__
159 #if __has_attribute(cold) \
160  || __GNUC_PREREQ(4,3)
161 #define __attribute_cold__  __attribute__((__cold__))
162 #else
163 #define __attribute_cold__
164 #endif
165 #endif
166 
167 #ifndef __attribute_hot__
168 #if __has_attribute(hot) \
169  || __GNUC_PREREQ(4,3)
170 #define __attribute_hot__  __attribute__((__hot__))
171 #else
172 #define __attribute_hot__
173 #endif
174 #endif
175 
176 #ifndef __attribute_noreturn__
177 #if __has_attribute(noreturn) \
178  || __GNUC_PREREQ(2,5)
179 #define __attribute_noreturn__  __attribute__((__noreturn__))
180 #else
181 #define __attribute_noreturn__
182 #endif
183 #endif
184 
185 #ifndef __attribute_fallthrough__
186 #if __has_attribute(fallthrough) \
187  || __GNUC_PREREQ(7,0)
188 #define __attribute_fallthrough__  __attribute__((__fallthrough__));
189 #else
190 #define __attribute_fallthrough__  /* fall through */
191 #endif
192 #endif
193 
194 #ifndef __attribute_format__
195 #if __has_attribute(format) \
196  || __GNUC_PREREQ(2,95) /*(maybe earlier gcc, too)*/
197 #define __attribute_format__(x)  __attribute__((__format__ x))
198 #else
199 #define __attribute_format__(x)
200 #endif
201 #endif
202 
203 #ifndef __attribute_const__
204 #if __has_attribute(const) \
205  || __GNUC_PREREQ(2,5)
206 #define __attribute_const__  __attribute__((__const__))
207 #else
208 #define __attribute_const__
209 #endif
210 #endif
211 
212 #ifndef __attribute_pure__
213 #if __has_attribute(pure) \
214  || __GNUC_PREREQ(2,96)
215 #define __attribute_pure__  __attribute__((__pure__))
216 #else
217 #define __attribute_pure__
218 #endif
219 #endif
220 
221 #ifndef __attribute_returns_nonnull__
222 #if __has_attribute(returns_nonnull) \
223  || __GNUC_PREREQ(4,9)
224 #define __attribute_returns_nonnull__  __attribute__((__returns_nonnull__))
225 #else
226 #define __attribute_returns_nonnull__
227 #endif
228 #endif
229 
230 #ifndef __attribute_nonnull__
231 #if __has_attribute(nonnull) \
232  || __GNUC_PREREQ(3,3)
233 #define __attribute_nonnull__  __attribute__((__nonnull__))
234 #else
235 #define __attribute_nonnull__
236 #endif
237 #endif
238 
239 #ifndef __attribute_malloc__
240 #if __has_attribute(malloc) \
241  || __GNUC_PREREQ(2,96)
242 #define __attribute_malloc__  __attribute__((__malloc__))
243 #elif defined(_MSC_VER)
244 #undef  restrict
245 #define __attribute_malloc__  __declspec(restrict)
246 #define restrict __restrict
247 #else
248 #define __attribute_malloc__
249 #endif
250 #endif
251 
252 #ifndef __attribute_unused__
253 #if __has_attribute(unused) \
254  || __GNUC_PREREQ(2,95)
255 #define __attribute_unused__  __attribute__((__unused__))
256 #else
257 #define __attribute_unused__
258 #endif
259 #endif
260 
261 
262 #endif
263