Debian Codegraph @

mfbt/ArrayUtils.h

raw
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/. */
6
7 /*
8 * Implements various helper functions related to arrays.
9 */
10
11 #ifndef mozilla_ArrayUtils_h
12 #define mozilla_ArrayUtils_h
13
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Attributes.h"
16
17 #include <stddef.h>
18 #include <stdint.h>
19
20 #ifdef __cplusplus
21 # include <algorithm>
22 # include <array>
23 # include <type_traits>
24
25 # include "mozilla/Alignment.h"
26
27 namespace mozilla {
28
29 /*
30 * Safely subtract two pointers when it is known that aEnd >= aBegin, yielding a
31 * size_t result.
32 *
33 * Ordinary pointer subtraction yields a ptrdiff_t result, which, being signed,
34 * has insufficient range to express the distance between pointers at opposite
35 * ends of the address space. Furthermore, most compilers use ptrdiff_t to
36 * represent the intermediate byte address distance, before dividing by
37 * sizeof(T); if that intermediate result overflows, they'll produce results
38 * with the wrong sign even when the correct scaled distance would fit in a
39 * ptrdiff_t.
40 */
41 template <class T>
42 MOZ_ALWAYS_INLINE size_t PointerRangeSize(T* aBegin, T* aEnd) {
43 MOZ_ASSERT(aEnd >= aBegin);
44 return (size_t(aEnd) - size_t(aBegin)) / sizeof(T);
45 }
46
47 /**
48 * std::equal has subpar ergonomics.
49 */
50
51 template <typename T, typename U, size_t N>
52 bool ArrayEqual(const T (&a)[N], const U (&b)[N]) {
53 return std::equal(a, a + N, b);
54 }
55
56 template <typename T, typename U>
57 bool ArrayEqual(const T* const a, const U* const b, const size_t n) {
58 return std::equal(a, a + n, b);
59 }
60
61 namespace detail {
62
63 template <typename AlignType, typename Pointee, typename = void>
64 struct AlignedChecker {
65 static void test(const Pointee* aPtr) {
66 MOZ_ASSERT((uintptr_t(aPtr) % alignof(AlignType)) == 0,
67 "performing a range-check with a misaligned pointer");
68 }
69 };
70
71 template <typename AlignType, typename Pointee>
72 struct AlignedChecker<AlignType, Pointee,
73 std::enable_if_t<std::is_void_v<AlignType>>> {
74 static void test(const Pointee* aPtr) {}
75 };
76
77 } // namespace detail
78
79 /**
80 * Determines whether |aPtr| points at an object in the range [aBegin, aEnd).
81 *
82 * |aPtr| must have the same alignment as |aBegin| and |aEnd|. This usually
83 * should be achieved by ensuring |aPtr| points at a |U|, not just that it
84 * points at a |T|.
85 *
86 * It is a usage error for any argument to be misaligned.
87 *
88 * It's okay for T* to be void*, and if so U* may also be void*. In the latter
89 * case no argument is required to be aligned (obviously, as void* implies no
90 * particular alignment).
91 */
92 template <typename T, typename U>
93 inline std::enable_if_t<std::is_same_v<T, U> || std::is_base_of<T, U>::value ||
94 std::is_void_v<T>,
95 bool>
96 IsInRange(const T* aPtr, const U* aBegin, const U* aEnd) {
97 MOZ_ASSERT(aBegin <= aEnd);
98 detail::AlignedChecker<U, T>::test(aPtr);
99 detail::AlignedChecker<U, U>::test(aBegin);
100 detail::AlignedChecker<U, U>::test(aEnd);
101 return aBegin <= reinterpret_cast<const U*>(aPtr) &&
102 reinterpret_cast<const U*>(aPtr) < aEnd;
103 }
104
105 /**
106 * Convenience version of the above method when the valid range is specified as
107 * uintptr_t values. As above, |aPtr| must be aligned, and |aBegin| and |aEnd|
108 * must be aligned with respect to |T|.
109 */
110 template <typename T>
111 inline bool IsInRange(const T* aPtr, uintptr_t aBegin, uintptr_t aEnd) {
112 return IsInRange(aPtr, reinterpret_cast<const T*>(aBegin),
113 reinterpret_cast<const T*>(aEnd));
114 }
115
116 } /* namespace mozilla */
117
118 #endif /* __cplusplus */
119
120 #endif /* mozilla_ArrayUtils_h */