Sources Pipelines Documentation

Allocators  
free_list.hpp
1 // Copyright (c) 2016 Lukasz Laszko
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #pragma once
6 
7 #include <boost/memory/memory_block.hpp>
8 
9 #include <iterator>
10 
11 
12 namespace boost { namespace memory {
13 
22 template <typename free_list_type>
23 class free_list_helper final
24 {
25 public:
26  class iterator
27  {
28  public:
29  using iterator_category = std::forward_iterator_tag;
30  using value_type = memory_block;
31  using difference_type = std::size_t;
32  using pointer = memory_block*;
33  using reference = memory_block&;
34 
35  iterator(typename free_list_type::node* current);
36 
37  bool operator==(const iterator& other);
38  bool operator!=(const iterator& other);
39  iterator& operator++();
40  iterator operator++(int step) const;
41  value_type operator*() const;
42 
43  private:
44  typename free_list_type::node* current_;
45  };
46 
47  static constexpr auto node_size = sizeof(typename free_list_type::node);
48 
49  free_list_helper(free_list_type& instance);
50 
51  auto& get_allocator() noexcept;
52  iterator begin();
53  iterator end();
54 
55 private:
56  free_list_type& instance_;
57 };
58 
78 template <
79  typename allocator,
80  std::size_t batch_size,
81  std::size_t min,
82  std::size_t max,
83  std::size_t capacity>
84 class free_list
85 {
86 public:
92  memory_block allocate(std::size_t size);
93 
102  void deallocate(memory_block& block);
103 
109  bool owns(memory_block& block);
110 
111 private:
113  friend free_list_helper<this_free_list_type>;
114 
115  struct node
116  {
117  memory_block block_;
118  node* next_;
119  };
120 
121  allocator allocator_;
122  node* head_ {nullptr};
123 
124  std::size_t allocations_count_ {0ul};
125 };
126 
127 } }
128 
129 #include "free_list.ipp"
Provides mechanism for reusing deallocated memory blocks.
Definition: free_list.hpp:84
Represents an allocated memory block.