Bumping manifests a=b2g-bump
[gecko.git] / mfbt / ArrayUtils.h
blob44f5980c44cc66ca9d5548719b27430bfb1de4f3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Implements various helper functions related to arrays.
9 */
11 #ifndef mozilla_ArrayUtils_h
12 #define mozilla_ArrayUtils_h
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Attributes.h"
17 #include <stddef.h>
19 #ifdef __cplusplus
21 #include "mozilla/Alignment.h"
22 #include "mozilla/Array.h"
23 #include "mozilla/TypeTraits.h"
25 namespace mozilla {
28 * Safely subtract two pointers when it is known that aEnd >= aBegin. This
29 * avoids the common compiler bug that if (size_t(aEnd) - size_t(aBegin)) has
30 * the MSB set, the unsigned subtraction followed by right shift will produce
31 * -1, or size_t(-1), instead of the real difference.
33 template<class T>
34 MOZ_ALWAYS_INLINE size_t
35 PointerRangeSize(T* aBegin, T* aEnd)
37 MOZ_ASSERT(aEnd >= aBegin);
38 return (size_t(aEnd) - size_t(aBegin)) / sizeof(T);
42 * Compute the length of an array with constant length. (Use of this method
43 * with a non-array pointer will not compile.)
45 * Beware of the implicit trailing '\0' when using this with string constants.
47 template<typename T, size_t N>
48 MOZ_CONSTEXPR size_t
49 ArrayLength(T (&aArr)[N])
51 return N;
54 template<typename T, size_t N>
55 MOZ_CONSTEXPR size_t
56 ArrayLength(const Array<T, N>& aArr)
58 return N;
62 * Compute the address one past the last element of a constant-length array.
64 * Beware of the implicit trailing '\0' when using this with string constants.
66 template<typename T, size_t N>
67 MOZ_CONSTEXPR T*
68 ArrayEnd(T (&aArr)[N])
70 return aArr + ArrayLength(aArr);
73 template<typename T, size_t N>
74 MOZ_CONSTEXPR T*
75 ArrayEnd(Array<T, N>& aArr)
77 return &aArr[0] + ArrayLength(aArr);
80 template<typename T, size_t N>
81 MOZ_CONSTEXPR const T*
82 ArrayEnd(const Array<T, N>& aArr)
84 return &aArr[0] + ArrayLength(aArr);
87 namespace detail {
89 template<typename AlignType, typename Pointee>
90 struct AlignedChecker
92 static void
93 test(Pointee* aPtr)
95 MOZ_ASSERT((uintptr_t(aPtr) % MOZ_ALIGNOF(AlignType)) == 0,
96 "performing a range-check with a misaligned pointer");
100 template<typename Pointee>
101 struct AlignedChecker<void, Pointee>
103 static void
104 test(Pointee* aPtr)
109 } // namespace detail
112 * Determines whether |aPtr| points at an object in the range [aBegin, aEnd).
114 * |aPtr| must have the same alignment as |aBegin| and |aEnd|. This usually
115 * should be achieved by ensuring |aPtr| points at a |U|, not just that it
116 * points at a |T|.
118 * It is a usage error for any argument to be misaligned.
120 * It's okay for T* to be void*, and if so U* may also be void*. In the latter
121 * case no argument is required to be aligned (obviously, as void* implies no
122 * particular alignment).
124 template<typename T, typename U>
125 inline typename EnableIf<IsSame<T, U>::value ||
126 IsBaseOf<T, U>::value ||
127 IsVoid<T>::value,
128 bool>::Type
129 IsInRange(T* aPtr, U* aBegin, U* aEnd)
131 MOZ_ASSERT(aBegin <= aEnd);
132 detail::AlignedChecker<U, T>::test(aPtr);
133 detail::AlignedChecker<U, U>::test(aBegin);
134 detail::AlignedChecker<U, U>::test(aEnd);
135 return aBegin <= static_cast<U*>(aPtr) && static_cast<U*>(aPtr) < aEnd;
139 * Convenience version of the above method when the valid range is specified as
140 * uintptr_t values. As above, |aPtr| must be aligned, and |aBegin| and |aEnd|
141 * must be aligned with respect to |T|.
143 template<typename T>
144 inline bool
145 IsInRange(T* aPtr, uintptr_t aBegin, uintptr_t aEnd)
147 return IsInRange(aPtr,
148 reinterpret_cast<T*>(aBegin), reinterpret_cast<T*>(aEnd));
151 namespace detail {
154 * Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe
155 * compile-time constant even on compilers lacking constexpr support.
157 template <typename T, size_t N>
158 char (&ArrayLengthHelper(T (&array)[N]))[N];
160 } /* namespace detail */
162 } /* namespace mozilla */
164 #endif /* __cplusplus */
167 * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
168 * that can't use C++ template functions and for static_assert() calls that
169 * can't call ArrayLength() when it is not a C++11 constexpr function.
171 #ifdef __cplusplus
172 # define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
173 #else
174 # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
175 #endif
177 #endif /* mozilla_ArrayUtils_h */