Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
fallback_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 "allocator_base.hpp"
13 #include "internal/reallocator.hpp"
14 
15 namespace alb {
16  inline namespace v_100 {
25  template <class Primary, class Fallback>
26  class fallback_allocator : public Primary, public Fallback
27  {
28  using primary = Primary;
29  using fallback = Fallback;
30 
32  "Primary- and Fallback-Allocator cannot be both of the same base!");
33 
34  public:
35  static constexpr bool supports_truncated_deallocation = Primary::supports_truncated_deallocation ||
36  Fallback::supports_truncated_deallocation;
37 
38  static constexpr unsigned alignment = (Primary::alignment > Fallback::alignment) ?
39  Primary::alignment : Fallback::alignment;
40 
46  block allocate(size_t n) noexcept {
47  block result;
48  if (n == 0) {
49  return result;
50  }
51  result = Primary::allocate(n);
52  if (!result)
53  result = Fallback::allocate(n);
54 
55  return result;
56  }
57 
62  void deallocate(block &b) noexcept {
63  if (!b) {
64  return;
65  }
66 
67  if (Primary::owns(b))
68  Primary::deallocate(b);
69  else
70  Fallback::deallocate(b);
71  }
72 
80  bool reallocate(block &b, size_t n) noexcept {
81  if (Primary::owns(b)) {
82  if (internal::is_reallocation_handled_default(static_cast<Primary &>(*this), b, n)) {
83  return true;
84  }
85  }
86  else {
87  if (internal::is_reallocation_handled_default(static_cast<Fallback &>(*this), b,
88  n)) {
89  return true;
90  }
91  }
92 
93  if (Primary::owns(b)) {
94  if (Primary::reallocate(b, n)) {
95  return true;
96  }
97  return internal::reallocate_with_copy(static_cast<Primary &>(*this),
98  static_cast<Fallback &>(*this), b, n);
99  }
100 
101  return Fallback::reallocate(b, n);
102  }
103 
112  template <typename U = Primary, typename V = Fallback>
113  typename std::enable_if<traits::has_expand<U>::value || traits::has_expand<V>::value, bool>::type
114  expand(block &b, size_t delta) noexcept {
115  if (Primary::owns(b)) {
117  return traits::Expander<U>::do_it(static_cast<U&>(*this), b, delta);
118  }
119  return false;
120  }
122  return traits::Expander<V>::do_it(static_cast<V&>(*this), b, delta);
123  }
124  return false;
125  }
126 
133  template <typename U = Primary, typename V = Fallback>
134  typename std::enable_if<traits::has_owns<U>::value && traits::has_owns<V>::value, bool>::type
135  owns(const block &b) const noexcept {
136  return Primary::owns(b) || Fallback::owns(b);
137  }
138 
139  template <typename U = Primary, typename V = Fallback>
140  typename std::enable_if<traits::has_deallocate_all<U>::value &&
142  deallocate_all() noexcept {
143  Primary::deallocate_all();
144  Fallback::deallocate_all();
145  }
146  };
147  }
148 
149  using namespace v_100;
150 }
std::enable_if< traits::has_owns< U >::value &&traits::has_owns< V >::value, bool >::type owns(const block &b) const noexcept
bool reallocate(block &b, size_t n) noexcept
std::enable_if< traits::has_expand< U >::value||traits::has_expand< V >::value, bool >::type expand(block &b, size_t delta) noexcept
void deallocate(block &b) noexcept
bool reallocate_with_copy(OldAllocator &oldAllocator, NewAllocator &newAllocator, block &b, size_t n) noexcept
Definition: reallocator.hpp:35
block allocate(size_t n) noexcept