• 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 #include "paged_mem_pool.h"
16 
17 #include <algorithm>
18 #include <cerrno>
19 #include <cstring>
20 #include <sys/mman.h>
21 #include "logging.h"
22 
23 namespace {
24 #ifndef PAGE_SIZE
25 constexpr size_t PAGE_SIZE = 4096;
26 #endif
27 } // namespace
28 
29 FTRACE_NS_BEGIN
PagedMemPool(size_t pagePerBlock,size_t maxCacheSize)30 PagedMemPool::PagedMemPool(size_t pagePerBlock, size_t maxCacheSize)
31     : blockSize_(pagePerBlock * PAGE_SIZE), maxCacheSize_(maxCacheSize)
32 {
33     HILOG_INFO(LOG_CORE, "PagedMemPool init with %zu ppb, %zu slots!", pagePerBlock, maxCacheSize);
34 }
35 
~PagedMemPool()36 PagedMemPool::~PagedMemPool()
37 {
38     auto it = blockSet_.begin();
39     while (it != blockSet_.end()) {
40         Free(*it);
41         it = blockSet_.erase(it);
42     }
43     HILOG_INFO(LOG_CORE, "PagedMemPool deinit!");
44 }
45 
GetBlockSize() const46 size_t PagedMemPool::GetBlockSize() const
47 {
48     return blockSize_;
49 }
50 
Allocate()51 void* PagedMemPool::Allocate()
52 {
53     void* block = nullptr;
54     if (freeList_.size() > 0) {
55         block = freeList_.back();
56         freeList_.pop_back();
57         return block;
58     }
59 
60     block = mmap(nullptr, blockSize_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
61     CHECK_NOTNULL(block, nullptr, "mmap failed, %d", errno);
62     blockSet_.insert(block);
63     HILOG_INFO(LOG_CORE, "PagedMemPool::Allocate %zuB block done!", blockSize_);
64     return block;
65 }
66 
Recycle(void * block)67 bool PagedMemPool::Recycle(void* block)
68 {
69     CHECK_TRUE(Valid(block), false, "block not Allocate returned!");
70 
71     if (freeList_.size() < maxCacheSize_) {
72         freeList_.push_back(block);
73         return true;
74     }
75     CHECK_TRUE(Free(block), false, "Free block failed!");
76     blockSet_.erase(block);
77     return true;
78 }
79 
Free(void * block)80 bool PagedMemPool::Free(void* block)
81 {
82     CHECK_TRUE(munmap(block, blockSize_) == 0, false, "munmap failed, %d", errno);
83     return true;
84 }
85 
Valid(void * block)86 bool PagedMemPool::Valid(void* block)
87 {
88     return blockSet_.count(block) > 0;
89 }
90 FTRACE_NS_END
91