Bug 1635217 [wpt PR 23388] - Add WPT tests for bug 1074317, a=testonly
[gecko.git] / mfbt / PodOperations.h
blobc76e1b2557c05bed3515a4b1a33cb995ccd65ca6
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/. */
7 /*
8 * Operations for zeroing POD types, arrays, and so on.
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.
15 #ifndef mozilla_PodOperations_h
16 #define mozilla_PodOperations_h
18 #include "mozilla/Array.h"
19 #include "mozilla/ArrayUtils.h"
20 #include "mozilla/Attributes.h"
22 #include <algorithm>
23 #include <stdint.h>
24 #include <string.h>
26 namespace mozilla {
28 template <typename T>
29 class NotNull;
31 /** Set the contents of |aT| to 0. */
32 template <typename T>
33 static MOZ_ALWAYS_INLINE void PodZero(T* aT) {
34 memset(aT, 0, sizeof(T));
37 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
38 template <typename T>
39 static MOZ_ALWAYS_INLINE void PodZero(T* aT, size_t aNElem) {
41 * This function is often called with 'aNElem' small; we use an inline loop
42 * instead of calling 'memset' with a non-constant length. The compiler
43 * should inline the memset call with constant size, though.
45 for (T* end = aT + aNElem; aT < end; aT++) {
46 memset(aT, 0, sizeof(T));
50 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
51 template <typename T>
52 static MOZ_ALWAYS_INLINE void PodZero(NotNull<T*> aT, size_t aNElem) {
53 PodZero(aT.get(), aNElem);
57 * Arrays implicitly convert to pointers to their first element, which is
58 * dangerous when combined with the above PodZero definitions. Adding an
59 * overload for arrays is ambiguous, so we need another identifier. The
60 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
61 * compile error involving PodZero and array types, use PodArrayZero instead.
63 template <typename T, size_t N>
64 static void PodZero(T (&aT)[N]) = delete;
65 template <typename T, size_t N>
66 static void PodZero(T (&aT)[N], size_t aNElem) = delete;
68 /** Set the contents of the array |aT| to zero. */
69 template <class T, size_t N>
70 static MOZ_ALWAYS_INLINE void PodArrayZero(T (&aT)[N]) {
71 memset(aT, 0, N * sizeof(T));
74 template <typename T, size_t N>
75 static MOZ_ALWAYS_INLINE void PodArrayZero(Array<T, N>& aArr) {
76 memset(&aArr[0], 0, N * sizeof(T));
79 /**
80 * Assign |*aSrc| to |*aDst|. The locations must not be the same and must not
81 * overlap.
83 template <typename T>
84 static MOZ_ALWAYS_INLINE void PodAssign(T* aDst, const T* aSrc) {
85 MOZ_ASSERT(aDst + 1 <= aSrc || aSrc + 1 <= aDst,
86 "destination and source must not overlap");
87 memcpy(reinterpret_cast<char*>(aDst), reinterpret_cast<const char*>(aSrc),
88 sizeof(T));
91 /**
92 * Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must
93 * not overlap!
95 template <typename T>
96 static MOZ_ALWAYS_INLINE void PodCopy(T* aDst, const T* aSrc, size_t aNElem) {
97 MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
98 "destination and source must not overlap");
99 if (aNElem < 128) {
101 * Avoid using operator= in this loop, as it may have been
102 * intentionally deleted by the POD type.
104 for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) {
105 PodAssign(aDst, aSrc);
107 } else {
108 memcpy(aDst, aSrc, aNElem * sizeof(T));
112 template <typename T>
113 static MOZ_ALWAYS_INLINE void PodCopy(volatile T* aDst, const volatile T* aSrc,
114 size_t aNElem) {
115 MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
116 "destination and source must not overlap");
119 * Volatile |aDst| requires extra work, because it's undefined behavior to
120 * modify volatile objects using the mem* functions. Just write out the
121 * loops manually, using operator= rather than memcpy for the same reason,
122 * and let the compiler optimize to the extent it can.
124 for (const volatile T* srcend = aSrc + aNElem; aSrc < srcend;
125 aSrc++, aDst++) {
126 *aDst = *aSrc;
131 * Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
132 * The arrays must not overlap!
134 template <class T, size_t N>
135 static MOZ_ALWAYS_INLINE void PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N]) {
136 PodCopy(aDst, aSrc, N);
140 * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two
141 * memory ranges overlap, then the effect is as if the |aNElem| elements are
142 * first copied from |aSrc| to a temporary array, and then from the temporary
143 * array to |aDst|.
145 template <typename T>
146 static MOZ_ALWAYS_INLINE void PodMove(T* aDst, const T* aSrc, size_t aNElem) {
147 MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T),
148 "trying to move an impossible number of elements");
149 memmove(aDst, aSrc, aNElem * sizeof(T));
153 * Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h.
154 * Note that we *cannot* use memcmp for this, due to padding bytes, etc..
157 } // namespace mozilla
159 #endif /* mozilla_PodOperations_h */