QuantumLibrary
quantum_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_ALLOCATOR_H
17 #define BLOOMBERG_QUANTUM_ALLOCATOR_H
18 
19 #include <quantum/impl/quantum_stl_impl.h>
20 #include <quantum/quantum_allocator_traits.h>
21 #include <quantum/quantum_stack_allocator.h>
22 #include <quantum/quantum_heap_allocator.h>
23 #include <quantum/quantum_coroutine_pool_allocator.h>
24 #include <boost/context/stack_traits.hpp>
25 #include <boost/coroutine2/pooled_fixedsize_stack.hpp>
26 #include <boost/coroutine2/fixedsize_stack.hpp>
27 #include <memory>
28 
29 namespace Bloomberg {
30 namespace quantum {
31 
32 //==============================================================================================
33 // struct StlAllocator
34 //==============================================================================================
35 template <typename T>
36 struct StlAllocator : public std::allocator<T>
37 {
38  typedef std::true_type default_constructor;
39 };
40 
41 //==============================================================================================
42 // struct BoostAllocator
43 //==============================================================================================
44 template <typename Traits>
45 struct BoostAllocator : public boost::context::basic_fixedsize_stack<Traits>
46 {
47  typedef std::true_type default_constructor;
48 };
49 
50 //==============================================================================================
51 // struct Allocator (singleton)
52 //==============================================================================================
53 template <typename AllocType>
54 struct Allocator {
55  template <typename A = AllocType>
56  static AllocType& instance(std::enable_if_t<!A::default_constructor::value, uint16_t> size) {
57  static AllocType allocator(size);
58  return allocator;
59  }
60  template <typename A = AllocType>
61  static AllocType& instance(std::enable_if_t<A::default_constructor::value, uint16_t> = 0) {
62  static AllocType allocator;
63  return allocator;
64  }
65 };
66 
67 }
68 }
69 
70 #endif //BLOOMBERG_QUANTUM_ALLOCATOR_H
static AllocType & instance(std::enable_if_t<!A::default_constructor::value, uint16_t > size)
Definition: quantum_allocator.h:56
Definition: quantum_buffer_impl.h:22
static AllocType & instance(std::enable_if_t< A::default_constructor::value, uint16_t >=0)
Definition: quantum_allocator.h:61
Definition: quantum_allocator.h:36
Definition: quantum_allocator.h:45
Definition: quantum_allocator.h:54
std::true_type default_constructor
Definition: quantum_allocator.h:38
std::true_type default_constructor
Definition: quantum_allocator.h:47