Bug 886842 - Add clang trunk builds for ASan. r=froydnj
[gecko.git] / mfbt / Util.h
blobab62a9b035356429ce63997327ccca825a106a3c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 * Miscellaneous uncategorized functionality. Please add new functionality to
8 * new headers, or to other appropriate existing headers, not here.
9 */
11 #ifndef mozilla_Util_h_
12 #define mozilla_Util_h_
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/Types.h"
18 #ifdef __cplusplus
20 namespace mozilla {
23 * This class, and the corresponding macro MOZ_ALIGNOF, figure out how many
24 * bytes of alignment a given type needs.
26 template<class T>
27 class AlignmentFinder
29 struct Aligner
31 char c;
32 T t;
35 public:
36 static const size_t alignment = sizeof(Aligner) - sizeof(T);
39 #define MOZ_ALIGNOF(T) mozilla::AlignmentFinder<T>::alignment
42 * Declare the MOZ_ALIGNED_DECL macro for declaring aligned types.
44 * For instance,
46 * MOZ_ALIGNED_DECL(char arr[2], 8);
48 * will declare a two-character array |arr| aligned to 8 bytes.
51 #if defined(__GNUC__)
52 # define MOZ_ALIGNED_DECL(_type, _align) \
53 _type __attribute__((aligned(_align)))
54 #elif defined(_MSC_VER)
55 # define MOZ_ALIGNED_DECL(_type, _align) \
56 __declspec(align(_align)) _type
57 #else
58 # warning "We don't know how to align variables on this compiler."
59 # define MOZ_ALIGNED_DECL(_type, _align) _type
60 #endif
63 * AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
64 * bytes.
66 * We support 1, 2, 4, 8, and 16-bit alignment.
68 template<size_t align>
69 struct AlignedElem;
72 * We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
73 * foo is a template parameter.
76 template<>
77 struct AlignedElem<1>
79 MOZ_ALIGNED_DECL(uint8_t elem, 1);
82 template<>
83 struct AlignedElem<2>
85 MOZ_ALIGNED_DECL(uint8_t elem, 2);
88 template<>
89 struct AlignedElem<4>
91 MOZ_ALIGNED_DECL(uint8_t elem, 4);
94 template<>
95 struct AlignedElem<8>
97 MOZ_ALIGNED_DECL(uint8_t elem, 8);
100 template<>
101 struct AlignedElem<16>
103 MOZ_ALIGNED_DECL(uint8_t elem, 16);
107 * This utility pales in comparison to Boost's aligned_storage. The utility
108 * simply assumes that uint64_t is enough alignment for anyone. This may need
109 * to be extended one day...
111 * As an important side effect, pulling the storage into this template is
112 * enough obfuscation to confuse gcc's strict-aliasing analysis into not giving
113 * false negatives when we cast from the char buffer to whatever type we've
114 * constructed using the bytes.
116 template<size_t nbytes>
117 struct AlignedStorage
119 union U {
120 char bytes[nbytes];
121 uint64_t _;
122 } u;
124 const void* addr() const { return u.bytes; }
125 void* addr() { return u.bytes; }
128 template<class T>
129 struct AlignedStorage2
131 union U {
132 char bytes[sizeof(T)];
133 uint64_t _;
134 } u;
136 const T* addr() const { return reinterpret_cast<const T*>(u.bytes); }
137 T* addr() { return static_cast<T*>(static_cast<void*>(u.bytes)); }
141 * Small utility for lazily constructing objects without using dynamic storage.
142 * When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
143 * been constructed and no T destructor will be called when the Maybe<T> is
144 * destroyed. Upon calling |construct|, a T object will be constructed with the
145 * given arguments and that object will be destroyed when the owning Maybe<T>
146 * is destroyed.
148 * N.B. GCC seems to miss some optimizations with Maybe and may generate extra
149 * branches/loads/stores. Use with caution on hot paths.
151 template<class T>
152 class Maybe
154 AlignedStorage2<T> storage;
155 bool constructed;
157 T& asT() { return *storage.addr(); }
159 public:
160 Maybe() { constructed = false; }
161 ~Maybe() { if (constructed) asT().~T(); }
163 bool empty() const { return !constructed; }
165 void construct() {
166 MOZ_ASSERT(!constructed);
167 ::new (storage.addr()) T();
168 constructed = true;
171 template<class T1>
172 void construct(const T1& t1) {
173 MOZ_ASSERT(!constructed);
174 ::new (storage.addr()) T(t1);
175 constructed = true;
178 template<class T1, class T2>
179 void construct(const T1& t1, const T2& t2) {
180 MOZ_ASSERT(!constructed);
181 ::new (storage.addr()) T(t1, t2);
182 constructed = true;
185 template<class T1, class T2, class T3>
186 void construct(const T1& t1, const T2& t2, const T3& t3) {
187 MOZ_ASSERT(!constructed);
188 ::new (storage.addr()) T(t1, t2, t3);
189 constructed = true;
192 template<class T1, class T2, class T3, class T4>
193 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
194 MOZ_ASSERT(!constructed);
195 ::new (storage.addr()) T(t1, t2, t3, t4);
196 constructed = true;
199 template<class T1, class T2, class T3, class T4, class T5>
200 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) {
201 MOZ_ASSERT(!constructed);
202 ::new (storage.addr()) T(t1, t2, t3, t4, t5);
203 constructed = true;
206 template<class T1, class T2, class T3, class T4, class T5,
207 class T6>
208 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
209 const T6& t6) {
210 MOZ_ASSERT(!constructed);
211 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6);
212 constructed = true;
215 template<class T1, class T2, class T3, class T4, class T5,
216 class T6, class T7>
217 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
218 const T6& t6, const T7& t7) {
219 MOZ_ASSERT(!constructed);
220 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7);
221 constructed = true;
224 template<class T1, class T2, class T3, class T4, class T5,
225 class T6, class T7, class T8>
226 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
227 const T6& t6, const T7& t7, const T8& t8) {
228 MOZ_ASSERT(!constructed);
229 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8);
230 constructed = true;
233 template<class T1, class T2, class T3, class T4, class T5,
234 class T6, class T7, class T8, class T9>
235 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
236 const T6& t6, const T7& t7, const T8& t8, const T9& t9) {
237 MOZ_ASSERT(!constructed);
238 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9);
239 constructed = true;
242 template<class T1, class T2, class T3, class T4, class T5,
243 class T6, class T7, class T8, class T9, class T10>
244 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
245 const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10) {
246 MOZ_ASSERT(!constructed);
247 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
248 constructed = true;
251 T* addr() {
252 MOZ_ASSERT(constructed);
253 return &asT();
256 T& ref() {
257 MOZ_ASSERT(constructed);
258 return asT();
261 const T& ref() const {
262 MOZ_ASSERT(constructed);
263 return const_cast<Maybe*>(this)->asT();
266 void destroy() {
267 ref().~T();
268 constructed = false;
271 void destroyIfConstructed() {
272 if (!empty())
273 destroy();
276 private:
277 Maybe(const Maybe& other) MOZ_DELETE;
278 const Maybe& operator=(const Maybe& other) MOZ_DELETE;
282 * Safely subtract two pointers when it is known that end >= begin. This avoids
283 * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
284 * set, the unsigned subtraction followed by right shift will produce -1, or
285 * size_t(-1), instead of the real difference.
287 template<class T>
288 MOZ_ALWAYS_INLINE size_t
289 PointerRangeSize(T* begin, T* end)
291 MOZ_ASSERT(end >= begin);
292 return (size_t(end) - size_t(begin)) / sizeof(T);
296 * Compute the length of an array with constant length. (Use of this method
297 * with a non-array pointer will not compile.)
299 * Beware of the implicit trailing '\0' when using this with string constants.
301 template<typename T, size_t N>
302 MOZ_CONSTEXPR size_t
303 ArrayLength(T (&arr)[N])
305 return N;
309 * Compute the address one past the last element of a constant-length array.
311 * Beware of the implicit trailing '\0' when using this with string constants.
313 template<typename T, size_t N>
314 MOZ_CONSTEXPR T*
315 ArrayEnd(T (&arr)[N])
317 return arr + ArrayLength(arr);
320 } /* namespace mozilla */
322 #endif /* __cplusplus */
325 * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
326 * that can't use C++ template functions and for MOZ_STATIC_ASSERT() calls that
327 * can't call ArrayLength() when it is not a C++11 constexpr function.
329 #ifdef MOZ_HAVE_CXX11_CONSTEXPR
330 # define MOZ_ARRAY_LENGTH(array) mozilla::ArrayLength(array)
331 #else
332 # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
333 #endif
335 #endif /* mozilla_Util_h_ */