2013-07-24 00:41:39 -07: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-03 20:36:43 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 04:12:37 -07: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-03-31 18:46:35 -07:00
|
|
|
|
2011-12-28 08:48:54 -08:00
|
|
|
/*
|
|
|
|
* Miscellaneous uncategorized functionality. Please add new functionality to
|
|
|
|
* new headers, or to other appropriate existing headers, not here.
|
|
|
|
*/
|
|
|
|
|
2013-07-24 00:41:39 -07:00
|
|
|
#ifndef mozilla_Util_h
|
|
|
|
#define mozilla_Util_h
|
2011-03-31 18:46:35 -07:00
|
|
|
|
2011-12-19 11:28:35 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-12-17 13:45:29 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-28 15:48:52 -07:00
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
2011-04-20 16:27:30 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2013-08-14 00:00:52 -07:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-11-12 14:31:15 -08:00
|
|
|
#include "mozilla/Array.h"
|
2011-10-05 06:11:17 -07:00
|
|
|
|
2013-08-14 00:00:52 -07:00
|
|
|
namespace mozilla {
|
2011-04-28 15:48:52 -07:00
|
|
|
|
2011-06-06 11:02:34 -07:00
|
|
|
/*
|
|
|
|
* Safely subtract two pointers when it is known that end >= begin. This avoids
|
|
|
|
* the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
|
|
|
|
* set, the unsigned subtraction followed by right shift will produce -1, or
|
|
|
|
* size_t(-1), instead of the real difference.
|
|
|
|
*/
|
2012-06-03 20:36:43 -07:00
|
|
|
template<class T>
|
2011-06-06 11:02:34 -07:00
|
|
|
MOZ_ALWAYS_INLINE size_t
|
|
|
|
PointerRangeSize(T* begin, T* end)
|
|
|
|
{
|
2012-06-03 20:36:43 -07:00
|
|
|
MOZ_ASSERT(end >= begin);
|
|
|
|
return (size_t(end) - size_t(begin)) / sizeof(T);
|
2011-06-06 11:02:34 -07:00
|
|
|
}
|
|
|
|
|
2011-10-10 22:50:08 -07: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-07 23:29:00 -08:00
|
|
|
MOZ_CONSTEXPR size_t
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(T (&arr)[N])
|
|
|
|
{
|
2012-06-03 20:36:43 -07:00
|
|
|
return N;
|
2011-10-10 22:50:08 -07:00
|
|
|
}
|
|
|
|
|
2013-11-12 14:31:15 -08:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR size_t
|
|
|
|
ArrayLength(const Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:50:08 -07: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-07 23:29:00 -08:00
|
|
|
MOZ_CONSTEXPR T*
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayEnd(T (&arr)[N])
|
|
|
|
{
|
2012-06-03 20:36:43 -07:00
|
|
|
return arr + ArrayLength(arr);
|
2011-10-10 22:50:08 -07:00
|
|
|
}
|
|
|
|
|
2013-11-12 14:31:15 -08:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR T*
|
|
|
|
ArrayEnd(Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return &arr[0] + ArrayLength(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR const T*
|
|
|
|
ArrayEnd(const Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return &arr[0] + ArrayLength(arr);
|
|
|
|
}
|
|
|
|
|
2011-04-20 16:27:30 -07:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-03-31 18:46:35 -07:00
|
|
|
|
2013-01-05 23:37:25 -08:00
|
|
|
/*
|
|
|
|
* MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
|
2013-07-18 10:59:53 -07:00
|
|
|
* that can't use C++ template functions and for static_assert() calls that
|
2013-01-05 23:37:25 -08:00
|
|
|
* can't call ArrayLength() when it is not a C++11 constexpr function.
|
|
|
|
*/
|
2013-01-07 23:29:00 -08:00
|
|
|
#ifdef MOZ_HAVE_CXX11_CONSTEXPR
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) mozilla::ArrayLength(array)
|
|
|
|
#else
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
#endif
|
2013-01-05 23:37:25 -08:00
|
|
|
|
2013-07-24 00:41:39 -07:00
|
|
|
#endif /* mozilla_Util_h */
|