Debian Codegraph @

js/src/util/Memory.h

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