| 1 | #ifndef MY_COMPILER_INCLUDED |
| 2 | #define MY_COMPILER_INCLUDED |
| 3 | |
| 4 | /* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 18 | |
| 19 | /** |
| 20 | Header for compiler-dependent features. |
| 21 | |
| 22 | Intended to contain a set of reusable wrappers for preprocessor |
| 23 | macros, attributes, pragmas, and any other features that are |
| 24 | specific to a target compiler. |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | Compiler-dependent internal convenience macros. |
| 29 | */ |
| 30 | |
| 31 | /* GNU C/C++ */ |
| 32 | #if defined __GNUC__ |
| 33 | /* Convenience macro to test the minimum required GCC version. */ |
| 34 | # define MY_GNUC_PREREQ(maj, min) \ |
| 35 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
| 36 | /* Any after 2.95... */ |
| 37 | # define MY_ALIGN_EXT |
| 38 | /* Comunicate to the compiler the unreachability of the code. */ |
| 39 | # if MY_GNUC_PREREQ(4,5) |
| 40 | # define MY_ASSERT_UNREACHABLE() __builtin_unreachable() |
| 41 | # endif |
| 42 | |
| 43 | /* Microsoft Visual C++ */ |
| 44 | #elif defined _MSC_VER |
| 45 | # define MY_ALIGNOF(type) __alignof(type) |
| 46 | # define MY_ALIGNED(n) __declspec(align(n)) |
| 47 | |
| 48 | /* Oracle Solaris Studio */ |
| 49 | #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) |
| 50 | # if __SUNPRO_C >= 0x590 |
| 51 | # define MY_ALIGN_EXT |
| 52 | # endif |
| 53 | |
| 54 | /* IBM XL C/C++ */ |
| 55 | #elif defined __xlC__ |
| 56 | # if __xlC__ >= 0x0600 |
| 57 | # define MY_ALIGN_EXT |
| 58 | # endif |
| 59 | |
| 60 | /* HP aCC */ |
| 61 | #elif defined(__HP_aCC) || defined(__HP_cc) |
| 62 | # if (__HP_aCC >= 60000) || (__HP_cc >= 60000) |
| 63 | # define MY_ALIGN_EXT |
| 64 | # endif |
| 65 | #endif |
| 66 | |
| 67 | #ifdef MY_ALIGN_EXT |
| 68 | /** Specifies the minimum alignment of a type. */ |
| 69 | # define MY_ALIGNOF(type) __alignof__(type) |
| 70 | /** Determine the alignment requirement of a type. */ |
| 71 | # define MY_ALIGNED(n) __attribute__((__aligned__((n)))) |
| 72 | #endif |
| 73 | |
| 74 | /** |
| 75 | Generic (compiler-independent) features. |
| 76 | */ |
| 77 | |
| 78 | #ifndef MY_GNUC_PREREQ |
| 79 | # define MY_GNUC_PREREQ(maj, min) (0) |
| 80 | #endif |
| 81 | |
| 82 | #ifndef MY_ALIGNOF |
| 83 | # ifdef __cplusplus |
| 84 | template<typename type> struct my_alignof_helper { char m1; type m2; }; |
| 85 | /* Invalid for non-POD types, but most compilers give the right answer. */ |
| 86 | # define MY_ALIGNOF(type) offsetof(my_alignof_helper<type>, m2) |
| 87 | # else |
| 88 | # define MY_ALIGNOF(type) offsetof(struct { char m1; type m2; }, m2) |
| 89 | # endif |
| 90 | #endif |
| 91 | |
| 92 | #ifndef MY_ASSERT_UNREACHABLE |
| 93 | # define MY_ASSERT_UNREACHABLE() do { assert(0); } while (0) |
| 94 | #endif |
| 95 | |
| 96 | /** |
| 97 | C++ Type Traits |
| 98 | */ |
| 99 | |
| 100 | #ifdef __cplusplus |
| 101 | |
| 102 | /** |
| 103 | Opaque storage with a particular alignment. |
| 104 | */ |
| 105 | # if defined(MY_ALIGNED) |
| 106 | /* Partial specialization used due to MSVC++. */ |
| 107 | template<size_t alignment> struct my_alignment_imp; |
| 108 | template<> struct MY_ALIGNED(1) my_alignment_imp<1> {}; |
| 109 | template<> struct MY_ALIGNED(2) my_alignment_imp<2> {}; |
| 110 | template<> struct MY_ALIGNED(4) my_alignment_imp<4> {}; |
| 111 | template<> struct MY_ALIGNED(8) my_alignment_imp<8> {}; |
| 112 | template<> struct MY_ALIGNED(16) my_alignment_imp<16> {}; |
| 113 | /* ... expand as necessary. */ |
| 114 | # else |
| 115 | template<size_t alignment> |
| 116 | struct my_alignment_imp { double m1; }; |
| 117 | # endif |
| 118 | |
| 119 | /** |
| 120 | A POD type with a given size and alignment. |
| 121 | |
| 122 | @remark If the compiler does not support a alignment attribute |
| 123 | (MY_ALIGN macro), the default alignment of a double is |
| 124 | used instead. |
| 125 | |
| 126 | @tparam size The minimum size. |
| 127 | @tparam alignment The desired alignment: 1, 2, 4, 8 or 16. |
| 128 | */ |
| 129 | template <size_t size, size_t alignment> |
| 130 | struct my_aligned_storage |
| 131 | { |
| 132 | union |
| 133 | { |
| 134 | char data[size]; |
| 135 | my_alignment_imp<alignment> align; |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | #endif /* __cplusplus */ |
| 140 | |
| 141 | # ifndef MY_ALIGNED |
| 142 | /* |
| 143 | Make sure MY_ALIGNED can be used also on platforms where we don't |
| 144 | have a way of aligning data structures. |
| 145 | */ |
| 146 | #define MY_ALIGNED(size) |
| 147 | #endif |
| 148 | |
| 149 | #ifdef __GNUC__ |
| 150 | # define ATTRIBUTE_NORETURN __attribute__((noreturn)) |
| 151 | # if MY_GNUC_PREREQ(4,3) |
| 152 | /** Starting with GCC 4.3, the "cold" attribute is used to inform the |
| 153 | compiler that a function is unlikely executed. The function is |
| 154 | optimized for size rather than speed and on many targets it is placed |
| 155 | into special subsection of the text section so all cold functions |
| 156 | appears close together improving code locality of non-cold parts of |
| 157 | program. The paths leading to call of cold functions within code are |
| 158 | marked as unlikely by the branch prediction mechanism. optimize a |
| 159 | rarely invoked function for size instead for speed. */ |
| 160 | # define ATTRIBUTE_COLD __attribute__((cold)) |
| 161 | # endif |
| 162 | #elif defined _MSC_VER |
| 163 | # define ATTRIBUTE_NORETURN __declspec(noreturn) |
| 164 | #else |
| 165 | # define ATTRIBUTE_NORETURN /* empty */ |
| 166 | #endif |
| 167 | |
| 168 | #ifndef ATTRIBUTE_COLD |
| 169 | # define ATTRIBUTE_COLD /* empty */ |
| 170 | #endif |
| 171 | |
| 172 | #include <my_attribute.h> |
| 173 | |
| 174 | #endif /* MY_COMPILER_INCLUDED */ |
| 175 | |