1 /*
2 * Copyright (c) 2021 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 #include "runtime/mem/tlab.h"
17
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/mem/object_helpers.h"
20
21 namespace panda::mem {
22
23 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
24 #define LOG_TLAB_ALLOCATOR(level) LOG(level, ALLOC) << "TLAB: "
25
TLAB(void * address,size_t size)26 TLAB::TLAB(void *address, size_t size)
27 {
28 prev_tlab_ = nullptr;
29 next_tlab_ = nullptr;
30 Fill(address, size);
31 LOG_TLAB_ALLOCATOR(DEBUG) << "Construct a new TLAB at addr " << std::hex << address << " with size " << std::dec
32 << size;
33 }
34
Fill(void * address,size_t size)35 void TLAB::Fill(void *address, size_t size)
36 {
37 ASSERT(ToUintPtr(address) == AlignUp(ToUintPtr(address), DEFAULT_ALIGNMENT_IN_BYTES));
38 memory_start_addr_ = address;
39 memory_end_addr_ = ToVoidPtr(ToUintPtr(address) + size);
40 cur_free_position_ = address;
41 ASAN_POISON_MEMORY_REGION(memory_start_addr_, GetSize());
42 LOG_TLAB_ALLOCATOR(DEBUG) << "Fill a TLAB with buffer at addr " << std::hex << address << " with size " << std::dec
43 << size;
44 }
45
~TLAB()46 TLAB::~TLAB()
47 {
48 LOG_TLAB_ALLOCATOR(DEBUG) << "Destroy a TLAB at addr " << std::hex << memory_start_addr_ << " with size "
49 << std::dec << GetSize();
50 }
51
Destroy()52 void TLAB::Destroy()
53 {
54 LOG_TLAB_ALLOCATOR(DEBUG) << "Destroy the TLAB at addr " << std::hex << this;
55 ASAN_UNPOISON_MEMORY_REGION(memory_start_addr_, GetSize());
56 }
57
Alloc(size_t size)58 void *TLAB::Alloc(size_t size)
59 {
60 void *ret = nullptr;
61 size_t free_size = GetFreeSize();
62 size_t requested_size = GetAlignedObjectSize(size);
63 if (requested_size <= free_size) {
64 ASSERT(ToUintPtr(cur_free_position_) == AlignUp(ToUintPtr(cur_free_position_), DEFAULT_ALIGNMENT_IN_BYTES));
65 ret = cur_free_position_;
66 ASAN_UNPOISON_MEMORY_REGION(ret, size);
67 cur_free_position_ = ToVoidPtr(ToUintPtr(cur_free_position_) + requested_size);
68 }
69 LOG_TLAB_ALLOCATOR(DEBUG) << "Alloc size = " << size << " at addr = " << ret;
70 return ret;
71 }
72
IterateOverObjects(const std::function<void (ObjectHeader * object_header)> & object_visitor)73 void TLAB::IterateOverObjects(const std::function<void(ObjectHeader *object_header)> &object_visitor)
74 {
75 LOG_TLAB_ALLOCATOR(DEBUG) << __func__ << " started";
76 auto *cur_ptr = memory_start_addr_;
77 void *end_ptr = cur_free_position_;
78 while (cur_ptr < end_ptr) {
79 auto object_header = static_cast<ObjectHeader *>(cur_ptr);
80 size_t object_size = GetObjectSize(cur_ptr);
81 object_visitor(object_header);
82 cur_ptr = ToVoidPtr(AlignUp(ToUintPtr(cur_ptr) + object_size, DEFAULT_ALIGNMENT_IN_BYTES));
83 }
84 LOG_TLAB_ALLOCATOR(DEBUG) << __func__ << " finished";
85 }
86
IterateOverObjectsInRange(const std::function<void (ObjectHeader * object_header)> & mem_visitor,const MemRange & mem_range)87 void TLAB::IterateOverObjectsInRange(const std::function<void(ObjectHeader *object_header)> &mem_visitor,
88 const MemRange &mem_range)
89 {
90 LOG_TLAB_ALLOCATOR(DEBUG) << __func__ << " started";
91 if (!GetMemRangeForOccupiedMemory().IsIntersect(mem_range)) {
92 return;
93 }
94 void *current_ptr = memory_start_addr_;
95 void *end_ptr = ToVoidPtr(std::min(ToUintPtr(cur_free_position_), mem_range.GetEndAddress() + 1));
96 void *start_iterate_pos = ToVoidPtr(std::max(ToUintPtr(current_ptr), mem_range.GetStartAddress()));
97 while (current_ptr < start_iterate_pos) {
98 size_t object_size = GetObjectSize(static_cast<ObjectHeader *>(current_ptr));
99 current_ptr = ToVoidPtr(AlignUp(ToUintPtr(current_ptr) + object_size, DEFAULT_ALIGNMENT_IN_BYTES));
100 }
101 while (current_ptr < end_ptr) {
102 auto object_header = static_cast<ObjectHeader *>(current_ptr);
103 size_t object_size = GetObjectSize(current_ptr);
104 mem_visitor(object_header);
105 current_ptr = ToVoidPtr(AlignUp(ToUintPtr(current_ptr) + object_size, DEFAULT_ALIGNMENT_IN_BYTES));
106 }
107 LOG_TLAB_ALLOCATOR(DEBUG) << __func__ << " finished";
108 }
109
ContainObject(const ObjectHeader * obj)110 bool TLAB::ContainObject(const ObjectHeader *obj)
111 {
112 return (ToUintPtr(cur_free_position_) > ToUintPtr(obj)) && (ToUintPtr(memory_start_addr_) <= ToUintPtr(obj));
113 }
114
IsLive(const ObjectHeader * obj)115 bool TLAB::IsLive(const ObjectHeader *obj)
116 {
117 ASSERT(ContainObject(obj));
118 return ContainObject(obj);
119 }
120
121 #undef LOG_TLAB_ALLOCATOR
122
123 } // namespace panda::mem
124