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