2013-07-24 07:41:39 +00: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 03:36:43 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00: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 01:46:35 +00:00
|
|
|
|
2011-12-28 16:48:54 +00:00
|
|
|
/*
|
2013-12-09 02:52:54 +00:00
|
|
|
* Implements various helper functions related to arrays.
|
2011-12-28 16:48:54 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#ifndef mozilla_ArrayUtils_h
|
|
|
|
#define mozilla_ArrayUtils_h
|
2011-04-01 01:46:35 +00:00
|
|
|
|
2011-12-19 19:28:35 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-12-17 21:45:29 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-09 02:52:54 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
2011-04-28 22:48:52 +00:00
|
|
|
|
2011-04-20 23:27:30 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2014-06-23 21:14:26 +00:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-11-12 22:31:15 +00:00
|
|
|
#include "mozilla/Array.h"
|
2014-06-23 21:14:26 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2011-10-05 13:11:17 +00:00
|
|
|
|
2013-08-14 07:00:52 +00:00
|
|
|
namespace mozilla {
|
2011-04-28 22:48:52 +00:00
|
|
|
|
2011-06-06 18:02:34 +00:00
|
|
|
/*
|
2014-11-03 23:55:59 +00: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 18:02:34 +00:00
|
|
|
*/
|
2012-06-04 03:36:43 +00:00
|
|
|
template<class T>
|
2011-06-06 18:02:34 +00:00
|
|
|
MOZ_ALWAYS_INLINE size_t
|
2014-05-30 05:40:33 +00:00
|
|
|
PointerRangeSize(T* aBegin, T* aEnd)
|
2011-06-06 18:02:34 +00:00
|
|
|
{
|
2014-05-30 05:40:33 +00:00
|
|
|
MOZ_ASSERT(aEnd >= aBegin);
|
|
|
|
return (size_t(aEnd) - size_t(aBegin)) / sizeof(T);
|
2011-06-06 18:02:34 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00: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>
|
2013-01-08 07:29:00 +00:00
|
|
|
MOZ_CONSTEXPR size_t
|
2014-05-30 05:40:33 +00:00
|
|
|
ArrayLength(T (&aArr)[N])
|
2011-10-11 05:50:08 +00:00
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return N;
|
2011-10-11 05:50:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 22:31:15 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR size_t
|
2014-05-30 05:40:33 +00:00
|
|
|
ArrayLength(const Array<T, N>& aArr)
|
2013-11-12 22:31:15 +00:00
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00: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>
|
2013-01-08 07:29:00 +00:00
|
|
|
MOZ_CONSTEXPR T*
|
2014-05-30 05:40:33 +00:00
|
|
|
ArrayEnd(T (&aArr)[N])
|
2011-10-11 05:50:08 +00:00
|
|
|
{
|
2014-05-30 05:40:33 +00:00
|
|
|
return aArr + ArrayLength(aArr);
|
2011-10-11 05:50:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 22:31:15 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR T*
|
2014-05-30 05:40:33 +00:00
|
|
|
ArrayEnd(Array<T, N>& aArr)
|
2013-11-12 22:31:15 +00:00
|
|
|
{
|
2014-05-30 05:40:33 +00:00
|
|
|
return &aArr[0] + ArrayLength(aArr);
|
2013-11-12 22:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR const T*
|
2014-05-30 05:40:33 +00:00
|
|
|
ArrayEnd(const Array<T, N>& aArr)
|
2013-11-12 22:31:15 +00:00
|
|
|
{
|
2014-05-30 05:40:33 +00:00
|
|
|
return &aArr[0] + ArrayLength(aArr);
|
2013-11-12 22:31:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 18:31:58 +00:00
|
|
|
namespace detail {
|
|
|
|
|
2015-03-04 08:04:53 +00:00
|
|
|
template<typename AlignType, typename Pointee,
|
|
|
|
typename = EnableIf<!IsVoid<AlignType>::value>>
|
2014-06-23 21:14:26 +00:00
|
|
|
struct AlignedChecker
|
|
|
|
{
|
|
|
|
static void
|
2015-03-04 08:04:53 +00:00
|
|
|
test(const Pointee* aPtr)
|
2014-06-23 21:14:26 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT((uintptr_t(aPtr) % MOZ_ALIGNOF(AlignType)) == 0,
|
|
|
|
"performing a range-check with a misaligned pointer");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-04 08:04:53 +00:00
|
|
|
template<typename AlignType, typename Pointee>
|
|
|
|
struct AlignedChecker<AlignType, Pointee>
|
2014-06-23 21:14:26 +00:00
|
|
|
{
|
|
|
|
static void
|
2015-03-04 08:04:53 +00:00
|
|
|
test(const Pointee* aPtr)
|
2014-06-23 21:14:26 +00: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 08:04:53 +00:00
|
|
|
IsInRange(const T* aPtr, const U* aBegin, const U* aEnd)
|
2014-06-23 21:14:26 +00: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 08:04:53 +00:00
|
|
|
return aBegin <= reinterpret_cast<const U*>(aPtr) &&
|
|
|
|
reinterpret_cast<const U*>(aPtr) < aEnd;
|
2014-06-23 21:14:26 +00: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 08:04:53 +00:00
|
|
|
IsInRange(const T* aPtr, uintptr_t aBegin, uintptr_t aEnd)
|
2014-06-23 21:14:26 +00:00
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
return IsInRange(aPtr,
|
2015-03-04 08:04:53 +00:00
|
|
|
reinterpret_cast<const T*>(aBegin),
|
|
|
|
reinterpret_cast<const T*>(aEnd));
|
2014-06-23 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2014-02-11 18:31:58 +00: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-20 23:27:30 +00:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-04-01 01:46:35 +00:00
|
|
|
|
2013-01-06 07:37:25 +00:00
|
|
|
/*
|
|
|
|
* MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
|
2013-07-18 17:59:53 +00:00
|
|
|
* that can't use C++ template functions and for static_assert() calls that
|
2013-01-06 07:37:25 +00:00
|
|
|
* can't call ArrayLength() when it is not a C++11 constexpr function.
|
|
|
|
*/
|
2014-02-11 18:31:58 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
|
2013-01-08 07:29:00 +00:00
|
|
|
#else
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
#endif
|
2013-01-06 07:37:25 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#endif /* mozilla_ArrayUtils_h */
|