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/. */
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
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"
28 /** Set the contents of |aT| to 0. */
30 static MOZ_ALWAYS_INLINE
void PodZero(T
* aT
) {
31 memset(aT
, 0, sizeof(T
));
34 /** Set the contents of |aNElem| elements starting at |aT| to 0. */
36 static MOZ_ALWAYS_INLINE
void PodZero(T
* aT
, size_t aNElem
) {
38 * This function is often called with 'aNElem' small; we use an inline loop
39 * instead of calling 'memset' with a non-constant length. The compiler
40 * should inline the memset call with constant size, though.
42 for (T
* end
= aT
+ aNElem
; aT
< end
; aT
++) {
43 memset(aT
, 0, sizeof(T
));
48 * Arrays implicitly convert to pointers to their first element, which is
49 * dangerous when combined with the above PodZero definitions. Adding an
50 * overload for arrays is ambiguous, so we need another identifier. The
51 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
52 * compile error involving PodZero and array types, use PodArrayZero instead.
54 template <typename T
, size_t N
>
55 static void PodZero(T (&aT
)[N
]) = delete;
56 template <typename T
, size_t N
>
57 static void PodZero(T (&aT
)[N
], size_t aNElem
) = delete;
59 /** Set the contents of the array |aT| to zero. */
60 template <class T
, size_t N
>
61 static MOZ_ALWAYS_INLINE
void PodArrayZero(T (&aT
)[N
]) {
62 memset(aT
, 0, N
* sizeof(T
));
65 template <typename T
, size_t N
>
66 static MOZ_ALWAYS_INLINE
void PodArrayZero(Array
<T
, N
>& aArr
) {
67 memset(&aArr
[0], 0, N
* sizeof(T
));
71 * Assign |*aSrc| to |*aDst|. The locations must not be the same and must not
75 static MOZ_ALWAYS_INLINE
void PodAssign(T
* aDst
, const T
* aSrc
) {
76 MOZ_ASSERT(aDst
+ 1 <= aSrc
|| aSrc
+ 1 <= aDst
,
77 "destination and source must not overlap");
78 memcpy(reinterpret_cast<char*>(aDst
), reinterpret_cast<const char*>(aSrc
),
83 * Copy |aNElem| T elements from |aSrc| to |aDst|. The two memory ranges must
87 static MOZ_ALWAYS_INLINE
void PodCopy(T
* aDst
, const T
* aSrc
, size_t aNElem
) {
88 MOZ_ASSERT(aDst
+ aNElem
<= aSrc
|| aSrc
+ aNElem
<= aDst
,
89 "destination and source must not overlap");
92 * Avoid using operator= in this loop, as it may have been
93 * intentionally deleted by the POD type.
95 for (const T
* srcend
= aSrc
+ aNElem
; aSrc
< srcend
; aSrc
++, aDst
++) {
96 PodAssign(aDst
, aSrc
);
99 memcpy(aDst
, aSrc
, aNElem
* sizeof(T
));
103 template <typename T
>
104 static MOZ_ALWAYS_INLINE
void PodCopy(volatile T
* aDst
, const volatile T
* aSrc
,
106 MOZ_ASSERT(aDst
+ aNElem
<= aSrc
|| aSrc
+ aNElem
<= aDst
,
107 "destination and source must not overlap");
110 * Volatile |aDst| requires extra work, because it's undefined behavior to
111 * modify volatile objects using the mem* functions. Just write out the
112 * loops manually, using operator= rather than memcpy for the same reason,
113 * and let the compiler optimize to the extent it can.
115 for (const volatile T
* srcend
= aSrc
+ aNElem
; aSrc
< srcend
;
122 * Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
123 * The arrays must not overlap!
125 template <class T
, size_t N
>
126 static MOZ_ALWAYS_INLINE
void PodArrayCopy(T (&aDst
)[N
], const T (&aSrc
)[N
]) {
127 PodCopy(aDst
, aSrc
, N
);
131 * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two
132 * memory ranges overlap, then the effect is as if the |aNElem| elements are
133 * first copied from |aSrc| to a temporary array, and then from the temporary
136 template <typename T
>
137 static MOZ_ALWAYS_INLINE
void PodMove(T
* aDst
, const T
* aSrc
, size_t aNElem
) {
138 MOZ_ASSERT(aNElem
<= SIZE_MAX
/ sizeof(T
),
139 "trying to move an impossible number of elements");
140 memmove(aDst
, aSrc
, aNElem
* sizeof(T
));
144 * Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h.
145 * Note that we *cannot* use memcmp for this, due to padding bytes, etc..
148 } // namespace mozilla
150 #endif /* mozilla_PodOperations_h */