• 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 #include "pool_manager.h"
17 
18 #include "malloc_mem_pool-inl.h"
19 #include "mmap_mem_pool-inl.h"
20 
21 namespace panda {
22 
23 // default is mmap_mem_pool
24 PoolType PoolManager::pool_type = PoolType::MMAP;
25 bool PoolManager::is_initialized = false;
26 MallocMemPool *PoolManager::malloc_mem_pool = nullptr;
27 MmapMemPool *PoolManager::mmap_mem_pool = nullptr;
28 
AllocArena(size_t size,SpaceType space_type,AllocatorType allocator_type,const void * allocator_addr)29 Arena *PoolManager::AllocArena(size_t size, SpaceType space_type, AllocatorType allocator_type,
30                                const void *allocator_addr)
31 {
32     if (pool_type == PoolType::MMAP) {
33         return mmap_mem_pool->AllocArenaImpl(size, space_type, allocator_type, allocator_addr);
34     }
35     return malloc_mem_pool->AllocArenaImpl(size, space_type, allocator_type, allocator_addr);
36 }
37 
FreeArena(Arena * arena)38 void PoolManager::FreeArena(Arena *arena)
39 {
40     if (pool_type == PoolType::MMAP) {
41         return mmap_mem_pool->FreeArenaImpl(arena);
42     }
43     return malloc_mem_pool->FreeArenaImpl(arena);
44 }
45 
Initialize(PoolType type)46 void PoolManager::Initialize(PoolType type)
47 {
48     ASSERT(!is_initialized);
49     is_initialized = true;
50     pool_type = type;
51     if (pool_type == PoolType::MMAP) {
52         mmap_mem_pool = new MmapMemPool();
53     } else {
54         malloc_mem_pool = new MallocMemPool();
55     }
56     LOG(DEBUG, ALLOC) << "PoolManager Initialized";
57 }
58 
GetMmapMemPool()59 MmapMemPool *PoolManager::GetMmapMemPool()
60 {
61     ASSERT(is_initialized);
62     ASSERT(pool_type == PoolType::MMAP);
63     return mmap_mem_pool;
64 }
65 
GetMallocMemPool()66 MallocMemPool *PoolManager::GetMallocMemPool()
67 {
68     ASSERT(is_initialized);
69     ASSERT(pool_type == PoolType::MALLOC);
70     return malloc_mem_pool;
71 }
72 
Finalize()73 void PoolManager::Finalize()
74 {
75     ASSERT(is_initialized);
76     is_initialized = false;
77     if (pool_type == PoolType::MMAP) {
78         delete mmap_mem_pool;
79         mmap_mem_pool = nullptr;
80     } else {
81         delete malloc_mem_pool;
82         malloc_mem_pool = nullptr;
83     }
84 }
85 
86 }  // namespace panda
87