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