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/. */
7 * Miscellaneous uncategorized functionality. Please add new functionality to
8 * new headers, or to other appropriate existing headers, not here.
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"
23 * This class, and the corresponding macro MOZ_ALIGNOF, figure out how many
24 * bytes of alignment a given type needs.
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.
46 * MOZ_ALIGNED_DECL(char arr[2], 8);
48 * will declare a two-character array |arr| aligned to 8 bytes.
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
58 # warning "We don't know how to align variables on this compiler."
59 # define MOZ_ALIGNED_DECL(_type, _align) _type
63 * AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
66 * We support 1, 2, 4, 8, and 16-bit alignment.
68 template<size_t align
>
72 * We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
73 * foo is a template parameter.
79 MOZ_ALIGNED_DECL(uint8_t elem
, 1);
85 MOZ_ALIGNED_DECL(uint8_t elem
, 2);
91 MOZ_ALIGNED_DECL(uint8_t elem
, 4);
97 MOZ_ALIGNED_DECL(uint8_t elem
, 8);
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
124 const void* addr() const { return u
.bytes
; }
125 void* addr() { return u
.bytes
; }
129 struct AlignedStorage2
132 char bytes
[sizeof(T
)];
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>
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.
154 AlignedStorage2
<T
> storage
;
157 T
& asT() { return *storage
.addr(); }
160 Maybe() { constructed
= false; }
161 ~Maybe() { if (constructed
) asT().~T(); }
163 bool empty() const { return !constructed
; }
166 MOZ_ASSERT(!constructed
);
167 ::new (storage
.addr()) T();
172 void construct(const T1
& t1
) {
173 MOZ_ASSERT(!constructed
);
174 ::new (storage
.addr()) T(t1
);
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
);
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
);
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
);
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
);
206 template<class T1
, class T2
, class T3
, class T4
, class T5
,
208 void construct(const T1
& t1
, const T2
& t2
, const T3
& t3
, const T4
& t4
, const T5
& t5
,
210 MOZ_ASSERT(!constructed
);
211 ::new (storage
.addr()) T(t1
, t2
, t3
, t4
, t5
, t6
);
215 template<class T1
, class T2
, class T3
, class T4
, class T5
,
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
);
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
);
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
);
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
);
252 MOZ_ASSERT(constructed
);
257 MOZ_ASSERT(constructed
);
261 const T
& ref() const {
262 MOZ_ASSERT(constructed
);
263 return const_cast<Maybe
*>(this)->asT();
271 void destroyIfConstructed() {
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.
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
>
303 ArrayLength(T (&arr
)[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
>
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)
332 # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
335 #endif /* mozilla_Util_h_ */