• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MIN = LOG_ALIGN_2,
70     LOG_ALIGN_MAX = LOG_ALIGN_13,
71 };
72 
73 /**
74  * @param logAlignment - logarithmic alignment
75  * @return alingnment in bytes
76  */
GetAlignmentInBytes(const Alignment LOG_ALIGNMENT)77 constexpr size_t GetAlignmentInBytes(const Alignment LOG_ALIGNMENT)
78 {
79     return 1U << static_cast<uint32_t>(LOG_ALIGNMENT);
80 }
81 
82 /**
83  * \brief returns log2 for alignment in bytes
84  * @param ALIGNMENT_IN_BYTES - should be power of 2
85  * @return alignment in bits
86  */
GetLogAlignment(const uint32_t ALIGNMENT_IN_BYTES)87 constexpr Alignment GetLogAlignment(const uint32_t ALIGNMENT_IN_BYTES)
88 {
89     using helpers::math::GetIntLog2;
90     // check if it is power of 2
91     ASSERT((ALIGNMENT_IN_BYTES != 0) && !(ALIGNMENT_IN_BYTES & (ALIGNMENT_IN_BYTES - 1)));
92     ASSERT(GetIntLog2(ALIGNMENT_IN_BYTES) >= Alignment::LOG_ALIGN_MIN);
93     ASSERT(GetIntLog2(ALIGNMENT_IN_BYTES) <= Alignment::LOG_ALIGN_MAX);
94     return static_cast<Alignment>(GetIntLog2(ALIGNMENT_IN_BYTES));
95 }
96 
97 template <class T>
AlignUp(T value,size_t alignment)98 constexpr std::enable_if_t<std::is_unsigned_v<T>, T> AlignUp(T value, size_t alignment)
99 {
100     return (value + alignment - 1U) & ~(alignment - 1U);
101 }
102 
103 template <class T>
AlignDown(T value,size_t alignment)104 constexpr std::enable_if_t<std::is_unsigned_v<T>, T> AlignDown(T value, size_t alignment)
105 {
106     return value & ~(alignment - 1U);
107 }
108 
109 template <class T>
ToUintPtr(T * val)110 inline uintptr_t ToUintPtr(T *val)
111 {
112     return reinterpret_cast<uintptr_t>(val);
113 }
114 
ToUintPtr(std::nullptr_t)115 inline uintptr_t ToUintPtr(std::nullptr_t)
116 {
117     return reinterpret_cast<uintptr_t>(nullptr);
118 }
119 
120 template <class T>
ToNativePtr(uintptr_t val)121 inline T *ToNativePtr(uintptr_t val)
122 {
123     return reinterpret_cast<T *>(val);
124 }
125 
ToVoidPtr(uintptr_t val)126 inline void *ToVoidPtr(uintptr_t val)
127 {
128     return reinterpret_cast<void *>(val);
129 }
130 
131 constexpr Alignment DEFAULT_ALIGNMENT = GetLogAlignment(alignof(uintptr_t));
132 constexpr size_t DEFAULT_ALIGNMENT_IN_BYTES = GetAlignmentInBytes(DEFAULT_ALIGNMENT);
133 
GetAlignedObjectSize(size_t size)134 constexpr size_t GetAlignedObjectSize(size_t size)
135 {
136     return AlignUp(size, DEFAULT_ALIGNMENT_IN_BYTES);
137 }
138 
139 template <typename T>
GetAlignment()140 constexpr Alignment GetAlignment()
141 {
142     return GetLogAlignment(std::max(alignof(T), DEFAULT_ALIGNMENT_IN_BYTES));
143 }
144 
145 /*
146     uint64_t return type usage in memory literals for giving
147     compile-time error in case of integer overflow
148 */
149 
150 constexpr uint64_t SHIFT_KB = 10ULL;
151 constexpr uint64_t SHIFT_MB = 20ULL;
152 constexpr uint64_t SHIFT_GB = 30ULL;
153 
154 constexpr uint64_t operator"" _KB(long double count)
155 {
156     return count * (1ULL << SHIFT_KB);
157 }
158 
159 // NOLINTNEXTLINE(google-runtime-int)
160 constexpr uint64_t operator"" _KB(unsigned long long count)
161 {
162     return count * (1ULL << SHIFT_KB);
163 }
164 
165 constexpr uint64_t operator"" _MB(long double count)
166 {
167     return count * (1ULL << SHIFT_MB);
168 }
169 
170 // NOLINTNEXTLINE(google-runtime-int)
171 constexpr uint64_t operator"" _MB(unsigned long long count)
172 {
173     return count * (1ULL << SHIFT_MB);
174 }
175 
176 constexpr uint64_t operator"" _GB(long double count)
177 {
178     return count * (1ULL << SHIFT_GB);
179 }
180 
181 // NOLINTNEXTLINE(google-runtime-int)
182 constexpr uint64_t operator"" _GB(unsigned long long count)
183 {
184     return count * (1ULL << SHIFT_GB);
185 }
186 
187 constexpr uint64_t SIZE_1K = 1_KB;
188 constexpr uint64_t SIZE_1M = 1_MB;
189 constexpr uint64_t SIZE_1G = 1_GB;
190 
191 constexpr uint64_t PANDA_MAX_HEAP_SIZE = 4_GB;
192 constexpr size_t PANDA_POOL_ALIGNMENT_IN_BYTES = 256_KB;
193 
194 constexpr size_t PANDA_DEFAULT_POOL_SIZE = 1_MB;
195 constexpr size_t PANDA_DEFAULT_ARENA_SIZE = 1_MB;
196 constexpr size_t PANDA_DEFAULT_ALLOCATOR_POOL_SIZE = 4_MB;
197 static_assert(PANDA_DEFAULT_POOL_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
198 static_assert(PANDA_DEFAULT_ARENA_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
199 static_assert(PANDA_DEFAULT_ALLOCATOR_POOL_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
200 
201 constexpr Alignment DEFAULT_FRAME_ALIGNMENT = LOG_ALIGN_6;
202 
203 constexpr uintptr_t PANDA_32BITS_HEAP_START_ADDRESS = AlignUp(1U, PANDA_POOL_ALIGNMENT_IN_BYTES);
204 constexpr uint64_t PANDA_32BITS_HEAP_END_OBJECTS_ADDRESS = 4_GB;
205 
IsAddressInObjectsHeap(uintptr_t address)206 inline bool IsAddressInObjectsHeap([[maybe_unused]] uintptr_t address)
207 {
208 #ifdef PANDA_USE_32_BIT_POINTER
209     return PANDA_32BITS_HEAP_START_ADDRESS <= address && address < PANDA_32BITS_HEAP_END_OBJECTS_ADDRESS;
210 #else  // In this case, all 64 bits addresses are valid
211     return true;
212 #endif
213 }
214 
IsInObjectsAddressSpace(uintptr_t address)215 inline bool IsInObjectsAddressSpace(uintptr_t address)
216 {
217     return address == ToUintPtr(nullptr) || IsAddressInObjectsHeap(address);
218 }
219 
220 template <class T>
IsInObjectsAddressSpace(T * address)221 inline bool IsInObjectsAddressSpace(T *address)
222 {
223     return IsInObjectsAddressSpace(ToUintPtr(address));
224 }
225 
226 template <class T>
ToObjPtrType(T * val)227 inline object_pointer_type ToObjPtrType(T *val)
228 {
229     ASSERT(IsInObjectsAddressSpace(ToUintPtr(val)));
230     return static_cast<object_pointer_type>(ToUintPtr(val));
231 }
232 
ToObjPtrType(std::nullptr_t)233 inline object_pointer_type ToObjPtrType(std::nullptr_t)
234 {
235     return static_cast<object_pointer_type>(ToUintPtr(nullptr));
236 }
237 
238 enum class ObjectStatus : bool {
239     DEAD_OBJECT,
240     ALIVE_OBJECT,
241 };
242 
243 using MemVisitor = std::function<void(void *mem, size_t size)>;
244 using GCObjectVisitor = std::function<ObjectStatus(ObjectHeader *)>;
245 using ObjectMoveVisitor = std::add_pointer<size_t(void *mem)>::type;
246 using ObjectVisitor = std::function<void(ObjectHeader *)>;
247 /**
248  * from_object is object from which we found to_object by reference.
249  */
250 using ObjectVisitorEx = std::function<void(ObjectHeader *from_object, ObjectHeader *to_object)>;
251 using ObjectChecker = std::function<bool(const ObjectHeader *)>;
252 using GCRootVisitor = std::function<void(const mem::GCRoot &)>;
253 using MemRangeChecker = std::function<bool(mem::MemRange &)>;
254 
NoFilterChecker(const ObjectHeader * object_header)255 inline bool NoFilterChecker([[maybe_unused]] const ObjectHeader *object_header)
256 {
257     return true;
258 }
259 
GCKillEmAllVisitor(const ObjectHeader * mem)260 inline ObjectStatus GCKillEmAllVisitor([[maybe_unused]] const ObjectHeader *mem)
261 {
262     return ObjectStatus::DEAD_OBJECT;
263 }
264 
265 }  // namespace panda
266 
267 #endif  // LIBPANDABASE_MEM_MEM_H
268