Tervel  1.0.0
A collection of wait-free containers and algorithms.
pool_element.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_UTIL_MEMORY_RC_POOL_ELEMENT_H_
26 #define TERVEL_UTIL_MEMORY_RC_POOL_ELEMENT_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>
32 
33 namespace tervel {
34 namespace util {
35 namespace memory {
36 namespace rc {
37 
38 const long DEBUG_EXPECTED_STAMP = 0xDEADBEEF;
39 
46 class PoolElement {
47  public:
52  struct Header {
54  std::atomic<int64_t> ref_count {0};
55 
56 #ifdef DEBUG_POOL
57  std::atomic<bool> descriptor_in_use {false};
58  std::atomic<uint64_t> allocation_count {0};
59  std::atomic<uint64_t> free_count {0};
60  long debug_pool_stamp = DEBUG_EXPECTED_STAMP;
61 #endif
62  };
63 
64  explicit PoolElement(PoolElement *next=nullptr) {
65  this->header().next = next;
66  assert(this->header().ref_count.load() == 0);
67  }
68 
70  assert(false && "PoolElement should never be deleted, return it to Tervel please");
71  }
72 
83  Descriptor * descriptor() { return reinterpret_cast<Descriptor*>(this); }
84 
90  Header & header() { return header_; }
91 
95  PoolElement * next() { return header().next; }
96 
101 
110  template<typename DescrType, typename... Args>
111  void init_descriptor(Args&&... args);
112 
118  void cleanup_descriptor();
119  private:
120  char padding_[CACHE_LINE_SIZE - sizeof(Header)];
122 
124 };
125 static_assert(sizeof(PoolElement) == CACHE_LINE_SIZE,
126  "Pool elements should be cache-aligned. Padding calculation is probably"
127  " wrong.");
128 
142 
143 
144 // IMPLEMENTATIONS
145 // ===============
146 template<typename DescrType, typename... Args>
147 void PoolElement::init_descriptor(Args&&... args) {
148  static_assert(sizeof(DescrType) <= sizeof(padding_),
149  "Descriptor is too large to use in a pool element");
150 #ifdef DEBUG_POOL
151  this->header().descriptor_in_use.store(true);
152 #endif
153  new(descriptor()) DescrType(std::forward<Args>(args)...);
154 }
155 
156 
158 #ifdef DEBUG_POOL
159  assert(this->header().descriptor_in_use.load());
160  this->header().descriptor_in_use.store(false);
161 #endif
162  this->descriptor()->~Descriptor();
163 }
164 
165 
167  PoolElement *elem = reinterpret_cast<PoolElement *>(descr);
168 #ifdef DEBUG_POOL
169  assert(elem->header().debug_pool_stamp == DEBUG_EXPECTED_STAMP &&
170  "Tried to get a PoolElement from a descriptor which does not have an "
171  "associated one. This probably means the user is attempting to free the "
172  "descriptor through a DescriptorPool but the descriptor wasn't allocated "
173  "through a DescriptorPool to begin with.");
174 #endif
175  return reinterpret_cast<PoolElement *>(elem);
176 }
177 
178 
179 } // namespace rc
180 } // namespace memory
181 } // namespace util
182 } // namespace tervel
183 
184 #endif // TERVEL_UTIL_MEMORY_RC_POOL_ELEMENT_H_
void next(PoolElement *next)
Helper method for setting the next pointer.
Definition: pool_element.h:100
PoolElement(PoolElement *next=nullptr)
Definition: pool_element.h:64
PoolElement * next
Definition: pool_element.h:53
PoolElement * get_elem_from_descriptor(tervel::util::Descriptor *descr)
If the given descriptor was allocated through a DescriptorPool, then it has an associated PoolElement...
Definition: pool_element.h:166
void cleanup_descriptor()
Should be called by the owner of this element when the descriptor in this element is no longer needed...
Definition: pool_element.h:157
Descriptor * descriptor()
Returns a pointer to the associated descriptor of this element.
Definition: pool_element.h:83
TODO(steven):
Definition: mcas.h:36
Header & header()
A reference to the header which houses all the special info.
Definition: pool_element.h:90
All the member variables of PoolElement are stored in a struct so that the left over memory for cache...
Definition: pool_element.h:52
This defines the Descriptor class, this class is designed to be extend and be used in conjunction wit...
Definition: descriptor.h:60
#define CACHE_LINE_SIZE
Definition: system.h:41
PoolElement * next()
Helper method for getting the next pointer.
Definition: pool_element.h:95
This class is used to hold the memory management information (Header) and a descriptor object...
Definition: pool_element.h:46
~PoolElement()
Definition: pool_element.h:69
void init_descriptor(Args &&...args)
Constructs a descriptor of the given type within this pool element.
Definition: pool_element.h:147
char padding_[CACHE_LINE_SIZE-sizeof(Header)]
Definition: pool_element.h:120
std::atomic< int64_t > ref_count
Definition: pool_element.h:54
virtual ~Descriptor()
Definition: descriptor.h:63
Header header_
Definition: pool_element.h:121
const long DEBUG_EXPECTED_STAMP
Definition: pool_element.h:38