basictypes.h 15 KB

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