michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Implements various helper functions related to arrays. michael@0: */ michael@0: michael@0: #ifndef mozilla_ArrayUtils_h michael@0: #define mozilla_ArrayUtils_h michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: #include "mozilla/Array.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /* michael@0: * Safely subtract two pointers when it is known that end >= begin. This avoids michael@0: * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB michael@0: * set, the unsigned subtraction followed by right shift will produce -1, or michael@0: * size_t(-1), instead of the real difference. michael@0: */ michael@0: template michael@0: MOZ_ALWAYS_INLINE size_t michael@0: PointerRangeSize(T* begin, T* end) michael@0: { michael@0: MOZ_ASSERT(end >= begin); michael@0: return (size_t(end) - size_t(begin)) / sizeof(T); michael@0: } michael@0: michael@0: /* michael@0: * Compute the length of an array with constant length. (Use of this method michael@0: * with a non-array pointer will not compile.) michael@0: * michael@0: * Beware of the implicit trailing '\0' when using this with string constants. michael@0: */ michael@0: template michael@0: MOZ_CONSTEXPR size_t michael@0: ArrayLength(T (&arr)[N]) michael@0: { michael@0: return N; michael@0: } michael@0: michael@0: template michael@0: MOZ_CONSTEXPR size_t michael@0: ArrayLength(const Array& arr) michael@0: { michael@0: return N; michael@0: } michael@0: michael@0: /* michael@0: * Compute the address one past the last element of a constant-length array. michael@0: * michael@0: * Beware of the implicit trailing '\0' when using this with string constants. michael@0: */ michael@0: template michael@0: MOZ_CONSTEXPR T* michael@0: ArrayEnd(T (&arr)[N]) michael@0: { michael@0: return arr + ArrayLength(arr); michael@0: } michael@0: michael@0: template michael@0: MOZ_CONSTEXPR T* michael@0: ArrayEnd(Array& arr) michael@0: { michael@0: return &arr[0] + ArrayLength(arr); michael@0: } michael@0: michael@0: template michael@0: MOZ_CONSTEXPR const T* michael@0: ArrayEnd(const Array& arr) michael@0: { michael@0: return &arr[0] + ArrayLength(arr); michael@0: } michael@0: michael@0: namespace detail { michael@0: michael@0: /* michael@0: * Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe michael@0: * compile-time constant even on compilers lacking constexpr support. michael@0: */ michael@0: template michael@0: char (&ArrayLengthHelper(T (&array)[N]))[N]; michael@0: michael@0: } /* namespace detail */ michael@0: michael@0: } /* namespace mozilla */ michael@0: michael@0: #endif /* __cplusplus */ michael@0: michael@0: /* michael@0: * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files michael@0: * that can't use C++ template functions and for static_assert() calls that michael@0: * can't call ArrayLength() when it is not a C++11 constexpr function. michael@0: */ michael@0: #ifdef __cplusplus michael@0: # define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array)) michael@0: #else michael@0: # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0])) michael@0: #endif michael@0: michael@0: #endif /* mozilla_ArrayUtils_h */