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 #include "pool_map.h"
17 
18 #include "mem/mem.h"
19 #include "mem/space.h"
20 
21 namespace panda {
22 
AddPoolToMap(const void *pool_addr, size_t pool_size, SpaceType space_type, AllocatorType allocator_type, const void *allocator_addr)23 void PoolMap::AddPoolToMap(const void *pool_addr, size_t pool_size, SpaceType space_type, AllocatorType allocator_type,
24                            const void *allocator_addr)
25 {
26     ASSERT(ToUintPtr(pool_addr) % POOL_MAP_GRANULARITY == 0);
27     ASSERT(pool_size % POOL_MAP_GRANULARITY == 0);
28     ASSERT(allocator_addr != nullptr);
29     size_t first_map_num = AddrToMapNum(pool_addr);
30     size_t last_map_num = AddrToMapNum(ToVoidPtr(ToUintPtr(pool_addr) + pool_size - 1U));
31     pool_map_[first_map_num].Initialize(FIRST_BYTE_IN_SEGMENT_VALUE, space_type, allocator_type, allocator_addr);
32     for (size_t i = first_map_num + 1U; i <= last_map_num; i++) {
33         pool_map_[i].Initialize(!FIRST_BYTE_IN_SEGMENT_VALUE, space_type, allocator_type, allocator_addr);
34     }
35 }
36 
RemovePoolFromMap(const void *pool_addr, size_t pool_size)37 void PoolMap::RemovePoolFromMap(const void *pool_addr, size_t pool_size)
38 {
39     ASSERT(ToUintPtr(pool_addr) % POOL_MAP_GRANULARITY == 0);
40     ASSERT(pool_size % POOL_MAP_GRANULARITY == 0);
41     size_t first_map_num = AddrToMapNum(pool_addr);
42     size_t last_map_num = AddrToMapNum(ToVoidPtr(ToUintPtr(pool_addr) + pool_size - 1U));
43     for (size_t i = first_map_num; i <= last_map_num; i++) {
44         pool_map_[i].Destroy();
45     }
46 }
47 
GetAllocatorInfo(const void *addr) const48 AllocatorInfo PoolMap::GetAllocatorInfo(const void *addr) const
49 {
50     size_t map_num = AddrToMapNum(addr);
51     AllocatorType allocator_type = pool_map_[map_num].GetAllocatorType();
52     const void *allocator_addr = pool_map_[map_num].GetAllocatorAddr();
53     // We can't get allocator info for not properly initialized pools
54     ASSERT(allocator_type != AllocatorType::UNDEFINED);
55     ASSERT(allocator_addr != nullptr);
56     return AllocatorInfo(allocator_type, allocator_addr);
57 }
58 
GetSpaceType(const void *addr) const59 SpaceType PoolMap::GetSpaceType(const void *addr) const
60 {
61     if (ToUintPtr(addr) > (POOL_MAP_COVERAGE - 1U)) {
62         return SpaceType::SPACE_TYPE_UNDEFINED;
63     }
64     size_t map_num = AddrToMapNum(addr);
65     SpaceType space_type = pool_map_[map_num].GetSpaceType();
66     // We can't get space type for not properly initialized pools
67     ASSERT(space_type != SpaceType::SPACE_TYPE_UNDEFINED);
68     return space_type;
69 }
70 
GetFirstByteOfPoolForAddr(const void *addr) const71 void *PoolMap::GetFirstByteOfPoolForAddr(const void *addr) const
72 {
73     return GetFirstByteInSegment(addr);
74 }
75 
GetFirstByteInSegment(const void *addr) const76 void *PoolMap::GetFirstByteInSegment(const void *addr) const
77 {
78     size_t current_map_num = AddrToMapNum(addr);
79     while (!pool_map_[current_map_num].IsFirstByteInSegment()) {
80         ASSERT(current_map_num != 0);
81         current_map_num--;
82     }
83     return MapNumToAddr(current_map_num);
84 }
85 
IsEmpty() const86 bool PoolMap::IsEmpty() const
87 {
88     for (auto i : pool_map_) {
89         if (!i.IsEmpty()) {
90             return false;
91         }
92     }
93     return true;
94 }
95 
96 }  // namespace panda
97