Bug 835381 - Unit tests for the MediaSniffer to make sure Matroska files are not...
[gecko.git] / mfbt / Util.h
blob097c5478eb5e9580ce9b7af110fc68c6241b15ed
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 T* addr() {
200 MOZ_ASSERT(constructed);
201 return &asT();
204 T& ref() {
205 MOZ_ASSERT(constructed);
206 return asT();
209 const T& ref() const {
210 MOZ_ASSERT(constructed);
211 return const_cast<Maybe*>(this)->asT();
214 void destroy() {
215 ref().~T();
216 constructed = false;
219 void destroyIfConstructed() {
220 if (!empty())
221 destroy();
224 private:
225 Maybe(const Maybe& other) MOZ_DELETE;
226 const Maybe& operator=(const Maybe& other) MOZ_DELETE;
230 * Safely subtract two pointers when it is known that end >= begin. This avoids
231 * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
232 * set, the unsigned subtraction followed by right shift will produce -1, or
233 * size_t(-1), instead of the real difference.
235 template<class T>
236 MOZ_ALWAYS_INLINE size_t
237 PointerRangeSize(T* begin, T* end)
239 MOZ_ASSERT(end >= begin);
240 return (size_t(end) - size_t(begin)) / sizeof(T);
244 * Compute the length of an array with constant length. (Use of this method
245 * with a non-array pointer will not compile.)
247 * Beware of the implicit trailing '\0' when using this with string constants.
249 template<typename T, size_t N>
250 MOZ_CONSTEXPR size_t
251 ArrayLength(T (&arr)[N])
253 return N;
257 * Compute the address one past the last element of a constant-length array.
259 * Beware of the implicit trailing '\0' when using this with string constants.
261 template<typename T, size_t N>
262 MOZ_CONSTEXPR T*
263 ArrayEnd(T (&arr)[N])
265 return arr + ArrayLength(arr);
268 } /* namespace mozilla */
270 #endif /* __cplusplus */
273 * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
274 * that can't use C++ template functions and for MOZ_STATIC_ASSERT() calls that
275 * can't call ArrayLength() when it is not a C++11 constexpr function.
277 #ifdef MOZ_HAVE_CXX11_CONSTEXPR
278 # define MOZ_ARRAY_LENGTH(array) mozilla::ArrayLength(array)
279 #else
280 # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
281 #endif
283 #endif /* mozilla_Util_h_ */