QuantumLibrary
quantum_contiguous_pool_manager.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_POOL_MANAGER_H
17 #define BLOOMBERG_QUANTUM_POOL_MANAGER_H
18 
19 #include <memory>
20 #include <assert.h>
21 #include <limits>
22 #include <type_traits>
23 #include <utility>
24 #include <quantum/quantum_spinlock.h>
25 
26 namespace Bloomberg {
27 namespace quantum {
28 
29 //==============================================================================
30 // struct ContiguousPoolManager
31 //==============================================================================
39 template <typename T>
41 {
42  //------------------------------ Typedefs ----------------------------------
44  typedef T value_type;
45  typedef value_type* pointer;
46  typedef const value_type* const_pointer;
48  typedef const value_type& const_reference;
49  typedef size_t size_type;
50  typedef uint16_t index_type;
51  typedef std::aligned_storage<sizeof(T), alignof(T)> storage_type;
52  typedef typename storage_type::type aligned_type;
53 
54  //------------------------------- Methods ----------------------------------
57  ContiguousPoolManager(const this_type&) = delete;
59  ContiguousPoolManager& operator=(const this_type&) = delete;
61  virtual ~ContiguousPoolManager();
62 
63  // Accessors
64  void setBuffer(aligned_type* buffer, index_type size);
65  pointer address(reference x) const;
67  size_type max_size() const;
68  template <typename... Args >
69  void construct(T* p, Args&&... args);
70  void destroy(pointer p);
72  void deallocate(pointer p, size_type = 1);
73  template <typename... Args >
74  pointer create(Args&&... args);
75  void dispose(pointer p);
76  size_t allocatedBlocks() const;
77  size_t allocatedHeapBlocks() const;
78  bool isFull() const;
79  bool isEmpty() const;
80 
81 private:
82  pointer bufferStart();
83  pointer bufferEnd();
84  bool isManaged(pointer p);
85  index_type blockIndex(pointer p);
86  bool findContiguous(index_type n);
87 
88  //------------------------------- Members ----------------------------------
89  index_type _size{0};
90  aligned_type* _buffer{nullptr}; //non-owning
91  index_type* _freeBlocks{nullptr};
92  ssize_t _freeBlockIndex{-1};
93  size_t _numHeapAllocatedBlocks{0};
94  mutable SpinLock _spinlock;
95 };
96 
97 }} //namespaces
98 
99 #include <quantum/impl/quantum_contiguous_pool_manager_impl.h>
100 
101 #endif //BLOOMBERG_QUANTUM_POOL_MANAGER_H
102 
virtual ~ContiguousPoolManager()
Definition: quantum_contiguous_pool_manager_impl.h:62
const value_type * const_pointer
Definition: quantum_contiguous_pool_manager.h:46
uint16_t index_type
Definition: quantum_contiguous_pool_manager.h:50
ContiguousPoolManager< T > this_type
Definition: quantum_contiguous_pool_manager.h:43
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
size_t allocatedBlocks() const
Definition: quantum_contiguous_pool_manager_impl.h:180
bool isFull() const
Definition: quantum_contiguous_pool_manager_impl.h:192
size_t allocatedHeapBlocks() const
Definition: quantum_contiguous_pool_manager_impl.h:186
void dispose(pointer p)
Definition: quantum_contiguous_pool_manager_impl.h:173
ContiguousPoolManager & operator=(const this_type &)=delete
value_type & reference
Definition: quantum_contiguous_pool_manager.h:47
value_type * pointer
Definition: quantum_contiguous_pool_manager.h:45
void construct(T *p, Args &&... args)
Definition: quantum_contiguous_pool_manager_impl.h:109
T value_type
Definition: quantum_contiguous_pool_manager.h:44
const value_type & const_reference
Definition: quantum_contiguous_pool_manager.h:48
void deallocate(pointer p, size_type=1)
Definition: quantum_contiguous_pool_manager_impl.h:142
void setBuffer(aligned_type *buffer, index_type size)
Definition: quantum_contiguous_pool_manager_impl.h:68
bool isEmpty() const
Definition: quantum_contiguous_pool_manager_impl.h:198
pointer create(Args &&... args)
Definition: quantum_contiguous_pool_manager_impl.h:165
pointer allocate(size_type=1, const_pointer=0)
Definition: quantum_contiguous_pool_manager_impl.h:125
size_type max_size() const
Definition: quantum_contiguous_pool_manager_impl.h:102
pointer address(reference x) const
Definition: quantum_contiguous_pool_manager_impl.h:90
std::aligned_storage< sizeof(T), alignof(T)> storage_type
Definition: quantum_contiguous_pool_manager.h:51
ContiguousPoolManager()
Definition: quantum_contiguous_pool_manager_impl.h:28
storage_type::type aligned_type
Definition: quantum_contiguous_pool_manager.h:52
void destroy(pointer p)
Definition: quantum_contiguous_pool_manager_impl.h:115
size_t size_type
Definition: quantum_contiguous_pool_manager.h:49