Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / mfbt / PodOperations.h
blobf4e5da4c799ce034906fc39680614804ec3eace0
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/Assertions.h"
19 #include "mozilla/Attributes.h"
21 #include <stdint.h>
22 #include <string.h>
24 namespace mozilla {
26 template <typename T, size_t Length>
27 class Array;
29 template <typename T>
30 class NotNull;
32 /** Set the contents of |aT| to 0. */
33 template <typename T>
34 static MOZ_ALWAYS_INLINE void PodZero(T* aT) {
35 memset(aT, 0, sizeof(T));
38 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
39 template <typename T>
40 static MOZ_ALWAYS_INLINE void PodZero(T* aT, size_t aNElem) {
42 * This function is often called with 'aNElem' small; we use an inline loop
43 * instead of calling 'memset' with a non-constant length. The compiler
44 * should inline the memset call with constant size, though.
46 for (T* end = aT + aNElem; aT < end; aT++) {
47 memset(aT, 0, sizeof(T));
51 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
52 template <typename T>
53 static MOZ_ALWAYS_INLINE void PodZero(NotNull<T*> aT, size_t aNElem) {
54 PodZero(aT.get(), aNElem);
58 * Arrays implicitly convert to pointers to their first element, which is
59 * dangerous when combined with the above PodZero definitions. Adding an
60 * overload for arrays is ambiguous, so we need another identifier. The
61 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
62 * compile error involving PodZero and array types, use PodArrayZero instead.
64 template <typename T, size_t N>
65 static void PodZero(T (&aT)[N]) = delete;
66 template <typename T, size_t N>
67 static void PodZero(T (&aT)[N], size_t aNElem) = delete;
69 /** Set the contents of the array |aT| to zero. */
70 template <class T, size_t N>
71 static MOZ_ALWAYS_INLINE void PodArrayZero(T (&aT)[N]) {
72 memset(aT, 0, N * sizeof(T));
75 template <typename T, size_t N>
76 static MOZ_ALWAYS_INLINE void PodArrayZero(Array<T, N>& aArr) {
77 memset(&aArr[0], 0, N * sizeof(T));
80 /**
81 * Assign |*aSrc| to |*aDst|. The locations must not be the same and must not
82 * overlap.
84 template <typename T>
85 static MOZ_ALWAYS_INLINE void PodAssign(T* aDst, const T* aSrc) {
86 MOZ_ASSERT(aDst + 1 <= aSrc || aSrc + 1 <= aDst,
87 "destination and source must not overlap");
88 memcpy(reinterpret_cast<char*>(aDst), reinterpret_cast<const char*>(aSrc),
89 sizeof(T));
92 /**
93 * Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must
94 * not overlap!
96 template <typename T>
97 static MOZ_ALWAYS_INLINE void PodCopy(T* aDst, const T* aSrc, size_t aNElem) {
98 MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
99 "destination and source must not overlap");
100 if (aNElem < 128) {
102 * Avoid using operator= in this loop, as it may have been
103 * intentionally deleted by the POD type.
105 for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) {
106 PodAssign(aDst, aSrc);
108 } else {
109 memcpy(aDst, aSrc, aNElem * sizeof(T));
113 template <typename T>
114 static MOZ_ALWAYS_INLINE void PodCopy(volatile T* aDst, const volatile T* aSrc,
115 size_t aNElem) {
116 MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
117 "destination and source must not overlap");
120 * Volatile |aDst| requires extra work, because it's undefined behavior to
121 * modify volatile objects using the mem* functions. Just write out the
122 * loops manually, using operator= rather than memcpy for the same reason,
123 * and let the compiler optimize to the extent it can.
125 for (const volatile T* srcend = aSrc + aNElem; aSrc < srcend;
126 aSrc++, aDst++) {
127 *aDst = *aSrc;
132 * Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
133 * The arrays must not overlap!
135 template <class T, size_t N>
136 static MOZ_ALWAYS_INLINE void PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N]) {
137 PodCopy(aDst, aSrc, N);
141 * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two
142 * memory ranges overlap, then the effect is as if the |aNElem| elements are
143 * first copied from |aSrc| to a temporary array, and then from the temporary
144 * array to |aDst|.
146 template <typename T>
147 static MOZ_ALWAYS_INLINE void PodMove(T* aDst, const T* aSrc, size_t aNElem) {
148 MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T),
149 "trying to move an impossible number of elements");
150 memmove(aDst, aSrc, aNElem * sizeof(T));
154 * Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h.
155 * Note that we *cannot* use memcmp for this, due to padding bytes, etc..
158 } // namespace mozilla
160 #endif /* mozilla_PodOperations_h */