• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 ark::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     prevTlab_ = nullptr;
29     nextTlab_ = 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     memoryStartAddr_ = address;
39     memoryEndAddr_ = ToVoidPtr(ToUintPtr(address) + size);
40     curFreePosition_ = address;
41     ASAN_POISON_MEMORY_REGION(memoryStartAddr_, 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 << memoryStartAddr_ << " with size " << std::dec
49                               << 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(memoryStartAddr_, GetSize());
56 }
57 
Alloc(size_t size)58 void *TLAB::Alloc(size_t size)
59 {
60     void *ret = nullptr;
61     size_t freeSize = GetFreeSize();
62     size_t requestedSize = GetAlignedObjectSize(size);
63     if (requestedSize <= freeSize) {
64         ASSERT(ToUintPtr(curFreePosition_) == AlignUp(ToUintPtr(curFreePosition_), DEFAULT_ALIGNMENT_IN_BYTES));
65         ret = curFreePosition_;
66         ASAN_UNPOISON_MEMORY_REGION(ret, size);
67         curFreePosition_ = ToVoidPtr(ToUintPtr(curFreePosition_) + requestedSize);
68     }
69     LOG_TLAB_ALLOCATOR(DEBUG) << "Alloc size = " << size << " at addr = " << ret;
70     return ret;
71 }
72 
IterateOverObjects(const std::function<void (ObjectHeader * objectHeader)> & objectVisitor)73 void TLAB::IterateOverObjects(const std::function<void(ObjectHeader *objectHeader)> &objectVisitor)
74 {
75     LOG_TLAB_ALLOCATOR(DEBUG) << "IterateOverObjects started";
76     auto *curPtr = memoryStartAddr_;
77     void *endPtr = curFreePosition_;
78     while (curPtr < endPtr) {
79         auto objectHeader = static_cast<ObjectHeader *>(curPtr);
80         size_t objectSize = GetObjectSize(curPtr);
81         objectVisitor(objectHeader);
82         curPtr = ToVoidPtr(AlignUp(ToUintPtr(curPtr) + objectSize, DEFAULT_ALIGNMENT_IN_BYTES));
83     }
84     LOG_TLAB_ALLOCATOR(DEBUG) << "IterateOverObjects finished";
85 }
86 
IterateOverObjectsInRange(const std::function<void (ObjectHeader * objectHeader)> & memVisitor,const MemRange & memRange)87 void TLAB::IterateOverObjectsInRange(const std::function<void(ObjectHeader *objectHeader)> &memVisitor,
88                                      const MemRange &memRange)
89 {
90     LOG_TLAB_ALLOCATOR(DEBUG) << "IterateOverObjectsInRange started";
91     if (GetOccupiedSize() == 0 || !GetMemRangeForOccupiedMemory().IsIntersect(memRange)) {
92         return;
93     }
94     void *currentPtr = memoryStartAddr_;
95     void *endPtr = ToVoidPtr(std::min(ToUintPtr(curFreePosition_), memRange.GetEndAddress() + 1));
96     void *startIteratePos = ToVoidPtr(std::max(ToUintPtr(currentPtr), memRange.GetStartAddress()));
97     while (currentPtr < startIteratePos) {
98         size_t objectSize = GetObjectSize(static_cast<ObjectHeader *>(currentPtr));
99         currentPtr = ToVoidPtr(AlignUp(ToUintPtr(currentPtr) + objectSize, DEFAULT_ALIGNMENT_IN_BYTES));
100     }
101     while (currentPtr < endPtr) {
102         auto objectHeader = static_cast<ObjectHeader *>(currentPtr);
103         size_t objectSize = GetObjectSize(currentPtr);
104         memVisitor(objectHeader);
105         currentPtr = ToVoidPtr(AlignUp(ToUintPtr(currentPtr) + objectSize, DEFAULT_ALIGNMENT_IN_BYTES));
106     }
107     LOG_TLAB_ALLOCATOR(DEBUG) << "IterateOverObjects finished";
108 }
109 
ContainObject(const ObjectHeader * obj)110 bool TLAB::ContainObject(const ObjectHeader *obj)
111 {
112     return (ToUintPtr(curFreePosition_) > ToUintPtr(obj)) && (ToUintPtr(memoryStartAddr_) <= 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 ark::mem
124