Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
stl_allocator_adapter.hpp
1 //
3 // Copyright 2014 Felix Petriconi
4 //
5 // License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0
6 //
7 // Authors: http://petriconi.net, Felix Petriconi
8 //
10 #pragma once
11 
12 #include <cstddef>
13 #include "allocator_base.hpp"
14 
15 namespace alb {
16 
17  inline namespace v_100 {
18 
19  template <typename T, class Allocator>
21 
22  template <class Allocator>
23  class std_allocator_adapter<void, Allocator> {
24  public:
25  using pointer = void *;
26  using const_pointer = const void *;
27  using value_type = void;
28  template <class U> struct rebind {
30  };
31  };
32 
33  template <typename T, class Allocator>
34  class std_allocator_adapter {
35  const Allocator& allocator_;
36 
37  public:
38  using size_type = size_t;
39  using difference_type = ptrdiff_t;
40  using pointer = T *;
41  using const_pointer = const T *;
42  using reference = T &;
43  using const_reference = const T &;
44  using value_type = T;
45 
46  template <typename U> struct rebind {
48  };
49 
50  explicit std_allocator_adapter(const Allocator& allocator) noexcept
51  : allocator_(allocator)
52  {
53  }
54 
55  ~std_allocator_adapter() noexcept {}
56 
57  const Allocator& allocator() const
58  {
59  return allocator_;
60  }
61 
62  template <typename U>
63  explicit std_allocator_adapter(const std_allocator_adapter<U, Allocator> &other)
64  : allocator_(other.allocator())
65  {
66  }
67 
68  pointer address(reference r) const
69  {
70  return &r;
71  };
72 
73  const_pointer address(const_reference r) const
74  {
75  return &r;
76  };
77 
78  T *allocate(std::size_t n, const void * /*hint*/ = nullptr)
79  {
80  auto b = const_cast<Allocator&>(allocator_).allocate(n * sizeof(T));
81  if (b) {
82  auto p = allocator_.outer_to_prefix(b);
83  *p = b.length;
84  return static_cast<T *>(b.ptr);
85  }
86  return nullptr;
87  }
88 
89  void deallocate(T *ptr, std::size_t n)
90  {
91  block pseudoBlock(ptr, n);
92  auto p = allocator_.outer_to_prefix(pseudoBlock);
93  block realBlock(ptr, *p);
94  const_cast<Allocator&>(allocator_).deallocate(realBlock);
95  }
96 
97  size_t max_size() const
98  { // estimate maximum array size
99  return ((size_t)(-1) / sizeof(T));
100  }
101 
102  void construct(pointer ptr)
103  {
104  ::new (static_cast<void *>(ptr)) T;
105  };
106 
107  template <class... U>
108  void construct(pointer ptr, U&&... val)
109  {
110  ::new (static_cast<void *>(ptr)) T(std::forward<U>(val)...);
111  };
112 
113  void destroy(pointer p)
114  {
115  p->T::~T();
116  };
117  };
118 
119  template <class Allocator, typename T1, typename T2>
120  bool operator==(const std_allocator_adapter<T1, Allocator> &, const std_allocator_adapter<T2, Allocator> &)
121  {
122  return true;
123  }
124 
125  template <class Allocator, typename T1, typename T2>
126  bool operator!=(const std_allocator_adapter<T1, Allocator> &x, const std_allocator_adapter<T2, Allocator> &y)
127  {
128  return !(x == y);
129  }
130  }
131 
132  using namespace v_100;
133 }