Sources Pipelines Documentation

Allocators  
affix_allocator.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 #include <boost/memory/null_allocator.hpp>
9 
10 #include <type_traits>
11 
12 
13 namespace boost { namespace memory {
14 
15 //C++03, ยง14.7.3/2
16 // an explicit specialization can be only declared at the namespace level,
17 // never at a class member scope.
18 template <typename type>
19 constexpr std::size_t size_of();
20 
21 template <>
22 constexpr std::size_t size_of<void>();
23 
24 template <typename type>
25 void construct_at(memory_block& block, std::size_t offset);
26 
27 template <>
28 void construct_at<void>(memory_block& block, std::size_t offset);
29 
30 template <typename type>
31 void destruct_at(memory_block& block, std::size_t offset);
32 
33 template <>
34 void destruct_at<void>(memory_block& block, std::size_t offset);
35 
43 template <typename affix_allocator_type>
44 class affix_allocator_helper final
45 {
46 public:
47  affix_allocator_helper(affix_allocator_type& instance);
48 
49  auto& get_allocator();
50 
51 private:
52  affix_allocator_type& instance_;
53 };
54 
71 template <
72  typename allocator,
73  typename prefix,
74  typename suffix = void,
75  bool verify = false>
77 {
78 public:
79  static_assert(
80  std::is_default_constructible<prefix>::value || std::is_same<prefix, void>::value,
81  "prefix has to be default constructible or void");
82  static_assert(
83  std::is_default_constructible<suffix>::value || std::is_same<suffix, void>::value,
84  "suffix has to be default constructible or void");
85 
86  using allocator_type = allocator;
87  using prefix_type = prefix;
88  using suffix_type = suffix;
89 
102  memory_block allocate(std::size_t size);
103 
114  void deallocate(memory_block& block);
115 
126  bool owns(memory_block& block);
127 
128 private:
130  friend affix_allocator_helper<this_affix_allocator_type>;
131 
132  static std::size_t size_of_allocation(std::size_t size) noexcept;
133  static memory_block offset_block(memory_block& block, std::size_t size) noexcept;
134  static memory_block original_block(memory_block& block) noexcept;
135  static void construct_prefix(memory_block& block);
136  static void construct_suffix(memory_block& block);
137  static void verify_prefix(memory_block& block);
138  static void verify_suffix(memory_block& block);
139 
140  allocator allocator_;
141 };
142 
143 } }
144 
145 #include "affix_allocator.ipp"
146 
Allocator adding prefix and suffix objects in boundries of allocated block.
Represents an allocated memory block.