Sources Pipelines Documentation

Allocators  
affix_allocator.ipp
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 <cstring>
10 
11 #include "affix_allocator.hpp"
12 
13 
14 namespace boost { namespace memory {
15 
16 template <typename type>
17 inline constexpr std::size_t size_of()
18 {
19  return sizeof(type);
20 }
21 
22 template <>
23 inline constexpr std::size_t size_of<void>()
24 {
25  return 0ul;
26 }
27 
28 template <typename type>
29 inline void construct_at(memory_block& block, std::size_t offset)
30 {
31  new (block + offset) type();
32 }
33 
34 template <>
35 inline void construct_at<void>(memory_block& block, std::size_t offset)
36 {
37 }
38 
39 template <typename type>
40 inline void destruct_at(memory_block& block, std::size_t offset)
41 {
42  reinterpret_cast<type*>(block + offset)->~type();
43 }
44 
45 template <>
46 inline void destruct_at<void>(memory_block& block, std::size_t offset)
47 {
48 }
49 
50 template <
51  typename affix_allocator_type>
52 inline affix_allocator_helper<
53  affix_allocator_type>::affix_allocator_helper(affix_allocator_type& instance)
54  :
55  instance_(instance)
56 {
57 }
58 
59 template <
60  typename affix_allocator_type>
61 inline auto& affix_allocator_helper<
62  affix_allocator_type>::get_allocator()
63 {
64  return instance_.allocator_;
65 }
66 
67 template <
68  typename allocator,
69  typename prefix,
70  typename suffix,
71  bool verify>
72 inline memory_block affix_allocator<
73  allocator,
74  prefix,
75  suffix,
76  verify>::allocate(std::size_t size)
77 {
78  auto block = allocator_.allocate(size_of_allocation(size));
79  if (block == null_block)
80  return block;
81 
82  construct_prefix(block);
83  construct_suffix(block);
84 
85  return offset_block(block, size);
86 }
87 
88 template <
89  typename allocator,
90  typename prefix,
91  typename suffix,
92  bool verify>
93 inline void affix_allocator<
94  allocator,
95  prefix,
96  suffix,
97  verify>::deallocate(memory_block& block)
98 {
99  auto original = original_block(block);
100  if (allocator_.owns(original))
101  {
102  if (verify)
103  {
104  verify_prefix(original);
105  verify_suffix(original);
106  }
107 
108  allocator_.deallocate(original);
109  block = original;
110  }
111 }
112 
113 template <
114  typename allocator,
115  typename prefix,
116  typename suffix,
117  bool verify>
118 inline bool affix_allocator<
119  allocator,
120  prefix,
121  suffix,
122  verify>::owns(memory_block& block)
123 {
124  auto original = original_block(block);
125  return allocator_.owns(original);
126 }
127 
128 template <
129  typename allocator,
130  typename prefix,
131  typename suffix,
132  bool verify>
133 inline std::size_t affix_allocator<
134  allocator,
135  prefix,
136  suffix,
137  verify>::size_of_allocation(std::size_t size) noexcept
138 {
139  return size + size_of<prefix>() + size_of<suffix>();
140 }
141 
142 template <
143  typename allocator,
144  typename prefix,
145  typename suffix,
146  bool verify>
148  allocator,
149  prefix,
150  suffix,
151  verify>::offset_block(memory_block& block, std::size_t size) noexcept
152 {
153  return { block + size_of<prefix>(), size };
154 }
155 
156 template <
157  typename allocator,
158  typename prefix,
159  typename suffix,
160  bool verify>
162  allocator,
163  prefix,
164  suffix,
165  verify>::original_block(memory_block& block) noexcept
166 {
167  return { block - size_of<prefix>(), block.size + size_of<prefix>() + size_of<suffix>() };
168 }
169 
170 template <
171  typename allocator,
172  typename prefix,
173  typename suffix,
174  bool verify>
175 inline void affix_allocator<
176  allocator,
177  prefix,
178  suffix,
179  verify>::construct_prefix(memory_block& block)
180 {
181  construct_at<prefix>(block, 0ul);
182 }
183 
184 template <
185  typename allocator,
186  typename prefix,
187  typename suffix,
188  bool verify>
189 inline void affix_allocator<
190  allocator,
191  prefix,
192  suffix,
193  verify>::construct_suffix(memory_block& block)
194 {
195  construct_at<suffix>(block, block.size - size_of<suffix>());
196 }
197 
198 template <
199  typename allocator,
200  typename prefix,
201  typename suffix,
202  bool verify>
203 inline void affix_allocator<
204  allocator,
205  prefix,
206  suffix,
207  verify>::verify_prefix(memory_block& block)
208 {
209  destruct_at<prefix>(block, 0ul);
210 }
211 
212 template <
213  typename allocator,
214  typename prefix,
215  typename suffix,
216  bool verify>
217 inline void affix_allocator<
218  allocator,
219  prefix,
220  suffix,
221  verify>::verify_suffix(memory_block& block)
222 {
223  destruct_at<suffix>(block, block.size - size_of<suffix>());
224 }
225 
226 } }
void deallocate(memory_block &block)
Deallocates the given memory_block
bool owns(memory_block &block)
Determines if the given memory_block is owned by this allocator.
memory_block allocate(std::size_t size)
Allocates a memory_block with configured affixes.
Allocator adding prefix and suffix objects in boundries of allocated block.
std::size_t size
Block&#39;s size.
Represents an allocated memory block.