00001 // Copyright (c) 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 #ifndef _BASICTYPES_H_ 00031 #define _BASICTYPES_H_ 00032 00033 #include "config.h" 00034 00035 // To use this in an autoconf setting, make sure you run the following 00036 // autoconf macros: 00037 // AC_HEADER_STDC /* for stdint_h and inttypes_h */ 00038 // AC_CHECK_TYPES([__int64]) /* defined in some windows platforms */ 00039 00040 #ifdef HAVE_INTTYPES_H 00041 #include <inttypes.h> // uint16_t might be here; PRId64 too. 00042 #endif 00043 #ifdef HAVE_STDINT_H 00044 #include <stdint.h> // to get uint16_t (ISO naming madness) 00045 #endif 00046 #include <sys/types.h> // our last best hope for uint16_t 00047 00048 // Standard typedefs 00049 // All Google code is compiled with -funsigned-char to make "char" 00050 // unsigned. Google code therefore doesn't need a "uchar" type. 00051 // TODO(csilvers): how do we make sure unsigned-char works on non-gcc systems? 00052 typedef signed char schar; 00053 typedef int8_t int8; 00054 typedef int16_t int16; 00055 typedef int32_t int32; 00056 typedef int64_t int64; 00057 00058 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical 00059 // places. Use the signed types unless your variable represents a bit 00060 // pattern (eg a hash value) or you really need the extra bit. Do NOT 00061 // use 'unsigned' to express "this value should always be positive"; 00062 // use assertions for this. 00063 00064 typedef uint8_t uint8; 00065 typedef uint16_t uint16; 00066 typedef uint32_t uint32; 00067 typedef uint64_t uint64; 00068 00069 const uint16 kuint16max = ( (uint16) 0xFFFF); 00070 const uint32 kuint32max = ( (uint32) 0xFFFFFFFF); 00071 const uint64 kuint64max = ( (((uint64) kuint32max) << 32) | kuint32max ); 00072 00073 const int8 kint8max = ( ( int8) 0x7F); 00074 const int16 kint16max = ( ( int16) 0x7FFF); 00075 const int32 kint32max = ( ( int32) 0x7FFFFFFF); 00076 const int64 kint64max = ( ((( int64) kint32max) << 32) | kuint32max ); 00077 00078 const int8 kint8min = ( ( int8) 0x80); 00079 const int16 kint16min = ( ( int16) 0x8000); 00080 const int32 kint32min = ( ( int32) 0x80000000); 00081 const int64 kint64min = ( ((( int64) kint32min) << 32) | 0 ); 00082 00083 // Define the "portable" printf and scanf macros, if they're not already there 00084 // We just do something that works on many systems, and hope for the best 00085 #ifndef PRIx64 00086 #define PRIx64 "llx" 00087 #endif 00088 #ifndef SCNx64 00089 #define SCNx64 "llx" 00090 #endif 00091 #ifndef PRId64 00092 #define PRId64 "lld" 00093 #endif 00094 #ifndef SCNd64 00095 #define SCNd64 "lld" 00096 #endif 00097 #ifndef PRIu64 00098 #define PRIu64 "llu" 00099 #endif 00100 #ifndef PRIxPTR 00101 #define PRIxPTR PRIx64 00102 #endif 00103 00104 // A macro to disallow the evil copy constructor and operator= functions 00105 // This should be used in the private: declarations for a class 00106 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ 00107 TypeName(const TypeName&); \ 00108 void operator=(const TypeName&) 00109 00110 // The COMPILE_ASSERT macro can be used to verify that a compile time 00111 // expression is true. For example, you could use it to verify the 00112 // size of a static array: 00113 // 00114 // COMPILE_ASSERT(sizeof(num_content_type_names) == sizeof(int), 00115 // content_type_names_incorrect_size); 00116 // 00117 // or to make sure a struct is smaller than a certain size: 00118 // 00119 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 00120 // 00121 // The second argument to the macro is the name of the variable. If 00122 // the expression is false, most compilers will issue a warning/error 00123 // containing the name of the variable. 00124 // 00125 // Implementation details of COMPILE_ASSERT: 00126 // 00127 // - COMPILE_ASSERT works by defining an array type that has -1 00128 // elements (and thus is invalid) when the expression is false. 00129 // 00130 // - The simpler definition 00131 // 00132 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 00133 // 00134 // does not work, as gcc supports variable-length arrays whose sizes 00135 // are determined at run-time (this is gcc's extension and not part 00136 // of the C++ standard). As a result, gcc fails to reject the 00137 // following code with the simple definition: 00138 // 00139 // int foo; 00140 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is 00141 // // not a compile-time constant. 00142 // 00143 // - By using the type CompileAssert<(bool(expr))>, we ensures that 00144 // expr is a compile-time constant. (Template arguments must be 00145 // determined at compile-time.) 00146 // 00147 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary 00148 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written 00149 // 00150 // CompileAssert<bool(expr)> 00151 // 00152 // instead, these compilers will refuse to compile 00153 // 00154 // COMPILE_ASSERT(5 > 0, some_message); 00155 // 00156 // (They seem to think the ">" in "5 > 0" marks the end of the 00157 // template argument list.) 00158 // 00159 // - The array size is (bool(expr) ? 1 : -1), instead of simply 00160 // 00161 // ((expr) ? 1 : -1). 00162 // 00163 // This is to avoid running into a bug in MS VC 7.1, which 00164 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 00165 00166 template <bool> 00167 struct CompileAssert { 00168 }; 00169 00170 #define COMPILE_ASSERT(expr, msg) \ 00171 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 00172 00173 #define arraysize(a) (sizeof(a) / sizeof(*(a))) 00174 00175 #define OFFSETOF_MEMBER(strct, field) \ 00176 (reinterpret_cast<char*>(&reinterpret_cast<strct*>(16)->field) - \ 00177 reinterpret_cast<char*>(16)) 00178 00179 #ifdef HAVE___ATTRIBUTE__ 00180 # define ATTRIBUTE_WEAK __attribute__((weak)) 00181 #else 00182 # define ATTRIBUTE_WEAK 00183 #endif 00184 00185 // Section attributes are supported for both ELF and Mach-O, but in 00186 // very different ways. Here's the API we provide: 00187 // 1) ATTRIBUTE_SECTION: put this with the declaration of all functions 00188 // you want to be in the same linker section 00189 // 2) DEFINE_ATTRIBUTE_SECTION_VARS: must be called once per unique 00190 // name. You want to make sure this is executed before any 00191 // DECLARE_ATTRIBUTE_SECTION_VARS; the easiest way is to put them 00192 // in the same .cc file. Put this call at the global level. 00193 // 3) INIT_ATTRIBUTE_SECTION_VARS: you can scatter calls to this in 00194 // multiple places to help ensure execution before any 00195 // DECLARE_ATTRIBUTE_SECTION_VARS. You must have at least one 00196 // DEFINE, but you can have many INITs. Put each in its own scope. 00197 // 4) DECLARE_ATTRIBUTE_SECTION_VARS: must be called before using 00198 // ATTRIBUTE_SECTION_START or ATTRIBUTE_SECTION_STOP on a name. 00199 // Put this call at the global level. 00200 // 5) ATTRIBUTE_SECTION_START/ATTRIBUTE_SECTION_STOP: call this to say 00201 // where in memory a given section is. All functions declared with 00202 // ATTRIBUTE_SECTION are guaranteed to be between START and STOP. 00203 00204 #if defined(HAVE___ATTRIBUTE__) && defined(__ELF__) 00205 # define ATTRIBUTE_SECTION(name) __attribute__ ((section (#name))) 00206 00207 // Weak section declaration to be used as a global declaration 00208 // for ATTRIBUTE_SECTION_START|STOP(name) to compile and link 00209 // even without functions with ATTRIBUTE_SECTION(name). 00210 # define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 00211 extern char __start_##name[] ATTRIBUTE_WEAK; \ 00212 extern char __stop_##name[] ATTRIBUTE_WEAK 00213 # define INIT_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF 00214 # define DEFINE_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF 00215 00216 // Return void* pointers to start/end of a section of code with functions 00217 // having ATTRIBUTE_SECTION(name), or 0 if no such function exists. 00218 // One must DECLARE_ATTRIBUTE_SECTION(name) for this to compile and link. 00219 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name)) 00220 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name)) 00221 # define HAVE_ATTRIBUTE_SECTION_START 1 00222 00223 #elif defined(HAVE___ATTRIBUTE__) && defined(__MACH__) 00224 # define ATTRIBUTE_SECTION(name) __attribute__ ((section ("__DATA, " #name))) 00225 00226 extern "C" { 00227 #include <mach-o/getsect.h> 00228 #include <mach-o/dyld.h> 00229 } 00230 00231 class AssignAttributeStartEnd { 00232 public: 00233 AssignAttributeStartEnd(const char* name, char** pstart, char** pend) { 00234 // Find out what dynamic library name is defined in 00235 if (_dyld_present()) { 00236 for (int i = _dyld_image_count() - 1; i >= 0; --i) { 00237 const mach_header* hdr = _dyld_get_image_header(i); 00238 uint32_t len; 00239 *pstart = getsectdatafromheader(hdr, "__DATA", name, &len); 00240 if (*pstart) { // NULL if not defined in this dynamic library 00241 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc 00242 *pend = *pstart + len; 00243 return; 00244 } 00245 } 00246 } 00247 // If we get here, not defined in a dll at all. See if defined statically. 00248 unsigned long len; // don't ask me why this type isn't uint32_t too... 00249 *pstart = getsectdata("__DATA", name, &len); 00250 *pend = *pstart + len; 00251 } 00252 }; 00253 00254 #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 00255 extern char* __start_##name; \ 00256 extern char* __stop_##name 00257 00258 #define INIT_ATTRIBUTE_SECTION_VARS(name) \ 00259 DECLARE_ATTRIBUTE_SECTION_VARS(name); \ 00260 static const AssignAttributeStartEnd __assign_##name( \ 00261 #name, &__start_##name, &__stop_##name) 00262 00263 #define DEFINE_ATTRIBUTE_SECTION_VARS(name) \ 00264 char* __start_##name, *__stop_##name; \ 00265 INIT_ATTRIBUTE_SECTION_VARS(name) 00266 00267 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name)) 00268 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name)) 00269 # define HAVE_ATTRIBUTE_SECTION_START 1 00270 00271 #else // not HAVE___ATTRIBUTE__ && __ELF__, nor HAVE___ATTRIBUTE__ && __MACH__ 00272 # define ATTRIBUTE_SECTION(name) 00273 # define DECLARE_ATTRIBUTE_SECTION_VARS(name) 00274 # define INIT_ATTRIBUTE_SECTION_VARS(name) 00275 # define DEFINE_ATTRIBUTE_SECTION_VARS(name) 00276 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(0)) 00277 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(0)) 00278 00279 #endif // HAVE___ATTRIBUTE__ and __ELF__ or __MACH__ 00280 00281 #endif // _BASICTYPES_H_