Orocos Real-Time Toolkit  2.8.3
TsPool.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 TsPool.hpp
3 
4  TsPool.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 /*
40  * TsPool.hpp
41  *
42  * Created on: Jul 10, 2010
43  * Author: Butch Slayer
44  */
45 
46 #ifndef RTT_TSPOOL_HPP_
47 #define RTT_TSPOOL_HPP_
48 
49 #include "../os/CAS.hpp"
50 #include <assert.h>
51 
52 namespace RTT
53 {
54  namespace internal
55  {
56 
61  template<typename T>
62  class TsPool
63  {
64  public:
65  typedef T value_t;
66  private:
67  union Pointer_t
68  {
69  unsigned int value;
70  struct _ptr_type
71  {
72  unsigned short tag;
73  unsigned short index;
74  } ptr;
75  };
76 
82  struct Item
83  {
84  value_t value;
85  volatile Pointer_t next;
86 
87  Item() :
88  value(value_t())
89  {
90  next.value = 0;
91  }
92  };
93 
94  Item* pool;
95  Item head;
96 
97  unsigned int pool_size, pool_capacity;
98  public:
99 
100  typedef unsigned int size_type;
105  TsPool(unsigned int ssize, const T& sample = T()) :
106  pool_size(0), pool_capacity(ssize)
107  {
108  pool = new Item[ssize];
109  data_sample( sample );
110  }
111 
113  {
114 #ifndef NDEBUG
115  /*Check pool consistency.*/
116  unsigned int i = 0, endseen = 0;
117  for (; i < pool_capacity; i++)
118  {
119  if (pool[i].next.ptr.index == (unsigned short) -1)
120  {
121  ++endseen;
122  }
123  }
124  assert( endseen == 1);
125  assert( size() == pool_capacity && "TsPool: not all pieces were deallocated !" );
126 #endif
127  delete[] pool;
128  }
129 
137  void clear()
138  {
139  for (unsigned int i = 0; i < pool_capacity; i++)
140  {
141  pool[i].next.ptr.index = i + 1;
142  }
143  pool[pool_capacity - 1].next.ptr.index = (unsigned short) -1;
144  head.next.ptr.index = 0;
145  }
146 
153  void data_sample(const T& sample) {
154  for (unsigned int i = 0; i < pool_capacity; i++)
155  pool[i].value = sample;
156  clear();
157  }
158 
159  value_t* allocate()
160  {
161  volatile Pointer_t oldval;
162  volatile Pointer_t newval;
163  Item* item;
164  do
165  {
166  oldval.value = head.next.value;
167  //List empty?
168  if (oldval.ptr.index == (unsigned short) -1)
169  {
170  return 0;
171  }
172  item = &pool[oldval.ptr.index];
173  newval.ptr.index = item->next.ptr.index;
174  newval.ptr.tag = oldval.ptr.tag + 1;
175  } while (!os::CAS(&head.next.value, oldval.value, newval.value));
176  return &item->value;
177  }
178 
179  bool deallocate(T* Value)
180  {
181  if (Value == 0)
182  {
183  return false;
184  }
185  assert(Value >= (T*) &pool[0] && Value <= (T*) &pool[pool_capacity]);
186  volatile Pointer_t oldval;
187  Pointer_t head_next;
188  Item* item = reinterpret_cast<Item*> (Value);
189  do
190  {
191  oldval.value = head.next.value;
192  item->next.value = oldval.value;
193  head_next.ptr.index = (item - pool);
194  head_next.ptr.tag = oldval.ptr.tag + 1;
195  } while (!os::CAS(&head.next.value, oldval.value, head_next.value));
196  return true;
197  }
198 
205  unsigned int size()
206  {
207  unsigned int ret = 0;
208  volatile Item* oldval;
209  oldval = &head;
210  while ( oldval->next.ptr.index != (unsigned short) -1) {
211  ++ret;
212  oldval = &pool[oldval->next.ptr.index];
213  assert(ret <= pool_capacity); // abort on corruption due to concurrency.
214  }
215  return ret;
216  }
217 
222  unsigned int capacity()
223  {
224  return pool_capacity;
225  }
226 
227  private:
228 
229  };
230  }
231 }
232 
233 #endif /* RTT_TSPOOL_HPP_ */
unsigned int capacity()
The maximum number of elements available for allocation.
Definition: TsPool.hpp:222
TsPool(unsigned int ssize, const T &sample=T())
Creates a fixed size memory pool holding ssize blocks of memory that can hold an object of class T...
Definition: TsPool.hpp:105
void data_sample(const T &sample)
Initializes every element of the pool with the given sample and clears the pool.
Definition: TsPool.hpp:153
unsigned int size_type
Definition: TsPool.hpp:100
value_t * allocate()
Definition: TsPool.hpp:159
bool CAS(volatile T *addr, const V &expected, const W &value)
Compare And Swap.
Definition: CAS.hpp:54
bool deallocate(T *Value)
Definition: TsPool.hpp:179
void clear()
Clears all internal management data of this Memory Pool.
Definition: TsPool.hpp:137
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
unsigned int size()
Return the number of elements that are available to be allocated.
Definition: TsPool.hpp:205