106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "paged_mem_pool.h"
1606f6ba60Sopenharmony_ci
1706f6ba60Sopenharmony_ci#include <algorithm>
1806f6ba60Sopenharmony_ci#include <cerrno>
1906f6ba60Sopenharmony_ci#include <cstring>
2006f6ba60Sopenharmony_ci#include <sys/mman.h>
2106f6ba60Sopenharmony_ci#include "logging.h"
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_cinamespace {
2406f6ba60Sopenharmony_ci#ifndef PAGE_SIZE
2506f6ba60Sopenharmony_ciconstexpr size_t PAGE_SIZE = 4096;
2606f6ba60Sopenharmony_ci#endif
2706f6ba60Sopenharmony_ci} // namespace
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_ciFTRACE_NS_BEGIN
3006f6ba60Sopenharmony_ciPagedMemPool::PagedMemPool(size_t pagePerBlock, size_t maxCacheSize)
3106f6ba60Sopenharmony_ci    : blockSize_(pagePerBlock * PAGE_SIZE), maxCacheSize_(maxCacheSize)
3206f6ba60Sopenharmony_ci{
3306f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "PagedMemPool init with %zu ppb, %zu slots!", pagePerBlock, maxCacheSize);
3406f6ba60Sopenharmony_ci}
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ciPagedMemPool::~PagedMemPool()
3706f6ba60Sopenharmony_ci{
3806f6ba60Sopenharmony_ci    auto it = blockSet_.begin();
3906f6ba60Sopenharmony_ci    while (it != blockSet_.end()) {
4006f6ba60Sopenharmony_ci        Free(*it);
4106f6ba60Sopenharmony_ci        it = blockSet_.erase(it);
4206f6ba60Sopenharmony_ci    }
4306f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "PagedMemPool deinit!");
4406f6ba60Sopenharmony_ci}
4506f6ba60Sopenharmony_ci
4606f6ba60Sopenharmony_cisize_t PagedMemPool::GetBlockSize() const
4706f6ba60Sopenharmony_ci{
4806f6ba60Sopenharmony_ci    return blockSize_;
4906f6ba60Sopenharmony_ci}
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_civoid* PagedMemPool::Allocate()
5206f6ba60Sopenharmony_ci{
5306f6ba60Sopenharmony_ci    void* block = nullptr;
5406f6ba60Sopenharmony_ci    if (freeList_.size() > 0) {
5506f6ba60Sopenharmony_ci        block = freeList_.back();
5606f6ba60Sopenharmony_ci        freeList_.pop_back();
5706f6ba60Sopenharmony_ci        return block;
5806f6ba60Sopenharmony_ci    }
5906f6ba60Sopenharmony_ci
6006f6ba60Sopenharmony_ci    block = mmap(nullptr, blockSize_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
6106f6ba60Sopenharmony_ci    CHECK_NOTNULL(block, nullptr, "mmap failed, %d", errno);
6206f6ba60Sopenharmony_ci    blockSet_.insert(block);
6306f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "PagedMemPool::Allocate %zuB block done!", blockSize_);
6406f6ba60Sopenharmony_ci    return block;
6506f6ba60Sopenharmony_ci}
6606f6ba60Sopenharmony_ci
6706f6ba60Sopenharmony_cibool PagedMemPool::Recycle(void* block)
6806f6ba60Sopenharmony_ci{
6906f6ba60Sopenharmony_ci    CHECK_TRUE(Valid(block), false, "block not Allocate returned!");
7006f6ba60Sopenharmony_ci
7106f6ba60Sopenharmony_ci    if (freeList_.size() < maxCacheSize_) {
7206f6ba60Sopenharmony_ci        freeList_.push_back(block);
7306f6ba60Sopenharmony_ci        return true;
7406f6ba60Sopenharmony_ci    }
7506f6ba60Sopenharmony_ci    CHECK_TRUE(Free(block), false, "Free block failed!");
7606f6ba60Sopenharmony_ci    blockSet_.erase(block);
7706f6ba60Sopenharmony_ci    return true;
7806f6ba60Sopenharmony_ci}
7906f6ba60Sopenharmony_ci
8006f6ba60Sopenharmony_cibool PagedMemPool::Free(void* block)
8106f6ba60Sopenharmony_ci{
8206f6ba60Sopenharmony_ci    CHECK_TRUE(munmap(block, blockSize_) == 0, false, "munmap failed, %d", errno);
8306f6ba60Sopenharmony_ci    return true;
8406f6ba60Sopenharmony_ci}
8506f6ba60Sopenharmony_ci
8606f6ba60Sopenharmony_cibool PagedMemPool::Valid(void* block)
8706f6ba60Sopenharmony_ci{
8806f6ba60Sopenharmony_ci    return blockSet_.count(block) > 0;
8906f6ba60Sopenharmony_ci}
9006f6ba60Sopenharmony_ciFTRACE_NS_END
91