henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_BASICTYPES_H_ |
| 29 | #define TALK_BASE_BASICTYPES_H_ |
| 30 | |
| 31 | #include <stddef.h> // for NULL, size_t |
| 32 | |
| 33 | #if !(defined(_MSC_VER) && (_MSC_VER < 1600)) |
| 34 | #include <stdint.h> // for uintptr_t |
| 35 | #endif |
| 36 | |
| 37 | #ifdef HAVE_CONFIG_H |
| 38 | #include "config.h" // NOLINT |
| 39 | #endif |
| 40 | |
| 41 | #include "talk/base/constructormagic.h" |
| 42 | |
| 43 | #if !defined(INT_TYPES_DEFINED) |
| 44 | #define INT_TYPES_DEFINED |
| 45 | #ifdef COMPILER_MSVC |
| 46 | typedef unsigned __int64 uint64; |
| 47 | typedef __int64 int64; |
| 48 | #ifndef INT64_C |
| 49 | #define INT64_C(x) x ## I64 |
| 50 | #endif |
| 51 | #ifndef UINT64_C |
| 52 | #define UINT64_C(x) x ## UI64 |
| 53 | #endif |
| 54 | #define INT64_F "I64" |
| 55 | #else // COMPILER_MSVC |
| 56 | // On Mac OS X, cssmconfig.h defines uint64 as uint64_t |
| 57 | // TODO(fbarchard): Use long long for compatibility with chromium on BSD/OSX. |
| 58 | #if defined(OSX) |
| 59 | typedef uint64_t uint64; |
| 60 | typedef int64_t int64; |
| 61 | #ifndef INT64_C |
| 62 | #define INT64_C(x) x ## LL |
| 63 | #endif |
| 64 | #ifndef UINT64_C |
| 65 | #define UINT64_C(x) x ## ULL |
| 66 | #endif |
| 67 | #define INT64_F "l" |
| 68 | #elif defined(__LP64__) |
| 69 | typedef unsigned long uint64; // NOLINT |
| 70 | typedef long int64; // NOLINT |
| 71 | #ifndef INT64_C |
| 72 | #define INT64_C(x) x ## L |
| 73 | #endif |
| 74 | #ifndef UINT64_C |
| 75 | #define UINT64_C(x) x ## UL |
| 76 | #endif |
| 77 | #define INT64_F "l" |
| 78 | #else // __LP64__ |
| 79 | typedef unsigned long long uint64; // NOLINT |
| 80 | typedef long long int64; // NOLINT |
| 81 | #ifndef INT64_C |
| 82 | #define INT64_C(x) x ## LL |
| 83 | #endif |
| 84 | #ifndef UINT64_C |
| 85 | #define UINT64_C(x) x ## ULL |
| 86 | #endif |
| 87 | #define INT64_F "ll" |
| 88 | #endif // __LP64__ |
| 89 | #endif // COMPILER_MSVC |
| 90 | typedef unsigned int uint32; |
| 91 | typedef int int32; |
| 92 | typedef unsigned short uint16; // NOLINT |
| 93 | typedef short int16; // NOLINT |
| 94 | typedef unsigned char uint8; |
| 95 | typedef signed char int8; |
| 96 | #endif // INT_TYPES_DEFINED |
| 97 | |
| 98 | // Detect compiler is for x86 or x64. |
| 99 | #if defined(__x86_64__) || defined(_M_X64) || \ |
| 100 | defined(__i386__) || defined(_M_IX86) |
| 101 | #define CPU_X86 1 |
| 102 | #endif |
| 103 | // Detect compiler is for arm. |
| 104 | #if defined(__arm__) || defined(_M_ARM) |
| 105 | #define CPU_ARM 1 |
| 106 | #endif |
| 107 | #if defined(CPU_X86) && defined(CPU_ARM) |
| 108 | #error CPU_X86 and CPU_ARM both defined. |
| 109 | #endif |
| 110 | #if !defined(ARCH_CPU_BIG_ENDIAN) && !defined(ARCH_CPU_LITTLE_ENDIAN) |
| 111 | // x86, arm or GCC provided __BYTE_ORDER__ macros |
| 112 | #if CPU_X86 || CPU_ARM || \ |
| 113 | (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 114 | #define ARCH_CPU_LITTLE_ENDIAN |
| 115 | #elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 116 | #define ARCH_CPU_BIG_ENDIAN |
| 117 | #else |
| 118 | #error ARCH_CPU_BIG_ENDIAN or ARCH_CPU_LITTLE_ENDIAN should be defined. |
| 119 | #endif |
| 120 | #endif |
| 121 | #if defined(ARCH_CPU_BIG_ENDIAN) && defined(ARCH_CPU_LITTLE_ENDIAN) |
| 122 | #error ARCH_CPU_BIG_ENDIAN and ARCH_CPU_LITTLE_ENDIAN both defined. |
| 123 | #endif |
| 124 | |
| 125 | #ifdef WIN32 |
| 126 | typedef int socklen_t; |
| 127 | #endif |
| 128 | |
| 129 | // The following only works for C++ |
| 130 | #ifdef __cplusplus |
| 131 | namespace talk_base { |
| 132 | template<class T> inline T _min(T a, T b) { return (a > b) ? b : a; } |
| 133 | template<class T> inline T _max(T a, T b) { return (a < b) ? b : a; } |
| 134 | |
| 135 | // For wait functions that take a number of milliseconds, kForever indicates |
| 136 | // unlimited time. |
| 137 | const int kForever = -1; |
| 138 | } |
| 139 | |
| 140 | #define ALIGNP(p, t) \ |
| 141 | (reinterpret_cast<uint8*>(((reinterpret_cast<uintptr_t>(p) + \ |
| 142 | ((t) - 1)) & ~((t) - 1)))) |
| 143 | #define IS_ALIGNED(p, a) (!((uintptr_t)(p) & ((a) - 1))) |
| 144 | |
| 145 | // Note: UNUSED is also defined in common.h |
| 146 | #ifndef UNUSED |
| 147 | #define UNUSED(x) Unused(static_cast<const void*>(&x)) |
| 148 | #define UNUSED2(x, y) Unused(static_cast<const void*>(&x)); \ |
| 149 | Unused(static_cast<const void*>(&y)) |
| 150 | #define UNUSED3(x, y, z) Unused(static_cast<const void*>(&x)); \ |
| 151 | Unused(static_cast<const void*>(&y)); \ |
| 152 | Unused(static_cast<const void*>(&z)) |
| 153 | #define UNUSED4(x, y, z, a) Unused(static_cast<const void*>(&x)); \ |
| 154 | Unused(static_cast<const void*>(&y)); \ |
| 155 | Unused(static_cast<const void*>(&z)); \ |
| 156 | Unused(static_cast<const void*>(&a)) |
| 157 | #define UNUSED5(x, y, z, a, b) Unused(static_cast<const void*>(&x)); \ |
| 158 | Unused(static_cast<const void*>(&y)); \ |
| 159 | Unused(static_cast<const void*>(&z)); \ |
| 160 | Unused(static_cast<const void*>(&a)); \ |
| 161 | Unused(static_cast<const void*>(&b)) |
| 162 | inline void Unused(const void*) {} |
| 163 | #endif // UNUSED |
| 164 | |
| 165 | // Use these to declare and define a static local variable (static T;) so that |
| 166 | // it is leaked so that its destructors are not called at exit. |
| 167 | #define LIBJINGLE_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
| 168 | static type& name = *new type arguments |
| 169 | |
| 170 | #endif // __cplusplus |
| 171 | #endif // TALK_BASE_BASICTYPES_H_ |