• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pool_map.h"
17 
18 namespace panda {
19 
AddPoolToMap(const void * pool_addr,size_t pool_size,SpaceType space_type,AllocatorType allocator_type,const void * allocator_addr)20 void PoolMap::AddPoolToMap(const void *pool_addr, size_t pool_size, SpaceType space_type, AllocatorType allocator_type,
21                            const void *allocator_addr)
22 {
23     ASSERT(ToUintPtr(pool_addr) % POOL_MAP_GRANULARITY == 0);
24     ASSERT(pool_size % POOL_MAP_GRANULARITY == 0);
25     ASSERT(allocator_addr != nullptr);
26     size_t first_map_num = AddrToMapNum(pool_addr);
27     size_t last_map_num = AddrToMapNum(ToVoidPtr(ToUintPtr(pool_addr) + pool_size - 1U));
28     pool_map_[first_map_num].Initialize(FIRST_BYTE_IN_SEGMENT_VALUE, space_type, allocator_type, allocator_addr);
29     for (size_t i = first_map_num + 1U; i <= last_map_num; i++) {
30         pool_map_[i].Initialize(!FIRST_BYTE_IN_SEGMENT_VALUE, space_type, allocator_type, allocator_addr);
31     }
32 }
33 
RemovePoolFromMap(void * pool_addr,size_t pool_size)34 void PoolMap::RemovePoolFromMap(void *pool_addr, size_t pool_size)
35 {
36     ASSERT(ToUintPtr(pool_addr) % POOL_MAP_GRANULARITY == 0);
37     ASSERT(pool_size % POOL_MAP_GRANULARITY == 0);
38     size_t first_map_num = AddrToMapNum(pool_addr);
39     size_t last_map_num = AddrToMapNum(ToVoidPtr(ToUintPtr(pool_addr) + pool_size - 1U));
40     for (size_t i = first_map_num; i <= last_map_num; i++) {
41         pool_map_[i].Destroy();
42     }
43 }
44 
GetAllocatorInfo(const void * addr) const45 AllocatorInfo PoolMap::GetAllocatorInfo(const void *addr) const
46 {
47     size_t map_num = AddrToMapNum(addr);
48     AllocatorType allocator_type = pool_map_[map_num].GetAllocatorType();
49     const void *allocator_addr = pool_map_[map_num].GetAllocatorAddr();
50     // We can't get allocator info for not properly initialized pools
51     ASSERT(allocator_type != AllocatorType::UNDEFINED);
52     ASSERT(allocator_addr != nullptr);
53     return AllocatorInfo(allocator_type, allocator_addr);
54 }
55 
GetSpaceType(const void * addr) const56 SpaceType PoolMap::GetSpaceType(const void *addr) const
57 {
58     if (ToUintPtr(addr) > (POOL_MAP_COVERAGE - 1U)) {
59         return SpaceType::SPACE_TYPE_UNDEFINED;
60     }
61     size_t map_num = AddrToMapNum(addr);
62     SpaceType space_type = pool_map_[map_num].GetSpaceType();
63     // We can't get space type for not properly initialized pools
64     ASSERT(space_type != SpaceType::SPACE_TYPE_UNDEFINED);
65     return space_type;
66 }
67 
GetFirstByteOfPoolForAddr(const void * addr)68 void *PoolMap::GetFirstByteOfPoolForAddr(const void *addr)
69 {
70     return GetFirstByteInSegment(addr);
71 }
72 
GetFirstByteInSegment(const void * addr)73 void *PoolMap::GetFirstByteInSegment(const void *addr)
74 {
75     size_t current_map_num = AddrToMapNum(addr);
76     while (!pool_map_[current_map_num].IsFirstByteInSegment()) {
77         ASSERT(current_map_num != 0);
78         current_map_num--;
79     }
80     return MapNumToAddr(current_map_num);
81 }
82 
83 }  // namespace panda
84