blob: 5b9bdb64635880281fe8e78fe2f1a79151c4124b [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
Shih-wei Liao24782c62012-01-08 12:46:11 -080019#define GCC_VERSION ( _GNUC_ * 10000 \
20 + _GNUC_MINOR_ * 100 \
21 + __GNUC_PATCHLEVEL__)
22
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070023// The COMPILE_ASSERT macro can be used to verify that a compile time
24// expression is true. For example, you could use it to verify the
25// size of a static array:
26//
27// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
28// content_type_names_incorrect_size);
29//
30// or to make sure a struct is smaller than a certain size:
31//
32// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
33//
34// The second argument to the macro is the name of the variable. If
35// the expression is false, most compilers will issue a warning/error
36// containing the name of the variable.
37
38template <bool>
39struct CompileAssert {
40};
41
42#define COMPILE_ASSERT(expr, msg) \
43 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
44
Carl Shapiro6c21dc12011-06-20 15:20:52 -070045// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
46// It goes in the private: declarations in a class.
47#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
48 TypeName(const TypeName&); \
49 void operator=(const TypeName&)
50
51// A macro to disallow all the implicit constructors, namely the
52// default constructor, copy constructor and operator= functions.
53//
54// This should be used in the private: declarations for a class
55// that wants to prevent anyone from instantiating it. This is
56// especially useful for classes containing only static methods.
57#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
58 TypeName(); \
59 DISALLOW_COPY_AND_ASSIGN(TypeName)
60
Carl Shapiroa2e18e12011-06-21 18:57:55 -070061// The arraysize(arr) macro returns the # of elements in an array arr.
62// The expression is a compile-time constant, and therefore can be
63// used in defining new arrays, for example. If you use arraysize on
64// a pointer by mistake, you will get a compile-time error.
65//
66// One caveat is that arraysize() doesn't accept any array of an
67// anonymous type or a type defined inside a function. In these rare
Carl Shapirod2bdb572011-06-22 11:45:37 -070068// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
Carl Shapiroa2e18e12011-06-21 18:57:55 -070069// due to a limitation in C++'s template system. The limitation might
70// eventually be removed, but it hasn't happened yet.
71
72// This template function declaration is used in defining arraysize.
73// Note that the function doesn't need an implementation, as we only
74// use its type.
75template <typename T, size_t N>
76char (&ArraySizeHelper(T (&array)[N]))[N];
77
78#define arraysize(array) (sizeof(ArraySizeHelper(array)))
79
Carl Shapirod2bdb572011-06-22 11:45:37 -070080// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
81// but can be used on anonymous types or types defined inside
82// functions. It's less safe than arraysize as it accepts some
83// (although not all) pointers. Therefore, you should use arraysize
84// whenever possible.
85//
86// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
87// size_t.
88//
89// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
90//
91// "warning: division by zero in ..."
92//
93// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
94// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
95//
96// The following comments are on the implementation details, and can
97// be ignored by the users.
98//
99// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
100// the array) and sizeof(*(arr)) (the # of bytes in one array
101// element). If the former is divisible by the latter, perhaps arr is
102// indeed an array, in which case the division result is the # of
103// elements in the array. Otherwise, arr cannot possibly be an array,
104// and we generate a compiler error to prevent the code from
105// compiling.
106//
107// Since the size of bool is implementation-defined, we need to cast
108// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
109// result has type size_t.
110//
111// This macro is not perfect as it wrongfully accepts certain
112// pointers, namely where the pointer size is divisible by the pointee
113// size. Since all our code has to go through a 32-bit compiler,
114// where a pointer is 4 bytes, this means all pointers to a type whose
115// size is 3 or greater than 4 will be (righteously) rejected.
116#define ARRAYSIZE_UNSAFE(a) \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700117 ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Carl Shapirod2bdb572011-06-22 11:45:37 -0700118
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700119#define SIZEOF_MEMBER(t, f) sizeof(((t*) 4096)->f)
120
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700121#define OFFSETOF_MEMBER(t, f) \
122 (reinterpret_cast<char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<char*>(16))
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700123
Elliott Hughes93e74e82011-09-13 11:07:03 -0700124#define OFFSETOF_VOLATILE_MEMBER(t, f) \
125 (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16))
126
Elliott Hughes85d15452011-09-16 17:33:01 -0700127#define PACKED __attribute__ ((__packed__))
128
Ian Rogerscaab8c42011-10-12 12:11:18 -0700129#define LIKELY(x) __builtin_expect((x),true)
130#define UNLIKELY(x) __builtin_expect((x),false)
131
Elliott Hughes74787a32011-12-16 15:39:48 -0800132// bionic and glibc both have TEMP_FAILURE_RETRY, but Mac OS' libc doesn't.
133#ifndef TEMP_FAILURE_RETRY
134#define TEMP_FAILURE_RETRY(exp) ({ \
135 typeof (exp) _rc; \
136 do { \
137 _rc = (exp); \
138 } while (_rc == -1 && errno == EINTR); \
139 _rc; })
140#endif
141
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700142#endif // ART_SRC_MACROS_H_