1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef LIBPANDABASE_MEM_MEM_H
17 #define LIBPANDABASE_MEM_MEM_H
18
19 #include "macros.h"
20 #include "utils/math_helpers.h"
21
22 #include <cstddef>
23 #include <cstdint>
24 #include <cmath>
25 #include <functional>
26
27 namespace panda {
28
29 namespace mem {
30 class GCRoot;
31
32 class MemStatsAdditionalInfo;
33 class MemStatsDefault;
34 class MemRange;
35
36 #ifndef NDEBUG
37 using MemStatsType = MemStatsAdditionalInfo;
38 #else
39 using MemStatsType = MemStatsDefault;
40 #endif
41 } // namespace mem
42
43 class ObjectHeader;
44
45 #ifdef PANDA_USE_32_BIT_POINTER
46 using object_pointer_type = uint32_t;
47 #else
48 using object_pointer_type = uintptr_t;
49 #endif
50
51 constexpr size_t OBJECT_POINTER_SIZE = sizeof(object_pointer_type);
52
53 /**
54 * \brief Logarithmic/bit alignment
55 */
56 enum Alignment {
57 LOG_ALIGN_2 = 2,
58 LOG_ALIGN_3 = 3,
59 LOG_ALIGN_4 = 4,
60 LOG_ALIGN_5 = 5,
61 LOG_ALIGN_6 = 6,
62 LOG_ALIGN_7 = 7,
63 LOG_ALIGN_8 = 8,
64 LOG_ALIGN_9 = 9,
65 LOG_ALIGN_10 = 10,
66 LOG_ALIGN_11 = 11,
67 LOG_ALIGN_12 = 12,
68 LOG_ALIGN_13 = 13,
69 LOG_ALIGN_14 = 14,
70 LOG_ALIGN_MIN = LOG_ALIGN_2,
71 LOG_ALIGN_MAX = LOG_ALIGN_14,
72 };
73
74 /**
75 * @param logAlignment - logarithmic alignment
76 * @return alingnment in bytes
77 */
GetAlignmentInBytes(const Alignment LOG_ALIGNMENT)78 constexpr size_t GetAlignmentInBytes(const Alignment LOG_ALIGNMENT)
79 {
80 return 1U << static_cast<uint32_t>(LOG_ALIGNMENT);
81 }
82
83 /**
84 * \brief returns log2 for alignment in bytes
85 * @param ALIGNMENT_IN_BYTES - should be power of 2
86 * @return alignment in bits
87 */
GetLogAlignment(const uint32_t ALIGNMENT_IN_BYTES)88 constexpr Alignment GetLogAlignment(const uint32_t ALIGNMENT_IN_BYTES)
89 {
90 using helpers::math::GetIntLog2;
91 // check if it is power of 2
92 ASSERT((ALIGNMENT_IN_BYTES != 0) && !(ALIGNMENT_IN_BYTES & (ALIGNMENT_IN_BYTES - 1)));
93 ASSERT(GetIntLog2(ALIGNMENT_IN_BYTES) >= Alignment::LOG_ALIGN_MIN);
94 ASSERT(GetIntLog2(ALIGNMENT_IN_BYTES) <= Alignment::LOG_ALIGN_MAX);
95 return static_cast<Alignment>(GetIntLog2(ALIGNMENT_IN_BYTES));
96 }
97
98 template <class T>
AlignUp(T value,size_t alignment)99 constexpr std::enable_if_t<std::is_unsigned_v<T>, T> AlignUp(T value, size_t alignment)
100 {
101 return (value + alignment - 1U) & ~(alignment - 1U);
102 }
103
104 template <class T>
AlignDown(T value,size_t alignment)105 constexpr std::enable_if_t<std::is_unsigned_v<T>, T> AlignDown(T value, size_t alignment)
106 {
107 return value & ~(alignment - 1U);
108 }
109
110 template <class T>
ToUintPtr(T * val)111 inline uintptr_t ToUintPtr(T *val)
112 {
113 return reinterpret_cast<uintptr_t>(val);
114 }
115
ToUintPtr(std::nullptr_t)116 inline uintptr_t ToUintPtr(std::nullptr_t)
117 {
118 return reinterpret_cast<uintptr_t>(nullptr);
119 }
120
121 template <class T>
ToNativePtr(uintptr_t val)122 inline T *ToNativePtr(uintptr_t val)
123 {
124 return reinterpret_cast<T *>(val);
125 }
126
ToVoidPtr(uintptr_t val)127 inline void *ToVoidPtr(uintptr_t val)
128 {
129 return reinterpret_cast<void *>(val);
130 }
131
132 constexpr Alignment DEFAULT_ALIGNMENT = GetLogAlignment(alignof(uintptr_t));
133 constexpr size_t DEFAULT_ALIGNMENT_IN_BYTES = GetAlignmentInBytes(DEFAULT_ALIGNMENT);
134
GetAlignedObjectSize(size_t size)135 constexpr size_t GetAlignedObjectSize(size_t size)
136 {
137 return AlignUp(size, DEFAULT_ALIGNMENT_IN_BYTES);
138 }
139
140 template <typename T>
GetAlignment()141 constexpr Alignment GetAlignment()
142 {
143 return GetLogAlignment(std::max(alignof(T), DEFAULT_ALIGNMENT_IN_BYTES));
144 }
145
146 /*
147 uint64_t return type usage in memory literals for giving
148 compile-time error in case of integer overflow
149 */
150
151 constexpr uint64_t SHIFT_KB = 10ULL;
152 constexpr uint64_t SHIFT_MB = 20ULL;
153 constexpr uint64_t SHIFT_GB = 30ULL;
154
155 constexpr uint64_t operator"" _KB(long double count)
156 {
157 return count * (1ULL << SHIFT_KB);
158 }
159
160 // NOLINTNEXTLINE(google-runtime-int)
161 constexpr uint64_t operator"" _KB(unsigned long long count)
162 {
163 return count * (1ULL << SHIFT_KB);
164 }
165
166 constexpr uint64_t operator"" _MB(long double count)
167 {
168 return count * (1ULL << SHIFT_MB);
169 }
170
171 // NOLINTNEXTLINE(google-runtime-int)
172 constexpr uint64_t operator"" _MB(unsigned long long count)
173 {
174 return count * (1ULL << SHIFT_MB);
175 }
176
177 constexpr uint64_t operator"" _GB(long double count)
178 {
179 return count * (1ULL << SHIFT_GB);
180 }
181
182 // NOLINTNEXTLINE(google-runtime-int)
183 constexpr uint64_t operator"" _GB(unsigned long long count)
184 {
185 return count * (1ULL << SHIFT_GB);
186 }
187
188 constexpr uint64_t SIZE_1K = 1_KB;
189 constexpr uint64_t SIZE_1M = 1_MB;
190 constexpr uint64_t SIZE_1G = 1_GB;
191
192 constexpr uint64_t PANDA_MAX_HEAP_SIZE = 4_GB;
193 constexpr size_t PANDA_POOL_ALIGNMENT_IN_BYTES = 256_KB;
194
195 constexpr size_t PANDA_DEFAULT_POOL_SIZE = 1_MB;
196 constexpr size_t PANDA_DEFAULT_ARENA_SIZE = 1_MB;
197 constexpr size_t PANDA_DEFAULT_ALLOCATOR_POOL_SIZE = 4_MB;
198 static_assert(PANDA_DEFAULT_POOL_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
199 static_assert(PANDA_DEFAULT_ARENA_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
200 static_assert(PANDA_DEFAULT_ALLOCATOR_POOL_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
201
202 constexpr Alignment DEFAULT_FRAME_ALIGNMENT = LOG_ALIGN_6;
203
204 constexpr uintptr_t PANDA_32BITS_HEAP_START_ADDRESS = AlignUp(1U, PANDA_POOL_ALIGNMENT_IN_BYTES);
205 constexpr uint64_t PANDA_32BITS_HEAP_END_OBJECTS_ADDRESS = 4_GB;
206
IsAddressInObjectsHeap(uintptr_t address)207 inline bool IsAddressInObjectsHeap([[maybe_unused]] uintptr_t address)
208 {
209 #ifdef PANDA_USE_32_BIT_POINTER
210 return PANDA_32BITS_HEAP_START_ADDRESS <= address && address < PANDA_32BITS_HEAP_END_OBJECTS_ADDRESS;
211 #else // In this case, all 64 bits addresses are valid
212 return true;
213 #endif
214 }
215
IsInObjectsAddressSpace(uintptr_t address)216 inline bool IsInObjectsAddressSpace(uintptr_t address)
217 {
218 return address == ToUintPtr(nullptr) || IsAddressInObjectsHeap(address);
219 }
220
221 template <class T>
IsInObjectsAddressSpace(T * address)222 inline bool IsInObjectsAddressSpace(T *address)
223 {
224 return IsInObjectsAddressSpace(ToUintPtr(address));
225 }
226
227 template <class T>
ToObjPtrType(T * val)228 inline object_pointer_type ToObjPtrType(T *val)
229 {
230 ASSERT(IsInObjectsAddressSpace(ToUintPtr(val)));
231 return static_cast<object_pointer_type>(ToUintPtr(val));
232 }
233
ToObjPtrType(std::nullptr_t)234 inline object_pointer_type ToObjPtrType(std::nullptr_t)
235 {
236 return static_cast<object_pointer_type>(ToUintPtr(nullptr));
237 }
238
239 enum class ObjectStatus : bool {
240 DEAD_OBJECT,
241 ALIVE_OBJECT,
242 };
243
244 using MemVisitor = std::function<void(void *mem, size_t size)>;
245 using GCObjectVisitor = std::function<ObjectStatus(ObjectHeader *)>;
246 using ObjectMoveVisitor = std::add_pointer<size_t(void *mem)>::type;
247 using ObjectVisitor = std::function<void(ObjectHeader *)>;
248 /**
249 * from_object is object from which we found to_object by reference.
250 */
251 using ObjectVisitorEx = std::function<void(ObjectHeader *from_object, ObjectHeader *to_object)>;
252 using ObjectChecker = std::function<bool(const ObjectHeader *)>;
253 using GCRootVisitor = std::function<void(const mem::GCRoot &)>;
254 using MemRangeChecker = std::function<bool(mem::MemRange &)>;
255
NoFilterChecker(const ObjectHeader * object_header)256 inline bool NoFilterChecker([[maybe_unused]] const ObjectHeader *object_header)
257 {
258 return true;
259 }
260
GCKillEmAllVisitor(const ObjectHeader * mem)261 inline ObjectStatus GCKillEmAllVisitor([[maybe_unused]] const ObjectHeader *mem)
262 {
263 return ObjectStatus::DEAD_OBJECT;
264 }
265
266 } // namespace panda
267
268 #endif // LIBPANDABASE_MEM_MEM_H
269