1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Operations for zeroing POD types, arrays, and so on.
9 * These operations are preferable to memset, memcmp, and the like because they
10 * don't require remembering to multiply by sizeof(T), array lengths, and so on
14 #ifndef mozilla_PodOperations_h
15 #define mozilla_PodOperations_h
17 #include "mozilla/Attributes.h"
18 #include "mozilla/Util.h"
24 /** Set the contents of |t| to 0. */
29 memset(t
, 0, sizeof(T
));
32 /** Set the contents of |nelem| elements starting at |t| to 0. */
35 PodZero(T
* t
, size_t nelem
)
38 * This function is often called with 'nelem' 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
= t
+ nelem
; t
< end
; t
++)
43 memset(t
, 0, sizeof(T
));
47 * Arrays implicitly convert to pointers to their first element, which is
48 * dangerous when combined with the above PodZero definitions. Adding an
49 * overload for arrays is ambiguous, so we need another identifier. The
50 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
51 * compile error involving PodZero and array types, use PodArrayZero instead.
53 template<typename T
, size_t N
>
54 static void PodZero(T (&t
)[N
]) MOZ_DELETE
;
55 template<typename T
, size_t N
>
56 static void PodZero(T (&t
)[N
], size_t nelem
) MOZ_DELETE
;
58 /** Set the contents of the array |t| to zero. */
59 template <class T
, size_t N
>
61 PodArrayZero(T (&t
)[N
])
63 memset(t
, 0, N
* sizeof(T
));
67 * Assign |*src| to |*dst|. The locations must not be the same and must not
72 PodAssign(T
* dst
, const T
* src
)
74 MOZ_ASSERT(dst
!= src
);
75 MOZ_ASSERT_IF(src
< dst
, PointerRangeSize(src
, static_cast<const T
*>(dst
)) >= 1);
76 MOZ_ASSERT_IF(dst
< src
, PointerRangeSize(static_cast<const T
*>(dst
), src
) >= 1);
77 memcpy(reinterpret_cast<char*>(dst
), reinterpret_cast<const char*>(src
), sizeof(T
));
81 * Copy |nelem| T elements from |src| to |dst|. The two memory ranges must not
85 MOZ_ALWAYS_INLINE
static void
86 PodCopy(T
* dst
, const T
* src
, size_t nelem
)
88 MOZ_ASSERT(dst
!= src
);
89 MOZ_ASSERT_IF(src
< dst
, PointerRangeSize(src
, static_cast<const T
*>(dst
)) >= nelem
);
90 MOZ_ASSERT_IF(dst
< src
, PointerRangeSize(static_cast<const T
*>(dst
), src
) >= nelem
);
94 * Avoid using operator= in this loop, as it may have been
95 * intentionally deleted by the POD type.
97 for (const T
* srcend
= src
+ nelem
; src
< srcend
; src
++, dst
++)
100 memcpy(dst
, src
, nelem
* sizeof(T
));
105 * Determine whether the |len| elements at |one| are memory-identical to the
106 * |len| elements at |two|.
109 MOZ_ALWAYS_INLINE
static bool
110 PodEqual(const T
* one
, const T
* two
, size_t len
)
113 const T
* p1end
= one
+ len
;
116 for (; p1
< p1end
; p1
++, p2
++) {
123 return !memcmp(one
, two
, len
* sizeof(T
));
126 } // namespace mozilla
128 #endif // mozilla_PodOperations_h_