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 * DebugOnly contains a value of type T, but only in debug builds. In release
24 * builds, it does not contain a value. This helper is intended to be used with
25 * MOZ_ASSERT()-style macros, allowing one to write:
27 * DebugOnly<bool> check = func();
30 * more concisely than declaring |check| conditional on #ifdef DEBUG, but also
31 * without allocating storage space for |check| in release builds.
33 * DebugOnly instances can only be coerced to T in debug builds. In release
34 * builds they don't have a value, so type coercion is not well defined.
43 DebugOnly(const T
& other
) : value(other
) { }
44 DebugOnly(const DebugOnly
& other
) : value(other
.value
) { }
45 DebugOnly
& operator=(const T
& rhs
) {
49 void operator++(int) {
52 void operator--(int) {
56 T
*operator&() { return &value
; }
58 operator T
&() { return value
; }
59 operator const T
&() const { return value
; }
61 T
& operator->() { return value
; }
65 DebugOnly(const T
&) { }
66 DebugOnly(const DebugOnly
&) { }
67 DebugOnly
& operator=(const T
&) { return *this; }
68 void operator++(int) { }
69 void operator--(int) { }
73 * DebugOnly must always have a destructor or else it will
74 * generate "unused variable" warnings, exactly what it's intended
81 * This class, and the corresponding macro MOZ_ALIGNOF, figure out how many
82 * bytes of alignment a given type needs.
94 static const size_t alignment
= sizeof(Aligner
) - sizeof(T
);
97 #define MOZ_ALIGNOF(T) mozilla::AlignmentFinder<T>::alignment
100 * Declare the MOZ_ALIGNED_DECL macro for declaring aligned types.
104 * MOZ_ALIGNED_DECL(char arr[2], 8);
106 * will declare a two-character array |arr| aligned to 8 bytes.
109 #if defined(__GNUC__)
110 # define MOZ_ALIGNED_DECL(_type, _align) \
111 _type __attribute__((aligned(_align)))
112 #elif defined(_MSC_VER)
113 # define MOZ_ALIGNED_DECL(_type, _align) \
114 __declspec(align(_align)) _type
116 # warning "We don't know how to align variables on this compiler."
117 # define MOZ_ALIGNED_DECL(_type, _align) _type
121 * AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
124 * We support 1, 2, 4, 8, and 16-bit alignment.
126 template<size_t align
>
130 * We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
131 * foo is a template parameter.
135 struct AlignedElem
<1>
137 MOZ_ALIGNED_DECL(uint8_t elem
, 1);
141 struct AlignedElem
<2>
143 MOZ_ALIGNED_DECL(uint8_t elem
, 2);
147 struct AlignedElem
<4>
149 MOZ_ALIGNED_DECL(uint8_t elem
, 4);
153 struct AlignedElem
<8>
155 MOZ_ALIGNED_DECL(uint8_t elem
, 8);
159 struct AlignedElem
<16>
161 MOZ_ALIGNED_DECL(uint8_t elem
, 16);
165 * This utility pales in comparison to Boost's aligned_storage. The utility
166 * simply assumes that uint64_t is enough alignment for anyone. This may need
167 * to be extended one day...
169 * As an important side effect, pulling the storage into this template is
170 * enough obfuscation to confuse gcc's strict-aliasing analysis into not giving
171 * false negatives when we cast from the char buffer to whatever type we've
172 * constructed using the bytes.
174 template<size_t nbytes
>
175 struct AlignedStorage
182 const void* addr() const { return u
.bytes
; }
183 void* addr() { return u
.bytes
; }
187 struct AlignedStorage2
190 char bytes
[sizeof(T
)];
194 const T
* addr() const { return reinterpret_cast<const T
*>(u
.bytes
); }
195 T
* addr() { return static_cast<T
*>(static_cast<void*>(u
.bytes
)); }
199 * Small utility for lazily constructing objects without using dynamic storage.
200 * When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
201 * been constructed and no T destructor will be called when the Maybe<T> is
202 * destroyed. Upon calling |construct|, a T object will be constructed with the
203 * given arguments and that object will be destroyed when the owning Maybe<T>
206 * N.B. GCC seems to miss some optimizations with Maybe and may generate extra
207 * branches/loads/stores. Use with caution on hot paths.
212 AlignedStorage2
<T
> storage
;
215 T
& asT() { return *storage
.addr(); }
218 Maybe() { constructed
= false; }
219 ~Maybe() { if (constructed
) asT().~T(); }
221 bool empty() const { return !constructed
; }
224 MOZ_ASSERT(!constructed
);
225 new (storage
.addr()) T();
230 void construct(const T1
& t1
) {
231 MOZ_ASSERT(!constructed
);
232 new (storage
.addr()) T(t1
);
236 template<class T1
, class T2
>
237 void construct(const T1
& t1
, const T2
& t2
) {
238 MOZ_ASSERT(!constructed
);
239 new (storage
.addr()) T(t1
, t2
);
243 template<class T1
, class T2
, class T3
>
244 void construct(const T1
& t1
, const T2
& t2
, const T3
& t3
) {
245 MOZ_ASSERT(!constructed
);
246 new (storage
.addr()) T(t1
, t2
, t3
);
250 template<class T1
, class T2
, class T3
, class T4
>
251 void construct(const T1
& t1
, const T2
& t2
, const T3
& t3
, const T4
& t4
) {
252 MOZ_ASSERT(!constructed
);
253 new (storage
.addr()) T(t1
, t2
, t3
, t4
);
258 MOZ_ASSERT(constructed
);
263 MOZ_ASSERT(constructed
);
267 const T
& ref() const {
268 MOZ_ASSERT(constructed
);
269 return const_cast<Maybe
*>(this)->asT();
277 void destroyIfConstructed() {
283 Maybe(const Maybe
& other
) MOZ_DELETE
;
284 const Maybe
& operator=(const Maybe
& other
) MOZ_DELETE
;
288 * Safely subtract two pointers when it is known that end >= begin. This avoids
289 * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
290 * set, the unsigned subtraction followed by right shift will produce -1, or
291 * size_t(-1), instead of the real difference.
294 MOZ_ALWAYS_INLINE
size_t
295 PointerRangeSize(T
* begin
, T
* end
)
297 MOZ_ASSERT(end
>= begin
);
298 return (size_t(end
) - size_t(begin
)) / sizeof(T
);
302 * Compute the length of an array with constant length. (Use of this method
303 * with a non-array pointer will not compile.)
305 * Beware of the implicit trailing '\0' when using this with string constants.
307 template<typename T
, size_t N
>
309 ArrayLength(T (&arr
)[N
])
315 * Compute the address one past the last element of a constant-length array.
317 * Beware of the implicit trailing '\0' when using this with string constants.
319 template<typename T
, size_t N
>
321 ArrayEnd(T (&arr
)[N
])
323 return arr
+ ArrayLength(arr
);
326 } /* namespace mozilla */
328 #endif /* __cplusplus */
330 #endif /* mozilla_Util_h_ */