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 #ifndef NEURAL_NETWORK_RUNTIME_MEMORY_MANAGER_H 17 #define NEURAL_NETWORK_RUNTIME_MEMORY_MANAGER_H 18 19 #include <unordered_map> 20 #include <mutex> 21 22 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime_type.h" 23 24 namespace OHOS { 25 namespace NeuralNetworkRuntime { 26 const int INVALID_FD = -1; 27 28 struct Memory { 29 int fd; 30 const void* data; 31 size_t length; 32 }; 33 34 class MemoryManager { 35 public: 36 ~MemoryManager() = default; 37 38 void* MapMemory(int fd, size_t length); 39 OH_NN_ReturnCode UnMapMemory(const void* buffer); 40 OH_NN_ReturnCode GetMemory(const void* buffer, Memory& memory); 41 GetInstance()42 static MemoryManager* GetInstance() 43 { 44 static MemoryManager instance; 45 return &instance; 46 } 47 48 private: MemoryManager()49 MemoryManager() {}; 50 MemoryManager(const MemoryManager&) = delete; 51 MemoryManager& operator=(const MemoryManager&) = delete; 52 53 private: 54 // key: OH_NN_Memory, value: fd 55 std::unordered_map<const void*, Memory> m_memorys; 56 std::mutex m_mtx; 57 }; 58 } // namespace NeuralNetworkRuntime 59 } // OHOS 60 #endif // NEURAL_NETWORK_RUNTIME_MEMORY_MANAGER_H