Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
stl_allocator.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  template <typename T, class Allocator>
19 
20  template <class Allocator>
21  class stl_allocator<void, Allocator> {
22  public:
23  using pointer = void *;
24  using const_pointer = const void *;
25  using value_type = void;
26  template <class U> struct rebind {
28  };
29  };
30 
31  template <typename T, class Allocator>
32  class stl_allocator {
33  typename Allocator::value_type &allocator_;
34 
35  public:
36  using size_type = size_t;
37  using difference_type = ptrdiff_t;
38  using pointer = T *;
39  using const_pointer = const T *;
40  using reference = T &;
41  using const_reference = const T &;
42  using value_type = T;
43 
44  template <typename U> struct rebind {
46  };
47 
49  : allocator_(Allocator::instance())
50  {
51  }
53  : allocator_(Allocator::instance())
54  {
55  }
56 
57  template <typename U>
58  explicit stl_allocator(const stl_allocator<U, Allocator> &)
59  : allocator_(Allocator::instance())
60  {
61  }
62 
63  ~stl_allocator()
64  {
65  }
66 
67  pointer address(reference r) const
68  {
69  return &r;
70  };
71  const_pointer address(const_reference r) const
72  {
73  return &r;
74  };
75 
76  T *allocate(std::size_t n, const void * /*hint*/ = nullptr)
77  {
78  auto b = allocator_.allocate(n * sizeof(T));
79  if (b) {
80  auto p = allocator_.outer_to_prefix(b);
81  p->length = static_cast<unsigned>(b.length);
82  return static_cast<T *>(b.ptr);
83  }
84  throw std::bad_alloc();
85  }
86 
87  void deallocate(T *ptr, std::size_t n)
88  {
89  block pseudoBlock(ptr, n);
90  auto p = allocator_.outer_to_prefix(pseudoBlock);
91  block realBlock(ptr, p->length);
92  allocator_.deallocate(realBlock);
93  }
94 
95  size_t max_size() const
96  { // estimate maximum array size
97  return ((size_t)(-1) / sizeof(T));
98  }
99 
100  void construct(pointer ptr)
101  {
102  ::new (static_cast<void *>(ptr)) T;
103  };
104 
105  template <class U> void construct(pointer ptr, const U &val)
106  {
107  ::new (static_cast<void *>(ptr)) T(val);
108  };
109 
110  void construct(pointer ptr, const T &val)
111  {
112  ::new (static_cast<void *>(ptr)) T(val);
113  };
114 
115  void destroy(pointer p)
116  {
117  p->T::~T();
118  };
119  };
120 
121  template <class Allocator, typename T1, typename T2>
122  bool operator==(const stl_allocator<T1, Allocator> &, const stl_allocator<T2, Allocator> &)
123  {
124  return true;
125  }
126 
127  template <class Allocator, typename T1, typename T2>
128  bool operator!=(const stl_allocator<T1, Allocator> &, const stl_allocator<T2, Allocator> &)
129  {
130  return false;
131  }
132 }