kwave  18.07.70
FixedPool.h
Go to the documentation of this file.
1 /***************************************************************************
2  FixedPool.h - simple pool with fixed number of elements
3  -------------------
4  begin : Thu Jan 02 2014
5  copyright : (C) 2014 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef FIXED_POOL_H
19 #define FIXED_POOL_H
20 
21 #include "config.h"
22 
23 #include <QMutex>
24 #include <QMutexLocker>
25 #include <QQueue>
26 #include <QSemaphore>
27 
28 namespace Kwave
29 {
30  template<unsigned int SIZE, class T> class FixedPool
31  {
32  public:
36  {
37  for (unsigned int i = 0; i < SIZE; ++i)
38  release(&(m_storage[i]));
39  }
40 
42  virtual ~FixedPool() { }
43 
48  T *allocate() {
49  m_sem_free.acquire();
50  {
51  QMutexLocker _lock(&m_lock);
52  return m_free_queue.dequeue();
53  }
54  }
55 
60  void release(T *element) {
61  QMutexLocker _lock(&m_lock);
62  m_free_queue.enqueue(element);
63  m_sem_free.release();
64  }
65 
66  private:
68  T m_storage[SIZE];
69 
71  QQueue<T *> m_free_queue;
72 
74  QSemaphore m_sem_free;
75 
77  QMutex m_lock;
78  };
79 }
80 
81 #endif /* FIXED_POOL_H */
82 
83 //***************************************************************************
84 //***************************************************************************
Definition: App.h:33
void release(T *element)
Definition: FixedPool.h:60
T m_storage[SIZE]
Definition: FixedPool.h:68
QQueue< T * > m_free_queue
Definition: FixedPool.h:71
virtual ~FixedPool()
Definition: FixedPool.h:42
QSemaphore m_sem_free
Definition: FixedPool.h:74