2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-04 07:36:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2011-12-28 20:48:54 +04:00
|
|
|
/*
|
2013-12-09 06:52:54 +04:00
|
|
|
* Implements various helper functions related to arrays.
|
2011-12-28 20:48:54 +04:00
|
|
|
*/
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#ifndef mozilla_ArrayUtils_h
|
|
|
|
#define mozilla_ArrayUtils_h
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2011-12-19 23:28:35 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-12-18 01:45:29 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
|
|
|
|
#include <stddef.h>
|
2011-04-29 02:48:52 +04:00
|
|
|
|
2011-04-21 03:27:30 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2014-06-24 01:14:26 +04:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-11-13 02:31:15 +04:00
|
|
|
#include "mozilla/Array.h"
|
2015-10-20 02:16:20 +03:00
|
|
|
#include "mozilla/EnumeratedArray.h"
|
2014-06-24 01:14:26 +04:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2011-10-05 17:11:17 +04:00
|
|
|
|
2013-08-14 11:00:52 +04:00
|
|
|
namespace mozilla {
|
2011-04-29 02:48:52 +04:00
|
|
|
|
2011-06-06 22:02:34 +04:00
|
|
|
/*
|
2014-11-04 02:55:59 +03:00
|
|
|
* Safely subtract two pointers when it is known that aEnd >= aBegin, yielding a
|
|
|
|
* size_t result.
|
|
|
|
*
|
|
|
|
* Ordinary pointer subtraction yields a ptrdiff_t result, which, being signed,
|
|
|
|
* has insufficient range to express the distance between pointers at opposite
|
|
|
|
* ends of the address space. Furthermore, most compilers use ptrdiff_t to
|
|
|
|
* represent the intermediate byte address distance, before dividing by
|
|
|
|
* sizeof(T); if that intermediate result overflows, they'll produce results
|
|
|
|
* with the wrong sign even when the correct scaled distance would fit in a
|
|
|
|
* ptrdiff_t.
|
2011-06-06 22:02:34 +04:00
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T>
|
2011-06-06 22:02:34 +04:00
|
|
|
MOZ_ALWAYS_INLINE size_t
|
2014-05-30 09:40:33 +04:00
|
|
|
PointerRangeSize(T* aBegin, T* aEnd)
|
2011-06-06 22:02:34 +04:00
|
|
|
{
|
2014-05-30 09:40:33 +04:00
|
|
|
MOZ_ASSERT(aEnd >= aBegin);
|
|
|
|
return (size_t(aEnd) - size_t(aBegin)) / sizeof(T);
|
2011-06-06 22:02:34 +04:00
|
|
|
}
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
/*
|
|
|
|
* Compute the length of an array with constant length. (Use of this method
|
|
|
|
* with a non-array pointer will not compile.)
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr size_t
|
2014-05-30 09:40:33 +04:00
|
|
|
ArrayLength(T (&aArr)[N])
|
2011-10-11 09:50:08 +04:00
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
return N;
|
2011-10-11 09:50:08 +04:00
|
|
|
}
|
|
|
|
|
2013-11-13 02:31:15 +04:00
|
|
|
template<typename T, size_t N>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr size_t
|
2014-05-30 09:40:33 +04:00
|
|
|
ArrayLength(const Array<T, N>& aArr)
|
2013-11-13 02:31:15 +04:00
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2015-10-20 02:16:20 +03:00
|
|
|
template<typename E, E N, typename T>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr size_t
|
2015-10-20 02:16:20 +03:00
|
|
|
ArrayLength(const EnumeratedArray<E, N, T>& aArr)
|
|
|
|
{
|
|
|
|
return size_t(N);
|
|
|
|
}
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
/*
|
|
|
|
* Compute the address one past the last element of a constant-length array.
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr T*
|
2014-05-30 09:40:33 +04:00
|
|
|
ArrayEnd(T (&aArr)[N])
|
2011-10-11 09:50:08 +04:00
|
|
|
{
|
2014-05-30 09:40:33 +04:00
|
|
|
return aArr + ArrayLength(aArr);
|
2011-10-11 09:50:08 +04:00
|
|
|
}
|
|
|
|
|
2013-11-13 02:31:15 +04:00
|
|
|
template<typename T, size_t N>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr T*
|
2014-05-30 09:40:33 +04:00
|
|
|
ArrayEnd(Array<T, N>& aArr)
|
2013-11-13 02:31:15 +04:00
|
|
|
{
|
2014-05-30 09:40:33 +04:00
|
|
|
return &aArr[0] + ArrayLength(aArr);
|
2013-11-13 02:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr const T*
|
2014-05-30 09:40:33 +04:00
|
|
|
ArrayEnd(const Array<T, N>& aArr)
|
2013-11-13 02:31:15 +04:00
|
|
|
{
|
2014-05-30 09:40:33 +04:00
|
|
|
return &aArr[0] + ArrayLength(aArr);
|
2013-11-13 02:31:15 +04:00
|
|
|
}
|
|
|
|
|
2014-02-11 22:31:58 +04:00
|
|
|
namespace detail {
|
|
|
|
|
2015-03-04 11:04:53 +03:00
|
|
|
template<typename AlignType, typename Pointee,
|
|
|
|
typename = EnableIf<!IsVoid<AlignType>::value>>
|
2014-06-24 01:14:26 +04:00
|
|
|
struct AlignedChecker
|
|
|
|
{
|
|
|
|
static void
|
2015-03-04 11:04:53 +03:00
|
|
|
test(const Pointee* aPtr)
|
2014-06-24 01:14:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT((uintptr_t(aPtr) % MOZ_ALIGNOF(AlignType)) == 0,
|
|
|
|
"performing a range-check with a misaligned pointer");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-04 11:04:53 +03:00
|
|
|
template<typename AlignType, typename Pointee>
|
|
|
|
struct AlignedChecker<AlignType, Pointee>
|
2014-06-24 01:14:26 +04:00
|
|
|
{
|
|
|
|
static void
|
2015-03-04 11:04:53 +03:00
|
|
|
test(const Pointee* aPtr)
|
2014-06-24 01:14:26 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether |aPtr| points at an object in the range [aBegin, aEnd).
|
|
|
|
*
|
|
|
|
* |aPtr| must have the same alignment as |aBegin| and |aEnd|. This usually
|
|
|
|
* should be achieved by ensuring |aPtr| points at a |U|, not just that it
|
|
|
|
* points at a |T|.
|
|
|
|
*
|
|
|
|
* It is a usage error for any argument to be misaligned.
|
|
|
|
*
|
|
|
|
* It's okay for T* to be void*, and if so U* may also be void*. In the latter
|
|
|
|
* case no argument is required to be aligned (obviously, as void* implies no
|
|
|
|
* particular alignment).
|
|
|
|
*/
|
|
|
|
template<typename T, typename U>
|
|
|
|
inline typename EnableIf<IsSame<T, U>::value ||
|
|
|
|
IsBaseOf<T, U>::value ||
|
|
|
|
IsVoid<T>::value,
|
|
|
|
bool>::Type
|
2015-03-04 11:04:53 +03:00
|
|
|
IsInRange(const T* aPtr, const U* aBegin, const U* aEnd)
|
2014-06-24 01:14:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
detail::AlignedChecker<U, T>::test(aPtr);
|
|
|
|
detail::AlignedChecker<U, U>::test(aBegin);
|
|
|
|
detail::AlignedChecker<U, U>::test(aEnd);
|
2015-03-04 11:04:53 +03:00
|
|
|
return aBegin <= reinterpret_cast<const U*>(aPtr) &&
|
|
|
|
reinterpret_cast<const U*>(aPtr) < aEnd;
|
2014-06-24 01:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience version of the above method when the valid range is specified as
|
|
|
|
* uintptr_t values. As above, |aPtr| must be aligned, and |aBegin| and |aEnd|
|
|
|
|
* must be aligned with respect to |T|.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
inline bool
|
2015-03-04 11:04:53 +03:00
|
|
|
IsInRange(const T* aPtr, uintptr_t aBegin, uintptr_t aEnd)
|
2014-06-24 01:14:26 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return IsInRange(aPtr,
|
2015-03-04 11:04:53 +03:00
|
|
|
reinterpret_cast<const T*>(aBegin),
|
|
|
|
reinterpret_cast<const T*>(aEnd));
|
2014-06-24 01:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2014-02-11 22:31:58 +04:00
|
|
|
/*
|
|
|
|
* Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe
|
|
|
|
* compile-time constant even on compilers lacking constexpr support.
|
|
|
|
*/
|
|
|
|
template <typename T, size_t N>
|
|
|
|
char (&ArrayLengthHelper(T (&array)[N]))[N];
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2011-04-21 03:27:30 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2013-01-06 11:37:25 +04:00
|
|
|
/*
|
|
|
|
* MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
|
2013-07-18 21:59:53 +04:00
|
|
|
* that can't use C++ template functions and for static_assert() calls that
|
2013-01-06 11:37:25 +04:00
|
|
|
* can't call ArrayLength() when it is not a C++11 constexpr function.
|
|
|
|
*/
|
2014-02-11 22:31:58 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
|
2013-01-08 11:29:00 +04:00
|
|
|
#else
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
#endif
|
2013-01-06 11:37:25 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#endif /* mozilla_ArrayUtils_h */
|