blob: fc65564f0513ac86ee78c21c428230b274ea189a [file] [log] [blame]
Carl Shapiro6c21dc12011-06-20 15:20:52 -07001// Copyright 2010 Google
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070014#ifndef ART_SRC_MACROS_H_
15#define ART_SRC_MACROS_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Carl Shapiro12eb78e2011-06-24 14:51:06 -070017#include <stddef.h> // for size_t
18
Elliott Hughesff17f1f2012-01-24 18:12:29 -080019#define GCC_VERSION (_GNUC_ * 10000 + _GNUC_MINOR_ * 100 + __GNUC_PATCHLEVEL__)
Shih-wei Liao24782c62012-01-08 12:46:11 -080020
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070021// The COMPILE_ASSERT macro can be used to verify that a compile time
22// expression is true. For example, you could use it to verify the
23// size of a static array:
24//
25// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
26// content_type_names_incorrect_size);
27//
28// or to make sure a struct is smaller than a certain size:
29//
30// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
31//
32// The second argument to the macro is the name of the variable. If
33// the expression is false, most compilers will issue a warning/error
34// containing the name of the variable.
35
36template <bool>
37struct CompileAssert {
38};
39
40#define COMPILE_ASSERT(expr, msg) \
41 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
42
Carl Shapiro6c21dc12011-06-20 15:20:52 -070043// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
44// It goes in the private: declarations in a class.
45#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
46 TypeName(const TypeName&); \
47 void operator=(const TypeName&)
48
49// A macro to disallow all the implicit constructors, namely the
50// default constructor, copy constructor and operator= functions.
51//
52// This should be used in the private: declarations for a class
53// that wants to prevent anyone from instantiating it. This is
54// especially useful for classes containing only static methods.
55#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
56 TypeName(); \
57 DISALLOW_COPY_AND_ASSIGN(TypeName)
58
Carl Shapiroa2e18e12011-06-21 18:57:55 -070059// The arraysize(arr) macro returns the # of elements in an array arr.
60// The expression is a compile-time constant, and therefore can be
61// used in defining new arrays, for example. If you use arraysize on
62// a pointer by mistake, you will get a compile-time error.
63//
64// One caveat is that arraysize() doesn't accept any array of an
65// anonymous type or a type defined inside a function. In these rare
Carl Shapirod2bdb572011-06-22 11:45:37 -070066// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
Carl Shapiroa2e18e12011-06-21 18:57:55 -070067// due to a limitation in C++'s template system. The limitation might
68// eventually be removed, but it hasn't happened yet.
69
70// This template function declaration is used in defining arraysize.
71// Note that the function doesn't need an implementation, as we only
72// use its type.
73template <typename T, size_t N>
74char (&ArraySizeHelper(T (&array)[N]))[N];
75
76#define arraysize(array) (sizeof(ArraySizeHelper(array)))
77
Carl Shapirod2bdb572011-06-22 11:45:37 -070078// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
79// but can be used on anonymous types or types defined inside
80// functions. It's less safe than arraysize as it accepts some
81// (although not all) pointers. Therefore, you should use arraysize
82// whenever possible.
83//
84// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
85// size_t.
86//
87// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
88//
89// "warning: division by zero in ..."
90//
91// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
92// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
93//
94// The following comments are on the implementation details, and can
95// be ignored by the users.
96//
97// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
98// the array) and sizeof(*(arr)) (the # of bytes in one array
99// element). If the former is divisible by the latter, perhaps arr is
100// indeed an array, in which case the division result is the # of
101// elements in the array. Otherwise, arr cannot possibly be an array,
102// and we generate a compiler error to prevent the code from
103// compiling.
104//
105// Since the size of bool is implementation-defined, we need to cast
106// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
107// result has type size_t.
108//
109// This macro is not perfect as it wrongfully accepts certain
110// pointers, namely where the pointer size is divisible by the pointee
111// size. Since all our code has to go through a 32-bit compiler,
112// where a pointer is 4 bytes, this means all pointers to a type whose
113// size is 3 or greater than 4 will be (righteously) rejected.
114#define ARRAYSIZE_UNSAFE(a) \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700115 ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Carl Shapirod2bdb572011-06-22 11:45:37 -0700116
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800117#define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700118
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700119#define OFFSETOF_MEMBER(t, f) \
120 (reinterpret_cast<char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<char*>(16))
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700121
Elliott Hughes93e74e82011-09-13 11:07:03 -0700122#define OFFSETOF_VOLATILE_MEMBER(t, f) \
123 (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16))
124
Elliott Hughes85d15452011-09-16 17:33:01 -0700125#define PACKED __attribute__ ((__packed__))
126
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800127#define LIKELY(x) __builtin_expect((x), true)
128#define UNLIKELY(x) __builtin_expect((x), false)
Ian Rogerscaab8c42011-10-12 12:11:18 -0700129
Elliott Hughes74787a32011-12-16 15:39:48 -0800130// bionic and glibc both have TEMP_FAILURE_RETRY, but Mac OS' libc doesn't.
131#ifndef TEMP_FAILURE_RETRY
132#define TEMP_FAILURE_RETRY(exp) ({ \
133 typeof (exp) _rc; \
134 do { \
135 _rc = (exp); \
136 } while (_rc == -1 && errno == EINTR); \
137 _rc; })
138#endif
139
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700140#endif // ART_SRC_MACROS_H_