ArrayUtils.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * Implements various helper functions related to arrays.
  8. */
  9. #ifndef mozilla_ArrayUtils_h
  10. #define mozilla_ArrayUtils_h
  11. #include "mozilla/Assertions.h"
  12. #include "mozilla/Attributes.h"
  13. #include <stddef.h>
  14. #ifdef __cplusplus
  15. #include "mozilla/Alignment.h"
  16. #include "mozilla/Array.h"
  17. #include "mozilla/EnumeratedArray.h"
  18. #include "mozilla/TypeTraits.h"
  19. namespace mozilla {
  20. /*
  21. * Safely subtract two pointers when it is known that aEnd >= aBegin, yielding a
  22. * size_t result.
  23. *
  24. * Ordinary pointer subtraction yields a ptrdiff_t result, which, being signed,
  25. * has insufficient range to express the distance between pointers at opposite
  26. * ends of the address space. Furthermore, most compilers use ptrdiff_t to
  27. * represent the intermediate byte address distance, before dividing by
  28. * sizeof(T); if that intermediate result overflows, they'll produce results
  29. * with the wrong sign even when the correct scaled distance would fit in a
  30. * ptrdiff_t.
  31. */
  32. template<class T>
  33. MOZ_ALWAYS_INLINE size_t
  34. PointerRangeSize(T* aBegin, T* aEnd)
  35. {
  36. MOZ_ASSERT(aEnd >= aBegin);
  37. return (size_t(aEnd) - size_t(aBegin)) / sizeof(T);
  38. }
  39. /*
  40. * Compute the length of an array with constant length. (Use of this method
  41. * with a non-array pointer will not compile.)
  42. *
  43. * Beware of the implicit trailing '\0' when using this with string constants.
  44. */
  45. template<typename T, size_t N>
  46. constexpr size_t
  47. ArrayLength(T (&aArr)[N])
  48. {
  49. return N;
  50. }
  51. template<typename T, size_t N>
  52. constexpr size_t
  53. ArrayLength(const Array<T, N>& aArr)
  54. {
  55. return N;
  56. }
  57. template<typename E, E N, typename T>
  58. constexpr size_t
  59. ArrayLength(const EnumeratedArray<E, N, T>& aArr)
  60. {
  61. return size_t(N);
  62. }
  63. /*
  64. * Compute the address one past the last element of a constant-length array.
  65. *
  66. * Beware of the implicit trailing '\0' when using this with string constants.
  67. */
  68. template<typename T, size_t N>
  69. constexpr T*
  70. ArrayEnd(T (&aArr)[N])
  71. {
  72. return aArr + ArrayLength(aArr);
  73. }
  74. template<typename T, size_t N>
  75. constexpr T*
  76. ArrayEnd(Array<T, N>& aArr)
  77. {
  78. return &aArr[0] + ArrayLength(aArr);
  79. }
  80. template<typename T, size_t N>
  81. constexpr const T*
  82. ArrayEnd(const Array<T, N>& aArr)
  83. {
  84. return &aArr[0] + ArrayLength(aArr);
  85. }
  86. namespace detail {
  87. template<typename AlignType, typename Pointee,
  88. typename = EnableIf<!IsVoid<AlignType>::value>>
  89. struct AlignedChecker
  90. {
  91. static void
  92. test(const Pointee* aPtr)
  93. {
  94. MOZ_ASSERT((uintptr_t(aPtr) % MOZ_ALIGNOF(AlignType)) == 0,
  95. "performing a range-check with a misaligned pointer");
  96. }
  97. };
  98. template<typename AlignType, typename Pointee>
  99. struct AlignedChecker<AlignType, Pointee>
  100. {
  101. static void
  102. test(const Pointee* aPtr)
  103. {
  104. }
  105. };
  106. } // namespace detail
  107. /**
  108. * Determines whether |aPtr| points at an object in the range [aBegin, aEnd).
  109. *
  110. * |aPtr| must have the same alignment as |aBegin| and |aEnd|. This usually
  111. * should be achieved by ensuring |aPtr| points at a |U|, not just that it
  112. * points at a |T|.
  113. *
  114. * It is a usage error for any argument to be misaligned.
  115. *
  116. * It's okay for T* to be void*, and if so U* may also be void*. In the latter
  117. * case no argument is required to be aligned (obviously, as void* implies no
  118. * particular alignment).
  119. */
  120. template<typename T, typename U>
  121. inline typename EnableIf<IsSame<T, U>::value ||
  122. IsBaseOf<T, U>::value ||
  123. IsVoid<T>::value,
  124. bool>::Type
  125. IsInRange(const T* aPtr, const U* aBegin, const U* aEnd)
  126. {
  127. MOZ_ASSERT(aBegin <= aEnd);
  128. detail::AlignedChecker<U, T>::test(aPtr);
  129. detail::AlignedChecker<U, U>::test(aBegin);
  130. detail::AlignedChecker<U, U>::test(aEnd);
  131. return aBegin <= reinterpret_cast<const U*>(aPtr) &&
  132. reinterpret_cast<const U*>(aPtr) < aEnd;
  133. }
  134. /**
  135. * Convenience version of the above method when the valid range is specified as
  136. * uintptr_t values. As above, |aPtr| must be aligned, and |aBegin| and |aEnd|
  137. * must be aligned with respect to |T|.
  138. */
  139. template<typename T>
  140. inline bool
  141. IsInRange(const T* aPtr, uintptr_t aBegin, uintptr_t aEnd)
  142. {
  143. return IsInRange(aPtr,
  144. reinterpret_cast<const T*>(aBegin),
  145. reinterpret_cast<const T*>(aEnd));
  146. }
  147. namespace detail {
  148. /*
  149. * Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe
  150. * compile-time constant even on compilers lacking constexpr support.
  151. */
  152. template <typename T, size_t N>
  153. char (&ArrayLengthHelper(T (&array)[N]))[N];
  154. } /* namespace detail */
  155. } /* namespace mozilla */
  156. #endif /* __cplusplus */
  157. /*
  158. * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
  159. * that can't use C++ template functions and for static_assert() calls that
  160. * can't call ArrayLength() when it is not a C++11 constexpr function.
  161. */
  162. #ifdef __cplusplus
  163. # define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
  164. #else
  165. # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
  166. #endif
  167. #endif /* mozilla_ArrayUtils_h */