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.
28 #include "pbd/error.h"
33 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
39 /* since some overloaded ::operator new() might use this,
40 its important that we use a "lower level" allocator to
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);
61 /** Allocate an item's worth of memory in the Pool by taking one from the free list.
62 * @return Pointer to free item.
69 if (free_list.read (&ptr, 1) < 1) {
70 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
78 /** Release an item's memory by writing its location to the free list */
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)
93 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
98 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems)
99 : Pool (n, isize, nitems)
104 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
110 MultiAllocSingleReleasePool::alloc ()
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 ();
125 MultiAllocSingleReleasePool::release (void* ptr)
131 SingleAllocMultiReleasePool::alloc ()
133 return Pool::alloc ();
137 SingleAllocMultiReleasePool::release (void* ptr)
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);
148 /*-------------------------------------------------------*/
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);
160 cp->parent()->add_to_trash (cp);
163 PerThreadPool::PerThreadPool ()
167 /* for some reason this appears necessary to get glib's thread private stuff to work */
169 key = g_private_new (NULL);
172 _key = g_private_new (free_per_thread_pool);
175 /** Create a new CrossThreadPool and set the current thread's private _key to point to it.
177 * @param isize Size of each item in the pool.
178 * @param nitems Number of items in the pool.
181 PerThreadPool::create_per_thread_pool (string n, unsigned long isize, unsigned long nitems)
183 CrossThreadPool* p = new CrossThreadPool (n, isize, nitems, this);
184 g_private_set (_key, p);
187 /** @return CrossThreadPool for the current thread, which must previously have been created by
188 * calling create_per_thread_pool in the current thread.
191 PerThreadPool::per_thread_pool ()
193 CrossThreadPool* p = static_cast<CrossThreadPool*> (g_private_get (_key));
195 fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_self() << endmsg;
202 PerThreadPool::set_trash (RingBuffer<CrossThreadPool*>* t)
204 Glib::Mutex::Lock lm (_trash_mutex);
208 /** Add a CrossThreadPool to our trash, if we have one. If not, a warning is emitted. */
210 PerThreadPool::add_to_trash (CrossThreadPool* p)
212 Glib::Mutex::Lock lm (_trash_mutex);
215 warning << "Pool " << p->name() << " has no trash collector; a memory leak has therefore occurred" << endmsg;
219 /* we have a lock here so that multiple threads can safely call add_to_trash (even though there
220 can only be one writer to the _trash RingBuffer)
223 _trash->write (&p, 1);
226 CrossThreadPool::CrossThreadPool (string n, unsigned long isize, unsigned long nitems, PerThreadPool* p)
227 : Pool (n, isize, nitems)
235 CrossThreadPool::alloc ()
238 while (pending.read (&ptr, 1) == 1) {
239 free_list.write (&ptr, 1);
241 return Pool::alloc ();
245 CrossThreadPool::push (void* t)
247 pending.write (&t, 1);
250 /** @return true if there is nothing in this pool */
252 CrossThreadPool::empty ()
254 return (free_list.write_space() == pending.read_space());