/[svn]/linuxsampler/trunk/src/common/Pool.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/Pool.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 553 by persson, Sat Mar 19 09:31:46 2005 UTC revision 554 by schoenebeck, Thu May 19 19:25:14 2005 UTC
# Line 3  Line 3 
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  *
# Line 23  Line 24 
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 // USE_EXCEPTIONS  #endif // CONFIG_RT_EXCEPTIONS
40    
41  #if DEVMODE  #if CONFIG_DEVMODE
42  const std::string __err_msg_iterator_invalidated = "Pool/RTList iterator invalidated";  # include <string>
43  # include <iostream>  # include <iostream>
44  #endif  const std::string __err_msg_iterator_invalidated = "Pool/RTList iterator invalidated";
45    #endif // CONFIG_DEVMODE
46    
47  // just symbol prototyping  // just symbol prototyping
48  template<typename T> class Pool;  template<typename T> class Pool;
# Line 54  class RTListBase { Line 56  class RTListBase {
56              _Node<T1>* next;              _Node<T1>* next;
57              _Node<T1>* prev;              _Node<T1>* prev;
58              T1* data;              T1* data;
59              #if DEVMODE              #if CONFIG_DEVMODE
60              RTListBase<T1>* 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;
# Line 76  class RTListBase { Line 78  class RTListBase {
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 (!isValid()) {                      if (!isValid()) {
90                          #if USE_EXCEPTIONS                          #if CONFIG_RT_EXCEPTIONS
91                          throw std::runtime_error(__err_msg_iterator_invalidated);                          throw std::runtime_error(__err_msg_iterator_invalidated);
92                          #else                          #else
93                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;
94                          return *(_Iterator*)NULL; // force segfault if iterator became invalidated                          return *(_Iterator*)NULL; // force segfault if iterator became invalidated
95                          #endif // USE_EXCEPTIONS                          #endif // CONFIG_RT_EXCEPTIONS
96                      }                      }
97                      #endif // DEVMODE                      #endif // CONFIG_DEVMODE
98                      fallback = current;                      fallback = current;
99                      current  = current->next;                      current  = current->next;
100                      return *this;                      return *this;
# Line 107  class RTListBase { Line 109  class RTListBase {
109    
110                  /// prefix decrement op.                  /// prefix decrement op.
111                  inline _Iterator& operator--() {                  inline _Iterator& operator--() {
112                      #if DEVMODE                                          #if CONFIG_DEVMODE
113                      if (!isValid()) {                      if (!isValid()) {
114                          #if USE_EXCEPTIONS                          #if CONFIG_RT_EXCEPTIONS
115                          throw std::runtime_error(__err_msg_iterator_invalidated);                          throw std::runtime_error(__err_msg_iterator_invalidated);
116                          #else                          #else
117                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;
118                          return *(_Iterator*)NULL; // force segfault if iterator became invalidated                          return *(_Iterator*)NULL; // force segfault if iterator became invalidated
119                          #endif // USE_EXCEPTIONS                          #endif // CONFIG_RT_EXCEPTIONS
120                      }                                          }
121                      #endif // DEVMODE                      #endif // CONFIG_DEVMODE
122                      fallback = current;                      fallback = current;
123                      current  = current->prev;                      current  = current->prev;
124                      return *this;                      return *this;
# Line 130  class RTListBase { Line 132  class RTListBase {
132                  }                  }
133    
134                  inline T1& operator*() {                  inline T1& operator*() {
135                      #if DEVMODE                                          #if CONFIG_DEVMODE
136                      if (!isValid()) { // if iterator became invalidated                      if (!isValid()) { // if iterator became invalidated
137                          #if USE_EXCEPTIONS                          #if CONFIG_RT_EXCEPTIONS
138                          throw std::runtime_error(__err_msg_iterator_invalidated);                          throw std::runtime_error(__err_msg_iterator_invalidated);
139                          #else                          #else
140                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;
141                          return *((T1*)NULL); // force segfault if iterator became invalidated                          return *((T1*)NULL); // force segfault if iterator became invalidated
142                          #endif // USE_EXCEPTIONS                          #endif // CONFIG_RT_EXCEPTIONS
143                      }                      }
144                      #endif // DEVMODE                      #endif // CONFIG_DEVMODE
145                      return *current->data;                      return *current->data;
146                        
147                  }                  }
148    
149                  inline T1* operator->() {                  inline T1* operator->() {
150                      #if DEVMODE                      #if CONFIG_DEVMODE
151                      if (!isValid()) { // if iterator became invalidated                      if (!isValid()) { // if iterator became invalidated
152                          #if USE_EXCEPTIONS                          #if CONFIG_RT_EXCEPTIONS
153                          throw std::runtime_error(__err_msg_iterator_invalidated);                          throw std::runtime_error(__err_msg_iterator_invalidated);
154                          #else                          #else
155                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;                          std::cerr << __err_msg_iterator_invalidated << std::endl << std::flush;
156                          return (T1*)NULL; // force segfault if iterator became invalidated                          return (T1*)NULL; // force segfault if iterator became invalidated
157                          #endif // USE_EXCEPTIONS                          #endif // CONFIG_RT_EXCEPTIONS
158                      }                      }
159                      #endif // DEVMODE                      #endif // CONFIG_DEVMODE
160                      return current->data;                                          return current->data;
161                  }                  }
162    
163                  inline bool operator==(const _Iterator<T1> other) {                  inline bool operator==(const _Iterator<T1> other) {
# Line 190  class RTListBase { Line 192  class RTListBase {
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;
# Line 203  class RTListBase { Line 205  class RTListBase {
205                      dir_forward,                      dir_forward,
206                      dir_backward                      dir_backward
207                  };                  };
208                  #if DEVMODE                  #if CONFIG_DEVMODE
209                  RTListBase<T1>* 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() {
# Line 270  class RTListBase { Line 272  class RTListBase {
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) {
# Line 283  class RTListBase { Line 285  class RTListBase {
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) {
# Line 296  class RTListBase { Line 298  class RTListBase {
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) {
# Line 311  class RTListBase { Line 313  class RTListBase {
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) {
# Line 324  class RTListBase { Line 326  class RTListBase {
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) {
# Line 380  class RTList : public RTListBase<T> { Line 382  class RTList : public RTListBase<T> {
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    
# Line 390  class RTList : public RTListBase<T> { Line 392  class RTList : public RTListBase<T> {
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    

Legend:
Removed from v.553  
changed lines
  Added in v.554

  ViewVC Help
Powered by ViewVC