newly created files for use in recording appear in a .stubs folder, and are moved...
[ardour2.git] / libs / pbd / pool.cc
blobaac98a2c155540a15b6f9b07770b8610f8e3cf20
1 /*
2 Copyright (C) 1998-99 Paul Barton-Davis
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.
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.
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.
18 $Id$
21 #include <cstdlib>
22 #include <iostream>
23 #include <vector>
24 #include <cstdlib>
25 #include <cassert>
27 #include "pbd/pool.h"
28 #include "pbd/error.h"
30 using namespace std;
31 using namespace PBD;
33 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
34 : free_list (nitems)
35 , _name (n)
37 _name = n;
39 /* since some overloaded ::operator new() might use this,
40 its important that we use a "lower level" allocator to
41 get more space.
44 block = malloc (nitems * item_size);
46 void **ptrlist = (void **) malloc (sizeof (void *) * nitems);
48 for (unsigned long i = 0; i < nitems; i++) {
49 ptrlist[i] = static_cast<void *> (static_cast<char*>(block) + (i * item_size));
52 free_list.write (ptrlist, nitems);
53 free (ptrlist);
56 Pool::~Pool ()
58 free (block);
61 /** Allocate an item's worth of memory in the Pool by taking one from the free list.
62 * @return Pointer to free item.
64 void *
65 Pool::alloc ()
67 void *ptr;
69 if (free_list.read (&ptr, 1) < 1) {
70 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
71 /*NOTREACHED*/
72 return 0;
73 } else {
74 return ptr;
78 /** Release an item's memory by writing its location to the free list */
79 void
80 Pool::release (void *ptr)
82 free_list.write (&ptr, 1);
85 /*---------------------------------------------*/
87 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems)
88 : Pool (n, isize, nitems)
89 , m_lock(0)
93 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
95 delete m_lock;
98 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems)
99 : Pool (n, isize, nitems)
100 , m_lock(0)
104 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
106 delete m_lock;
109 void*
110 MultiAllocSingleReleasePool::alloc ()
112 void *ptr;
113 if(!m_lock) {
114 m_lock = new Glib::Mutex();
115 // umm, I'm not sure that this doesn't also allocate memory.
116 if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
119 Glib::Mutex::Lock guard(*m_lock);
120 ptr = Pool::alloc ();
121 return ptr;
124 void
125 MultiAllocSingleReleasePool::release (void* ptr)
127 Pool::release (ptr);
130 void*
131 SingleAllocMultiReleasePool::alloc ()
133 return Pool::alloc ();
136 void
137 SingleAllocMultiReleasePool::release (void* ptr)
139 if(!m_lock) {
140 m_lock = new Glib::Mutex();
141 // umm, I'm not sure that this doesn't also allocate memory.
142 if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
144 Glib::Mutex::Lock guard(*m_lock);
145 Pool::release (ptr);
148 /*-------------------------------------------------------*/
150 static void
151 free_per_thread_pool (void* ptr)
153 /* Rather than deleting the CrossThreadPool now, we add it to our trash buffer.
154 * This prevents problems if other threads still require access to this CrossThreadPool.
155 * We assume that some other agent will clean out the trash buffer as required.
157 CrossThreadPool* cp = static_cast<CrossThreadPool*> (ptr);
158 assert (cp);
160 if (cp->empty()) {
161 /* This CrossThreadPool is already empty, and the thread is finishing so nothing
162 * more can be added to it. We can just delete the pool.
164 delete cp;
165 } else {
166 /* This CrossThreadPool is not empty, meaning that there's some Events in it
167 * which another thread may yet read, so we can't delete the pool just yet.
168 * Put it in the trash and hope someone deals with it at some stage.
170 cp->parent()->add_to_trash (cp);
174 PerThreadPool::PerThreadPool ()
175 : _trash (0)
178 /* for some reason this appears necessary to get glib's thread private stuff to work */
179 GPrivate* key;
180 key = g_private_new (NULL);
183 _key = g_private_new (free_per_thread_pool);
186 /** Create a new CrossThreadPool and set the current thread's private _key to point to it.
187 * @param n Name.
188 * @param isize Size of each item in the pool.
189 * @param nitems Number of items in the pool.
191 void
192 PerThreadPool::create_per_thread_pool (string n, unsigned long isize, unsigned long nitems)
194 CrossThreadPool* p = new CrossThreadPool (n, isize, nitems, this);
195 g_private_set (_key, p);
198 /** @return CrossThreadPool for the current thread, which must previously have been created by
199 * calling create_per_thread_pool in the current thread.
201 CrossThreadPool*
202 PerThreadPool::per_thread_pool ()
204 CrossThreadPool* p = static_cast<CrossThreadPool*> (g_private_get (_key));
205 if (!p) {
206 fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_self() << endmsg;
207 /*NOTREACHED*/
209 return p;
212 void
213 PerThreadPool::set_trash (RingBuffer<CrossThreadPool*>* t)
215 Glib::Mutex::Lock lm (_trash_mutex);
216 _trash = t;
219 /** Add a CrossThreadPool to our trash, if we have one. If not, a warning is emitted. */
220 void
221 PerThreadPool::add_to_trash (CrossThreadPool* p)
223 Glib::Mutex::Lock lm (_trash_mutex);
225 if (!_trash) {
226 warning << "Pool " << p->name() << " has no trash collector; a memory leak has therefore occurred" << endmsg;
227 return;
230 /* we have a lock here so that multiple threads can safely call add_to_trash (even though there
231 can only be one writer to the _trash RingBuffer)
234 _trash->write (&p, 1);
237 CrossThreadPool::CrossThreadPool (string n, unsigned long isize, unsigned long nitems, PerThreadPool* p)
238 : Pool (n, isize, nitems)
239 , pending (nitems)
240 , _parent (p)
245 void*
246 CrossThreadPool::alloc ()
248 void* ptr;
249 while (pending.read (&ptr, 1) == 1) {
250 free_list.write (&ptr, 1);
252 return Pool::alloc ();
255 void
256 CrossThreadPool::push (void* t)
258 pending.write (&t, 1);
261 /** @return true if there is nothing in this pool */
262 bool
263 CrossThreadPool::empty ()
265 return (free_list.write_space() == pending.read_space());