1 /*
2 * Copyright (c) 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
17 #include "memory_manager.h"
18
19 #include <sys/mman.h>
20 #include <unistd.h>
21
22 #include "cpp_type.h"
23 #include "common/log.h"
24
25 namespace OHOS {
26 namespace NeuralNetworkRuntime {
MapMemory(int fd,size_t length)27 void* MemoryManager::MapMemory(int fd, size_t length)
28 {
29 if (fd < 0) {
30 LOGE("Invalid fd, fd must greater than 0.");
31 return nullptr;
32 }
33
34 if (length <= 0 || length > ALLOCATE_BUFFER_LIMIT) {
35 LOGE("Invalid buffer size, it must greater than 0 and less than 1Gb. length=%zu", length);
36 return nullptr;
37 }
38
39 void* addr = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
40 if (addr == MAP_FAILED) {
41 LOGE("Map fd to address failed.");
42 return nullptr;
43 }
44
45 std::lock_guard<std::mutex> lock(m_mtx);
46 Memory memory {fd, addr, length};
47 m_memorys.emplace(addr, memory);
48 return addr;
49 }
50
UnMapMemory(const void * buffer)51 OH_NN_ReturnCode MemoryManager::UnMapMemory(const void* buffer)
52 {
53 if (buffer == nullptr) {
54 LOGE("Buffer is nullptr, no need to release.");
55 return OH_NN_INVALID_PARAMETER;
56 }
57
58 auto iter = m_memorys.find(buffer);
59 if (iter == m_memorys.end()) {
60 LOGE("This buffer is not found, cannot release.");
61 return OH_NN_INVALID_PARAMETER;
62 }
63
64 auto& memory = m_memorys[buffer];
65 auto unmapResult = munmap(const_cast<void*>(memory.data), memory.length);
66 if (unmapResult != 0) {
67 LOGE("Unmap memory failed. Please try again.");
68 return OH_NN_MEMORY_ERROR;
69 }
70 memory.data = nullptr;
71
72 if (close(memory.fd) != 0) {
73 LOGE("Close memory fd failed. fd=%d", memory.fd);
74 return OH_NN_MEMORY_ERROR;
75 }
76
77 std::lock_guard<std::mutex> lock(m_mtx);
78 m_memorys.erase(iter);
79 return OH_NN_SUCCESS;
80 }
81
GetMemory(const void * buffer,Memory & memory) const82 OH_NN_ReturnCode MemoryManager::GetMemory(const void* buffer, Memory& memory) const
83 {
84 if (buffer == nullptr) {
85 LOGE("Memory is nullptr.");
86 return OH_NN_NULL_PTR;
87 }
88
89 auto iter = m_memorys.find(buffer);
90 if (iter == m_memorys.end()) {
91 LOGE("Memory is not found.");
92 return OH_NN_INVALID_PARAMETER;
93 }
94
95 memory.fd = iter->second.fd;
96 memory.data = buffer;
97 memory.length = iter->second.length;
98
99 return OH_NN_SUCCESS;
100 }
101 } // NeuralNetworkRuntime
102 } // OHOS