1 | // |
2 | // Copyright (C) 1999 and onwards Google, Inc. |
3 | // |
4 | // |
5 | // These are weird things we need to do to get this compiling on |
6 | // random systems (and on SWIG). |
7 | |
8 | #ifndef BASE_PORT_H_ |
9 | #define BASE_PORT_H_ |
10 | |
11 | #include <limits.h> // So we can set the bounds of our types |
12 | #include <string.h> // for memcpy() |
13 | #include <stdlib.h> // for free() |
14 | |
15 | #if defined(OS_MACOSX) |
16 | #include <unistd.h> // for getpagesize() on mac |
17 | #elif defined(OS_CYGWIN) |
18 | #include <malloc.h> // for memalign() |
19 | #endif |
20 | |
21 | #include "base/integral_types.h" |
22 | |
23 | // Must happens before inttypes.h inclusion */ |
24 | #if defined(OS_MACOSX) |
25 | /* From MacOSX's inttypes.h: |
26 | * "C++ implementations should define these macros only when |
27 | * __STDC_FORMAT_MACROS is defined before <inttypes.h> is included." */ |
28 | #ifndef __STDC_FORMAT_MACROS |
29 | #define __STDC_FORMAT_MACROS |
30 | #endif /* __STDC_FORMAT_MACROS */ |
31 | #endif /* OS_MACOSX */ |
32 | |
33 | /* Default for most OSes */ |
34 | /* We use SIGPWR since that seems unlikely to be used for other reasons. */ |
35 | #define GOOGLE_OBSCURE_SIGNAL SIGPWR |
36 | |
37 | #if defined OS_LINUX || defined OS_CYGWIN |
38 | |
39 | // _BIG_ENDIAN |
40 | #include <endian.h> |
41 | |
42 | // The uint mess: |
43 | // mysql.h sets _GNU_SOURCE which sets __USE_MISC in <features.h> |
44 | // sys/types.h typedefs uint if __USE_MISC |
45 | // mysql typedefs uint if HAVE_UINT not set |
46 | // The following typedef is carefully considered, and should not cause |
47 | // any clashes |
48 | #if !defined(__USE_MISC) |
49 | #if !defined(HAVE_UINT) |
50 | #define HAVE_UINT 1 |
51 | typedef unsigned int uint; |
52 | #endif |
53 | #if !defined(HAVE_USHORT) |
54 | #define HAVE_USHORT 1 |
55 | typedef unsigned short ushort; |
56 | #endif |
57 | #if !defined(HAVE_ULONG) |
58 | #define HAVE_ULONG 1 |
59 | typedef unsigned long ulong; |
60 | #endif |
61 | #endif |
62 | |
63 | #if defined(__cplusplus) |
64 | #include <cstddef> // For _GLIBCXX macros |
65 | #endif |
66 | |
67 | #if !defined(HAVE_TLS) && defined(_GLIBCXX_HAVE_TLS) && defined(ARCH_K8) |
68 | #define HAVE_TLS 1 |
69 | #endif |
70 | |
71 | #elif defined OS_FREEBSD |
72 | |
73 | // _BIG_ENDIAN |
74 | #include <machine/endian.h> |
75 | |
76 | #elif defined OS_SOLARIS |
77 | |
78 | // _BIG_ENDIAN |
79 | #include <sys/isa_defs.h> |
80 | |
81 | // Solaris doesn't define sig_t (function taking an int, returning void) |
82 | typedef void (*sig_t)(int); |
83 | |
84 | // Solaris only defines strtoll, not strtoq |
85 | #define strtoq strtoll |
86 | #define strtouq strtoull |
87 | |
88 | // It doesn't define the posix-standard(?) u_int_16 |
89 | #include <sys/int_types.h> |
90 | typedef uint16_t u_int16_t; |
91 | |
92 | #elif defined OS_MACOSX |
93 | |
94 | // BIG_ENDIAN |
95 | #include <machine/endian.h> |
96 | /* Let's try and follow the Linux convention */ |
97 | #define __BYTE_ORDER BYTE_ORDER |
98 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
99 | #define __BIG_ENDIAN BIG_ENDIAN |
100 | |
101 | #endif |
102 | |
103 | // The following guarenty declaration of the byte swap functions, and |
104 | // define __BYTE_ORDER for MSVC |
105 | #ifdef COMPILER_MSVC |
106 | #include <stdlib.h> |
107 | #define __BYTE_ORDER __LITTLE_ENDIAN |
108 | #define bswap_16(x) _byteswap_ushort(x) |
109 | #define bswap_32(x) _byteswap_ulong(x) |
110 | #define bswap_64(x) _byteswap_uint64(x) |
111 | |
112 | #elif defined(OS_MACOSX) |
113 | // Mac OS X / Darwin features |
114 | #include <libkern/OSByteOrder.h> |
115 | #define bswap_16(x) OSSwapInt16(x) |
116 | #define bswap_32(x) OSSwapInt32(x) |
117 | #define bswap_64(x) OSSwapInt64(x) |
118 | |
119 | #else |
120 | #include <byteswap.h> |
121 | #endif |
122 | |
123 | |
124 | // define the macros IS_LITTLE_ENDIAN or IS_BIG_ENDIAN |
125 | // using the above endian defintions from endian.h if |
126 | // endian.h was included |
127 | #ifdef __BYTE_ORDER |
128 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
129 | #define IS_LITTLE_ENDIAN |
130 | #endif |
131 | |
132 | #if __BYTE_ORDER == __BIG_ENDIAN |
133 | #define IS_BIG_ENDIAN |
134 | #endif |
135 | |
136 | #else |
137 | |
138 | #if defined(__LITTLE_ENDIAN__) |
139 | #define IS_LITTLE_ENDIAN |
140 | #elif defined(__BIG_ENDIAN__) |
141 | #define IS_BIG_ENDIAN |
142 | #endif |
143 | |
144 | // there is also PDP endian ... |
145 | |
146 | #endif // __BYTE_ORDER |
147 | |
148 | // Define the OS's path separator |
149 | #ifdef __cplusplus // C won't merge duplicate const variables at link time |
150 | // Some headers provide a macro for this (GCC's system.h), remove it so that we |
151 | // can use our own. |
152 | #undef PATH_SEPARATOR |
153 | #if OS_WINDOWS |
154 | const char PATH_SEPARATOR = '\\'; |
155 | #else |
156 | const char PATH_SEPARATOR = '/'; |
157 | #endif |
158 | #endif |
159 | |
160 | // Windows has O_BINARY as a flag to open() (like "b" for fopen). |
161 | // Linux doesn't need make this distinction. |
162 | #if defined OS_LINUX && !defined O_BINARY |
163 | #define O_BINARY 0 |
164 | #endif |
165 | |
166 | // va_copy portability definitions |
167 | #ifdef COMPILER_MSVC |
168 | // MSVC doesn't have va_copy yet. |
169 | // This is believed to work for 32-bit msvc. This may not work at all for |
170 | // other platforms. |
171 | // If va_list uses the single-element-array trick, you will probably get |
172 | // a compiler error here. |
173 | // |
174 | #include <stdarg.h> |
175 | inline void va_copy(va_list& a, va_list& b) { |
176 | a = b; |
177 | } |
178 | |
179 | // Nor does it have uid_t |
180 | typedef int uid_t; |
181 | |
182 | #endif |
183 | |
184 | // Mac OS X / Darwin features |
185 | |
186 | #if defined(OS_MACOSX) |
187 | |
188 | // For mmap, Linux defines both MAP_ANONYMOUS and MAP_ANON and says MAP_ANON is |
189 | // deprecated. In Darwin, MAP_ANON is all there is. |
190 | #if !defined MAP_ANONYMOUS |
191 | #define MAP_ANONYMOUS MAP_ANON |
192 | #endif |
193 | |
194 | // Linux has this in <sys/cdefs.h> |
195 | #define __ptr_t void * |
196 | |
197 | // Linux has this in <linux/errno.h> |
198 | #define EXFULL ENOMEM // not really that great a translation... |
199 | |
200 | // Mach-O supports sections (albeit with small names), but doesn't have |
201 | // vars at the beginning and end. Instead you should call the function |
202 | // getsectdata("__DATA", name, &size). |
203 | #define HAVE_ATTRIBUTE_SECTION 1 |
204 | |
205 | // Any function with ATTRIBUTE_SECTION must not be inlined, or it will |
206 | // be placed into whatever section its caller is placed into. |
207 | #define ATTRIBUTE_SECTION(name) \ |
208 | __attribute__ ((section ("__DATA, " #name))) __attribute__ ((noinline)) |
209 | |
210 | #define ENUM_DYLD_BOOL // so that we don't pollute the global namespace |
211 | extern "C" { |
212 | #include <mach-o/getsect.h> |
213 | #include <mach-o/dyld.h> |
214 | } |
215 | class AssignAttributeStartEnd { |
216 | public: |
217 | AssignAttributeStartEnd(const char* name, char** pstart, char** pend) { |
218 | // Find out what dynamic library name is defined in |
219 | for (int i = _dyld_image_count() - 1; i >= 0; --i) { |
220 | const mach_header* hdr = _dyld_get_image_header(i); |
221 | uint32_t len; |
222 | *pstart = getsectdatafromheader(hdr, "__DATA" , name, &len); |
223 | if (*pstart) { // NULL if not defined in this dynamic library |
224 | *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc |
225 | *pend = *pstart + len; |
226 | return; |
227 | } |
228 | } |
229 | // If we get here, not defined in a dll at all. See if defined statically. |
230 | unsigned long len; // don't ask me why this type isn't uint32_t too... |
231 | *pstart = getsectdata("__DATA" , name, &len); |
232 | *pend = *pstart + len; |
233 | } |
234 | }; |
235 | |
236 | // 1) DEFINE_ATTRIBUTE_SECTION_VARS: must be called once per unique |
237 | // name. You want to make sure this is executed before any |
238 | // DECLARE_ATTRIBUTE_SECTION_VARS; the easiest way is to put them |
239 | // in the same .cc file. Put this call at the global level. |
240 | // 2) INIT_ATTRIBUTE_SECTION_VARS: you can scatter calls to this in |
241 | // multiple places to help ensure execution before any |
242 | // DECLARE_ATTRIBUTE_SECTION_VARS. You must have at least one |
243 | // DEFINE, but you can have many INITs. Put each in its own scope. |
244 | // 3) DECLARE_ATTRIBUTE_SECTION_VARS: must be called before using |
245 | // ATTRIBUTE_SECTION_START or ATTRIBUTE_SECTION_STOP on a name. |
246 | // Put this call at the global level. |
247 | #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ |
248 | extern char* __start_##name; \ |
249 | extern char* __stop_##name; |
250 | |
251 | #define INIT_ATTRIBUTE_SECTION_VARS(name) \ |
252 | DECLARE_ATTRIBUTE_SECTION_VARS(name); \ |
253 | static const AssignAttributeStartEnd __assign_##name( \ |
254 | #name, &__start_##name, &__stop_##name) |
255 | |
256 | #define DEFINE_ATTRIBUTE_SECTION_VARS(name) \ |
257 | char* __start_##name, *__stop_##name; \ |
258 | INIT_ATTRIBUTE_SECTION_VARS(name) |
259 | |
260 | // Darwin doesn't have strnlen. No comment. |
261 | inline size_t strnlen(const char *s, size_t maxlen) { |
262 | const char* end = (const char *)memchr(s, '\0', maxlen); |
263 | if (end) |
264 | return end - s; |
265 | return maxlen; |
266 | } |
267 | |
268 | using namespace std; // just like VC++, we need a using here |
269 | |
270 | // Doesn't exist on OSX; used in google.cc for send() to mean "no flags". |
271 | #define MSG_NOSIGNAL 0 |
272 | |
273 | // No SIGPWR on MacOSX. SIGINFO seems suitably obscure. |
274 | #undef GOOGLE_OBSCURE_SIGNAL |
275 | #define GOOGLE_OBSCURE_SIGNAL SIGINFO |
276 | |
277 | #elif defined(OS_CYGWIN) // Cygwin-specific behavior. |
278 | |
279 | #if defined(__CYGWIN32__) |
280 | #define __WORDSIZE 32 |
281 | #else |
282 | // It's probably possible to support 64-bit, but the #defines will need checked. |
283 | #error "Cygwin is currently only 32-bit." |
284 | #endif |
285 | |
286 | // No signalling on Windows. |
287 | #undef GOOGLE_OBSCURE_SIGNAL |
288 | #define GOOGLE_OBSCURE_SIGNAL 0 |
289 | |
290 | struct stack_t { |
291 | void* ss_sp; |
292 | int ss_flags; |
293 | size_t ss_size; |
294 | }; |
295 | inline int sigaltstack(stack_t* ss, stack_t* oss) { return 0; } |
296 | |
297 | #define PTHREAD_STACK_MIN 0 // Not provided by cygwin |
298 | |
299 | // Scans memory for a character. |
300 | // memrchr is used in a few places, but it's linux-specific. |
301 | inline void* memrchr(const void* bytes, int find_char, size_t len) { |
302 | const unsigned char* cursor = |
303 | reinterpret_cast<const unsigned char*>(bytes) + len - 1; |
304 | unsigned char actual_char = find_char; |
305 | for (; cursor >= bytes; --cursor) { |
306 | if (*cursor == actual_char) { |
307 | return const_cast<void*>(reinterpret_cast<const void*>(cursor)); |
308 | } |
309 | } |
310 | return NULL; |
311 | } |
312 | |
313 | #endif |
314 | |
315 | // Klocwork static analysis tool's C/C++ complier kwcc |
316 | #if defined(__KLOCWORK__) |
317 | #define STATIC_ANALYSIS |
318 | #endif // __KLOCWORK__ |
319 | |
320 | // GCC-specific features |
321 | |
322 | #if (defined(COMPILER_GCC3) || defined(COMPILER_ICC) || defined(OS_MACOSX)) && !defined(SWIG) |
323 | |
324 | // |
325 | // Tell the compiler to do printf format string checking if the |
326 | // compiler supports it; see the 'format' attribute in |
327 | // <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>. |
328 | // |
329 | // N.B.: As the GCC manual states, "[s]ince non-static C++ methods |
330 | // have an implicit 'this' argument, the arguments of such methods |
331 | // should be counted from two, not one." |
332 | // |
333 | #define PRINTF_ATTRIBUTE(string_index, first_to_check) \ |
334 | __attribute__((__format__ (__printf__, string_index, first_to_check))) |
335 | #define SCANF_ATTRIBUTE(string_index, first_to_check) \ |
336 | __attribute__((__format__ (__scanf__, string_index, first_to_check))) |
337 | |
338 | // |
339 | // Prevent the compiler from padding a structure to natural alignment |
340 | // |
341 | #define PACKED __attribute__ ((packed)) |
342 | |
343 | // Cache line alignment |
344 | #if defined(__i386__) || defined(__x86_64__) |
345 | #define CACHELINE_SIZE 64 |
346 | #define CACHELINE_ALIGNED __attribute__((aligned(CACHELINE_SIZE))) |
347 | #elif defined(__ARM_ARCH_5T__) |
348 | #define CACHELINE_SIZE 32 |
349 | #define CACHELINE_ALIGNED |
350 | #else |
351 | #define CACHELINE_ALIGNED |
352 | #endif |
353 | |
354 | // |
355 | // Prevent the compiler from complaining about or optimizing away variables |
356 | // that appear unused |
357 | // (careful, others e.g. third_party/libxml/xmlversion.h also define this) |
358 | #undef ATTRIBUTE_UNUSED |
359 | #define ATTRIBUTE_UNUSED __attribute__ ((unused)) |
360 | |
361 | // |
362 | // For functions we want to force inline or not inline. |
363 | // Introduced in gcc 3.1. |
364 | #define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline)) |
365 | #define HAVE_ATTRIBUTE_ALWAYS_INLINE 1 |
366 | #define ATTRIBUTE_NOINLINE __attribute__ ((noinline)) |
367 | #define HAVE_ATTRIBUTE_NOINLINE 1 |
368 | |
369 | // For weak functions |
370 | #undef ATTRIBUTE_WEAK |
371 | #define ATTRIBUTE_WEAK __attribute__ ((weak)) |
372 | #define HAVE_ATTRIBUTE_WEAK 1 |
373 | |
374 | // Tell the compiler to use "initial-exec" mode for a thread-local variable. |
375 | // See http://people.redhat.com/drepper/tls.pdf for the gory details. |
376 | #define ATTRIBUTE_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec"))) |
377 | |
378 | // |
379 | // Tell the compiler that a given function never returns |
380 | // |
381 | #define ATTRIBUTE_NORETURN __attribute__((noreturn)) |
382 | |
383 | // For deprecated functions, variables, and types. |
384 | // gcc 3.1.1 and later provide this attribute. |
385 | // gcc 3.1.1 and later provide -Wdeprecated-declarations, on by default, |
386 | // and then -Werror converts such warning to an error |
387 | // gcc 4.2.1 and later provide -Wno-error=deprecated-declarations, |
388 | // so that use of a deprecated entity is a warning but not an error |
389 | // |
390 | // gcc 4.2.1 and gcc 4.2.2 ignore ATTRIBUTE_DEPRECATED on virtual functions. |
391 | // this is fixed in gcc 4.3.1 (crosstool v12). -- mec, 2008-10-21 |
392 | // |
393 | // 2010-05-19(mec): Failed. |
394 | // Too many people started deprecations and then stopped working on them. |
395 | // The deprecation messages just became build noise. |
396 | // The two-part deletion plan: |
397 | // change definition of ATTRIBUTE_DEPRECATED to an empty macro |
398 | // then global change: ATTRIBUTE_DEPRECATED -> /* deprecated */ |
399 | // We may introduce a new facility like this in the future, |
400 | // probably with a different name. See message from iant to c-style: |
401 | #define ATTRIBUTE_DEPRECATED |
402 | |
403 | #ifndef HAVE_ATTRIBUTE_SECTION // may have been pre-set to 0, e.g. for Darwin |
404 | #define HAVE_ATTRIBUTE_SECTION 1 |
405 | #endif |
406 | |
407 | #if HAVE_ATTRIBUTE_SECTION // define section support for the case of GCC |
408 | |
409 | // |
410 | // Tell the compiler/linker to put a given function into a section and define |
411 | // "__start_ ## name" and "__stop_ ## name" symbols to bracket the section. |
412 | // Sections can not span more than none compilation unit. |
413 | // This functionality is supported by GNU linker. |
414 | // Any function with ATTRIBUTE_SECTION must not be inlined, or it will |
415 | // be placed into whatever section its caller is placed into. |
416 | // |
417 | #ifndef ATTRIBUTE_SECTION |
418 | #define ATTRIBUTE_SECTION(name) \ |
419 | __attribute__ ((section (#name))) __attribute__ ((noinline)) |
420 | #endif |
421 | |
422 | // |
423 | // Weak section declaration to be used as a global declaration |
424 | // for ATTRIBUTE_SECTION_START|STOP(name) to compile and link |
425 | // even without functions with ATTRIBUTE_SECTION(name). |
426 | // DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's |
427 | // a no-op on ELF but not on Mach-O. |
428 | // |
429 | #ifndef DECLARE_ATTRIBUTE_SECTION_VARS |
430 | #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ |
431 | extern char __start_##name[] ATTRIBUTE_WEAK; \ |
432 | extern char __stop_##name[] ATTRIBUTE_WEAK |
433 | #endif |
434 | #ifndef DEFINE_ATTRIBUTE_SECTION_VARS |
435 | #define INIT_ATTRIBUTE_SECTION_VARS(name) |
436 | #define DEFINE_ATTRIBUTE_SECTION_VARS(name) |
437 | #endif |
438 | |
439 | // |
440 | // Return void* pointers to start/end of a section of code with |
441 | // functions having ATTRIBUTE_SECTION(name). |
442 | // Returns 0 if no such functions exits. |
443 | // One must DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and link. |
444 | // |
445 | #define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name)) |
446 | #define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name)) |
447 | |
448 | #endif // HAVE_ATTRIBUTE_SECTION |
449 | |
450 | // |
451 | // The legacy prod71 libc does not provide the stack alignment required for use |
452 | // of SSE intrinsics. In order to properly use the intrinsics you need to use |
453 | // a trampoline function which aligns the stack prior to calling your code, |
454 | // or as of crosstool v10 with gcc 4.2.0 there is an attribute which asks |
455 | // gcc to do this for you. |
456 | // |
457 | // It has also been discovered that crosstool up to and including v10 does not |
458 | // provide proper alignment for pthread_once() functions in x86-64 code either. |
459 | // Unfortunately gcc does not provide force_align_arg_pointer as an option in |
460 | // x86-64 code, so this requires us to always have a trampoline. |
461 | // |
462 | // For an example of using this see util/hash/adler32* |
463 | |
464 | #if defined(__i386__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) |
465 | #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC __attribute__((force_align_arg_pointer)) |
466 | #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
467 | #elif defined(__i386__) || defined(__x86_64__) |
468 | #define REQUIRE_STACK_ALIGN_TRAMPOLINE (1) |
469 | #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
470 | #else |
471 | #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
472 | #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
473 | #endif |
474 | |
475 | |
476 | // |
477 | // Tell the compiler to warn about unused return values for functions declared |
478 | // with this macro. The macro should be used on function declarations |
479 | // following the argument list: |
480 | // |
481 | // Sprocket* AllocateSprocket() MUST_USE_RESULT; |
482 | // |
483 | #undef MUST_USE_RESULT |
484 | #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ |
485 | && !defined(COMPILER_ICC) |
486 | #define MUST_USE_RESULT __attribute__ ((warn_unused_result)) |
487 | #else |
488 | #define MUST_USE_RESULT |
489 | #endif |
490 | |
491 | |
492 | #if (defined(COMPILER_ICC) || defined(COMPILER_GCC3)) |
493 | // Defined behavior on some of the uarchs: |
494 | // PREFETCH_HINT_T0: |
495 | // prefetch to all levels of the hierarchy (except on p4: prefetch to L2) |
496 | // PREFETCH_HINT_NTA: |
497 | // p4: fetch to L2, but limit to 1 way (out of the 8 ways) |
498 | // core: skip L2, go directly to L1 |
499 | // k8 rev E and later: skip L2, can go to either of the 2-ways in L1 |
500 | enum PrefetchHint { |
501 | PREFETCH_HINT_T0 = 3, // More temporal locality |
502 | PREFETCH_HINT_T1 = 2, |
503 | PREFETCH_HINT_T2 = 1, // Less temporal locality |
504 | PREFETCH_HINT_NTA = 0 // No temporal locality |
505 | }; |
506 | #else |
507 | // prefetch is a no-op for this target. Feel free to add more sections above. |
508 | #endif |
509 | |
510 | extern inline void prefetch(const char *x, int hint) { |
511 | #if defined(COMPILER_ICC) || defined(__llvm__) |
512 | // In the gcc version of prefetch(), hint is only a constant _after_ inlining |
513 | // (assumed to have been successful). icc views things differently, and |
514 | // checks constant-ness _before_ inlining. This leads to compilation errors |
515 | // with the gcc version in icc. |
516 | // |
517 | // One way round this is to use a switch statement to explicitly match |
518 | // prefetch hint enumerations, and invoke __builtin_prefetch for each valid |
519 | // value. icc's optimization removes the switch and unused case statements |
520 | // after inlining, so that this boils down in the end to the same as for gcc; |
521 | // that is, a single inlined prefetchX instruction. Demonstrate by compiling |
522 | // with icc options -xK -O2 and viewing assembly language output. |
523 | // |
524 | // Note that this version of prefetch() cannot verify constant-ness of hint. |
525 | // If client code calls prefetch() with a variable value for hint, it will |
526 | // receive the full expansion of the switch below, perhaps also not inlined. |
527 | // This should however not be a problem in the general case of well behaved |
528 | // caller code that uses the supplied prefetch hint enumerations. |
529 | switch (hint) { |
530 | case PREFETCH_HINT_T0: |
531 | __builtin_prefetch(x, 0, PREFETCH_HINT_T0); |
532 | break; |
533 | case PREFETCH_HINT_T1: |
534 | __builtin_prefetch(x, 0, PREFETCH_HINT_T1); |
535 | break; |
536 | case PREFETCH_HINT_T2: |
537 | __builtin_prefetch(x, 0, PREFETCH_HINT_T2); |
538 | break; |
539 | case PREFETCH_HINT_NTA: |
540 | __builtin_prefetch(x, 0, PREFETCH_HINT_NTA); |
541 | break; |
542 | default: |
543 | __builtin_prefetch(x); |
544 | break; |
545 | } |
546 | #elif defined(COMPILER_GCC3) |
547 | #if !defined(ARCH_PIII) || defined(__SSE__) |
548 | if (__builtin_constant_p(hint)) { |
549 | __builtin_prefetch(x, 0, hint); |
550 | } else { |
551 | // Defaults to PREFETCH_HINT_T0 |
552 | __builtin_prefetch(x); |
553 | } |
554 | #else |
555 | // We want a __builtin_prefetch, but we build with the default -march=i386 |
556 | // where __builtin_prefetch quietly turns into nothing. |
557 | // Once we crank up to -march=pentium3 or higher the __SSE__ |
558 | // clause above will kick in with the builtin. |
559 | // -- mec 2006-06-06 |
560 | if (hint == PREFETCH_HINT_NTA) |
561 | __asm__ __volatile__("prefetchnta (%0)" : : "r" (x)); |
562 | #endif |
563 | #else |
564 | // You get no effect. Feel free to add more sections above. |
565 | #endif |
566 | } |
567 | |
568 | #ifdef __cplusplus |
569 | // prefetch intrinsic (bring data to L1 without polluting L2 cache) |
570 | extern inline void prefetch(const char *x) { |
571 | return prefetch(x, 0); |
572 | } |
573 | #endif // ifdef __cplusplus |
574 | |
575 | // |
576 | // GCC can be told that a certain branch is not likely to be taken (for |
577 | // instance, a CHECK failure), and use that information in static analysis. |
578 | // Giving it this information can help it optimize for the common case in |
579 | // the absence of better information (ie. -fprofile-arcs). |
580 | // |
581 | #if defined(COMPILER_GCC3) |
582 | #define PREDICT_FALSE(x) (__builtin_expect(x, 0)) |
583 | #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) |
584 | #else |
585 | #define PREDICT_FALSE(x) x |
586 | #define PREDICT_TRUE(x) x |
587 | #endif |
588 | |
589 | #define FTELLO ftello |
590 | #define FSEEKO fseeko |
591 | |
592 | #if !defined(__cplusplus) && !defined(OS_MACOSX) && !defined(OS_CYGWIN) |
593 | // stdlib.h only declares this in C++, not in C, so we declare it here. |
594 | // Also make sure to avoid declaring it on platforms which don't support it. |
595 | extern int posix_memalign(void **memptr, size_t alignment, size_t size); |
596 | #endif |
597 | |
598 | inline void *aligned_malloc(size_t size, int minimum_alignment) { |
599 | #if defined(OS_MACOSX) |
600 | // mac lacks memalign(), posix_memalign(), however, according to |
601 | // http://stackoverflow.com/questions/196329/osx-lacks-memalign |
602 | // mac allocs are already 16-byte aligned. |
603 | if (minimum_alignment <= 16) |
604 | return malloc(size); |
605 | // next, try to return page-aligned memory. perhaps overkill |
606 | if (minimum_alignment <= getpagesize()) |
607 | return valloc(size); |
608 | // give up |
609 | return NULL; |
610 | #elif defined(OS_CYGWIN) |
611 | return memalign(minimum_alignment, size); |
612 | #else // !OS_MACOSX && !OS_CYGWIN |
613 | void *ptr = NULL; |
614 | if (posix_memalign(&ptr, minimum_alignment, size) != 0) |
615 | return NULL; |
616 | else |
617 | return ptr; |
618 | #endif |
619 | } |
620 | |
621 | inline void aligned_free(void *aligned_memory) { |
622 | free(aligned_memory); |
623 | } |
624 | |
625 | #else // not GCC |
626 | |
627 | #define PRINTF_ATTRIBUTE(string_index, first_to_check) |
628 | #define SCANF_ATTRIBUTE(string_index, first_to_check) |
629 | #define PACKED |
630 | #define CACHELINE_ALIGNED |
631 | #define ATTRIBUTE_UNUSED |
632 | #define ATTRIBUTE_ALWAYS_INLINE |
633 | #define ATTRIBUTE_NOINLINE |
634 | #define ATTRIBUTE_WEAK |
635 | #define HAVE_ATTRIBUTE_WEAK 0 |
636 | #define ATTRIBUTE_INITIAL_EXEC |
637 | #define ATTRIBUTE_NORETURN |
638 | #define ATTRIBUTE_DEPRECATED |
639 | #define HAVE_ATTRIBUTE_SECTION 0 |
640 | #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
641 | #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
642 | #define MUST_USE_RESULT |
643 | extern inline void prefetch(const char *x) {} |
644 | #define PREDICT_FALSE(x) x |
645 | #define PREDICT_TRUE(x) x |
646 | |
647 | // These should be redefined appropriately if better alternatives to |
648 | // ftell/fseek exist in the compiler |
649 | #define FTELLO ftell |
650 | #define FSEEKO fseek |
651 | |
652 | #endif // GCC |
653 | |
654 | #if !HAVE_ATTRIBUTE_SECTION // provide dummy definitions |
655 | |
656 | #define ATTRIBUTE_SECTION(name) |
657 | #define INIT_ATTRIBUTE_SECTION_VARS(name) |
658 | #define DEFINE_ATTRIBUTE_SECTION_VARS(name) |
659 | #define DECLARE_ATTRIBUTE_SECTION_VARS(name) |
660 | #define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(0)) |
661 | #define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(0)) |
662 | |
663 | #endif // !HAVE_ATTRIBUTE_SECTION |
664 | |
665 | |
666 | #ifdef COMPILER_MSVC /* if Visual C++ */ |
667 | |
668 | // This compiler flag can be easily overlooked on MSVC. |
669 | // _CHAR_UNSIGNED gets set with the /J flag. |
670 | #ifndef _CHAR_UNSIGNED |
671 | #error chars must be unsigned! Use the /J flag on the compiler command line. |
672 | #endif |
673 | |
674 | // MSVC is a little hyper-active in it's warnings |
675 | // Signed vs. unsigned comparison is ok. |
676 | #pragma warning(disable : 4018 ) |
677 | // We know casting from a long to a char may lose data |
678 | #pragma warning(disable : 4244 ) |
679 | // Don't need performance warnings about converting ints to bools |
680 | #pragma warning(disable : 4800 ) |
681 | // Integral constant overflow is apparently ok too |
682 | // for example: |
683 | // short k; int n; |
684 | // k = k + n; |
685 | #pragma warning(disable : 4307 ) |
686 | // It's ok to use this* in constructor |
687 | // Example: |
688 | // class C { |
689 | // Container cont_; |
690 | // C() : cont_(this) { ... |
691 | #pragma warning(disable : 4355 ) |
692 | // Truncating from double to float is ok |
693 | #pragma warning(disable : 4305 ) |
694 | |
695 | #include <winsock2.h> |
696 | #include <assert.h> |
697 | #include <windows.h> |
698 | #undef ERROR |
699 | #include "base/stl_decl.h" |
700 | |
701 | #include <float.h> // for nextafter functionality on windows |
702 | #include <math.h> // for HUGE_VAL |
703 | |
704 | #ifndef HUGE_VALF |
705 | #define HUGE_VALF (static_cast<float>(HUGE_VAL)) |
706 | #endif |
707 | |
708 | using namespace std; |
709 | |
710 | // VC++ doesn't understand "uint" |
711 | #ifndef HAVE_UINT |
712 | #define HAVE_UINT 1 |
713 | typedef unsigned int uint; |
714 | #endif |
715 | |
716 | #define strtoq _strtoi64 |
717 | #define strtouq _strtoui64 |
718 | #define strtoll _strtoi64 |
719 | #define strtoull _strtoui64 |
720 | #define atoll _atoi64 |
721 | |
722 | |
723 | // VC++ 6 and before ship without an ostream << operator for 64-bit ints |
724 | #if (_MSC_VER <= 1200) |
725 | #include <iosfwd> |
726 | using std::ostream; |
727 | |
728 | inline ostream& operator<< (ostream& os, const unsigned __int64& num ) { |
729 | // Fake operator; doesn't actually do anything. |
730 | LOG(FATAL) << "64-bit ostream operator << not supported in VC++ 6" ; |
731 | return os; |
732 | } |
733 | #endif |
734 | |
735 | // You say tomato, I say atotom |
736 | #define PATH_MAX MAX_PATH |
737 | |
738 | // You say tomato, I say _tomato |
739 | #define vsnprintf _vsnprintf |
740 | #define snprintf _snprintf |
741 | #define strcasecmp _stricmp |
742 | #define strncasecmp _strnicmp |
743 | |
744 | #define nextafter _nextafter |
745 | |
746 | #define hypot _hypot |
747 | #define hypotf _hypotf |
748 | |
749 | #define strdup _strdup |
750 | #define tempnam _tempnam |
751 | #define chdir _chdir |
752 | #define getcwd _getcwd |
753 | #define putenv _putenv |
754 | |
755 | |
756 | // You say tomato, I say toma |
757 | #define random() rand() |
758 | #define srandom(x) srand(x) |
759 | |
760 | // You say juxtapose, I say transpose |
761 | #define bcopy(s, d, n) memcpy(d, s, n) |
762 | |
763 | inline void *aligned_malloc(size_t size, int minimum_alignment) { |
764 | return _aligned_malloc(size, minimum_alignment); |
765 | } |
766 | |
767 | inline void aligned_free(void *aligned_memory) { |
768 | _aligned_free(aligned_memory); |
769 | } |
770 | |
771 | // ----- BEGIN VC++ STUBS & FAKE DEFINITIONS --------------------------------- |
772 | |
773 | // See http://en.wikipedia.org/wiki/IEEE_754 for details of |
774 | // floating point format. |
775 | |
776 | enum { |
777 | FP_NAN, // is "Not a Number" |
778 | FP_INFINITE, // is either plus or minus infinity. |
779 | FP_ZERO, |
780 | FP_SUBNORMAL, // is too small to be represented in normalized format. |
781 | FP_NORMAL // if nothing of the above is correct that it must be a |
782 | // normal floating-point number. |
783 | }; |
784 | |
785 | inline int fpclassify_double(double x) { |
786 | const int float_point_class =_fpclass(x); |
787 | int c99_class; |
788 | switch (float_point_class) { |
789 | case _FPCLASS_SNAN: // Signaling NaN |
790 | case _FPCLASS_QNAN: // Quiet NaN |
791 | c99_class = FP_NAN; |
792 | break; |
793 | case _FPCLASS_NZ: // Negative zero ( -0) |
794 | case _FPCLASS_PZ: // Positive 0 (+0) |
795 | c99_class = FP_ZERO; |
796 | break; |
797 | case _FPCLASS_NINF: // Negative infinity ( -INF) |
798 | case _FPCLASS_PINF: // Positive infinity (+INF) |
799 | c99_class = FP_INFINITE; |
800 | break; |
801 | case _FPCLASS_ND: // Negative denormalized |
802 | case _FPCLASS_PD: // Positive denormalized |
803 | c99_class = FP_SUBNORMAL; |
804 | break; |
805 | case _FPCLASS_NN: // Negative normalized non-zero |
806 | case _FPCLASS_PN: // Positive normalized non-zero |
807 | c99_class = FP_NORMAL; |
808 | break; |
809 | default: |
810 | c99_class = FP_NAN; // Should never happen |
811 | break; |
812 | } |
813 | return c99_class; |
814 | } |
815 | |
816 | // This function handle the special subnormal case for float; it will |
817 | // become a normal number while casting to double. |
818 | // bit_cast is avoided to simplify dependency and to create a code that is |
819 | // easy to deploy in C code |
820 | inline int fpclassify_float(float x) { |
821 | uint32 bitwise_representation; |
822 | memcpy(&bitwise_representation, &x, 4); |
823 | if ((bitwise_representation & 0x7f800000) == 0 && |
824 | (bitwise_representation & 0x007fffff) != 0) |
825 | return FP_SUBNORMAL; |
826 | return fpclassify_double(x); |
827 | } |
828 | // |
829 | // This define takes care of the denormalized float; the casting to |
830 | // double make it a normal number |
831 | #define fpclassify(x) ((sizeof(x) == sizeof(float)) ? fpclassify_float(x) : fpclassify_double(x)) |
832 | |
833 | #define isnan _isnan |
834 | |
835 | inline int isinf(double x) { |
836 | const int float_point_class =_fpclass(x); |
837 | if (float_point_class == _FPCLASS_PINF) return 1; |
838 | if (float_point_class == _FPCLASS_NINF) return -1; |
839 | return 0; |
840 | } |
841 | |
842 | // #include "conflict-signal.h" |
843 | typedef void (*sig_t)(int); |
844 | |
845 | // These actually belong in errno.h but there's a name confilict in errno |
846 | // on WinNT. They (and a ton more) are also found in Winsock2.h, but |
847 | // if'd out under NT. We need this subset at minimum. |
848 | #define EXFULL ENOMEM // not really that great a translation... |
849 | #define EWOULDBLOCK WSAEWOULDBLOCK |
850 | #ifndef PTHREADS_REDHAT_WIN32 |
851 | #define ETIMEDOUT WSAETIMEDOUT |
852 | #endif |
853 | #define ENOTSOCK WSAENOTSOCK |
854 | #define EINPROGRESS WSAEINPROGRESS |
855 | #define ECONNRESET WSAECONNRESET |
856 | |
857 | |
858 | #include <utility> |
859 | using std::pair; |
860 | using std::make_pair; |
861 | |
862 | #include <vector> |
863 | using std::vector; |
864 | |
865 | |
866 | typedef vector<pair<const char*, const char*> > KeyValVec; |
867 | |
868 | // |
869 | // Really from <string.h> |
870 | // |
871 | |
872 | inline void bzero(void *s, int n) { |
873 | memset(s, 0, n); |
874 | } |
875 | |
876 | // From glob.h |
877 | #define __ptr_t void * |
878 | |
879 | // Defined all over the place. |
880 | typedef int pid_t; |
881 | |
882 | // From stat.h |
883 | typedef unsigned int mode_t; |
884 | |
885 | // u_int16_t, int16_t don't exist in MSVC |
886 | typedef unsigned short u_int16_t; |
887 | typedef short int16_t; |
888 | |
889 | // ----- END VC++ STUBS & FAKE DEFINITIONS ---------------------------------- |
890 | |
891 | #endif // COMPILER_MSVC |
892 | |
893 | #ifdef STL_MSVC // not always the same as COMPILER_MSVC |
894 | #include "base/port_hash.h" |
895 | #else |
896 | struct PortableHashBase { }; |
897 | #endif |
898 | |
899 | // The SWIGged version of an abstract class must be concrete if any methods |
900 | // return objects of the abstract type. |
901 | // |
902 | // This location is deprecated, the new preferred location is in base/macros.h. |
903 | #ifndef SWIG |
904 | #define ABSTRACT = 0 |
905 | #endif |
906 | |
907 | |
908 | #if defined(OS_WINDOWS) || defined(OS_MACOSX) |
909 | // gethostbyname() *is* thread-safe for Windows native threads. It is also |
910 | // safe on Mac OS X, where it uses thread-local storage, even though the |
911 | // manpages claim otherwise. For details, see |
912 | // http://lists.apple.com/archives/Darwin-dev/2006/May/msg00008.html |
913 | #else |
914 | // gethostbyname() is not thread-safe. So disallow its use. People |
915 | // should either use the HostLookup::Lookup*() methods, or gethostbyname_r() |
916 | #define gethostbyname gethostbyname_is_not_thread_safe_DO_NOT_USE |
917 | #endif |
918 | |
919 | // create macros in which the programmer should enclose all specializations |
920 | // for hash_maps and hash_sets. This is necessary since these classes are not |
921 | // STL standardized. Depending on the STL implementation they are in different |
922 | // namespaces. Right now the right namespace is passed by the Makefile |
923 | // Examples: gcc3: -DHASH_NAMESPACE=__gnu_cxx |
924 | // icc: -DHASH_NAMESPACE=std |
925 | // gcc2: empty |
926 | |
927 | #ifndef HASH_NAMESPACE |
928 | # define HASH_NAMESPACE_DECLARATION_START |
929 | # define HASH_NAMESPACE_DECLARATION_END |
930 | #else |
931 | # define HASH_NAMESPACE_DECLARATION_START namespace HASH_NAMESPACE { |
932 | # define HASH_NAMESPACE_DECLARATION_END } |
933 | #endif |
934 | |
935 | // Our STL-like classes use __STD. |
936 | #if defined(COMPILER_GCC3) || defined(COMPILER_ICC) || defined(OS_MACOSX) || defined(COMPILER_MSVC) |
937 | #define __STD std |
938 | #endif |
939 | |
940 | #if defined COMPILER_GCC3 || defined COMPILER_ICC |
941 | #define STREAM_SET(s, bit) (s).setstate(ios_base::bit) |
942 | #define STREAM_SETF(s, flag) (s).setf(ios_base::flag) |
943 | #else |
944 | #define STREAM_SET(s, bit) (s).set(ios::bit) |
945 | #define STREAM_SETF(s, flag) (s).setf(ios::flag) |
946 | #endif |
947 | |
948 | // Portable handling of unaligned loads and stores |
949 | |
950 | #if defined(ARCH_PIII) || defined(ARCH_ATHLON) || defined(ARCH_K8) || defined(_ARCH_PPC) |
951 | |
952 | // x86 and x86-64 can perform unaligned loads/stores directly; |
953 | // modern PowerPC hardware can also do unaligned integer loads and stores; |
954 | // but note: the FPU still sends unaligned loads and stores to a trap handler! |
955 | |
956 | #define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p)) |
957 | #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p)) |
958 | #define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p)) |
959 | |
960 | #define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val)) |
961 | #define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val)) |
962 | #define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val)) |
963 | |
964 | #else |
965 | |
966 | #define NEED_ALIGNED_LOADS |
967 | |
968 | // These functions are provided for architectures that don't support |
969 | // unaligned loads and stores. |
970 | |
971 | inline uint16 UNALIGNED_LOAD16(const void *p) { |
972 | uint16 t; |
973 | memcpy(&t, p, sizeof t); |
974 | return t; |
975 | } |
976 | |
977 | inline uint32 UNALIGNED_LOAD32(const void *p) { |
978 | uint32 t; |
979 | memcpy(&t, p, sizeof t); |
980 | return t; |
981 | } |
982 | |
983 | inline uint64 UNALIGNED_LOAD64(const void *p) { |
984 | uint64 t; |
985 | memcpy(&t, p, sizeof t); |
986 | return t; |
987 | } |
988 | |
989 | inline void UNALIGNED_STORE16(void *p, uint16 v) { |
990 | memcpy(p, &v, sizeof v); |
991 | } |
992 | |
993 | inline void UNALIGNED_STORE32(void *p, uint32 v) { |
994 | memcpy(p, &v, sizeof v); |
995 | } |
996 | |
997 | inline void UNALIGNED_STORE64(void *p, uint64 v) { |
998 | memcpy(p, &v, sizeof v); |
999 | } |
1000 | |
1001 | #endif |
1002 | |
1003 | #ifdef _LP64 |
1004 | #define UNALIGNED_LOADW(_p) UNALIGNED_LOAD64(_p) |
1005 | #define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE64(_p, _val) |
1006 | #else |
1007 | #define UNALIGNED_LOADW(_p) UNALIGNED_LOAD32(_p) |
1008 | #define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE32(_p, _val) |
1009 | #endif |
1010 | |
1011 | // printf macros for size_t, in the style of inttypes.h |
1012 | #ifdef _LP64 |
1013 | #define __PRIS_PREFIX "z" |
1014 | #else |
1015 | #define __PRIS_PREFIX |
1016 | #endif |
1017 | |
1018 | // Use these macros after a % in a printf format string |
1019 | // to get correct 32/64 bit behavior, like this: |
1020 | // size_t size = records.size(); |
1021 | // printf("%"PRIuS"\n", size); |
1022 | |
1023 | #define PRIdS __PRIS_PREFIX "d" |
1024 | #define PRIxS __PRIS_PREFIX "x" |
1025 | #define PRIuS __PRIS_PREFIX "u" |
1026 | #define PRIXS __PRIS_PREFIX "X" |
1027 | #define PRIoS __PRIS_PREFIX "o" |
1028 | |
1029 | #define GPRIuPTHREAD "lu" |
1030 | #define GPRIxPTHREAD "lx" |
1031 | #ifdef OS_CYGWIN |
1032 | #define PRINTABLE_PTHREAD(pthreadt) reinterpret_cast<uintptr_t>(pthreadt) |
1033 | #else |
1034 | #define PRINTABLE_PTHREAD(pthreadt) pthreadt |
1035 | #endif |
1036 | |
1037 | #define SIZEOF_MEMBER(t, f) sizeof(((t*) 4096)->f) |
1038 | |
1039 | #define OFFSETOF_MEMBER(t, f) \ |
1040 | (reinterpret_cast<char*>( \ |
1041 | &reinterpret_cast<t*>(16)->f) - \ |
1042 | reinterpret_cast<char*>(16)) |
1043 | |
1044 | #ifdef PTHREADS_REDHAT_WIN32 |
1045 | #include <iosfwd> |
1046 | using std::ostream; |
1047 | |
1048 | #include <pthread.h> |
1049 | // pthread_t is not a simple integer or pointer on Win32 |
1050 | std::ostream& operator << (std::ostream& out, const pthread_t& thread_id); |
1051 | #endif |
1052 | |
1053 | #endif // BASE_PORT_H_ |
1054 | |