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