41 |
|
|
42 |
USAGE: |
USAGE: |
43 |
|
|
44 |
|
//TODO: update usage documentation here to the new interface, means things below don't work anymore! |
45 |
|
|
46 |
creation of the memory pool: |
creation of the memory pool: |
47 |
|
|
48 |
RTELMemoryPool *mypool=RTLMemoryPool<my_datatype>(number_of_elements); |
RTELMemoryPool *mypool=RTLMemoryPool<my_datatype>(number_of_elements); |
72 |
|
|
73 |
THAT'S ALL FOLKS ! |
THAT'S ALL FOLKS ! |
74 |
|
|
75 |
|
|
76 |
*/ |
*/ |
77 |
|
|
78 |
|
|
79 |
#ifndef RTELMEMORYPOOL_H |
#ifndef RTELMEMORYPOOL_H |
80 |
#define RTELMEMORYPOOL_H |
#define RTELMEMORYPOOL_H |
81 |
|
|
82 |
|
template<class T> class RTELMemoryPool; |
83 |
|
|
84 |
template<class T> |
template<class T> |
85 |
class RTEList { |
class RTEList { |
86 |
typedef struct _RTEListNode { |
public: |
87 |
struct _RTEListNode *next; |
/** |
88 |
struct _RTEListNode *prev; |
* RTEList::Node contains the next and prev pointers needed to manage |
89 |
struct _RTEListNode *anext; |
* the free element list, and anext, aprev needed to manage the list |
90 |
struct _RTEListNode *aprev; |
* of allocated elements. This list is handy for the routines that make |
91 |
T data; |
* use of RTELMemoryPool because the list of elements can be traversed without |
92 |
} RTEListNode; |
* building a separate list outside RTELMemoryPool |
93 |
|
*/ |
94 |
public: |
template<class _T> |
95 |
typedef RTEListNode* NodeHandle; |
class Node { |
96 |
|
public: |
97 |
RTEList (void) { |
Node<_T>* next; |
98 |
// initialize alloclist fistnode and lastnode pointers |
Node<_T>* prev; |
99 |
firstnode.aprev=&firstnode; |
Node<_T>* anext; |
100 |
firstnode.anext=&lastnode; |
Node<_T>* aprev; |
101 |
lastnode.anext=&lastnode; |
_T data; |
102 |
lastnode.aprev=&firstnode; |
Node() {} |
103 |
acurrentnode=firstnode.anext; |
}; |
104 |
} |
protected: |
105 |
~RTEList (void) { |
Node<T> firstnode; |
106 |
} |
Node<T> lastnode; |
107 |
|
Node<T>* acurrentnode; |
108 |
/* returns the first element of the alloclist |
int free_offset; |
109 |
NULL if the list is empty (no elements allocated) |
RTELMemoryPool<T>* pPool; |
110 |
*/ |
|
111 |
inline T *first(void) { |
inline void move(Node<T>* pNode, RTEList<T>* pDstList) { |
112 |
acurrentnode=firstnode.anext; |
// remove element from this list |
113 |
// if element->anext points to itself it means last element |
RTEList<T>::Node<T>* prev = pNode->aprev; |
114 |
// return NULL to signal end of list |
RTEList<T>::Node<T>* next = pNode->anext; |
115 |
if(acurrentnode->anext == acurrentnode) return(NULL); |
prev->anext = next; |
116 |
return(&acurrentnode->data); |
next->aprev = prev; |
117 |
} |
|
118 |
|
// add element to destination list |
119 |
/* returns the last element of the alloclist |
Node<T>* last = pDstList->lastnode.aprev; |
120 |
NULL if the list is empty (no elements allocated) |
last->anext = pNode; |
121 |
*/ |
pNode->anext = &pDstList->lastnode; |
122 |
inline T *last(void) { |
pDstList->lastnode.aprev = pNode; |
123 |
acurrentnode=lastnode.aprev; |
pNode->aprev = last; |
124 |
// if element->aprev points to itself it means first element |
} |
125 |
// return NULL to signal begin of list |
|
126 |
if(acurrentnode->aprev == acurrentnode) return(NULL); |
friend class RTELMemoryPool<T>; |
127 |
return(&acurrentnode->data); |
public: |
128 |
} |
/** |
129 |
|
* Constructor |
130 |
/* returns the next element of the alloclist |
* |
131 |
NULL if we reach the end of the list |
* @param pPool - the allocation pool this external list belongs to |
132 |
*/ |
*/ |
133 |
inline T *next(void) { |
RTEList(RTELMemoryPool<T>* pPool) { |
134 |
acurrentnode=acurrentnode->anext; |
this->pPool = pPool; |
135 |
// if element->anext points to itself it means last element |
// initialize alloclist fistnode and lastnode pointers |
136 |
// return NULL to signal end of list |
firstnode.aprev = &firstnode; |
137 |
if(acurrentnode->anext == acurrentnode) return(NULL); |
firstnode.anext = &lastnode; |
138 |
return(&acurrentnode->data); |
lastnode.anext = &lastnode; |
139 |
} |
lastnode.aprev = &firstnode; |
140 |
|
acurrentnode = firstnode.anext; |
141 |
/* returns the previous element of the alloclist |
|
142 |
NULL if we reach the begin of the list |
/* yes ugly hack but assuming that the difference of between |
143 |
*/ |
RTEList::Node and RTList::Node.data is constant for all |
144 |
inline T *prev(void) { |
elements of the same class seems reasonable to me |
145 |
acurrentnode=acurrentnode->aprev; |
this is needed because when calling free() the user supplies |
146 |
// if element->aprev points to itself it means last element |
the pointer to the data T and not to the RTEListNode |
147 |
// return NULL to signal begin of list |
*/ |
148 |
if(acurrentnode->aprev == acurrentnode) return(NULL); |
free_offset = (int)(&firstnode.data) - (int)&firstnode; |
149 |
return(&acurrentnode->data); |
} |
150 |
} |
|
151 |
|
~RTEList() {} |
152 |
/// Returns a handle for the currently selected node or NULL if the list is empty. |
|
153 |
inline NodeHandle current(void) { |
/** |
154 |
if (acurrentnode->anext == acurrentnode) return NULL; |
* Returns the first element of the alloclist |
155 |
return acurrentnode; |
* NULL if the list is empty (no elements allocated) |
156 |
} |
*/ |
157 |
|
inline T* first() { |
158 |
/// Selects the node in the list respective to the node handle and returns it's data. |
acurrentnode = firstnode.anext; |
159 |
inline T* set_current(NodeHandle hNode) { |
// if element->anext points to itself it means last element |
160 |
acurrentnode = (RTEListNode*) hNode; |
// return NULL to signal end of list |
161 |
return &acurrentnode->data; |
if (acurrentnode->anext == acurrentnode) return NULL; |
162 |
//FIXME: there should be a check if the node could be selected and a return value of NULL if failed |
return &acurrentnode->data; |
163 |
} |
} |
164 |
|
|
165 |
/// Returns true if the list is empty. |
/** |
166 |
inline bool is_empty() { |
* Returns the last element of the alloclist |
167 |
return !first(); |
* NULL if the list is empty (no elements allocated) |
168 |
} |
*/ |
169 |
|
inline T* last() { |
170 |
RTEListNode firstnode; |
acurrentnode = lastnode.aprev; |
171 |
RTEListNode lastnode; |
// if element->aprev points to itself it means first element |
172 |
RTEListNode *acurrentnode; |
// return NULL to signal begin of list |
173 |
|
if (acurrentnode->aprev == acurrentnode) return NULL; |
174 |
|
return &acurrentnode->data; |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
|
* Returns the next element of the alloclist |
179 |
|
* NULL if we reach the end of the list |
180 |
|
*/ |
181 |
|
inline T* next() { |
182 |
|
acurrentnode = acurrentnode->anext; |
183 |
|
// if element->anext points to itself it means last element |
184 |
|
// return NULL to signal end of list |
185 |
|
if (acurrentnode->anext == acurrentnode) return NULL; |
186 |
|
return &acurrentnode->data; |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
|
* Returns the previous element of the alloclist |
191 |
|
* NULL if we reach the begin of the list |
192 |
|
*/ |
193 |
|
inline T* prev() { |
194 |
|
acurrentnode = acurrentnode->aprev; |
195 |
|
// if element->aprev points to itself it means last element |
196 |
|
// return NULL to signal begin of list |
197 |
|
if (acurrentnode->aprev == acurrentnode) return NULL; |
198 |
|
return &acurrentnode->data; |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* Selects the node in the list respective to the given element; |
203 |
|
* mandatory for subsequent operations like prev() or next(). |
204 |
|
* |
205 |
|
* @param element - element to be selected in the list |
206 |
|
*/ |
207 |
|
inline void set_current(T* element) { |
208 |
|
char* node = (char*) element; |
209 |
|
// calculate the offset of the RTEListNode (see free_offset calculation in the constructor) |
210 |
|
node -= free_offset; |
211 |
|
// select the node |
212 |
|
acurrentnode = (Node<T>*) node; |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
|
* Moves current selected element from this list to another list. |
217 |
|
* The element will be appended to the destination list. |
218 |
|
* |
219 |
|
* @param pDstList - destination list |
220 |
|
* @returns the moved element or NULL on error |
221 |
|
*/ |
222 |
|
inline T* move(RTEList<T>* pDstList) { |
223 |
|
// if there's a valid element selected |
224 |
|
if (acurrentnode != &firstnode && acurrentnode != &lastnode) { |
225 |
|
Node<T>* pNode = acurrentnode; |
226 |
|
acurrentnode = acurrentnode->aprev; // select previous element |
227 |
|
move(pNode, pDstList); // move element's node |
228 |
|
return &pNode->data; |
229 |
|
} |
230 |
|
return NULL; |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
|
* Moves the given element from this list to another list. |
235 |
|
* The element will be appended to the destination list. |
236 |
|
* |
237 |
|
* @param pElement - element to be moved |
238 |
|
* @param pDstList - destination list |
239 |
|
*/ |
240 |
|
inline void move(T* pElement, RTEList<T>* pDstList) { |
241 |
|
char* cNode = (char*) pElement; |
242 |
|
// calculate the offset of the RTEListNode (see free_offset calculation in the constructor) |
243 |
|
cNode -= free_offset; |
244 |
|
Node<T>* pNode = (Node<T>*) cNode; |
245 |
|
|
246 |
|
// if the node is selected, select previous element |
247 |
|
if (acurrentnode == pNode) acurrentnode = acurrentnode->aprev; |
248 |
|
|
249 |
|
// move the element's node |
250 |
|
move(pNode, pDstList); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
|
* Allocate one element from the pool and append it to this list. |
255 |
|
* |
256 |
|
* @returns allocated element |
257 |
|
*/ |
258 |
|
inline T* alloc() { |
259 |
|
return pPool->alloc_append(this); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* Allocate one element from the pool, assign given value and |
264 |
|
* append element to this list. |
265 |
|
* |
266 |
|
* @returns allocated element with already assigned value |
267 |
|
*/ |
268 |
|
inline T* alloc_assign(T data) { |
269 |
|
T* pData = alloc(); |
270 |
|
if (pData) *pData = data; |
271 |
|
return pData; |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
|
* Free the given (allocated) element from this list. The element |
276 |
|
* will be readded to the pool's list of free elements. |
277 |
|
*/ |
278 |
|
inline void free(T* element) { |
279 |
|
pPool->free(element); |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
|
* Returns true if the list is empty. |
284 |
|
*/ |
285 |
|
inline bool is_empty() { |
286 |
|
return !first(); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
|
* Free all allocated elements in the list. All elements of this list |
291 |
|
* will be readded to the pool's internal list of free nodes. |
292 |
|
* |
293 |
|
* @returns number of freed elements |
294 |
|
*/ |
295 |
|
inline int clear() { |
296 |
|
int count = 0; |
297 |
|
acurrentnode = firstnode.anext; |
298 |
|
while (acurrentnode != acurrentnode->anext) { |
299 |
|
pPool->free(&acurrentnode->data); count++; |
300 |
|
acurrentnode = firstnode.anext; |
301 |
|
} |
302 |
|
return count; |
303 |
|
} |
304 |
}; |
}; |
305 |
|
|
306 |
template<class T> |
template<class T> |
307 |
class RTELMemoryPool |
class RTELMemoryPool : public RTEList<T> { |
308 |
{ |
protected: |
309 |
|
RTEList<T>::Node<T>* currentnode; |
310 |
|
|
311 |
|
// array that contains the elements: |
312 |
/* |
// each list element is made of list header (prev,next) and the data |
313 |
RTEListNode contains the next and prev pointers needed to manage |
// of type T |
314 |
the free element list, and anext,aprev needed to manage the list |
RTEList<T>::Node<T>* memory_pool; |
315 |
of allocated elements. This list is handy for the routines that make |
|
316 |
use of RTELMemoryPool because the list of elements can be traversed without |
/** |
317 |
building a separate list outside RTELMemoryPool |
* Allocate one element of the memory pool |
318 |
*/ |
* if no elements are free return NULL |
319 |
|
* we find the first element of the list |
320 |
typedef struct _RTEListNode { |
* remove it from the free list and then |
321 |
struct _RTEListNode *next; |
* return the data associated to the element |
322 |
struct _RTEListNode *prev; |
*/ |
323 |
struct _RTEListNode *anext; |
inline T* alloc_append(RTEList<T>* rtelist) { |
324 |
struct _RTEListNode *aprev; |
// get the first element |
325 |
T data; |
currentnode = firstnode.next; |
326 |
} RTEListNode; |
// element->next points to itself which means last element |
327 |
|
// return NULL to signal end of list |
328 |
public: |
if (currentnode->next == currentnode) return NULL; |
329 |
|
|
330 |
inline RTELMemoryPool (int numelements) { |
// now remove the element from the freelist |
331 |
|
RTEList<T>::Node<T>* prevelem = currentnode->prev; |
332 |
|
RTEList<T>::Node<T>* nextelem = currentnode->next; |
333 |
// initialize freelist fistnode and lastnode pointers |
prevelem->next = nextelem; |
334 |
firstnode.prev=&firstnode; |
nextelem->prev = prevelem; |
335 |
firstnode.next=&lastnode; |
|
336 |
lastnode.next=&lastnode; |
// append the element to the external rtelist |
337 |
lastnode.prev=&firstnode; |
RTEList<T>::Node<T>* el_lastnode = (RTEList<T>::Node<T>*) &rtelist->lastnode; |
338 |
|
RTEList<T>::Node<T>* last = el_lastnode->aprev; |
339 |
currentnode=&lastnode; |
|
340 |
|
last->anext = currentnode; |
341 |
// initialize alloclist fistnode and lastnode pointers |
currentnode->anext = el_lastnode; |
342 |
firstnode.aprev=&firstnode; |
el_lastnode->aprev = currentnode; |
343 |
firstnode.anext=&lastnode; |
currentnode->aprev = last; |
344 |
lastnode.anext=&lastnode; |
|
345 |
lastnode.aprev=&firstnode; |
// finally return the allocated elment |
346 |
|
//printf("alloc_append returning elem=%d\n",¤tnode->data); |
347 |
|
return ¤tnode->data; |
348 |
memory_pool=new RTEListNode[numelements]; |
} |
349 |
|
|
350 |
for(int i=0; i < numelements; i++) { |
/** |
351 |
append(&memory_pool[i]); |
* same as alloc_append but the element is inserted at the |
352 |
} |
* beginning of the list |
353 |
/* yes ugly hack but assuming that the difference of between |
*/ |
354 |
RTEListNode and RTListNode.data is constant for all |
inline T* alloc_prepend(RTEList<T>* rtelist) { |
355 |
elements of the same class seems reasonable to me |
RTEList<T>::Node<T>* prevelem; |
356 |
this is needed because when calling free() the user supplies |
RTEList<T>::Node<T>* nextelem; |
357 |
the pointer to the data T and not to the RTEListNode |
// get the first element |
358 |
*/ |
currentnode = firstnode.next; |
359 |
free_offset=(int)(&firstnode.data)-(int)&firstnode; |
// element->next points to itself which means last element |
360 |
} |
// return NULL to signal end of list |
361 |
inline ~RTELMemoryPool() { |
if (currentnode->next == currentnode) return NULL; |
362 |
delete[] memory_pool; |
|
363 |
} |
// now remove the element from the freelist |
364 |
|
prevelem = currentnode->prev; |
365 |
/* returns the first element of the alloclist |
nextelem = currentnode->next; |
366 |
NULL if the list is empty (no elements allocated) |
prevelem->next = nextelem; |
367 |
*/ |
nextelem->prev = prevelem; |
368 |
inline T *first(void) { |
|
369 |
acurrentnode=firstnode.anext; |
// prepend the element to the external rtelist |
370 |
// if element->anext points to itself it means last element |
RTEList<T>::Node<T>* el_firstnode = (RTEList<T>::Node<T>*) &rtelist->firstnode; |
371 |
// return NULL to signal end of list |
RTEList<T>::Node<T>* first = el_firstnode->anext; |
372 |
if(acurrentnode->anext == acurrentnode) return(NULL); |
|
373 |
return(&acurrentnode->data); |
currentnode->anext = first; |
374 |
} |
currentnode->aprev = el_firstnode; |
375 |
|
el_firstnode->anext = currentnode; |
376 |
/* returns the last element of the alloclist |
first->aprev = currentnode; |
377 |
NULL if the list is empty (no elements allocated) |
|
378 |
*/ |
// finally return the allocated elment |
379 |
inline T *last(void) { |
return ¤tnode->data; |
380 |
acurrentnode=lastnode.aprev; |
} |
381 |
// if element->aprev points to itself it means first element |
|
382 |
// return NULL to signal begin of list |
inline void append(RTEList<T>::Node<T>* elem) { |
383 |
if(acurrentnode->aprev == acurrentnode) return(NULL); |
RTEList<T>::Node<T>* last = lastnode.prev; |
384 |
return(&acurrentnode->data); |
|
385 |
} |
last->next = elem; |
386 |
|
elem->next = &lastnode; |
387 |
/* returns the next element of the alloclist |
lastnode.prev = elem; |
388 |
NULL if we reach the end of the list |
elem->prev = last; |
389 |
*/ |
} |
390 |
inline T *next(void) { |
|
391 |
acurrentnode=acurrentnode->anext; |
inline void prepend(RTEList<T>::Node<T>* elem) { |
392 |
// if element->anext points to itself it means last element |
RTEList<T>::Node<T>* first = firstnode.next; |
393 |
// return NULL to signal end of list |
|
394 |
if(acurrentnode->anext == acurrentnode) return(NULL); |
elem->next = first; |
395 |
return(&acurrentnode->data); |
elem->prev = &firstnode; |
396 |
} |
firstnode.next = elem; |
397 |
|
first->prev = elem; |
398 |
/* returns the previous element of the alloclist |
} |
399 |
NULL if we reach the begin of the list |
|
400 |
*/ |
friend class RTEList<T>; |
401 |
inline T *prev(void) { |
|
402 |
acurrentnode=acurrentnode->aprev; |
public: |
403 |
// if element->aprev points to itself it means last element |
/** |
404 |
// return NULL to signal begin of list |
* Constructor |
405 |
if(acurrentnode->aprev == acurrentnode) return(NULL); |
* |
406 |
return(&acurrentnode->data); |
* @param numelements - number of elements this pool should offer |
407 |
} |
*/ |
408 |
|
RTELMemoryPool(int numelements) : RTEList<T>::RTEList(this) { |
409 |
inline void append(RTEListNode *elem) { |
// initialize freelist listnode and lastnode pointers |
410 |
|
firstnode.prev = &firstnode; |
411 |
RTEListNode *last=lastnode.prev; |
firstnode.next = &lastnode; |
412 |
|
lastnode.next = &lastnode; |
413 |
last->next=elem; |
lastnode.prev = &firstnode; |
414 |
elem->next=&lastnode; |
|
415 |
lastnode.prev=elem; |
currentnode = &lastnode; |
416 |
elem->prev=last; |
|
417 |
|
// initialize alloclist listnode and lastnode pointers |
418 |
} |
firstnode.aprev = &firstnode; |
419 |
|
firstnode.anext = &lastnode; |
420 |
inline void prepend(RTEListNode *elem) { |
lastnode.anext = &lastnode; |
421 |
|
lastnode.aprev = &firstnode; |
422 |
RTEListNode *first=firstnode.next; |
|
423 |
|
|
424 |
elem->next=first; |
memory_pool = new RTEList<T>::Node<T>[numelements]; |
425 |
elem->prev=&firstnode; |
|
426 |
firstnode.next=elem; |
for (int i = 0; i < numelements; i++) { |
427 |
first->prev=elem; |
append(&memory_pool[i]); |
428 |
} |
} |
429 |
|
} |
430 |
|
|
431 |
|
inline ~RTELMemoryPool() { |
432 |
/* alloc_append: |
if (memory_pool) delete[] memory_pool; |
433 |
allocate one element of the memory pool |
} |
434 |
if no elements are free return NULL |
|
435 |
we find the first element of the list |
/** |
436 |
remove it from the free list and then |
* Allocate one element of the memory pool |
437 |
return the data associated to the element |
* if no elements are free return NULL |
438 |
*/ |
* we find the first element of the list |
439 |
|
* remove it from the free list and then |
440 |
inline T *alloc_append(RTEList<T> *rtelist) { |
* return the data associated to the element |
441 |
RTEListNode *prevelem; |
*/ |
442 |
RTEListNode *nextelem; |
inline T* alloc() { |
443 |
// get the first element |
// get the first element |
444 |
currentnode=firstnode.next; |
currentnode = firstnode.next; |
445 |
// element->next points to itself which means last element |
// element->next points to itself which means last element |
446 |
// return NULL to signal end of list |
// return NULL to signal end of list |
447 |
if(currentnode->next == currentnode) return(NULL); |
if (currentnode->next == currentnode) return NULL; |
448 |
|
|
449 |
// now remove the element from the freelist |
// now remove the element from the freelist |
450 |
prevelem=currentnode->prev; |
RTEList<T>::Node<T>* prevelem = currentnode->prev; |
451 |
nextelem=currentnode->next; |
RTEList<T>::Node<T>* nextelem = currentnode->next; |
452 |
prevelem->next=nextelem; |
prevelem->next = nextelem; |
453 |
nextelem->prev=prevelem; |
nextelem->prev = prevelem; |
454 |
|
|
455 |
// append the element to the external rtelist |
// append the element to the alloc list |
456 |
RTEListNode *el_lastnode=(RTEListNode *)&rtelist->lastnode; |
RTEList<T>::Node<T>* last = lastnode.aprev; |
457 |
RTEListNode *last=el_lastnode->aprev; |
last->anext = currentnode; |
458 |
|
currentnode->anext = &lastnode; |
459 |
last->anext=currentnode; |
lastnode.aprev = currentnode; |
460 |
currentnode->anext=el_lastnode; |
currentnode->aprev = last; |
461 |
el_lastnode->aprev=currentnode; |
|
462 |
currentnode->aprev=last; |
// finally return the allocated elment |
463 |
|
return ¤tnode->data; |
464 |
// finally return the allocated elment |
} |
465 |
//printf("alloc_append returning elem=%d\n",¤tnode->data); |
|
466 |
return(¤tnode->data); |
/** |
467 |
} |
* Free an allocated element by putting it back to the freelist. |
468 |
/* same as alloc_append but the element is inserted at the |
*/ |
469 |
beginning of the list |
inline void free(T* element) { |
470 |
*/ |
RTEList<T>::Node<T>* prevelem; |
471 |
inline T *alloc_prepend(RTEList<T> *rtelist) { |
RTEList<T>::Node<T>* nextelem; |
472 |
RTEListNode *prevelem; |
RTEList<T>::Node<T>* node; |
473 |
RTEListNode *nextelem; |
|
474 |
// get the first element |
char* node_to_free = (char*) element; |
475 |
currentnode=firstnode.next; |
// calculate the offset of the RTEListNode (see free_offset calculation in the constructor) |
476 |
// element->next points to itself which means last element |
node_to_free -= free_offset; |
477 |
// return NULL to signal end of list |
// insert the node to the beginning of the freelist |
478 |
if(currentnode->next == currentnode) return(NULL); |
node = (RTEList<T>::Node<T>*) node_to_free; |
479 |
|
prepend(node); |
480 |
// now remove the element from the freelist |
|
481 |
prevelem=currentnode->prev; |
// now remove the element from the alloclist |
482 |
nextelem=currentnode->next; |
prevelem = node->aprev; |
483 |
prevelem->next=nextelem; |
nextelem = node->anext; |
484 |
nextelem->prev=prevelem; |
prevelem->anext = nextelem; |
485 |
|
nextelem->aprev = prevelem; |
486 |
// prepend the element to the external rtelist |
//printf("free returning elem=%d\n",¤tnode->data); |
487 |
RTEListNode *el_firstnode=(RTEListNode *)&rtelist->firstnode; |
} |
488 |
RTEListNode *first=el_firstnode->anext; |
|
489 |
|
/** |
490 |
currentnode->anext=first; |
* Frees all allocated elements in the internal allocation list. |
491 |
currentnode->aprev=el_firstnode; |
* This method does not free elements allocated for external lists! |
492 |
el_firstnode->anext=currentnode; |
* For freeing elements allocated for external lists, use the empty() |
493 |
first->aprev=currentnode; |
* method of the respective RTEList object. |
494 |
|
* |
495 |
// finally return the allocated elment |
* @returns number of freed elements |
496 |
return(¤tnode->data); |
*/ |
497 |
} |
inline int clear() { |
498 |
|
RTEList<T>::Node<T>* nextnode; |
499 |
|
RTEList<T>::Node<T>* prevelem; |
500 |
|
RTEList<T>::Node<T>* nextelem; |
501 |
// allocate one element of the memory pool |
|
502 |
// if no elements are free return NULL |
int count = 0; |
503 |
// we find the first element of the list |
|
504 |
// remove it from the free list and then |
acurrentnode = firstnode.anext; |
505 |
// return the data associated to the element |
if (acurrentnode->anext == acurrentnode) return 0; |
506 |
inline T *alloc(void) { |
|
507 |
|
while (true) { |
508 |
RTEListNode *prevelem; |
nextnode = acurrentnode->anext; |
509 |
RTEListNode *nextelem; |
|
510 |
RTEListNode *last; |
// prepend (insert at the beginning) the node to the freelist |
511 |
|
//printf("empty: putting back elem (node) %d to freelist\n",acurrentnode); |
512 |
// get the first element |
prepend(acurrentnode); count++; |
513 |
currentnode=firstnode.next; |
|
514 |
// element->next points to itself which means last element |
// now remove the element from the alloclist |
515 |
// return NULL to signal end of list |
prevelem = acurrentnode->aprev; |
516 |
if(currentnode->next == currentnode) return(NULL); |
nextelem = acurrentnode->anext; |
517 |
|
prevelem->anext = nextelem; |
518 |
// now remove the element from the freelist |
nextelem->aprev = prevelem; |
519 |
prevelem=currentnode->prev; |
|
520 |
nextelem=currentnode->next; |
if (nextnode->anext == nextnode) return count; |
521 |
prevelem->next=nextelem; |
acurrentnode = nextnode; |
522 |
nextelem->prev=prevelem; |
} |
523 |
|
} |
|
// append the element to the alloc list |
|
|
last=lastnode.aprev; |
|
|
last->anext=currentnode; |
|
|
currentnode->anext=&lastnode; |
|
|
lastnode.aprev=currentnode; |
|
|
currentnode->aprev=last; |
|
|
|
|
|
// finally return the allocated elment |
|
|
return(¤tnode->data); |
|
|
} |
|
|
|
|
|
|
|
|
// free an allocated element by putting it back to the freelist |
|
|
inline void free(T *element) { |
|
|
RTEListNode *prevelem; |
|
|
RTEListNode *nextelem; |
|
|
RTEListNode *node; |
|
|
|
|
|
char *node_to_free=(char *)element; |
|
|
// calculate the offset of the RTEListNode (see free_offset calculation in the constructor) |
|
|
node_to_free -= free_offset; |
|
|
// insert the node to the beginning of the freelist |
|
|
node=(RTEListNode *)node_to_free; |
|
|
prepend(node); |
|
|
|
|
|
// now remove the element from the alloclist |
|
|
prevelem=node->aprev; |
|
|
nextelem=node->anext; |
|
|
prevelem->anext=nextelem; |
|
|
nextelem->aprev=prevelem; |
|
|
//printf("free returning elem=%d\n",¤tnode->data); |
|
|
} |
|
|
|
|
|
/// Selects the current element node by providing the pointer to the sought |
|
|
/// element's data. |
|
|
inline void set_current(T* element) { |
|
|
char* node = (char*) element; |
|
|
node -= free_offset; // calculate the offset of the RTEListNode (see free_offset calculation in the constructor) |
|
|
acurrentnode = (RTEListNode*) node; |
|
|
//FIXME: there should be a check if the element could be selected and a respective return value |
|
|
} |
|
|
|
|
|
/// Returns true if there's no allocated element. |
|
|
inline bool is_empty() { |
|
|
return !first(); |
|
|
} |
|
|
|
|
|
// empty the whole list |
|
|
inline void empty(void) { |
|
|
|
|
|
RTEListNode *nextnode; |
|
|
RTEListNode *prevelem; |
|
|
RTEListNode *nextelem; |
|
|
|
|
|
acurrentnode=firstnode.anext; |
|
|
if(acurrentnode->anext == acurrentnode) return; |
|
|
|
|
|
while(1) { |
|
|
nextnode=acurrentnode->anext; |
|
|
|
|
|
// prepend (insert at the beginning) the node to the freelist |
|
|
//printf("empty: putting back elem (node) %d to freelist\n",acurrentnode); |
|
|
prepend(acurrentnode); |
|
|
|
|
|
// now remove the element from the alloclist |
|
|
prevelem=acurrentnode->aprev; |
|
|
nextelem=acurrentnode->anext; |
|
|
prevelem->anext=nextelem; |
|
|
nextelem->aprev=prevelem; |
|
|
|
|
|
if(nextnode->anext == nextnode) return; |
|
|
acurrentnode=nextnode; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int free_offset; |
|
|
|
|
|
|
|
|
RTEListNode firstnode; |
|
|
RTEListNode lastnode; |
|
|
RTEListNode *currentnode; |
|
|
|
|
|
RTEListNode *acurrentnode; |
|
|
|
|
|
// array that contains the elements: |
|
|
// each list element is made of list header (prev,next) and the data |
|
|
// of type T |
|
|
RTEListNode *memory_pool; |
|
|
|
|
|
|
|
524 |
}; |
}; |
525 |
|
|
526 |
|
|