18#include "MemoryManager.h"
20#ifndef LUCI_INTERPRETER_BUDDY_MEMORY_MANAGER_H
21#define LUCI_INTERPRETER_BUDDY_MEMORY_MANAGER_H
26class BuddyMemoryManager :
public IMemoryManager
47 Block *_free_blocks[32]{};
49 static int32_t lowerLog2(uint32_t val)
58 void addToBlocks(Block *block, int32_t l)
63 block->next_free = _free_blocks[
l];
64 _free_blocks[
l] = block;
67 void removeFromBlocks(
const Block *block, int32_t l)
72 Block *tmp = _free_blocks[
l];
76 _free_blocks[
l] = block->next_free;
82 if (tmp->next_free == block)
84 tmp->next_free = block->next_free;
92 void divideBlock(Block *block, int32_t l)
94 int32_t
size = ((block->size +
sizeof(Block)) / 2) -
sizeof(Block);
96 removeFromBlocks(block, l);
99 block->is_free =
true;
104 buddy = (Block *)((uint8_t *)block +
sizeof(Block) +
size);
105 buddy->is_free =
true;
109 addToBlocks(buddy, l - 1);
112 Block *mergeBlock(Block *block)
116 const int32_t
l = lowerLog2(block->size +
sizeof(Block));
118 const int64_t address = ((uint8_t *)block - (uint8_t *)_start_block);
119 buddy = (Block *)((address ^ (1 << l)) + (uint8_t *)_start_block);
121 if (!buddy->is_free || buddy->size != block->size)
131 removeFromBlocks(block, l);
132 removeFromBlocks(buddy, l);
134 block->size = block->size * 2 +
sizeof(Block);
135 block->is_free =
true;
138 addToBlocks(block, l + 1);
void allocate_memory(luci_interpreter::Tensor &tensor) final
void release_memory(luci_interpreter::Tensor &tensor) final
BuddyMemoryManager(uint8_t *memory_start, int32_t memSize)