Debian Codegraph @

extra/gperftools/gperftools-2.15/src/base/basictypes.h

raw
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
31 #ifndef _BASICTYPES_H_
32 #define _BASICTYPES_H_
33
34 #include <config.h>
35 #include <string.h> // for memcpy()
36 #include <inttypes.h> // gets us PRId64, etc
37
38 // To use this in an autoconf setting, make sure you run the following
39 // autoconf macros:
40 // AC_HEADER_STDC /* for stdint_h and inttypes_h */
41 // AC_CHECK_TYPES([__int64]) /* defined in some windows platforms */
42
43 #include <stdint.h> // to get uint16_t (ISO naming madness)
44 #include <sys/types.h> // our last best hope for uint16_t
45
46 // Standard typedefs
47 // All Google code is compiled with -funsigned-char to make "char"
48 // unsigned. Google code therefore doesn't need a "uchar" type.
49 // TODO(csilvers): how do we make sure unsigned-char works on non-gcc systems?
50 typedef signed char schar;
51 typedef int8_t int8;
52 typedef int16_t int16;
53 typedef int32_t int32;
54 typedef int64_t int64;
55
56 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
57 // places. Use the signed types unless your variable represents a bit
58 // pattern (eg a hash value) or you really need the extra bit. Do NOT
59 // use 'unsigned' to express "this value should always be positive";
60 // use assertions for this.
61
62 typedef uint8_t uint8;
63 typedef uint16_t uint16;
64 typedef uint32_t uint32;
65 typedef uint64_t uint64;
66
67 const uint16 kuint16max = ( (uint16) 0xFFFF);
68 const uint32 kuint32max = ( (uint32) 0xFFFFFFFF);
69 const uint64 kuint64max = ( (((uint64) kuint32max) << 32) | kuint32max );
70
71 const int8 kint8max = ( ( int8) 0x7F);
72 const int16 kint16max = ( ( int16) 0x7FFF);
73 const int32 kint32max = ( ( int32) 0x7FFFFFFF);
74 const int64 kint64max = ( ((( int64) kint32max) << 32) | kuint32max );
75
76 const int8 kint8min = ( ( int8) 0x80);
77 const int16 kint16min = ( ( int16) 0x8000);
78 const int32 kint32min = ( ( int32) 0x80000000);
79 const int64 kint64min = ( (((uint64) kint32min) << 32) | 0 );
80
81 // Define the "portable" printf and scanf macros, if they're not
82 // already there (via the inttypes.h we #included above, hopefully).
83 // Mostly it's old systems that don't support inttypes.h, so we assume
84 // they're 32 bit.
85 #ifndef PRIx64
86 #define PRIx64 "llx"
87 #endif
88 #ifndef SCNx64
89 #define SCNx64 "llx"
90 #endif
91 #ifndef PRId64
92 #define PRId64 "lld"
93 #endif
94 #ifndef SCNd64
95 #define SCNd64 "lld"
96 #endif
97 #ifndef PRIu64
98 #define PRIu64 "llu"
99 #endif
100 #ifndef PRIxPTR
101 #define PRIxPTR "lx"
102 #endif
103
104 // Also allow for printing of a pthread_t.
105 #define GPRIuPTHREAD "lu"
106 #define GPRIxPTHREAD "lx"
107 #if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__APPLE__) || defined(__FreeBSD__)
108 #define PRINTABLE_PTHREAD(pthreadt) reinterpret_cast<uintptr_t>(pthreadt)
109 #elif defined(__QNXNTO__)
110 #define PRINTABLE_PTHREAD(pthreadt) static_cast<intptr_t>(pthreadt)
111 #else
112 #define PRINTABLE_PTHREAD(pthreadt) pthreadt
113 #endif
114
115 #if defined(__GNUC__)
116 #define PREDICT_TRUE(x) __builtin_expect(!!(x), 1)
117 #define PREDICT_FALSE(x) __builtin_expect(!!(x), 0)
118 #else
119 #define PREDICT_TRUE(x) (x)
120 #define PREDICT_FALSE(x) (x)
121 #endif
122
123 // A macro to disallow the evil copy constructor and operator= functions
124 // This should be used in the private: declarations for a class
125 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
126 TypeName(const TypeName&); \
127 void operator=(const TypeName&)
128
129 // An alternate name that leaves out the moral judgment... :-)
130 #define DISALLOW_COPY_AND_ASSIGN(TypeName) DISALLOW_EVIL_CONSTRUCTORS(TypeName)
131
132 // The COMPILE_ASSERT macro can be used to verify that a compile time
133 // expression is true. For example, you could use it to verify the
134 // size of a static array:
135 //
136 // COMPILE_ASSERT(sizeof(num_content_type_names) == sizeof(int),
137 // content_type_names_incorrect_size);
138 //
139 // or to make sure a struct is smaller than a certain size:
140 //
141 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
142 //
143 // The second argument to the macro is the name of the variable. If
144 // the expression is false, most compilers will issue a warning/error
145 // containing the name of the variable.
146 //
147 // Implementation details of COMPILE_ASSERT:
148 //
149 // - COMPILE_ASSERT works by defining an array type that has -1
150 // elements (and thus is invalid) when the expression is false.
151 //
152 // - The simpler definition
153 //
154 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
155 //
156 // does not work, as gcc supports variable-length arrays whose sizes
157 // are determined at run-time (this is gcc's extension and not part
158 // of the C++ standard). As a result, gcc fails to reject the
159 // following code with the simple definition:
160 //
161 // int foo;
162 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
163 // // not a compile-time constant.
164 //
165 // - By using the type CompileAssert<(bool(expr))>, we ensures that
166 // expr is a compile-time constant. (Template arguments must be
167 // determined at compile-time.)
168 //
169 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
170 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
171 //
172 // CompileAssert<bool(expr)>
173 //
174 // instead, these compilers will refuse to compile
175 //
176 // COMPILE_ASSERT(5 > 0, some_message);
177 //
178 // (They seem to think the ">" in "5 > 0" marks the end of the
179 // template argument list.)
180 //
181 // - The array size is (bool(expr) ? 1 : -1), instead of simply
182 //
183 // ((expr) ? 1 : -1).
184 //
185 // This is to avoid running into a bug in MS VC 7.1, which
186 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
187
188 template <bool>
189 struct CompileAssert {
190 };
191
192 #ifdef HAVE___ATTRIBUTE__
193 # define ATTRIBUTE_UNUSED __attribute__((unused))
194 #else
195 # define ATTRIBUTE_UNUSED
196 #endif
197
198 #if defined(HAVE___ATTRIBUTE__) && defined(HAVE_TLS)
199 #define ATTR_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec")))
200 #else
201 #define ATTR_INITIAL_EXEC
202 #endif
203
204 #define COMPILE_ASSERT(expr, msg) \
205 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED
206
207 #define arraysize(a) (sizeof(a) / sizeof(*(a)))
208
209 #define OFFSETOF_MEMBER(strct, field) \
210 (reinterpret_cast<char*>(&reinterpret_cast<strct*>(16)->field) - \
211 reinterpret_cast<char*>(16))
212
213 // bit_cast<Dest,Source> implements the equivalent of
214 // "*reinterpret_cast<Dest*>(&source)".
215 //
216 // The reinterpret_cast method would produce undefined behavior
217 // according to ISO C++ specification section 3.10 -15 -.
218 // bit_cast<> calls memcpy() which is blessed by the standard,
219 // especially by the example in section 3.9.
220 //
221 // Fortunately memcpy() is very fast. In optimized mode, with a
222 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
223 // code with the minimal amount of data movement. On a 32-bit system,
224 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
225 // compiles to two loads and two stores.
226
227 template <class Dest, class Source>
228 inline Dest bit_cast(const Source& source) {
229 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), bitcasting_unequal_sizes);
230 Dest dest;
231 memcpy(&dest, &source, sizeof(dest));
232 return dest;
233 }
234
235 // bit_store<Dest,Source> implements the equivalent of
236 // "dest = *reinterpret_cast<Dest*>(&source)".
237 //
238 // This prevents undefined behavior when the dest pointer is unaligned.
239 template <class Dest, class Source>
240 inline void bit_store(Dest *dest, const Source *source) {
241 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), bitcasting_unequal_sizes);
242 memcpy(dest, source, sizeof(Dest));
243 }
244
245 #ifdef HAVE___ATTRIBUTE__
246 # define ATTRIBUTE_WEAK __attribute__((weak))
247 # define ATTRIBUTE_NOINLINE __attribute__((noinline))
248 #else
249 # define ATTRIBUTE_WEAK
250 # define ATTRIBUTE_NOINLINE
251 #endif
252
253 #ifdef _MSC_VER
254 #undef ATTRIBUTE_NOINLINE
255 #define ATTRIBUTE_NOINLINE __declspec(noinline)
256 #endif
257
258 #if defined(HAVE___ATTRIBUTE__) && defined(__ELF__)
259 # define ATTRIBUTE_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
260 #else
261 # define ATTRIBUTE_VISIBILITY_HIDDEN
262 #endif
263
264 // Section attributes are supported for both ELF and Mach-O, but in
265 // very different ways. Here's the API we provide:
266 // 1) ATTRIBUTE_SECTION: put this with the declaration of all functions
267 // you want to be in the same linker section
268 // 2) DEFINE_ATTRIBUTE_SECTION_VARS: must be called once per unique
269 // name. You want to make sure this is executed before any
270 // DECLARE_ATTRIBUTE_SECTION_VARS; the easiest way is to put them
271 // in the same .cc file. Put this call at the global level.
272 // 3) INIT_ATTRIBUTE_SECTION_VARS: you can scatter calls to this in
273 // multiple places to help ensure execution before any
274 // DECLARE_ATTRIBUTE_SECTION_VARS. You must have at least one
275 // DEFINE, but you can have many INITs. Put each in its own scope.
276 // 4) DECLARE_ATTRIBUTE_SECTION_VARS: must be called before using
277 // ATTRIBUTE_SECTION_START or ATTRIBUTE_SECTION_STOP on a name.
278 // Put this call at the global level.
279 // 5) ATTRIBUTE_SECTION_START/ATTRIBUTE_SECTION_STOP: call this to say
280 // where in memory a given section is. All functions declared with
281 // ATTRIBUTE_SECTION are guaranteed to be between START and STOP.
282
283 #if defined(HAVE___ATTRIBUTE__) && defined(__ELF__)
284 # define ATTRIBUTE_SECTION(name) __attribute__ ((section (#name))) __attribute__((noinline))
285
286 // Weak section declaration to be used as a global declaration
287 // for ATTRIBUTE_SECTION_START|STOP(name) to compile and link
288 // even without functions with ATTRIBUTE_SECTION(name).
289 # define DECLARE_ATTRIBUTE_SECTION_VARS(name) \
290 extern char __start_##name[] ATTRIBUTE_WEAK; \
291 extern char __stop_##name[] ATTRIBUTE_WEAK
292 # define INIT_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF
293 # define DEFINE_ATTRIBUTE_SECTION_VARS(name) // no-op for ELF
294
295 // Return void* pointers to start/end of a section of code with functions
296 // having ATTRIBUTE_SECTION(name), or 0 if no such function exists.
297 // One must DECLARE_ATTRIBUTE_SECTION(name) for this to compile and link.
298 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name))
299 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name))
300 # define HAVE_ATTRIBUTE_SECTION_START 1
301
302 #elif defined(HAVE___ATTRIBUTE__) && defined(__MACH__)
303 # define ATTRIBUTE_SECTION(name) __attribute__ ((section ("__TEXT, " #name))) __attribute__((noinline))
304
305 #include <mach-o/getsect.h>
306 #include <mach-o/dyld.h>
307 class AssignAttributeStartEnd {
308 public:
309 AssignAttributeStartEnd(const char* name, char** pstart, char** pend) {
310 // Find out what dynamic library name is defined in
311 for (int i = _dyld_image_count() - 1; i >= 0; --i) {
312 const mach_header* hdr = _dyld_get_image_header(i);
313 #ifdef MH_MAGIC_64
314 if (hdr->magic == MH_MAGIC_64) {
315 uint64_t len;
316 *pstart = getsectdatafromheader_64((mach_header_64*)hdr,
317 "__TEXT", name, &len);
318 if (*pstart) { // NULL if not defined in this dynamic library
319 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc
320 *pend = *pstart + len;
321 return;
322 }
323 }
324 #endif
325 if (hdr->magic == MH_MAGIC) {
326 uint32_t len;
327 *pstart = getsectdatafromheader(hdr, "__TEXT", name, &len);
328 if (*pstart) { // NULL if not defined in this dynamic library
329 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc
330 *pend = *pstart + len;
331 return;
332 }
333 }
334 }
335
336 // If we get here, not defined in a dll at all. See if defined statically.
337 unsigned long len; // don't ask me why this type isn't uint32_t too...
338 *pstart = getsectdata("__TEXT", name, &len);
339 *pend = *pstart + len;
340 }
341 };
342
343 #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \
344 extern char* __start_##name; \
345 extern char* __stop_##name
346
347 #define INIT_ATTRIBUTE_SECTION_VARS(name) \
348 DECLARE_ATTRIBUTE_SECTION_VARS(name); \
349 static const AssignAttributeStartEnd __assign_##name( \
350 #name, &__start_##name, &__stop_##name)
351
352 #define DEFINE_ATTRIBUTE_SECTION_VARS(name) \
353 char* __start_##name, *__stop_##name; \
354 INIT_ATTRIBUTE_SECTION_VARS(name)
355
356 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(__start_##name))
357 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(__stop_##name))
358 # define HAVE_ATTRIBUTE_SECTION_START 1
359
360 #else // not HAVE___ATTRIBUTE__ && __ELF__, nor HAVE___ATTRIBUTE__ && __MACH__
361 # define ATTRIBUTE_SECTION(name)
362 # define DECLARE_ATTRIBUTE_SECTION_VARS(name)
363 # define INIT_ATTRIBUTE_SECTION_VARS(name)
364 # define DEFINE_ATTRIBUTE_SECTION_VARS(name)
365 # define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(0))
366 # define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(0))
367
368 #endif // HAVE___ATTRIBUTE__ and __ELF__ or __MACH__
369
370 #if defined(HAVE___ATTRIBUTE__)
371 # if (defined(__i386__) || defined(__x86_64__))
372 # define CACHELINE_ALIGNED __attribute__((aligned(64)))
373 # elif (defined(__PPC__) || defined(__PPC64__) || defined(__ppc__) || defined(__ppc64__))
374 # define CACHELINE_ALIGNED __attribute__((aligned(16)))
375 # elif (defined(__arm__))
376 # define CACHELINE_ALIGNED __attribute__((aligned(64)))
377 // some ARMs have shorter cache lines (ARM1176JZF-S is 32 bytes for example) but obviously 64-byte aligned implies 32-byte aligned
378 # elif (defined(__mips__))
379 # define CACHELINE_ALIGNED __attribute__((aligned(128)))
380 # elif (defined(__aarch64__))
381 # define CACHELINE_ALIGNED __attribute__((aligned(64)))
382 // implementation specific, Cortex-A53 and 57 should have 64 bytes
383 # elif (defined(__s390__))
384 # define CACHELINE_ALIGNED __attribute__((aligned(256)))
385 # elif (defined(__riscv) && __riscv_xlen == 64)
386 # define CACHELINE_ALIGNED __attribute__((aligned(64)))
387 # elif defined(__loongarch64)
388 # define CACHELINE_ALIGNED __attribute__((aligned(64)))
389 # else
390 # error Could not determine cache line length - unknown architecture
391 # endif
392 #else
393 # define CACHELINE_ALIGNED
394 #endif // defined(HAVE___ATTRIBUTE__)
395
396 #if defined(HAVE___ATTRIBUTE__ALIGNED_FN)
397 # define CACHELINE_ALIGNED_FN CACHELINE_ALIGNED
398 #else
399 # define CACHELINE_ALIGNED_FN
400 #endif
401
402 // Structure for discovering alignment
403 union MemoryAligner {
404 void* p;
405 double d;
406 size_t s;
407 } CACHELINE_ALIGNED;
408
409 #if defined(HAVE___ATTRIBUTE__) && defined(__ELF__)
410 #define ATTRIBUTE_HIDDEN __attribute__((visibility("hidden")))
411 #else
412 #define ATTRIBUTE_HIDDEN
413 #endif
414
415 #if defined(__GNUC__)
416 #define ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
417 #elif defined(_MSC_VER)
418 #define ATTRIBUTE_ALWAYS_INLINE __forceinline
419 #else
420 #define ATTRIBUTE_ALWAYS_INLINE
421 #endif
422
423 // The following enum should be used only as a constructor argument to indicate
424 // that the variable has static storage class, and that the constructor should
425 // do nothing to its state. It indicates to the reader that it is legal to
426 // declare a static nistance of the class, provided the constructor is given
427 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
428 // static variable that has a constructor or a destructor because invocation
429 // order is undefined. However, IF the type can be initialized by filling with
430 // zeroes (which the loader does for static variables), AND the destructor also
431 // does nothing to the storage, then a constructor declared as
432 // explicit MyClass(base::LinkerInitialized x) {}
433 // and invoked as
434 // static MyClass my_variable_name(base::LINKER_INITIALIZED);
435 namespace base {
436 enum LinkerInitialized { LINKER_INITIALIZED };
437 }
438
439 #endif // _BASICTYPES_H_