Memory.h (1363B)
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/. */ 6 7 #ifndef util_Memory_h 8 #define util_Memory_h 9 10 #include "mozilla/Assertions.h" 11 #include "mozilla/Attributes.h" 12 13 #include <stddef.h> 14 #include <string.h> 15 #include <type_traits> 16 17 static MOZ_ALWAYS_INLINE void* js_memcpy(void* dst_, const void* src_, 18 size_t len) { 19 char* dst = (char*)dst_; 20 const char* src = (const char*)src_; 21 MOZ_ASSERT_IF(dst >= src, (size_t)(dst - src) >= len); 22 MOZ_ASSERT_IF(src >= dst, (size_t)(src - dst) >= len); 23 24 return memcpy(dst, src, len); 25 } 26 27 namespace js { 28 29 template <typename T, typename U> 30 static constexpr U ComputeByteAlignment(T bytes, U alignment) { 31 static_assert(std::is_unsigned_v<U>, "alignment amount must be unsigned"); 32 33 return (alignment - (bytes % alignment)) % alignment; 34 } 35 36 template <typename T, typename U> 37 static constexpr T AlignBytes(T bytes, U alignment) { 38 static_assert(std::is_unsigned_v<U>, "alignment amount must be unsigned"); 39 40 return bytes + ComputeByteAlignment(bytes, alignment); 41 } 42 43 } /* namespace js */ 44 45 #endif /* util_Memory_h */