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: */
|
2013-04-02 09:43:19 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Operations for zeroing POD types, arrays, and so on.
|
|
|
|
*
|
|
|
|
* These operations are preferable to memset, memcmp, and the like because they
|
|
|
|
* don't require remembering to multiply by sizeof(T), array lengths, and so on
|
|
|
|
* everywhere.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_PodOperations_h
|
|
|
|
#define mozilla_PodOperations_h
|
|
|
|
|
2013-11-13 02:31:15 +04:00
|
|
|
#include "mozilla/Array.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-04-02 09:43:19 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include <stdint.h>
|
2013-04-02 09:43:19 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Set the contents of |aT| to 0. */
|
2013-04-02 09:43:19 +04:00
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodZero(T* aT)
|
2013-04-02 09:43:19 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
memset(aT, 0, sizeof(T));
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Set the contents of |aNElem| elements starting at |aT| to 0. */
|
2013-04-02 09:43:19 +04:00
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodZero(T* aT, size_t aNElem)
|
2013-04-02 09:43:19 +04:00
|
|
|
{
|
|
|
|
/*
|
2014-06-13 10:34:08 +04:00
|
|
|
* This function is often called with 'aNElem' small; we use an inline loop
|
2013-04-02 09:43:19 +04:00
|
|
|
* instead of calling 'memset' with a non-constant length. The compiler
|
|
|
|
* should inline the memset call with constant size, though.
|
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
for (T* end = aT + aNElem; aT < end; aT++) {
|
|
|
|
memset(aT, 0, sizeof(T));
|
|
|
|
}
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Arrays implicitly convert to pointers to their first element, which is
|
|
|
|
* dangerous when combined with the above PodZero definitions. Adding an
|
|
|
|
* overload for arrays is ambiguous, so we need another identifier. The
|
|
|
|
* ambiguous overload is left to catch mistaken uses of PodZero; if you get a
|
|
|
|
* compile error involving PodZero and array types, use PodArrayZero instead.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2014-06-13 10:34:08 +04:00
|
|
|
static void PodZero(T (&aT)[N]) MOZ_DELETE;
|
2013-04-02 09:43:19 +04:00
|
|
|
template<typename T, size_t N>
|
2014-06-13 10:34:08 +04:00
|
|
|
static void PodZero(T (&aT)[N], size_t aNElem) MOZ_DELETE;
|
2013-04-02 09:43:19 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Set the contents of the array |aT| to zero. */
|
2013-04-02 09:43:19 +04:00
|
|
|
template <class T, size_t N>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodArrayZero(T (&aT)[N])
|
2013-04-02 09:43:19 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
memset(aT, 0, N * sizeof(T));
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
|
2013-11-13 02:31:15 +04:00
|
|
|
template <typename T, size_t N>
|
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodArrayZero(Array<T, N>& aArr)
|
2013-11-13 02:31:15 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
memset(&aArr[0], 0, N * sizeof(T));
|
2013-11-13 02:31:15 +04:00
|
|
|
}
|
|
|
|
|
2013-04-02 09:43:19 +04:00
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Assign |*aSrc| to |*aDst|. The locations must not be the same and must not
|
2013-04-02 09:43:19 +04:00
|
|
|
* overlap.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodAssign(T* aDst, const T* aSrc)
|
2013-04-02 09:43:19 +04:00
|
|
|
{
|
2014-11-04 02:55:59 +03:00
|
|
|
MOZ_ASSERT(aDst + 1 <= aSrc || aSrc + 1 <= aDst,
|
|
|
|
"destination and source must not overlap");
|
2014-06-13 10:34:08 +04:00
|
|
|
memcpy(reinterpret_cast<char*>(aDst), reinterpret_cast<const char*>(aSrc),
|
|
|
|
sizeof(T));
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must
|
|
|
|
* not overlap!
|
2013-04-02 09:43:19 +04:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodCopy(T* aDst, const T* aSrc, size_t aNElem)
|
2013-04-02 09:43:19 +04:00
|
|
|
{
|
2014-11-04 02:55:59 +03:00
|
|
|
MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
|
|
|
|
"destination and source must not overlap");
|
2014-06-13 10:34:08 +04:00
|
|
|
if (aNElem < 128) {
|
2013-04-02 09:43:19 +04:00
|
|
|
/*
|
|
|
|
* Avoid using operator= in this loop, as it may have been
|
|
|
|
* intentionally deleted by the POD type.
|
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) {
|
|
|
|
PodAssign(aDst, aSrc);
|
|
|
|
}
|
2013-04-02 09:43:19 +04:00
|
|
|
} else {
|
2014-06-13 10:34:08 +04:00
|
|
|
memcpy(aDst, aSrc, aNElem * sizeof(T));
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 22:05:52 +04:00
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodCopy(volatile T* aDst, const volatile T* aSrc, size_t aNElem)
|
2013-05-06 22:05:52 +04:00
|
|
|
{
|
2014-11-04 02:55:59 +03:00
|
|
|
MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
|
|
|
|
"destination and source must not overlap");
|
2013-05-06 22:05:52 +04:00
|
|
|
|
|
|
|
/*
|
2014-06-13 10:34:08 +04:00
|
|
|
* Volatile |aDst| requires extra work, because it's undefined behavior to
|
2013-05-06 22:05:52 +04:00
|
|
|
* modify volatile objects using the mem* functions. Just write out the
|
|
|
|
* loops manually, using operator= rather than memcpy for the same reason,
|
|
|
|
* and let the compiler optimize to the extent it can.
|
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
for (const volatile T* srcend = aSrc + aNElem;
|
|
|
|
aSrc < srcend;
|
|
|
|
aSrc++, aDst++) {
|
|
|
|
*aDst = *aSrc;
|
|
|
|
}
|
2013-05-06 22:05:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-13 10:34:08 +04:00
|
|
|
* Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
|
2013-05-06 22:05:52 +04:00
|
|
|
* The arrays must not overlap!
|
|
|
|
*/
|
|
|
|
template <class T, size_t N>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N])
|
2013-05-06 22:05:52 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
PodCopy(aDst, aSrc, N);
|
2013-05-06 22:05:52 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 04:02:54 +04:00
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two
|
|
|
|
* memory ranges overlap, then the effect is as if the |aNElem| elements are
|
|
|
|
* first copied from |aSrc| to a temporary array, and then from the temporary
|
|
|
|
* array to |aDst|.
|
2013-11-05 04:02:54 +04:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2014-06-13 10:34:08 +04:00
|
|
|
PodMove(T* aDst, const T* aSrc, size_t aNElem)
|
2013-11-05 04:02:54 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T),
|
2013-11-05 04:02:54 +04:00
|
|
|
"trying to move an impossible number of elements");
|
2014-06-13 10:34:08 +04:00
|
|
|
memmove(aDst, aSrc, aNElem * sizeof(T));
|
2013-11-05 04:02:54 +04:00
|
|
|
}
|
|
|
|
|
2013-04-02 09:43:19 +04:00
|
|
|
/**
|
|
|
|
* Determine whether the |len| elements at |one| are memory-identical to the
|
|
|
|
* |len| elements at |two|.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2013-11-05 06:52:51 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2013-04-02 09:43:19 +04:00
|
|
|
PodEqual(const T* one, const T* two, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 128) {
|
|
|
|
const T* p1end = one + len;
|
|
|
|
const T* p1 = one;
|
|
|
|
const T* p2 = two;
|
|
|
|
for (; p1 < p1end; p1++, p2++) {
|
2014-06-13 10:34:08 +04:00
|
|
|
if (*p1 != *p2) {
|
2013-04-02 09:43:19 +04:00
|
|
|
return false;
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-04-02 09:43:19 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !memcmp(one, two, len * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_PodOperations_h */
|