7 #include "memory_block.hpp" 8 #include "free_list.hpp" 13 namespace boost {
namespace memory {
15 template <
typename free_list_type>
16 inline free_list_helper<free_list_type>::free_list_helper(free_list_type& instance)
22 template <
typename free_list_type>
23 inline auto& free_list_helper<free_list_type>::get_allocator() noexcept
25 return instance_.allocator_;
28 template <
typename free_list_type>
29 inline typename free_list_helper<free_list_type>::iterator free_list_helper<free_list_type>::begin()
31 return iterator(instance_.head_);
34 template <
typename free_list_type>
35 inline typename free_list_helper<free_list_type>::iterator free_list_helper<free_list_type>::end()
37 return iterator(
nullptr);
40 template <
typename free_list_type>
41 inline free_list_helper<free_list_type>::iterator::iterator(
typename free_list_type::node* current)
47 template <
typename free_list_type>
48 inline bool free_list_helper<free_list_type>::iterator::operator ==(
const free_list_helper<free_list_type>::iterator& other)
50 return current_ == other.current_;
53 template <
typename free_list_type>
54 inline bool free_list_helper<free_list_type>::iterator::operator !=(
const free_list_helper<free_list_type>::iterator& other)
56 return current_ != other.current_;
59 template <
typename free_list_type>
60 inline typename free_list_helper<free_list_type>::iterator& free_list_helper<free_list_type>::iterator::operator ++()
62 current_ = current_ !=
nullptr ? current_->next_ :
nullptr;
66 template <
typename free_list_type>
67 inline typename free_list_helper<free_list_type>::iterator free_list_helper<free_list_type>::iterator::operator ++(
int step)
const 69 iterator advanced(current_);
70 for (
auto i = 0; i < step && advanced.current_ !=
nullptr; i++)
76 template <
typename free_list_type>
77 inline memory_block free_list_helper<free_list_type>::iterator::operator *()
const 79 return current_->block_;
84 std::size_t batch_size,
88 inline memory_block free_list<
95 if (min <= size && size <= max)
99 auto block = head_->block_;
100 head_ = head_->next_;
108 for (
auto i = 0ul; i < batch_size; i++)
110 if (allocations_count_++ == capacity)
113 auto node_with_block = allocator_.allocate(
sizeof(node) + max);
114 auto current_node =
reinterpret_cast<node*
>(node_with_block.address);
115 current_node->block_ = { node_with_block +
sizeof(node), max };
119 first = current_node->block_;
123 current_node->next_ = head_;
124 head_ = current_node;
133 return {
nullptr, 0ul };
139 std::size_t batch_size,
142 std::size_t capacity>
152 auto current_node =
reinterpret_cast<node*
>(
153 reinterpret_cast<std::uint8_t*
>(block.
address) -
sizeof(node));
154 current_node->next_ = head_;
155 head_ = current_node;
163 std::size_t batch_size,
166 std::size_t capacity>
174 if (min <= block.
size && block.
size <= max)
176 return allocator_.owns(block);
void deallocate(memory_block &block)
Deallocates the given memory_block.
bool owns(memory_block &block)
Determines if the list owns the block.
Provides mechanism for reusing deallocated memory blocks.
memory_block allocate(std::size_t size)
Allocates a memory_block from either the list or using the underlying allocator.
std::size_t size
Block's size.
Represents an allocated memory block.