Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu_common / MemoryManager.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_BACKEND_CPU_MEMORY_MANAGER_H__
18 #define __ONERT_BACKEND_CPU_MEMORY_MANAGER_H__
19
20 #include "backend/IMemoryManager.h"
21 #include "MemoryPlanner.h"
22 #include "ir/OperandIndexMap.h"
23
24 namespace onert
25 {
26 namespace backend
27 {
28 namespace cpu_common
29 {
30
31 class MemoryManager : public backend::IMemoryManager
32 {
33 public:
34   MemoryManager();
35   MemoryManager(const std::string);
36   virtual ~MemoryManager() = default;
37
38   void allocate(void) override;
39   uint8_t *getBuffer(const ir::OperandIndex &ind) const;
40   void deallocate(void) override { _mem_alloc->release(); }
41
42   void claimPlan(const ir::OperandIndex &ind, uint32_t size);
43   void releasePlan(const ir::OperandIndex &ind);
44
45 private:
46   cpu_common::IMemoryPlanner *createMemoryPlanner();
47   cpu_common::IMemoryPlanner *createMemoryPlanner(const std::string);
48
49 private:
50   ir::OperandIndexMap<cpu_common::Block> _tensor_mem_map;
51   std::shared_ptr<cpu_common::IMemoryPlanner> _mem_planner;
52   std::shared_ptr<cpu_common::Allocator> _mem_alloc;
53 };
54
55 class DynamicMemoryManager
56 {
57 public:
58   DynamicMemoryManager() = default;
59   virtual ~DynamicMemoryManager() = default;
60
61   std::shared_ptr<cpu_common::Allocator> allocate(const ir::OperandIndex &ind, uint32_t capacity);
62   void deallocate(void);
63
64 private:
65   ir::OperandIndexMap<std::shared_ptr<cpu_common::Allocator>> _mem_alloc_map;
66 };
67
68 } // namespace cpu_common
69 } // namespace backend
70 } // namespace onert
71
72 #endif // __ONERT_BACKEND_CPU_MEMORY_MANAGER_H__