Bug 860222: use our own isascii() for sanitizing TURN passwords r=glandium
[gecko.git] / mfbt / PodOperations.h
blob62480d47980fe84d51c1994d7f752a8646d2a8f1
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/. */
6 /*
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
11 * everywhere.
14 #ifndef mozilla_PodOperations_h
15 #define mozilla_PodOperations_h
17 #include "mozilla/Attributes.h"
18 #include "mozilla/Util.h"
20 #include <string.h>
22 namespace mozilla {
24 /** Set the contents of |t| to 0. */
25 template<typename T>
26 static void
27 PodZero(T* t)
29 memset(t, 0, sizeof(T));
32 /** Set the contents of |nelem| elements starting at |t| to 0. */
33 template<typename T>
34 static void
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>
60 static void
61 PodArrayZero(T (&t)[N])
63 memset(t, 0, N * sizeof(T));
66 /**
67 * Assign |*src| to |*dst|. The locations must not be the same and must not
68 * overlap.
70 template<typename T>
71 static void
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));
80 /**
81 * Copy |nelem| T elements from |src| to |dst|. The two memory ranges must not
82 * overlap!
84 template<typename T>
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);
92 if (nelem < 128) {
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++)
98 PodAssign(dst, src);
99 } else {
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|.
108 template<typename T>
109 MOZ_ALWAYS_INLINE static bool
110 PodEqual(const T* one, const T* two, size_t len)
112 if (len < 128) {
113 const T* p1end = one + len;
114 const T* p1 = one;
115 const T* p2 = two;
116 for (; p1 < p1end; p1++, p2++) {
117 if (*p1 != *p2)
118 return false;
120 return true;
123 return !memcmp(one, two, len * sizeof(T));
126 } // namespace mozilla
128 #endif // mozilla_PodOperations_h_