QuantumLibrary
quantum_heap_allocator.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_HEAP_ALLOCATOR_H
17 #define BLOOMBERG_QUANTUM_HEAP_ALLOCATOR_H
18 
19 #include <quantum/quantum_contiguous_pool_manager.h>
20 
21 namespace Bloomberg {
22 namespace quantum {
23 
24 //==============================================================================
25 // struct HeapAllocator
26 //==============================================================================
32 template <typename T>
34 {
35  //------------------------------ Typedefs ----------------------------------
37  typedef T value_type;
38  typedef value_type* pointer;
39  typedef const value_type* const_pointer;
41  typedef const value_type& const_reference;
42  typedef size_t size_type;
43  typedef uint16_t index_type;
44  typedef std::ptrdiff_t difference_type;
46  typedef std::false_type propagate_on_container_copy_assignment;
47  typedef std::true_type propagate_on_container_swap;
48  typedef std::true_type is_always_equal;
49  typedef std::false_type default_constructor;
50  typedef std::aligned_storage<sizeof(value_type),
52  typedef typename storage_type::type aligned_type;
53 
54  template <typename U>
55  struct rebind
56  {
58  };
59  //------------------------------- Methods ----------------------------------
61  _size(size),
62  _buffer(new aligned_type[size])
63  {
64  if (!_buffer) {
65  throw std::bad_alloc();
66  }
67  this->setBuffer(_buffer, _size);
68  }
69  HeapAllocator(const this_type& other) :
70  HeapAllocator(other._size)
71  {}
73  {
74  *this = std::move(other);
75  }
77  {}
79  {
80  static_cast<ContiguousPoolManager<T>>(*this) = static_cast<ContiguousPoolManager<T>&&>(other);
81  _size = other._size;
82  _buffer = other._buffer;
83  other._size = 0;
84  other._buffer = nullptr;
85  }
87  delete[] _buffer;
88  }
90  return HeapAllocator(other.size());
91  }
92  template <typename U>
94  {}
95  template <typename U>
97  {}
98  bool operator==(const this_type&) const {
99  return true;
100  }
101  bool operator!=(const this_type&) const {
102  return false;
103  }
104  index_type size() const { return _size; }
105 
106 private:
107  //------------------------------- Members ----------------------------------
108  index_type _size;
109  aligned_type* _buffer;
110 };
111 
112 }} //namespaces
113 
114 #endif //BLOOMBERG_QUANTUM_HEAP_ALLOCATOR_H
value_type * pointer
Definition: quantum_heap_allocator.h:38
uint16_t index_type
Definition: quantum_contiguous_pool_manager.h:50
Provides fast (quasi zero-time) in-place allocation for STL containers. Objects are allocated from a ...
Definition: quantum_contiguous_pool_manager.h:40
Definition: quantum_buffer_impl.h:22
value_type & reference
Definition: quantum_heap_allocator.h:40
std::false_type default_constructor
Definition: quantum_heap_allocator.h:49
size_t size_type
Definition: quantum_heap_allocator.h:42
HeapAllocator(const HeapAllocator< U > &other)
Definition: quantum_heap_allocator.h:93
std::false_type propagate_on_container_copy_assignment
Definition: quantum_heap_allocator.h:46
HeapAllocator(this_type &&other)
Definition: quantum_heap_allocator.h:72
HeapAllocator(index_type size)
Definition: quantum_heap_allocator.h:60
bool operator!=(const this_type &) const
Definition: quantum_heap_allocator.h:101
index_type size() const
Definition: quantum_heap_allocator.h:104
std::aligned_storage< sizeof(value_type), alignof(value_type)> storage_type
Definition: quantum_heap_allocator.h:51
HeapAllocator< U > other
Definition: quantum_heap_allocator.h:57
Definition: quantum_heap_allocator.h:55
std::ptrdiff_t difference_type
Definition: quantum_heap_allocator.h:44
const value_type & const_reference
Definition: quantum_heap_allocator.h:41
T value_type
Definition: quantum_contiguous_pool_manager.h:44
std::true_type propagate_on_container_move_assignment
Definition: quantum_heap_allocator.h:45
T value_type
Definition: quantum_heap_allocator.h:37
void setBuffer(aligned_type *buffer, index_type size)
Definition: quantum_contiguous_pool_manager_impl.h:68
HeapAllocator & operator=(const this_type &)
Definition: quantum_heap_allocator.h:76
HeapAllocator & operator=(const HeapAllocator< U > &)
Definition: quantum_heap_allocator.h:96
bool operator==(const this_type &) const
Definition: quantum_heap_allocator.h:98
~HeapAllocator()
Definition: quantum_heap_allocator.h:86
static HeapAllocator select_on_container_copy_construction(const HeapAllocator &other)
Definition: quantum_heap_allocator.h:89
Provides a heap-based object pool to the underlying ContiguousPoolManager. The default buffer size is...
Definition: quantum_heap_allocator.h:33
uint16_t index_type
Definition: quantum_heap_allocator.h:43
storage_type::type aligned_type
Definition: quantum_heap_allocator.h:52
HeapAllocator & operator=(this_type &&other)
Definition: quantum_heap_allocator.h:78
const value_type * const_pointer
Definition: quantum_heap_allocator.h:39
std::true_type propagate_on_container_swap
Definition: quantum_heap_allocator.h:47
HeapAllocator(const this_type &other)
Definition: quantum_heap_allocator.h:69
HeapAllocator< T > this_type
Definition: quantum_heap_allocator.h:36
std::true_type is_always_equal
Definition: quantum_heap_allocator.h:48