/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * Operations for zeroing POD types, arrays, and so on. * * These operations are preferable to memset, memcmp, and the like because they * don't require remembering to multiply by sizeof(T), array lengths, and so on * everywhere.
*/
/** Set the contents of |aT| to 0. */ template <typename T> static MOZ_ALWAYS_INLINE void PodZero(T* aT) {
static_assert(std::is_trivially_copyable_v<T>, "PodZero requires trivially copyable types");
memset(aT, 0, sizeof(T));
}
/** Set the contents of |aNElem| elements starting at |aT| to 0. */ template <typename T> static MOZ_ALWAYS_INLINE void PodZero(T* aT, size_t aNElem) {
static_assert(std::is_trivially_copyable_v<T>, "PodZero requires trivially copyable types"); /* * This function is often called with 'aNElem' small; we use an inline loop * instead of calling 'memset' with a non-constant length. The compiler * should inline the memset call with constant size, though.
*/ for (T* end = aT + aNElem; aT < end; aT++) {
memset(aT, 0, sizeof(T));
}
}
/** Set the contents of |aNElem| elements starting at |aT| to 0. */ template <typename T> static MOZ_ALWAYS_INLINE void PodZero(NotNull<T*> aT, size_t aNElem) {
PodZero(aT.get(), aNElem);
}
/* * Arrays implicitly convert to pointers to their first element, which is * dangerous when combined with the above PodZero definitions. Adding an * overload for arrays is ambiguous, so we need another identifier. The * ambiguous overload is left to catch mistaken uses of PodZero; if you get a * compile error involving PodZero and array types, use PodArrayZero instead.
*/ template <typename T, size_t N> staticvoid PodZero(T (&aT)[N]) = delete; template <typename T, size_t N> staticvoid PodZero(T (&aT)[N], size_t aNElem) = delete;
/** Set the contents of the array |aT| to zero. */ template <class T, size_t N> static MOZ_ALWAYS_INLINE void PodArrayZero(T (&aT)[N]) {
static_assert(std::is_trivially_copyable_v<T>, "PodArrayZero requires trivially copyable types");
memset(aT, 0, N * sizeof(T));
}
/* * Volatile |aDst| requires extra work, because it's undefined behavior to * modify volatile objects using the mem* functions. Just write out the * loops manually, using operator= rather than memcpy for the same reason, * and let the compiler optimize to the extent it can.
*/ for (constvolatile T* srcend = aSrc + aNElem; aSrc < srcend;
aSrc++, aDst++) {
*aDst = *aSrc;
}
}
/* * Copy the contents of the array |aSrc| into the array |aDst|, both of size N. * The arrays must not overlap!
*/ template <class T, size_t N> static MOZ_ALWAYS_INLINE void PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N]) {
PodCopy(aDst, aSrc, N);
}
/** * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|. If the two * memory ranges overlap, then the effect is as if the |aNElem| elements are * first copied from |aSrc| to a temporary array, and then from the temporary * array to |aDst|.
*/ template <typename T> static MOZ_ALWAYS_INLINE void PodMove(T* aDst, const T* aSrc, size_t aNElem) {
static_assert(std::is_trivially_copyable_v<T>, "PodMove requires trivially copyable types");
MOZ_ASSERT(aNElem <= SIZE_MAX / sizeof(T), "trying to move an impossible number of elements");
memmove(aDst, aSrc, aNElem * sizeof(T));
}
/** * Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h. * Note that we *cannot* use memcmp for this, due to padding bytes, etc..
*/
} // namespace mozilla
#endif/* mozilla_PodOperations_h */
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.