ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BuddyMemoryManager.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
2 *
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#if 0
17
18#include "MemoryManager.h"
19
20#ifndef LUCI_INTERPRETER_BUDDY_MEMORY_MANAGER_H
21#define LUCI_INTERPRETER_BUDDY_MEMORY_MANAGER_H
22
23namespace luci_interpreter
24{
25
26class BuddyMemoryManager : public IMemoryManager
27{
28public:
29 BuddyMemoryManager(uint8_t *memory_start, int32_t memSize);
30
31 void allocate_memory(luci_interpreter::Tensor &tensor) final;
32 void release_memory(luci_interpreter::Tensor &tensor) final;
33
34private:
35 struct Block
36 {
37 Block *next_free;
38 bool is_free;
39 uint32_t size;
40 // debug field
41 Block *self;
42 };
43
44 Block *_start_block;
45 int32_t _num_blocks;
46 uint32_t _size;
47 Block *_free_blocks[32]{};
48
49 static int32_t lowerLog2(uint32_t val)
50 {
51 int32_t i = 0;
52 while (val >>= 1)
53 i++;
54
55 return i;
56 }
57
58 void addToBlocks(Block *block, int32_t l)
59 {
60 if (!block)
61 return;
62
63 block->next_free = _free_blocks[l];
64 _free_blocks[l] = block;
65 }
66
67 void removeFromBlocks(const Block *block, int32_t l)
68 {
69 if (!block)
70 return;
71
72 Block *tmp = _free_blocks[l];
73
74 if (block == tmp)
75 {
76 _free_blocks[l] = block->next_free;
77 return;
78 }
79
80 while (tmp)
81 {
82 if (tmp->next_free == block)
83 {
84 tmp->next_free = block->next_free;
85 return;
86 }
87
88 tmp = tmp->next_free;
89 }
90 }
91
92 void divideBlock(Block *block, int32_t l)
93 {
94 int32_t size = ((block->size + sizeof(Block)) / 2) - sizeof(Block);
95
96 removeFromBlocks(block, l);
97
98 // there is no need to add to the free_blocks list here
99 block->is_free = true;
100 block->size = size;
101 block->self = block;
102
103 Block *buddy;
104 buddy = (Block *)((uint8_t *)block + sizeof(Block) + size);
105 buddy->is_free = true;
106 buddy->size = size;
107 buddy->self = buddy;
108
109 addToBlocks(buddy, l - 1);
110 }
111
112 Block *mergeBlock(Block *block)
113 {
114 Block *buddy;
115
116 const int32_t l = lowerLog2(block->size + sizeof(Block));
117
118 const int64_t address = ((uint8_t *)block - (uint8_t *)_start_block);
119 buddy = (Block *)((address ^ (1 << l)) + (uint8_t *)_start_block);
120
121 if (!buddy->is_free || buddy->size != block->size)
122 return nullptr;
123
124 if (block > buddy)
125 {
126 Block *x = block;
127 block = buddy;
128 buddy = x;
129 }
130
131 removeFromBlocks(block, l);
132 removeFromBlocks(buddy, l);
133
134 block->size = block->size * 2 + sizeof(Block);
135 block->is_free = true;
136 block->self = block;
137
138 addToBlocks(block, l + 1);
139
140 return block;
141 }
142};
143
144} // namespace luci_interpreter
145
146#endif // LUCI_INTERPRETER_BUDDY_MEMORY_MANAGER_H
147
148#endif
void allocate_memory(luci_interpreter::Tensor &tensor) final
void release_memory(luci_interpreter::Tensor &tensor) final
BuddyMemoryManager(uint8_t *memory_start, int32_t memSize)
int32_t size[5]
Definition Slice.cpp:35