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/Attributes.h"
19 #include "mozilla/Util.h"
25 /** Set the contents of |t| to 0. */
30 memset(t
, 0, sizeof(T
));
33 /** Set the contents of |nelem| elements starting at |t| to 0. */
36 PodZero(T
* t
, size_t nelem
)
39 * This function is often called with 'nelem' small; we use an inline loop
40 * instead of calling 'memset' with a non-constant length. The compiler
41 * should inline the memset call with constant size, though.
43 for (T
* end
= t
+ nelem
; t
< end
; t
++)
44 memset(t
, 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 (&t
)[N
]) MOZ_DELETE
;
56 template<typename T
, size_t N
>
57 static void PodZero(T (&t
)[N
], size_t nelem
) MOZ_DELETE
;
59 /** Set the contents of the array |t| to zero. */
60 template <class T
, size_t N
>
62 PodArrayZero(T (&t
)[N
])
64 memset(t
, 0, N
* sizeof(T
));
68 * Assign |*src| to |*dst|. The locations must not be the same and must not
73 PodAssign(T
* dst
, const T
* src
)
75 MOZ_ASSERT(dst
!= src
);
76 MOZ_ASSERT_IF(src
< dst
, PointerRangeSize(src
, static_cast<const T
*>(dst
)) >= 1);
77 MOZ_ASSERT_IF(dst
< src
, PointerRangeSize(static_cast<const T
*>(dst
), src
) >= 1);
78 memcpy(reinterpret_cast<char*>(dst
), reinterpret_cast<const char*>(src
), sizeof(T
));
82 * Copy |nelem| T elements from |src| to |dst|. The two memory ranges must not
86 MOZ_ALWAYS_INLINE
static void
87 PodCopy(T
* dst
, const T
* src
, size_t nelem
)
89 MOZ_ASSERT(dst
!= src
);
90 MOZ_ASSERT_IF(src
< dst
, PointerRangeSize(src
, static_cast<const T
*>(dst
)) >= nelem
);
91 MOZ_ASSERT_IF(dst
< src
, PointerRangeSize(static_cast<const T
*>(dst
), src
) >= nelem
);
95 * Avoid using operator= in this loop, as it may have been
96 * intentionally deleted by the POD type.
98 for (const T
* srcend
= src
+ nelem
; src
< srcend
; src
++, dst
++)
101 memcpy(dst
, src
, nelem
* sizeof(T
));
106 MOZ_ALWAYS_INLINE
static void
107 PodCopy(volatile T
* dst
, const volatile T
* src
, size_t nelem
)
109 MOZ_ASSERT(dst
!= src
);
110 MOZ_ASSERT_IF(src
< dst
,
111 PointerRangeSize(src
, static_cast<const volatile T
*>(dst
)) >= nelem
);
112 MOZ_ASSERT_IF(dst
< src
,
113 PointerRangeSize(static_cast<const volatile T
*>(dst
), src
) >= nelem
);
116 * Volatile |dst| requires extra work, because it's undefined behavior to
117 * modify volatile objects using the mem* functions. Just write out the
118 * loops manually, using operator= rather than memcpy for the same reason,
119 * and let the compiler optimize to the extent it can.
121 for (const volatile T
* srcend
= src
+ nelem
; src
< srcend
; src
++, dst
++)
126 * Copy the contents of the array |src| into the array |dst|, both of size N.
127 * The arrays must not overlap!
129 template <class T
, size_t N
>
131 PodArrayCopy(T (&dst
)[N
], const T (&src
)[N
])
133 PodCopy(dst
, src
, N
);
137 * Determine whether the |len| elements at |one| are memory-identical to the
138 * |len| elements at |two|.
141 MOZ_ALWAYS_INLINE
static bool
142 PodEqual(const T
* one
, const T
* two
, size_t len
)
145 const T
* p1end
= one
+ len
;
148 for (; p1
< p1end
; p1
++, p2
++) {
155 return !memcmp(one
, two
, len
* sizeof(T
));
158 } // namespace mozilla
160 #endif /* mozilla_PodOperations_h */