Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
segregator.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 "allocator_base.hpp"
13 #include "internal/reallocator.hpp"
14 
15 namespace alb {
16  inline namespace v_100 {
26  template <size_t Threshold, class SmallAllocator, class LargeAllocator>
27  class segregator : private SmallAllocator, private LargeAllocator
28  {
30  "Small- and Large-Allocator cannot be both of base!");
31 
32  public:
33  using small_allocator = SmallAllocator;
34  using large_allocator = LargeAllocator;
35 
36  static constexpr size_t threshold = Threshold;
37 
38  static constexpr bool supports_truncated_deallocation =
39  SmallAllocator::supports_truncated_deallocation &&
40  LargeAllocator::supports_truncated_deallocation;
41 
42  static constexpr unsigned alignment =
43  (SmallAllocator::alignment > LargeAllocator::alignment) ?
44  SmallAllocator::alignment : LargeAllocator::alignment;
45 
53  block allocate(size_t n) noexcept {
54  block result;
55  if (n <= Threshold) {
56  result = SmallAllocator::allocate(n);
57 
58  }
59  else {
60  result = LargeAllocator::allocate(n);
61  }
62 
63  return result;
64  }
65 
70  void deallocate(block &b) noexcept {
71  if (!b) {
72  return;
73  }
74 
75  if (b.length <= Threshold) {
76  return SmallAllocator::deallocate(b);
77  }
78  return LargeAllocator::deallocate(b);
79  }
80 
90  bool reallocate(block &b, size_t n) noexcept {
91  if (internal::is_reallocation_handled_default(*this, b, n)) {
92  return true;
93  }
94 
95  if (b.length <= Threshold) {
96  if (n <= Threshold) {
97  return SmallAllocator::reallocate(b, n);
98  }
99  return internal::reallocate_with_copy(*this, static_cast<LargeAllocator &>(*this), b, n);
100  }
101 
102  if (n <= Threshold) {
103  return internal::reallocate_with_copy(*this, static_cast<SmallAllocator &>(*this), b, n);
104  }
105  return LargeAllocator::reallocate(b, n);
106  }
107 
115  template <typename U = SmallAllocator, typename V = LargeAllocator>
116  typename std::enable_if<traits::has_expand<SmallAllocator>::value ||
118  expand(block &b, size_t delta) noexcept {
119 
120  if (b.length <= Threshold && b.length + delta > Threshold) {
121  return false;
122  }
123  if (b.length <= Threshold) {
125  return traits::Expander<U>::do_it(static_cast<U&>(*this), b,
126  delta);
127  }
128  return false;
129  }
131  return traits::Expander<V>::do_it(static_cast<V&>(*this), b,
132  delta);
133  }
134  return false;
135  }
136 
143  template <typename U = SmallAllocator, typename V = LargeAllocator>
144  typename std::enable_if<traits::has_expand<SmallAllocator>::value ||
146  owns(const block &b) const noexcept {
147 
148  if (b.length <= Threshold) {
149  return U::owns(b);
150  }
151  return V::owns(b);
152  }
153 
158  template <typename U = SmallAllocator, typename V = LargeAllocator>
159  typename std::enable_if<traits::has_expand<SmallAllocator>::value ||
161  deallocate_all() noexcept {
162  traits::AllDeallocator<U>::do_it(static_cast<U&>(*this));
163  traits::AllDeallocator<V>::do_it(static_cast<V&>(*this));
164  }
165  };
166  }
167  using namespace v_100;
168 }
bool reallocate(block &b, size_t n) noexcept
Definition: segregator.hpp:90
void deallocate(block &b) noexcept
Definition: segregator.hpp:70
std::enable_if< traits::has_expand< SmallAllocator >::value||traits::has_expand< LargeAllocator >::value, bool >::type expand(block &b, size_t delta) noexcept
Definition: segregator.hpp:118
bool reallocate_with_copy(OldAllocator &oldAllocator, NewAllocator &newAllocator, block &b, size_t n) noexcept
Definition: reallocator.hpp:35
std::enable_if< traits::has_expand< SmallAllocator >::value||traits::has_expand< LargeAllocator >::value, bool >::type owns(const block &b) const noexcept
Definition: segregator.hpp:146
std::enable_if< traits::has_expand< SmallAllocator >::value||traits::has_expand< LargeAllocator >::value, void >::type deallocate_all() noexcept
Definition: segregator.hpp:161
block allocate(size_t n) noexcept
Definition: segregator.hpp:53