Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
reallocator.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 "traits.hpp"
13 #include <type_traits>
14 
15 namespace alb {
16  inline namespace v_100 {
17  namespace internal {
18 
34  template <class OldAllocator, class NewAllocator>
35  bool reallocate_with_copy(OldAllocator &oldAllocator, NewAllocator &newAllocator, block &b,
36  size_t n) noexcept
37  {
38  auto newBlock = newAllocator.allocate(n);
39  if (!newBlock) {
40  return false;
41  }
42  block_copy(b, newBlock);
43  oldAllocator.deallocate(b);
44  b = newBlock;
45  return true;
46  }
47 
57  template <class Allocator, typename Enabled = void> struct reallocator;
58 
65  template <class Allocator>
66  struct reallocator<Allocator,
67  typename std::enable_if<traits::has_expand<Allocator>::value>::type> {
68  static bool is_handled_default(Allocator &allocator, block &b, size_t n) noexcept
69  {
70  if (b.length == n) {
71  return true;
72  }
73  if (n == 0) {
74  allocator.deallocate(b);
75  return true;
76  }
77  if (!b) {
78  b = allocator.allocate(n);
79  return true;
80  }
81  if (n > b.length) {
82  if (allocator.expand(b, n - b.length)) {
83  return true;
84  }
85  }
86  return false;
87  }
88  };
89 
96  template <class Allocator>
97  struct reallocator<Allocator,
98  typename std::enable_if<!traits::has_expand<Allocator>::value>::type> {
99 
100  static bool is_handled_default(Allocator &allocator, block &b, size_t n) noexcept
101  {
102  if (b.length == n) {
103  return true;
104  }
105  if (n == 0) {
106  allocator.deallocate(b);
107  return true;
108  }
109  if (!b) {
110  b = allocator.allocate(n);
111  return true;
112  }
113  return false;
114  }
115  };
116 
117  template <typename Allocator>
118  bool is_reallocation_handled_default(Allocator& allocator, block& b, size_t n) noexcept
119  {
120  return reallocator<Allocator>::is_handled_default(allocator, b, n);
121  }
122  } // namespace Helper
123  }
124  using namespace v_100;
125 }
void block_copy(const block &source, block &destination) noexcept
size_t length
This describes the length of the reserved bytes.
bool reallocate_with_copy(OldAllocator &oldAllocator, NewAllocator &newAllocator, block &b, size_t n) noexcept
Definition: reallocator.hpp:35