• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "share_memory_allocator.h"
17 #include "logging.h"
18 #include "share_memory_block.h"
19 
20 namespace {
21 const int MIN_SHARE_MEMORY_SIZE = 1024;
22 }
GetInstance()23 ShareMemoryAllocator& ShareMemoryAllocator::GetInstance()
24 {
25     static ShareMemoryAllocator shareMemoryAllocator;
26 
27     return shareMemoryAllocator;
28 }
29 
ShareMemoryAllocator()30 ShareMemoryAllocator::ShareMemoryAllocator()
31 {
32     memoryBlocks.clear();
33 }
~ShareMemoryAllocator()34 ShareMemoryAllocator::~ShareMemoryAllocator()
35 {
36     memoryBlocks.clear();
37 }
38 
ReleaseMemoryBlockLocal(std::string name)39 bool ShareMemoryAllocator::ReleaseMemoryBlockLocal(std::string name)
40 {
41     auto pmb = FindMemoryBlockByName(name);
42 
43     CHECK_NOTNULL(pmb, false, "FAIL %s", name.c_str());
44 
45     memoryBlocks.erase(name);
46     return true;
47 }
48 
ReleaseMemoryBlockRemote(std::string name)49 bool ShareMemoryAllocator::ReleaseMemoryBlockRemote(std::string name)
50 {
51     auto pmb = FindMemoryBlockByName(name);
52 
53     CHECK_NOTNULL(pmb, false, "FAIL %s", name.c_str());
54 
55     memoryBlocks.erase(name);
56     return true;
57 }
58 
CreateMemoryBlockLocal(std::string name,uint32_t size)59 ShareMemoryBlockPtr ShareMemoryAllocator::CreateMemoryBlockLocal(std::string name, uint32_t size)
60 {
61     CHECK_TRUE(memoryBlocks.find(name) == memoryBlocks.end(), nullptr, "%s already used", name.c_str());
62     CHECK_TRUE(size >= MIN_SHARE_MEMORY_SIZE, nullptr, "%s %d size less than %d", name.c_str(), size,
63                MIN_SHARE_MEMORY_SIZE);
64 
65     auto block = std::make_shared<ShareMemoryBlock>(name, size);
66     if (!block->Valid()) {
67         PROFILER_LOG_INFO(LOG_CORE, "CreateMemoryBlockLocal FAIL");
68         return nullptr;
69     }
70     memoryBlocks[name] = block;
71     return block;
72 }
73 
CreateMemoryBlockRemote(std::string name,uint32_t size,int fd)74 ShareMemoryBlockPtr ShareMemoryAllocator::CreateMemoryBlockRemote(std::string name, uint32_t size, int fd)
75 {
76     CHECK_TRUE(memoryBlocks.find(name) == memoryBlocks.end(), nullptr, "%s already used", name.c_str());
77     CHECK_TRUE(size >= MIN_SHARE_MEMORY_SIZE, nullptr, "%s %d size less than %d", name.c_str(), size,
78                MIN_SHARE_MEMORY_SIZE);
79 
80     auto block = std::make_shared<ShareMemoryBlock>(name, size, fd);
81     if (!block->Valid()) {
82         PROFILER_LOG_INFO(LOG_CORE, "CreateMemoryBlockRemote FAIL");
83         return nullptr;
84     }
85     memoryBlocks[name] = block;
86     return block;
87 }
88 
FindMemoryBlockByName(std::string name)89 ShareMemoryBlockPtr ShareMemoryAllocator::FindMemoryBlockByName(std::string name)
90 {
91     auto it = memoryBlocks.find(name);
92     CHECK_TRUE(it != memoryBlocks.end(), nullptr, "FAIL");
93     return it->second;
94 }
95