more vst parameter related stuff
[ardour.git] / libs / pbd / pbd / pool.h
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis
3  
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __qm_pool_h__
21 #define __qm_pool_h__
22
23 #include <vector>
24 #include <string>
25
26 #include <glibmm/threads.h>
27
28 #include "pbd/ringbuffer.h"
29
30 /** A pool of data items that can be allocated, read from and written to
31  *  without system memory allocation or locking.
32  */
33 class Pool 
34 {
35   public:
36         Pool (std::string name, unsigned long item_size, unsigned long nitems);
37         virtual ~Pool ();
38
39         virtual void *alloc ();
40         virtual void release (void *);
41         
42         std::string name() const { return _name; }
43
44   protected:
45         RingBuffer<void*> free_list; ///< a list of pointers to free items within block
46         std::string _name;
47
48   private:
49         void *block; ///< data storage area
50 };
51
52 class SingleAllocMultiReleasePool : public Pool
53 {
54   public:
55         SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
56         ~SingleAllocMultiReleasePool ();
57
58         virtual void *alloc ();
59         virtual void release (void *);
60
61   private:
62         Glib::Threads::Mutex m_lock;
63 };
64
65
66 class MultiAllocSingleReleasePool : public Pool
67 {
68   public:
69         MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
70         ~MultiAllocSingleReleasePool ();
71
72         virtual void *alloc ();
73         virtual void release (void *);
74
75   private:
76         Glib::Threads::Mutex m_lock;
77 };
78
79 class PerThreadPool;
80
81 /** A per-thread pool of data */
82 class CrossThreadPool : public Pool
83 {
84   public:
85         CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool *);
86
87         void* alloc ();
88         void push (void *);
89
90         PerThreadPool* parent () const {
91                 return _parent;
92         }
93
94         bool empty ();
95         
96   private:
97         RingBuffer<void*> pending;
98         PerThreadPool* _parent;
99 };
100
101 /** A class to manage per-thread pools of memory.  One object of this class is instantiated,
102  *  and then it is used to create per-thread pools as required.
103  */
104 class PerThreadPool
105 {
106   public:
107         PerThreadPool ();
108
109         const Glib::Threads::Private<CrossThreadPool>& key() const { return _key; }
110
111         void  create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems);
112         CrossThreadPool* per_thread_pool ();
113
114         void set_trash (RingBuffer<CrossThreadPool*>* t);
115         void add_to_trash (CrossThreadPool *);
116
117   private:
118         Glib::Threads::Private<CrossThreadPool> _key;
119         std::string _name;
120         unsigned long _item_size;
121         unsigned long _nitems;
122
123         /** mutex to protect either changes to the _trash variable, or writes to the RingBuffer */
124         Glib::Threads::Mutex _trash_mutex;
125         RingBuffer<CrossThreadPool*>* _trash;
126 };
127
128 #endif // __qm_pool_h__