| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* |
| 8 |
* Operations for zeroing POD types, arrays, and so on. |
| 9 |
* |
| 10 |
* These operations are preferable to memset, memcmp, and the like because they |
| 11 |
* don't require remembering to multiply by sizeof(T), array lengths, and so on |
| 12 |
* everywhere. |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef mozilla_PodOperations_h |
| 16 |
#define mozilla_PodOperations_h |
| 17 |
|
| 18 |
#include "mozilla/Assertions.h" |
| 19 |
#include "mozilla/Attributes.h" |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
namespace mozilla { |
| 25 |
|
| 26 |
template <typename T, size_t Length> |
| 27 |
class Array; |
| 28 |
|
| 29 |
template <typename T> |
| 30 |
class NotNull; |
| 31 |
|
| 32 |
/** Set the contents of |aT| to 0. */ |
| 33 |
template <typename T> |
| 34 |
static MOZ_ALWAYS_INLINE void PodZero(T* aT) { |
| 35 |
static_assert(std::is_trivially_copyable_v<T>, |
| 36 |
"PodZero requires trivially copyable types"); |
| 37 |
memset(aT, 0, sizeof(T)); |
| 38 |
} |
| 39 |
|
| 40 |
/** Set the contents of |aNElem| elements starting at |aT| to 0. */ |
| 41 |
template <typename T> |
| 42 |
static MOZ_ALWAYS_INLINE void PodZero(T* aT, size_t aNElem) { |
| 43 |
static_assert(std::is_trivially_copyable_v<T>, |
| 44 |
"PodZero requires trivially copyable types"); |
| 45 |
/* |
| 46 |
* This function is often called with 'aNElem' small; we use an inline loop |
| 47 |
* instead of calling 'memset' with a non-constant length. The compiler |
| 48 |
* should inline the memset call with constant size, though. |
| 49 |
*/ |
| 50 |
for (T* end = aT + aNElem; aT < end; aT++) { |
| 51 |
memset(aT, 0, sizeof(T)); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** Set the contents of |aNElem| elements starting at |aT| to 0. */ |
| 56 |
template <typename T> |
| 57 |
static MOZ_ALWAYS_INLINE void PodZero(NotNull<T*> aT, size_t aNElem) { |
| 58 |
PodZero(aT.get(), aNElem); |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Arrays implicitly convert to pointers to their first element, which is |
| 63 |
* dangerous when combined with the above PodZero definitions. Adding an |
| 64 |
* overload for arrays is ambiguous, so we need another identifier. The |
| 65 |
* ambiguous overload is left to catch mistaken uses of PodZero; if you get a |
| 66 |
* compile error involving PodZero and array types, use PodArrayZero instead. |
| 67 |
*/ |
| 68 |
template <typename T, size_t N> |
| 69 |
static void PodZero(T (&aT)[N]) = delete; |
| 70 |
template <typename T, size_t N> |
| 71 |
static void PodZero(T (&aT)[N], size_t aNElem) = delete; |
| 72 |
|
| 73 |
/** Set the contents of the array |aT| to zero. */ |
| 74 |
template <class T, size_t N> |
| 75 |
static MOZ_ALWAYS_INLINE void PodArrayZero(T (&aT)[N]) { |
| 76 |
static_assert(std::is_trivially_copyable_v<T>, |
| 77 |
"PodArrayZero requires trivially copyable types"); |
| 78 |
memset(aT, 0, N * sizeof(T)); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename T, size_t N> |
| 82 |
static MOZ_ALWAYS_INLINE void PodArrayZero(Array<T, N>& aArr) { |
| 83 |
static_assert(std::is_trivially_copyable_v<T>, |
| 84 |
"PodArrayZero requires trivially copyable types"); |
| 85 |
memset(&aArr[0], 0, N * sizeof(T)); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must |
| 90 |
* not overlap! |
| 91 |
*/ |
| 92 |
template <typename T> |
| 93 |
static MOZ_ALWAYS_INLINE void PodCopy(T* aDst, const T* aSrc, size_t aNElem) { |
| 94 |
static_assert(std::is_trivially_copyable_v<T>, |
| 95 |
"PodCopy requires trivially copyable types"); |
| 96 |
MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst, |
| 97 |
"destination and source must not overlap"); |
| 98 |
|
| 99 |
// Linux memcpy for small sizes seems slower than on other |
| 100 |
// platforms. So we use a loop for small sizes there only. |
| 101 |
// |
| 102 |
// See Bug 1967062 for details. |
| 103 |
#if defined(XP_LINUX) |
| 104 |
if (aNElem < 128) { |
| 105 |
for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) { |
| 106 |
*aDst = *aSrc; |
| 107 |
} |
| 108 |
return; |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
memcpy(aDst, aSrc, aNElem * sizeof(T)); |
| 113 |
} |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
static MOZ_ALWAYS_INLINE void PodCopy(volatile T* aDst, const volatile T* aSrc, |
| 117 |
size_t aNElem) { |
| 118 |
static_assert(std::is_trivially_copyable_v<T>, |
| 119 |
"PodCopy requires trivially copyable types"); |
| 120 |
MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst, |
| 121 |
"destination and source must not overlap"); |
| 122 |
|
| 123 |
/* |
| 124 |
* Volatile |aDst| requires extra work, because it's undefined behavior to |
| 125 |
* modify volatile objects using the mem* functions. Just write out the |
| 126 |
* loops manually, using operator= rather than memcpy for the same reason, |
| 127 |
* and let the compiler optimize to the extent it can. |
| 128 |
*/ |
| 129 |
for (const volatile T* srcend = aSrc + aNElem; aSrc < srcend; |
| 130 |
aSrc++, aDst++) { |
| 131 |
*aDst = *aSrc; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* Copy the contents of the array |aSrc| into the array |aDst|, both of size N. |
| 137 |
* The arrays must not overlap! |
| 138 |
*/ |
| 139 |
template <class T, size_t N> |
| 140 |
static MOZ_ALWAYS_INLINE void PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N]) { |
| 141 |
PodCopy(aDst, aSrc, N); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two |
| 146 |
* memory ranges overlap, then the effect is as if the |aNElem| elements are |
| 147 |
* first copied from |aSrc| to a temporary array, and then from the temporary |
| 148 |
* array to |aDst|. |
| 149 |
*/ |
| 150 |
template <typename T> |
| 151 |
static MOZ_ALWAYS_INLINE void PodMove(T* aDst, const T* aSrc, size_t aNElem) { |
| 152 |
static_assert(std::is_trivially_copyable_v<T>, |
| 153 |
"PodMove requires trivially copyable types"); |
| 154 |
MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T), |
| 155 |
"trying to move an impossible number of elements"); |
| 156 |
memmove(aDst, aSrc, aNElem * sizeof(T)); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h. |
| 161 |
* Note that we *cannot* use memcmp for this, due to padding bytes, etc.. |
| 162 |
*/ |
| 163 |
|
| 164 |
} // namespace mozilla |
| 165 |
|
| 166 |
#endif /* mozilla_PodOperations_h */ |