Bumping manifests a=b2g-bump
[gecko.git] / mfbt / PodOperations.h
blob843e1311d3ac834e3aacfab474b8accc5ee6a8c3
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 <stdint.h>
23 #include <string.h>
25 namespace mozilla {
27 /** Set the contents of |aT| to 0. */
28 template<typename T>
29 static MOZ_ALWAYS_INLINE void
30 PodZero(T* aT)
32 memset(aT, 0, sizeof(T));
35 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
36 template<typename T>
37 static MOZ_ALWAYS_INLINE void
38 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));
51 * Arrays implicitly convert to pointers to their first element, which is
52 * dangerous when combined with the above PodZero definitions. Adding an
53 * overload for arrays is ambiguous, so we need another identifier. The
54 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
55 * compile error involving PodZero and array types, use PodArrayZero instead.
57 template<typename T, size_t N>
58 static void PodZero(T (&aT)[N]) MOZ_DELETE;
59 template<typename T, size_t N>
60 static void PodZero(T (&aT)[N], size_t aNElem) MOZ_DELETE;
62 /** Set the contents of the array |aT| to zero. */
63 template <class T, size_t N>
64 static MOZ_ALWAYS_INLINE void
65 PodArrayZero(T (&aT)[N])
67 memset(aT, 0, N * sizeof(T));
70 template <typename T, size_t N>
71 static MOZ_ALWAYS_INLINE void
72 PodArrayZero(Array<T, N>& aArr)
74 memset(&aArr[0], 0, N * sizeof(T));
77 /**
78 * Assign |*aSrc| to |*aDst|. The locations must not be the same and must not
79 * overlap.
81 template<typename T>
82 static MOZ_ALWAYS_INLINE void
83 PodAssign(T* aDst, const T* aSrc)
85 MOZ_ASSERT(aDst != aSrc);
86 MOZ_ASSERT_IF(aSrc < aDst,
87 PointerRangeSize(aSrc, static_cast<const T*>(aDst)) >= 1);
88 MOZ_ASSERT_IF(aDst < aSrc,
89 PointerRangeSize(static_cast<const T*>(aDst), aSrc) >= 1);
90 memcpy(reinterpret_cast<char*>(aDst), reinterpret_cast<const char*>(aSrc),
91 sizeof(T));
94 /**
95 * Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must
96 * not overlap!
98 template<typename T>
99 static MOZ_ALWAYS_INLINE void
100 PodCopy(T* aDst, const T* aSrc, size_t aNElem)
102 MOZ_ASSERT(aDst != aSrc);
103 MOZ_ASSERT_IF(aSrc < aDst,
104 PointerRangeSize(aSrc, static_cast<const T*>(aDst)) >= aNElem);
105 MOZ_ASSERT_IF(aDst < aSrc,
106 PointerRangeSize(static_cast<const T*>(aDst), aSrc) >= aNElem);
108 if (aNElem < 128) {
110 * Avoid using operator= in this loop, as it may have been
111 * intentionally deleted by the POD type.
113 for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) {
114 PodAssign(aDst, aSrc);
116 } else {
117 memcpy(aDst, aSrc, aNElem * sizeof(T));
121 template<typename T>
122 static MOZ_ALWAYS_INLINE void
123 PodCopy(volatile T* aDst, const volatile T* aSrc, size_t aNElem)
125 MOZ_ASSERT(aDst != aSrc);
126 MOZ_ASSERT_IF(aSrc < aDst,
127 PointerRangeSize(aSrc, static_cast<const volatile T*>(aDst)) >= aNElem);
128 MOZ_ASSERT_IF(aDst < aSrc,
129 PointerRangeSize(static_cast<const volatile T*>(aDst), aSrc) >= aNElem);
132 * Volatile |aDst| requires extra work, because it's undefined behavior to
133 * modify volatile objects using the mem* functions. Just write out the
134 * loops manually, using operator= rather than memcpy for the same reason,
135 * and let the compiler optimize to the extent it can.
137 for (const volatile T* srcend = aSrc + aNElem;
138 aSrc < srcend;
139 aSrc++, aDst++) {
140 *aDst = *aSrc;
145 * Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
146 * The arrays must not overlap!
148 template <class T, size_t N>
149 static MOZ_ALWAYS_INLINE void
150 PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N])
152 PodCopy(aDst, aSrc, N);
156 * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two
157 * memory ranges overlap, then the effect is as if the |aNElem| elements are
158 * first copied from |aSrc| to a temporary array, and then from the temporary
159 * array to |aDst|.
161 template<typename T>
162 static MOZ_ALWAYS_INLINE void
163 PodMove(T* aDst, const T* aSrc, size_t aNElem)
165 MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T),
166 "trying to move an impossible number of elements");
167 memmove(aDst, aSrc, aNElem * sizeof(T));
171 * Determine whether the |len| elements at |one| are memory-identical to the
172 * |len| elements at |two|.
174 template<typename T>
175 static MOZ_ALWAYS_INLINE bool
176 PodEqual(const T* one, const T* two, size_t len)
178 if (len < 128) {
179 const T* p1end = one + len;
180 const T* p1 = one;
181 const T* p2 = two;
182 for (; p1 < p1end; p1++, p2++) {
183 if (*p1 != *p2) {
184 return false;
187 return true;
190 return !memcmp(one, two, len * sizeof(T));
193 } // namespace mozilla
195 #endif /* mozilla_PodOperations_h */