basictypes.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
19720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Copyright (c) 2005, Google Inc. 29720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// All rights reserved. 39720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// 49720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Redistribution and use in source and binary forms, with or without 59720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// modification, are permitted provided that the following conditions are 69720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// met: 79720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// 89720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// * Redistributions of source code must retain the above copyright 99720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// notice, this list of conditions and the following disclaimer. 109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// * Redistributions in binary form must reproduce the above 119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// copyright notice, this list of conditions and the following disclaimer 129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// in the documentation and/or other materials provided with the 139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// distribution. 149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// * Neither the name of Google Inc. nor the names of its 159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// contributors may be used to endorse or promote products derived from 169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// this software without specific prior written permission. 179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// 189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block 309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifndef _BASICTYPES_H_ 319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#define _BASICTYPES_H_ 32e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 33e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <config.h> 34e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <string.h> // for memcpy() 35e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#ifdef HAVE_INTTYPES_H 36e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <inttypes.h> // gets us PRId64, etc 37e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif 38e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 39e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// To use this in an autoconf setting, make sure you run the following 409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// autoconf macros: 41e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// AC_HEADER_STDC /* for stdint_h and inttypes_h */ 42e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// AC_CHECK_TYPES([__int64]) /* defined in some windows platforms */ 43e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 44e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#ifdef HAVE_INTTYPES_H 45e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <inttypes.h> // uint16_t might be here; PRId64 too. 46e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif 479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifdef HAVE_STDINT_H 489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <stdint.h> // to get uint16_t (ISO naming madness) 499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif 509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <sys/types.h> // our last best hope for uint16_t 519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block 529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Standard typedefs 539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// All Google code is compiled with -funsigned-char to make "char" 54// unsigned. Google code therefore doesn't need a "uchar" type. 55// TODO(csilvers): how do we make sure unsigned-char works on non-gcc systems? 56typedef signed char schar; 57typedef int8_t int8; 58typedef int16_t int16; 59typedef int32_t int32; 60typedef int64_t int64; 61 62// NOTE: unsigned types are DANGEROUS in loops and other arithmetical 63// places. Use the signed types unless your variable represents a bit 64// pattern (eg a hash value) or you really need the extra bit. Do NOT 65// use 'unsigned' to express "this value should always be positive"; 66// use assertions for this. 67 68typedef uint8_t uint8; 69typedef uint16_t uint16; 70typedef uint32_t uint32; 71typedef uint64_t uint64; 72 73const uint16 kuint16max = ( (uint16) 0xFFFF); 74const uint32 kuint32max = ( (uint32) 0xFFFFFFFF); 75const uint64 kuint64max = ( (((uint64) kuint32max) << 32) | kuint32max ); 76 77const int8 kint8max = ( ( int8) 0x7F); 78const int16 kint16max = ( ( int16) 0x7FFF); 79const int32 kint32max = ( ( int32) 0x7FFFFFFF); 80const int64 kint64max = ( ((( int64) kint32max) << 32) | kuint32max ); 81 82const int8 kint8min = ( ( int8) 0x80); 83const int16 kint16min = ( ( int16) 0x8000); 84const int32 kint32min = ( ( int32) 0x80000000); 85const int64 kint64min = ( ((( int64) kint32min) << 32) | 0 ); 86 87// Define the "portable" printf and scanf macros, if they're not 88// already there (via the inttypes.h we #included above, hopefully). 89// Mostly it's old systems that don't support inttypes.h, so we assume 90// they're 32 bit. 91#ifndef PRIx64 92#define PRIx64 "llx" 93#endif 94#ifndef SCNx64 95#define SCNx64 "llx" 96#endif 97#ifndef PRId64 98#define PRId64 "lld" 99#endif 100#ifndef SCNd64 101#define SCNd64 "lld" 102#endif 103#ifndef PRIu64 104#define PRIu64 "llu" 105#endif 106#ifndef PRIxPTR 107#define PRIxPTR "lx" 108#endif 109 110// Also allow for printing of a pthread_t. 111#define GPRIuPTHREAD "lu" 112#define GPRIxPTHREAD "lx" 113#if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__APPLE__) || defined(__FreeBSD__) 114#define PRINTABLE_PTHREAD(pthreadt) reinterpret_cast<uintptr_t>(pthreadt) 115#else 116#define PRINTABLE_PTHREAD(pthreadt) pthreadt 117#endif 118 119// A macro to disallow the evil copy constructor and operator= functions 120// This should be used in the private: declarations for a class 121#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ 122 TypeName(const TypeName&); \ 123 void operator=(const TypeName&) 124 125// An alternate name that leaves out the moral judgment... :-) 126#define DISALLOW_COPY_AND_ASSIGN(TypeName) DISALLOW_EVIL_CONSTRUCTORS(TypeName) 127 128// The COMPILE_ASSERT macro can be used to verify that a compile time 129// expression is true. For example, you could use it to verify the 130// size of a static array: 131// 132// COMPILE_ASSERT(sizeof(num_content_type_names) == sizeof(int), 133// content_type_names_incorrect_size); 134// 135// or to make sure a struct is smaller than a certain size: 136// 137// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 138// 139// The second argument to the macro is the name of the variable. If 140// the expression is false, most compilers will issue a warning/error 141// containing the name of the variable. 142// 143// Implementation details of COMPILE_ASSERT: 144// 145// - COMPILE_ASSERT works by defining an array type that has -1 146// elements (and thus is invalid) when the expression is false. 147// 148// - The simpler definition 149// 150// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 151// 152// does not work, as gcc supports variable-length arrays whose sizes 153// are determined at run-time (this is gcc's extension and not part 154// of the C++ standard). As a result, gcc fails to reject the 155// following code with the simple definition: 156// 157// int foo; 158// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is 159// // not a compile-time constant. 160// 161// - By using the type CompileAssert<(bool(expr))>, we ensures that 162// expr is a compile-time constant. (Template arguments must be 163// determined at compile-time.) 164// 165// - The outter parentheses in CompileAssert<(bool(expr))> are necessary 166// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written 167// 168// CompileAssert<bool(expr)> 169// 170// instead, these compilers will refuse to compile 171// 172// COMPILE_ASSERT(5 > 0, some_message); 173// 174// (They seem to think the ">" in "5 > 0" marks the end of the 175// template argument list.) 176// 177// - The array size is (bool(expr) ? 1 : -1), instead of simply 178// 179// ((expr) ? 1 : -1). 180// 181// This is to avoid running into a bug in MS VC 7.1, which 182// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 183 184template <bool> 185struct CompileAssert { 186}; 187 188#define COMPILE_ASSERT(expr, msg) \ 189 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 190 191#define arraysize(a) (sizeof(a) / sizeof(*(a))) 192 193#define OFFSETOF_MEMBER(strct, field) \ 194 (reinterpret_cast<char*>(&reinterpret_cast<strct*>(16)->field) - \ 195 reinterpret_cast<char*>(16)) 196 197// bit_cast<Dest,Source> implements the equivalent of 198// "*reinterpret_cast<Dest*>(&source)". 199// 200// The reinterpret_cast method would produce undefined behavior 201// according to ISO C++ specification section 3.10 -15 -. 202// bit_cast<> calls memcpy() which is blessed by the standard, 203// especially by the example in section 3.9. 204// 205// Fortunately memcpy() is very fast. In optimized mode, with a 206// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline 207// code with the minimal amount of data movement. On a 32-bit system, 208// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) 209// compiles to two loads and two stores. 210 211template <class Dest, class Source> 212inline Dest bit_cast(const Source& source) { 213 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), bitcasting_unequal_sizes); 214 Dest dest; 215 memcpy(&dest, &source, sizeof(dest)); 216 return dest; 217} 218 219#ifdef HAVE___ATTRIBUTE__ 220# define ATTRIBUTE_WEAK __attribute__((weak)) 221# define ATTRIBUTE_NOINLINE __attribute__((noinline)) 222#else 223# define ATTRIBUTE_WEAK 224# define ATTRIBUTE_NOINLINE 225#endif 226 227// Section attributes are supported for both ELF and Mach-O, but in 228// very different ways. Here's the API we provide: 229// 1) ATTRIBUTE_SECTION: put this with the declaration of all functions 230// you want to be in the same linker section 231// 2) DEFINE_ATTRIBUTE_SECTION_VARS: must be called once per unique 232// name. You want to make sure this is executed before any 233// DECLARE_ATTRIBUTE_SECTION_VARS; the easiest way is to put them 234// in the same .cc file. Put this call at the global level. 235// 3) INIT_ATTRIBUTE_SECTION_VARS: you can scatter calls to this in 236// multiple places to help ensure execution before any 237// DECLARE_ATTRIBUTE_SECTION_VARS. You must have at least one 238// DEFINE, but you can have many INITs. Put each in its own scope. 239// 4) DECLARE_ATTRIBUTE_SECTION_VARS: must be called before using 240// ATTRIBUTE_SECTION_START or ATTRIBUTE_SECTION_STOP on a name. 241// Put this call at the global level. 242// 5) ATTRIBUTE_SECTION_START/ATTRIBUTE_SECTION_STOP: call this to say 243// where in memory a given section is. All functions declared with 244// ATTRIBUTE_SECTION are guaranteed to be between START and STOP. 245 246#if defined(HAVE___ATTRIBUTE__) && defined(__ELF__) 247# define ATTRIBUTE_SECTION(name) __attribute__ ((section (#name))) 248 249 // Weak section declaration to be used as a global declaration 250 // for ATTRIBUTE_SECTION_START|STOP(name) to compile and link 251 // even without functions with ATTRIBUTE_SECTION(name). 252# define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 253 extern char __start_##name[] ATTRIBUTE_WEAK; \ 254 extern char __stop_##name[] ATTRIBUTE_WEAK 255# define INIT_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF 256# define DEFINE_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF 257 258 // Return void* pointers to start/end of a section of code with functions 259 // having ATTRIBUTE_SECTION(name), or 0 if no such function exists. 260 // One must DECLARE_ATTRIBUTE_SECTION(name) for this to compile and link. 261# define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name)) 262# define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name)) 263# define HAVE_ATTRIBUTE_SECTION_START 1 264 265#elif defined(HAVE___ATTRIBUTE__) && defined(__MACH__) 266# define ATTRIBUTE_SECTION(name) __attribute__ ((section ("__TEXT, " #name))) 267 268#include <mach-o/getsect.h> 269#include <mach-o/dyld.h> 270class AssignAttributeStartEnd { 271 public: 272 AssignAttributeStartEnd(const char* name, char** pstart, char** pend) { 273 // Find out what dynamic library name is defined in 274 if (_dyld_present()) { 275 for (int i = _dyld_image_count() - 1; i >= 0; --i) { 276 const mach_header* hdr = _dyld_get_image_header(i); 277#ifdef MH_MAGIC_64 278 if (hdr->magic == MH_MAGIC_64) { 279 uint64_t len; 280 *pstart = getsectdatafromheader_64((mach_header_64*)hdr, 281 "__TEXT", name, &len); 282 if (*pstart) { // NULL if not defined in this dynamic library 283 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc 284 *pend = *pstart + len; 285 return; 286 } 287 } 288#endif 289 if (hdr->magic == MH_MAGIC) { 290 uint32_t len; 291 *pstart = getsectdatafromheader(hdr, "__TEXT", name, &len); 292 if (*pstart) { // NULL if not defined in this dynamic library 293 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc 294 *pend = *pstart + len; 295 return; 296 } 297 } 298 } 299 } 300 // If we get here, not defined in a dll at all. See if defined statically. 301 unsigned long len; // don't ask me why this type isn't uint32_t too... 302 *pstart = getsectdata("__TEXT", name, &len); 303 *pend = *pstart + len; 304 } 305}; 306 307#define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 308 extern char* __start_##name; \ 309 extern char* __stop_##name 310 311#define INIT_ATTRIBUTE_SECTION_VARS(name) \ 312 DECLARE_ATTRIBUTE_SECTION_VARS(name); \ 313 static const AssignAttributeStartEnd __assign_##name( \ 314 #name, &__start_##name, &__stop_##name) 315 316#define DEFINE_ATTRIBUTE_SECTION_VARS(name) \ 317 char* __start_##name, *__stop_##name; \ 318 INIT_ATTRIBUTE_SECTION_VARS(name) 319 320# define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name)) 321# define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name)) 322# define HAVE_ATTRIBUTE_SECTION_START 1 323 324#else // not HAVE___ATTRIBUTE__ && __ELF__, nor HAVE___ATTRIBUTE__ && __MACH__ 325# define ATTRIBUTE_SECTION(name) 326# define DECLARE_ATTRIBUTE_SECTION_VARS(name) 327# define INIT_ATTRIBUTE_SECTION_VARS(name) 328# define DEFINE_ATTRIBUTE_SECTION_VARS(name) 329# define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(0)) 330# define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(0)) 331 332#endif // HAVE___ATTRIBUTE__ and __ELF__ or __MACH__ 333 334#if defined(HAVE___ATTRIBUTE__) && (defined(__i386__) || defined(__x86_64__)) 335# define CACHELINE_ALIGNED __attribute__((aligned(64))) 336#else 337# define CACHELINE_ALIGNED 338#endif // defined(HAVE___ATTRIBUTE__) && (__i386__ || __x86_64__) 339 340 341// The following enum should be used only as a constructor argument to indicate 342// that the variable has static storage class, and that the constructor should 343// do nothing to its state. It indicates to the reader that it is legal to 344// declare a static nistance of the class, provided the constructor is given 345// the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a 346// static variable that has a constructor or a destructor because invocation 347// order is undefined. However, IF the type can be initialized by filling with 348// zeroes (which the loader does for static variables), AND the destructor also 349// does nothing to the storage, then a constructor declared as 350// explicit MyClass(base::LinkerInitialized x) {} 351// and invoked as 352// static MyClass my_variable_name(base::LINKER_INITIALIZED); 353namespace base { 354enum LinkerInitialized { LINKER_INITIALIZED }; 355} 356 357#endif // _BASICTYPES_H_ 358