46 #ifndef RTT_TSPOOL_HPP_ 47 #define RTT_TSPOOL_HPP_ 49 #include "../os/CAS.hpp" 85 volatile Pointer_t next;
97 unsigned int pool_size, pool_capacity;
105 TsPool(
unsigned int ssize,
const T& sample = T()) :
106 pool_size(0), pool_capacity(ssize)
108 pool =
new Item[ssize];
116 unsigned int i = 0, endseen = 0;
117 for (; i < pool_capacity; i++)
119 if (pool[i].next.ptr.index == (
unsigned short) -1)
124 assert( endseen == 1);
125 assert(
size() == pool_capacity &&
"TsPool: not all pieces were deallocated !" );
139 for (
unsigned int i = 0; i < pool_capacity; i++)
141 pool[i].next.ptr.index = i + 1;
143 pool[pool_capacity - 1].next.ptr.index = (
unsigned short) -1;
144 head.next.ptr.index = 0;
154 for (
unsigned int i = 0; i < pool_capacity; i++)
155 pool[i].value = sample;
161 volatile Pointer_t oldval;
162 volatile Pointer_t newval;
166 oldval.value = head.next.value;
168 if (oldval.ptr.index == (
unsigned short) -1)
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));
185 assert(Value >= (T*) &pool[0] && Value <= (T*) &pool[pool_capacity]);
186 volatile Pointer_t oldval;
188 Item* item =
reinterpret_cast<Item*
> (Value);
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));
207 unsigned int ret = 0;
208 volatile Item* oldval;
210 while ( oldval->next.ptr.index != (
unsigned short) -1) {
212 oldval = &pool[oldval->next.ptr.index];
213 assert(ret <= pool_capacity);
224 return pool_capacity;
unsigned int capacity()
The maximum number of elements available for allocation.
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...
void data_sample(const T &sample)
Initializes every element of the pool with the given sample and clears the pool.
bool CAS(volatile T *addr, const V &expected, const W &value)
Compare And Swap.
bool deallocate(T *Value)
void clear()
Clears all internal management data of this Memory Pool.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
unsigned int size()
Return the number of elements that are available to be allocated.