3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
24 |
#ifndef __LS_POOL_H__ |
#ifndef __LS_POOL_H__ |
25 |
#define __LS_POOL_H__ |
#define __LS_POOL_H__ |
26 |
|
|
27 |
#ifndef DEVMODE |
#ifdef HAVE_CONFIG_H |
28 |
# include "global.h" // just to check if we should compile in 'DEVMODE' |
# include <config.h> |
29 |
#endif |
#endif |
30 |
|
|
31 |
// we just use exceptions for debugging, better not in the final realtime thread ! |
// we just use exceptions for debugging, better not in the final realtime thread ! |
32 |
#ifndef USE_EXCEPTIONS |
#ifndef CONFIG_RT_EXCEPTIONS |
33 |
# define USE_EXCEPTIONS 0 |
# define CONFIG_RT_EXCEPTIONS 0 |
34 |
#endif |
#endif |
35 |
|
|
36 |
#if USE_EXCEPTIONS |
#if CONFIG_RT_EXCEPTIONS |
37 |
# include <stdexcept> |
# include <stdexcept> |
38 |
# include <string> |
# include <string> |
39 |
|
#endif // CONFIG_RT_EXCEPTIONS |
40 |
|
|
41 |
|
#if CONFIG_DEVMODE |
42 |
|
# include <string> |
43 |
|
# include <iostream> |
44 |
const std::string __err_msg_iterator_invalidated = "Pool/RTList iterator invalidated"; |
const std::string __err_msg_iterator_invalidated = "Pool/RTList iterator invalidated"; |
45 |
#endif // USE_EXCEPTIONS |
#endif // CONFIG_DEVMODE |
46 |
|
|
47 |
// just symbol prototyping |
// just symbol prototyping |
48 |
template<typename T> class Pool; |
template<typename T> class Pool; |
51 |
template<typename T> |
template<typename T> |
52 |
class RTListBase { |
class RTListBase { |
53 |
protected: |
protected: |
54 |
template<typename _T> |
template<typename T1> |
55 |
struct _Node { |
struct _Node { |
56 |
_Node<_T>* next; |
_Node<T1>* next; |
57 |
_Node<_T>* prev; |
_Node<T1>* prev; |
58 |
_T* data; |
T1* data; |
59 |
#if DEVMODE |
#if CONFIG_DEVMODE |
60 |
RTListBase<_T>* list; // list to which this node currently belongs to |
RTListBase<T1>* list; // list to which this node currently belongs to |
61 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
62 |
|
|
63 |
_Node() { |
_Node() { |
64 |
next = NULL; |
next = NULL; |
65 |
prev = NULL; |
prev = NULL; |
66 |
data = NULL; |
data = NULL; |
67 |
#if DEVMODE |
#if CONFIG_DEVMODE |
68 |
list = NULL; |
list = NULL; |
69 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
70 |
} |
} |
71 |
}; |
}; |
72 |
typedef _Node<T> Node; |
typedef _Node<T> Node; |
73 |
|
|
74 |
public: |
public: |
75 |
template<typename _T> |
template<typename T1> |
76 |
class _Iterator { |
class _Iterator { |
77 |
public: |
public: |
78 |
_Iterator() { |
_Iterator() { |
79 |
current = NULL; |
current = NULL; |
80 |
fallback = NULL; |
fallback = NULL; |
81 |
#if DEVMODE |
#if CONFIG_DEVMODE |
82 |
list = NULL; |
list = NULL; |
83 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
84 |
} |
} |
85 |
|
|
86 |
/// prefix increment op. |
/// prefix increment op. |
87 |
inline _Iterator& operator++() { |
inline _Iterator& operator++() { |
88 |
#if DEVMODE |
#if CONFIG_DEVMODE |
89 |
#if USE_EXCEPTIONS |
if (!isValid()) { |
90 |
if (!isValid()) throw std::runtime_error(__err_msg_iterator_invalidated); |
#if CONFIG_RT_EXCEPTIONS |
91 |
#else |
throw std::runtime_error(__err_msg_iterator_invalidated); |
92 |
if (!isValid()) return *(_Iterator*)NULL; // force segfault if iterator became invalidated |
#else |
93 |
#endif // USE_EXCEPTIONS |
std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush; |
94 |
#endif // DEVMODE |
return *(_Iterator*)NULL; // force segfault if iterator became invalidated |
95 |
|
#endif // CONFIG_RT_EXCEPTIONS |
96 |
|
} |
97 |
|
#endif // CONFIG_DEVMODE |
98 |
fallback = current; |
fallback = current; |
99 |
current = current->next; |
current = current->next; |
100 |
return *this; |
return *this; |
103 |
/// postfix increment op. |
/// postfix increment op. |
104 |
inline _Iterator operator++(int) { |
inline _Iterator operator++(int) { |
105 |
_Iterator preval = *this; |
_Iterator preval = *this; |
106 |
++*this; |
++*this; // use prefix operator implementation |
107 |
return preval; |
return preval; |
108 |
} |
} |
109 |
|
|
110 |
/// prefix decrement op. |
/// prefix decrement op. |
111 |
inline _Iterator& operator--() { |
inline _Iterator& operator--() { |
112 |
#if DEVMODE |
#if CONFIG_DEVMODE |
113 |
#if USE_EXCEPTIONS |
if (!isValid()) { |
114 |
if (!isValid()) throw std::runtime_error(__err_msg_iterator_invalidated); |
#if CONFIG_RT_EXCEPTIONS |
115 |
#else |
throw std::runtime_error(__err_msg_iterator_invalidated); |
116 |
if (!isValid()) return *(_Iterator*)NULL; // force segfault if iterator became invalidated |
#else |
117 |
#endif // USE_EXCEPTIONS |
std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush; |
118 |
#endif // DEVMODE |
return *(_Iterator*)NULL; // force segfault if iterator became invalidated |
119 |
|
#endif // CONFIG_RT_EXCEPTIONS |
120 |
|
} |
121 |
|
#endif // CONFIG_DEVMODE |
122 |
fallback = current; |
fallback = current; |
123 |
current = current->prev; |
current = current->prev; |
124 |
return *this; |
return *this; |
127 |
/// postfix decrement op. |
/// postfix decrement op. |
128 |
inline _Iterator operator--(int) { |
inline _Iterator operator--(int) { |
129 |
_Iterator preval = *this; |
_Iterator preval = *this; |
130 |
--*this; |
--*this; // use prefix operator implementation |
131 |
return preval; |
return preval; |
132 |
} |
} |
133 |
|
|
134 |
inline _T& operator*() { |
inline T1& operator*() { |
135 |
#if DEVMODE |
#if CONFIG_DEVMODE |
136 |
#if USE_EXCEPTIONS |
if (!isValid()) { // if iterator became invalidated |
137 |
if (isValid()) return *current->data; |
#if CONFIG_RT_EXCEPTIONS |
138 |
else throw std::runtime_error(__err_msg_iterator_invalidated); |
throw std::runtime_error(__err_msg_iterator_invalidated); |
139 |
#else |
#else |
140 |
return *(isValid() ? current->data : (_T*)NULL); // force segfault if iterator became invalidated |
std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush; |
141 |
#endif // USE_EXCEPTIONS |
return *((T1*)NULL); // force segfault if iterator became invalidated |
142 |
#else |
#endif // CONFIG_RT_EXCEPTIONS |
143 |
|
} |
144 |
|
#endif // CONFIG_DEVMODE |
145 |
return *current->data; |
return *current->data; |
146 |
#endif // DEVMODE |
|
147 |
} |
} |
148 |
|
|
149 |
inline _T* operator->() { |
inline T1* operator->() { |
150 |
#if DEVMODE |
#if CONFIG_DEVMODE |
151 |
#if USE_EXCEPTIONS |
if (!isValid()) { // if iterator became invalidated |
152 |
if (isValid()) return current->data; |
#if CONFIG_RT_EXCEPTIONS |
153 |
else throw std::runtime_error(__err_msg_iterator_invalidated); |
throw std::runtime_error(__err_msg_iterator_invalidated); |
154 |
#else |
#else |
155 |
return isValid() ? current->data : (_T*)NULL; // force segfault if iterator became invalidated |
std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush; |
156 |
#endif // USE_EXCEPTIONS |
return (T1*)NULL; // force segfault if iterator became invalidated |
157 |
#else |
#endif // CONFIG_RT_EXCEPTIONS |
158 |
|
} |
159 |
|
#endif // CONFIG_DEVMODE |
160 |
return current->data; |
return current->data; |
|
#endif // DEVMODE |
|
161 |
} |
} |
162 |
|
|
163 |
inline bool operator==(const _Iterator<_T> other) { |
inline bool operator==(const _Iterator<T1> other) { |
164 |
return current == other.current; |
return current == other.current; |
165 |
} |
} |
166 |
|
|
167 |
inline bool operator!=(const _Iterator<_T> other) { |
inline bool operator!=(const _Iterator<T1> other) { |
168 |
return current != other.current; |
return current != other.current; |
169 |
} |
} |
170 |
|
|
176 |
return !(current && current->data); |
return !(current && current->data); |
177 |
} |
} |
178 |
|
|
179 |
inline _Iterator moveToEndOf(RTListBase<_T>* pDstList) { |
inline _Iterator moveToEndOf(RTListBase<T1>* pDstList) { |
180 |
detach(); |
detach(); |
181 |
pDstList->append(*this); |
pDstList->append(*this); |
182 |
_Iterator iterOnDstList = _Iterator(current); |
_Iterator iterOnDstList = _Iterator(current); |
184 |
return iterOnDstList; |
return iterOnDstList; |
185 |
} |
} |
186 |
|
|
187 |
inline _Iterator moveToBeginOf(RTListBase<_T>* pDstList) { |
inline _Iterator moveToBeginOf(RTListBase<T1>* pDstList) { |
188 |
detach(); |
detach(); |
189 |
pDstList->prepend(*this); |
pDstList->prepend(*this); |
190 |
_Iterator iterOnDstList = _Iterator(current); |
_Iterator iterOnDstList = _Iterator(current); |
192 |
return iterOnDstList; |
return iterOnDstList; |
193 |
} |
} |
194 |
|
|
195 |
#if DEVMODE |
#if CONFIG_DEVMODE |
196 |
inline bool isValid() { |
inline bool isValid() { |
197 |
return current->list == list; |
return current->list == list; |
198 |
} |
} |
199 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
200 |
|
|
201 |
protected: |
protected: |
202 |
Node* current; |
Node* current; |
205 |
dir_forward, |
dir_forward, |
206 |
dir_backward |
dir_backward |
207 |
}; |
}; |
208 |
#if DEVMODE |
#if CONFIG_DEVMODE |
209 |
RTListBase<_T>* list; |
RTListBase<T1>* list; |
210 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
211 |
|
|
212 |
_Iterator(Node* pNode, dir_t direction = dir_forward) { |
_Iterator(Node* pNode, dir_t direction = dir_forward) { |
213 |
current = pNode; |
current = pNode; |
214 |
fallback = (direction == dir_forward) ? pNode->prev : pNode->next; |
fallback = (direction == dir_forward) ? pNode->prev : pNode->next; |
215 |
#if DEVMODE |
#if CONFIG_DEVMODE |
216 |
list = pNode->list; |
list = pNode->list; |
217 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
218 |
} |
} |
219 |
|
|
220 |
inline Node* node() { |
inline Node* node() { |
221 |
#if DEVMODE |
#if CONFIG_DEVMODE |
222 |
#if USE_EXCEPTIONS |
#if CONFIG_RT_EXCEPTIONS |
223 |
if (isValid()) return current; |
if (isValid()) return current; |
224 |
else throw std::runtime_error(__err_msg_iterator_invalidated); |
else throw std::runtime_error(__err_msg_iterator_invalidated); |
225 |
#else |
#else |
226 |
return (isValid()) ? current : (Node*)NULL; // force segfault if iterator became invalidated |
return (isValid()) ? current : (Node*)NULL; // force segfault if iterator became invalidated |
227 |
#endif // USE_EXCEPTIONS |
#endif // CONFIG_RT_EXCEPTIONS |
228 |
#else |
#else |
229 |
return current; |
return current; |
230 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
231 |
} |
} |
232 |
|
|
233 |
inline void detach() { |
inline void detach() { |
234 |
RTListBase<_T>::detach(*this); |
RTListBase<T1>::detach(*this); |
235 |
} |
} |
236 |
|
|
237 |
friend class RTListBase<_T>; |
friend class RTListBase<T1>; |
238 |
friend class RTList<_T>; |
friend class RTList<T1>; |
239 |
friend class Pool<_T>; |
friend class Pool<T1>; |
240 |
}; |
}; |
241 |
typedef _Iterator<T> Iterator; |
typedef _Iterator<T> Iterator; |
242 |
|
|
272 |
_end.next = &_end; |
_end.next = &_end; |
273 |
_end.prev = &_begin; |
_end.prev = &_begin; |
274 |
_end.data = NULL; |
_end.data = NULL; |
275 |
#if DEVMODE |
#if CONFIG_DEVMODE |
276 |
_begin.list = this; |
_begin.list = this; |
277 |
_end.list = this; |
_end.list = this; |
278 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
279 |
} |
} |
280 |
|
|
281 |
inline void append(Iterator itElement) { |
inline void append(Iterator itElement) { |
285 |
pNode->prev = last; // if a segfault happens here, then because 'itElement' Iterator became invalidated |
pNode->prev = last; // if a segfault happens here, then because 'itElement' Iterator became invalidated |
286 |
pNode->next = &_end; |
pNode->next = &_end; |
287 |
_end.prev = pNode; |
_end.prev = pNode; |
288 |
#if DEVMODE |
#if CONFIG_DEVMODE |
289 |
pNode->list = this; |
pNode->list = this; |
290 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
291 |
} |
} |
292 |
|
|
293 |
inline void append(Iterator itFirst, Iterator itLast) { |
inline void append(Iterator itFirst, Iterator itLast) { |
298 |
pFirst->prev = last; // if a segfault happens here, then because 'itFirst' Iterator became invalidated |
pFirst->prev = last; // if a segfault happens here, then because 'itFirst' Iterator became invalidated |
299 |
pLast->next = &_end; // if a segfault happens here, then because 'itLast' Iterator became invalidated |
pLast->next = &_end; // if a segfault happens here, then because 'itLast' Iterator became invalidated |
300 |
_end.prev = pLast; |
_end.prev = pLast; |
301 |
#if DEVMODE |
#if CONFIG_DEVMODE |
302 |
for (Node* pNode = pFirst; true; pNode = pNode->next) { |
for (Node* pNode = pFirst; true; pNode = pNode->next) { |
303 |
pNode->list = this; |
pNode->list = this; |
304 |
if (pNode == pLast) break; |
if (pNode == pLast) break; |
305 |
} |
} |
306 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
307 |
} |
} |
308 |
|
|
309 |
inline void prepend(Iterator itElement) { |
inline void prepend(Iterator itElement) { |
313 |
pNode->prev = &_begin; // if a segfault happens here, then because 'itElement' Iterator became invalidated |
pNode->prev = &_begin; // if a segfault happens here, then because 'itElement' Iterator became invalidated |
314 |
pNode->next = first; |
pNode->next = first; |
315 |
first->prev = pNode; |
first->prev = pNode; |
316 |
#if DEVMODE |
#if CONFIG_DEVMODE |
317 |
pNode->list = this; |
pNode->list = this; |
318 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
319 |
} |
} |
320 |
|
|
321 |
inline void prepend(Iterator itFirst, Iterator itLast) { |
inline void prepend(Iterator itFirst, Iterator itLast) { |
326 |
pFirst->prev = &_begin; // if a segfault happens here, then because 'itFirst' Iterator became invalidated |
pFirst->prev = &_begin; // if a segfault happens here, then because 'itFirst' Iterator became invalidated |
327 |
pLast->next = first; // if a segfault happens here, then because 'itLast' Iterator became invalidated |
pLast->next = first; // if a segfault happens here, then because 'itLast' Iterator became invalidated |
328 |
first->prev = pLast; |
first->prev = pLast; |
329 |
#if DEVMODE |
#if CONFIG_DEVMODE |
330 |
for (Node* pNode = pFirst; true; pNode = pNode->next) { |
for (Node* pNode = pFirst; true; pNode = pNode->next) { |
331 |
pNode->list = this; |
pNode->list = this; |
332 |
if (pNode == pLast) break; |
if (pNode == pLast) break; |
333 |
} |
} |
334 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
335 |
} |
} |
336 |
|
|
337 |
static inline void detach(Iterator itElement) { |
static inline void detach(Iterator itElement) { |
349 |
next->prev = prev; |
next->prev = prev; |
350 |
} |
} |
351 |
|
|
352 |
|
friend class _Iterator<T>; |
353 |
|
friend class RTList<T>; |
354 |
friend class Pool<T>; |
friend class Pool<T>; |
355 |
}; |
}; |
356 |
|
|
382 |
if (pPool->poolIsEmpty()) return RTListBase<T>::begin(); |
if (pPool->poolIsEmpty()) return RTListBase<T>::begin(); |
383 |
Iterator element = pPool->alloc(); |
Iterator element = pPool->alloc(); |
384 |
append(element); |
append(element); |
385 |
#if DEVMODE |
#if CONFIG_DEVMODE |
386 |
element.list = this; |
element.list = this; |
387 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
388 |
return element; |
return element; |
389 |
} |
} |
390 |
|
|
392 |
if (pPool->poolIsEmpty()) return RTListBase<T>::end(); |
if (pPool->poolIsEmpty()) return RTListBase<T>::end(); |
393 |
Iterator element = pPool->alloc(); |
Iterator element = pPool->alloc(); |
394 |
prepend(element); |
prepend(element); |
395 |
#if DEVMODE |
#if CONFIG_DEVMODE |
396 |
element.list = this; |
element.list = this; |
397 |
#endif // DEVMODE |
#endif // CONFIG_DEVMODE |
398 |
return element; |
return element; |
399 |
} |
} |
400 |
|
|