Tervel  1.0.0
A collection of wait-free containers and algorithms.
descriptor_pool.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2015 University of Central Florida's Computer Software Engineering
5 Scalable & Secure Systems (CSE - S3) Lab
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 #ifndef TERVEL_MEMORY_RC_DESCRIPTOR_POOL_H_
26 #define TERVEL_MEMORY_RC_DESCRIPTOR_POOL_H_
27 
28 #include <tervel/util/info.h>
29 #include <tervel/util/util.h>
30 #include <tervel/util/system.h>
31 #include <tervel/util/descriptor.h>
34 
35 namespace tervel {
36 namespace util {
37 namespace memory {
38 namespace rc {
39 
40 class PoolManager;
58  public:
59  DescriptorPool(PoolManager *manager, uint64_t pool_id, int prefill = TERVEL_MEM_RC_MIN_NODES)
60  : manager_(manager)
61  , pool_id_(pool_id)
62  , safe_pool_{nullptr}
63  , unsafe_pool_{nullptr}
64  , safe_pool_count_(0)
65  , unsafe_pool_count_(0) {
66  this->reserve(prefill);
67  }
69  this->send_unsafe_to_manager();
70  this->send_safe_to_manager();
71  }
72 
76  void reserve(size_t num_descriptors = TERVEL_MEM_RC_MIN_NODES);
77 
83  template<typename DescrType, typename... Args>
84  DescrType * get_descriptor(Args&&... args);
85 
96  void free_descriptor(tervel::util::Descriptor *descr, bool dont_check=false);
97 
98 
99  private:
100  // -------------------------
101  // FOR DEALING WITH ELEMENTS
102  // -------------------------
103 
112  PoolElement * get_from_pool(bool allocate_new = true);
113 
114 
115 
116  // -------------------------
117  // FOR DEALING WITH MANAGERS
118  // -------------------------
119 
124  void send_safe_to_manager();
125 
130  void send_unsafe_to_manager();
131 
135  void offload();
136 
137  // --------------------------------
138  // DEALS WITH SAFE AND UNSAFE LISTS
139  // --------------------------------
140 
150 
159 
163  void try_clear_unsafe_pool(bool dont_check = false);
164 
167  bool verify_pool_count(PoolElement *pool, uint64_t count);
168  // -------
169  // MEMBERS
170  // -------
171 
176 
180  uint64_t pool_id_;
181 
189 
197 
202  uint64_t safe_pool_count_ {0};
203  uint64_t unsafe_pool_count_ {0};
204 
206 };
207 
208 // IMPLEMENTATIONS
209 // ===============
210 
211 template<typename DescrType, typename... Args>
212 DescrType * DescriptorPool::get_descriptor(Args&&... args) {
213  PoolElement *elem = this->get_from_pool();
214  if (elem == nullptr) {
215  return nullptr;
216  } else {
217  elem->init_descriptor<DescrType>(std::forward<Args>(args)...);
218  DescrType * descr = reinterpret_cast<DescrType *>(elem->descriptor());
219  return descr;
220  }
221 }
222 
223 } // namespace rc
224 } // namespace memory
225 } // namespace util
226 } // namespace tervel
227 
228 #endif // TERVEL_MEMORY_RC_DESCRIPTOR_POOL_H_
229 
uint64_t safe_pool_count_
Two counters used to track the number of elements in the linked list.
Definition: descriptor_pool.h:202
A manager class for the reference count protected memory pools.
Definition: pool_manager.h:54
Descriptor * descriptor()
Returns a pointer to the associated descriptor of this element.
Definition: pool_element.h:83
TODO(steven):
Definition: mcas.h:36
uint64_t unsafe_pool_count_
Definition: descriptor_pool.h:203
PoolElement * unsafe_pool_
A linked list of pool elements.
Definition: descriptor_pool.h:196
uint64_t pool_id_
The pool where excess elements are placed.
Definition: descriptor_pool.h:180
void free_descriptor(tervel::util::Descriptor *descr, bool dont_check=false)
Once a user is done with a descriptor, they should free it with this method.
bool verify_pool_count(PoolElement *pool, uint64_t count)
verifies that the length of the linked list matches the count
void send_unsafe_to_manager()
Sends the elements from the unsafe pool to the corresponding unsafe pool in this pool's manager...
PoolElement * get_from_pool(bool allocate_new=true)
Gets a free element.
PoolElement * safe_pool_
A linked list of pool elements.
Definition: descriptor_pool.h:188
DescriptorPool(PoolManager *manager, uint64_t pool_id, int prefill=TERVEL_MEM_RC_MIN_NODES)
Definition: descriptor_pool.h:59
This defines the Descriptor class, this class is designed to be extend and be used in conjunction wit...
Definition: descriptor.h:60
void reserve(size_t num_descriptors=TERVEL_MEM_RC_MIN_NODES)
Allocates an extra num_descriptors elements to the pool.
#define TERVEL_MEM_RC_MIN_NODES
Definition: util.h:80
void offload()
Sends a subset of the elements to the managers pool.
This class is used to hold the memory management information (Header) and a descriptor object...
Definition: pool_element.h:46
DescrType * get_descriptor(Args &&...args)
Constructs and returns a descriptor.
Definition: descriptor_pool.h:212
void init_descriptor(Args &&...args)
Constructs a descriptor of the given type within this pool element.
Definition: pool_element.h:147
void add_to_unsafe(tervel::util::Descriptor *descr)
Releases the descriptor back to the unsafe pool.
PoolManager * manager_
This pool's manager.
Definition: descriptor_pool.h:175
Defines a pool of descriptor objects which is used to allocate descriptors and to store them while th...
Definition: descriptor_pool.h:57
~DescriptorPool()
Definition: descriptor_pool.h:68
void add_to_safe(tervel::util::Descriptor *descr)
Releases the descriptor back to the safe pool.
void try_clear_unsafe_pool(bool dont_check=false)
Try to move elements from the unsafe pool to the safe pool.
void send_safe_to_manager()
Sends the elements from the safe pool to the corresponding safe pool in this pool's manager...