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     std::lock_guard<std::mutex> lock(m_mtx);
59     auto iter = m_memorys.find(buffer);
60     if (iter == m_memorys.end()) {
61         LOGE("This buffer is not found, cannot release.");
62         return OH_NN_INVALID_PARAMETER;
63     }
64 
65     auto& memory = m_memorys[buffer];
66     auto unmapResult = munmap(const_cast<void*>(memory.data), memory.length);
67     if (unmapResult != 0) {
68         LOGE("Unmap memory failed. Please try again.");
69         return OH_NN_MEMORY_ERROR;
70     }
71     memory.data = nullptr;
72 
73     m_memorys.erase(iter);
74     return OH_NN_SUCCESS;
75 }
76 
GetMemory(const void * buffer,Memory & memory)77 OH_NN_ReturnCode MemoryManager::GetMemory(const void* buffer, Memory& memory)
78 {
79     if (buffer == nullptr) {
80         LOGE("Memory is nullptr.");
81         return OH_NN_NULL_PTR;
82     }
83 
84     std::lock_guard<std::mutex> lock(m_mtx);
85     auto iter = m_memorys.find(buffer);
86     if (iter == m_memorys.end()) {
87         LOGE("Memory is not found.");
88         return OH_NN_INVALID_PARAMETER;
89     }
90 
91     memory.fd = iter->second.fd;
92     memory.data = iter->second.data;
93     memory.length = iter->second.length;
94 
95     return OH_NN_SUCCESS;
96 }
97 } // NeuralNetworkRuntime
98 } // OHOS