/[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 554 by schoenebeck, Thu May 19 19:25:14 2005 UTC revision 1800 by schoenebeck, Sun Dec 7 01:26:46 2008 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                              *   *   Copyright (C) 2005 - 2008 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 44  Line 44 
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 // CONFIG_DEVMODE  #endif // CONFIG_DEVMODE
46    
47    const std::string __err_msg_resize_while_in_use = "Pool::resizePool() ERROR: elements still in use!";
48    
49  // just symbol prototyping  // just symbol prototyping
50  template<typename T> class Pool;  template<typename T> class Pool;
51  template<typename T> class RTList;  template<typename T> class RTList;
# Line 260  class RTListBase { Line 262  class RTListBase {
262              return _begin.next == &_end;              return _begin.next == &_end;
263          }          }
264    
265            inline int count() {
266                int elements = 0;
267                for (Iterator it = first(); it != end(); ++it) ++elements;
268                return elements;
269            }
270    
271      protected:      protected:
272          Node _begin; // fake node (without data) which represents the begin of the list - not the first element!          Node _begin; // fake node (without data) which represents the begin of the list - not the first element!
273          Node _end;   // fake node (without data) which represents the end of the list - not the last element!          Node _end;   // fake node (without data) which represents the end of the list - not the last element!
274    
275          RTListBase() {          RTListBase() {
276                init();
277            }
278    
279            void init() {
280              // initialize boundary nodes              // initialize boundary nodes
281              _begin.prev = &_begin;              _begin.prev = &_begin;
282              _begin.next = &_end;              _begin.next = &_end;
# Line 426  class Pool : public RTList<T> { Line 438  class Pool : public RTList<T> {
438          Node*         nodes;          Node*         nodes;
439          T*            data;          T*            data;
440          RTListBase<T> freelist; // not yet allocated elements          RTListBase<T> freelist; // not yet allocated elements
441            int           poolsize;
442    
443          Pool(int Elements) : RTList<T>::RTList(this) {          Pool(int Elements) : RTList<T>::RTList(this) {
444              data  = new T[Elements];              _init(Elements);
             nodes = new Node[Elements];  
             for (int i = 0; i < Elements; i++) {  
                 nodes[i].data = &data[i];  
                 freelist.append(&nodes[i]);  
             }  
445          }          }
446    
447          virtual ~Pool() {          virtual ~Pool() {
# Line 445  class Pool : public RTList<T> { Line 453  class Pool : public RTList<T> {
453              return freelist.isEmpty();              return freelist.isEmpty();
454          }          }
455    
456            /**
457             * Returns the current size of the pool, that is the amount of
458             * pre-allocated elements from the operating system. It equals the
459             * amount of elements given to the constructor unless resizePool()
460             * is called.
461             *
462             * @see resizePool()
463             */
464            int poolSize() const {
465                return poolsize;
466            }
467    
468            /**
469             * Alters the amount of elements to be pre-allocated from the
470             * operating system for this pool object.
471             *
472             * @e CAUTION: you MUST free all elements in use before calling this
473             * method ( e.g. by calling clear() )! Also make sure that no
474             * references of elements before this call will still be used after this
475             * call, since all elements will be reallocated and their old memory
476             * addresses become invalid!
477             *
478             * @see poolSize()
479             */
480            void resizePool(int Elements) {
481                if (freelist.count() != poolsize) {
482                    #if CONFIG_DEVMODE
483                    throw std::runtime_error(__err_msg_resize_while_in_use);
484                    #else
485                    std::cerr << __err_msg_resize_while_in_use << std::endl << std::flush;
486                    // if we're here something's terribly wrong, but we try to do the best
487                    RTList<T>::clear();
488                    #endif
489                }
490                if (nodes) delete[] nodes;
491                if (data)  delete[] data;
492                freelist.init();
493                RTListBase<T>::init();
494                _init(Elements);
495            }
496    
497      protected:      protected:
498          // caution: assumes pool (that is freelist) is not empty!          // caution: assumes pool (that is freelist) is not empty!
499          inline Iterator alloc() {          inline Iterator alloc() {
# Line 462  class Pool : public RTList<T> { Line 511  class Pool : public RTList<T> {
511          }          }
512    
513          friend class RTList<T>;          friend class RTList<T>;
514    
515        private:
516            void _init(int Elements) {
517                data  = new T[Elements];
518                nodes = new Node[Elements];
519                for (int i = 0; i < Elements; i++) {
520                    nodes[i].data = &data[i];
521                    freelist.append(&nodes[i]);
522                }
523                poolsize = Elements;
524            }
525  };  };
526    
527  #endif // __LS_POOL_H__  #endif // __LS_POOL_H__

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

  ViewVC Help
Powered by ViewVC