Sources Pipelines Documentation

Allocators  
bitmapped_block.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/alignment.hpp>
8 #include <boost/memory/null_allocator.hpp>
9 #include <boost/memory/memory_block.hpp>
10 
11 
12 namespace boost { namespace memory {
13 
14 
20 template <typename bitmapped_block_type>
21 class bitmapped_block_helper final
22 {
23 public:
24  bitmapped_block_helper(bitmapped_block_type& instance) noexcept;
25 
26  auto& get_allocator() noexcept;
27  auto& get_bitmap() noexcept;
28  //auto& get_block(std::size_t index);
29 
30 private:
31  bitmapped_block_type& instance_;
32 };
33 
34 template <typename bitmap_type>
35 class bitmap_helper
36 {
37 public:
38  bitmap_helper(bitmap_type& instance) noexcept;
39 
40  std::uint8_t& get_flag(std::size_t index);
41  constexpr std::size_t flags_count() const noexcept;
42 
43 private:
44  bitmap_type& instance_;
45 };
46 
59 template <
60  typename allocator,
61  std::size_t min,
62  std::size_t max,
63  std::size_t capacity,
64  std::size_t alignment=NO_ALIGNMENT>
66 {
67 public:
68  static_assert(is_power_of_2(capacity), "capacity has to be a power of 2!");
69  static_assert(alignment > 0, "alignment has to be equal or greater than 1!");
70 
74  template <std::size_t size>
75  class bitmap
76  {
77  public:
78  void set(std::size_t index);
79  void reset(std::size_t index);
80  void reset_all();
81  bool is_set(std::size_t index);
82 
83  bool claim(std::size_t& index);
84 
85  private:
86  using bitmap_type = bitmap<size>;
87  friend bitmap_helper<bitmap_type>;
88 
89  static constexpr std::uint8_t all_mask = 0b11111111;
90  static constexpr std::uint8_t flag_mask(std::uint8_t index)
91  {
92  std::uint8_t flag_masks[8] = {
93  0b10000000,
94  0b01000000,
95  0b00100000,
96  0b00010000,
97  0b00001000,
98  0b00000100,
99  0b00000010,
100  0b00000001
101  };
102 
103  return flag_masks[index];
104  };
105 
106  std::uint8_t flags_[size] {};
107  };
108 
109  using allocator_type = allocator;
110  using bitmap_type = bitmap<(capacity % 8 == 0 ? capacity / 8 : capacity / 8 + 1)>;
111 
112  static const std::size_t min_size = min;
113  static const std::size_t max_size = max;
114  static const std::size_t aligned_max_size = align_size<alignment>(max);
115  static const std::size_t blocks_count = capacity;
116 
117  memory_block allocate(std::size_t size);
118  void deallocate(memory_block& block);
119 
120  bool owns(memory_block& block);
121 
122 private:
124  friend bitmapped_block_helper<this_bitmapped_block>;
125 
126  allocator allocator_;
127 
128  memory_block allocated_block_ { nullptr, 0ul };
129  bitmap_type* bitmap_ { nullptr };
130 };
131 
132 } }
133 
134 #include "bitmapped_block.ipp"
A continous memory block divided into equal number of sub-block with a bitmap indicating availability...
Represents an allocated memory block.