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.
27 #include "pbd/pthread_utils.h"
28 #include "pbd/error.h"
29 #include "pbd/debug.h"
30 #include "pbd/compose.h"
35 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
44 /* since some overloaded ::operator new() might use this,
45 its important that we use a "lower level" allocator to
49 block = malloc (nitems * item_size);
51 void **ptrlist = (void **) malloc (sizeof (void *) * nitems);
53 for (unsigned long i = 0; i < nitems; i++) {
54 ptrlist[i] = static_cast<void *> (static_cast<char*>(block) + (i * item_size));
57 free_list.write (ptrlist, nitems);
64 // TODO: after collecting some stats, use DEBUG::PoolStats here
65 cerr << "Pool: '" << _name << "' max: " << max_usage << " / " << total() << endmsg;
70 /** Allocate an item's worth of memory in the Pool by taking one from the free list.
71 * @return Pointer to free item.
79 if (used () > max_usage) {
80 max_usage = used () + 1;
84 if (free_list.read (&ptr, 1) < 1) {
85 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
86 abort(); /*NOTREACHED*/
93 /** Release an item's memory by writing its location to the free list */
95 Pool::release (void *ptr)
97 free_list.write (&ptr, 1);
100 /*---------------------------------------------*/
102 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems)
103 : Pool (n, isize, nitems)
107 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
111 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems)
112 : Pool (n, isize, nitems)
116 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
121 MultiAllocSingleReleasePool::alloc ()
124 Glib::Threads::Mutex::Lock guard (m_lock);
125 ptr = Pool::alloc ();
130 MultiAllocSingleReleasePool::release (void* ptr)
136 SingleAllocMultiReleasePool::alloc ()
138 return Pool::alloc ();
142 SingleAllocMultiReleasePool::release (void* ptr)
144 Glib::Threads::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);
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.
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 : _key (free_per_thread_pool)
180 /** Create a new CrossThreadPool and set the current thread's private _key to point to it.
182 * @param isize Size of each item in the pool.
183 * @param nitems Number of items in the pool.
186 PerThreadPool::create_per_thread_pool (string n, unsigned long isize, unsigned long nitems)
188 _key.set (new CrossThreadPool (n, isize, nitems, this));
191 /** @return True if CrossThreadPool for the current thread exists,
195 PerThreadPool::has_per_thread_pool ()
197 CrossThreadPool* p = _key.get();
205 /** @return CrossThreadPool for the current thread, which must previously have been created by
206 * calling create_per_thread_pool in the current thread.
209 PerThreadPool::per_thread_pool (bool must_exist)
211 CrossThreadPool* p = _key.get();
212 if (!p && must_exist) {
213 fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name() << endmsg;
214 abort(); /*NOTREACHED*/
220 PerThreadPool::set_trash (RingBuffer<CrossThreadPool*>* t)
222 Glib::Threads::Mutex::Lock lm (_trash_mutex);
226 /** Add a CrossThreadPool to our trash, if we have one. If not, a warning is emitted. */
228 PerThreadPool::add_to_trash (CrossThreadPool* p)
230 Glib::Threads::Mutex::Lock lm (_trash_mutex);
233 warning << "Pool " << p->name() << " has no trash collector; a memory leak has therefore occurred" << endmsg;
237 /* we have a lock here so that multiple threads can safely call add_to_trash (even though there
238 can only be one writer to the _trash RingBuffer)
241 _trash->write (&p, 1);
244 CrossThreadPool::CrossThreadPool (string n, unsigned long isize, unsigned long nitems, PerThreadPool* p)
245 : Pool (n, isize, nitems)
253 CrossThreadPool::flush_pending_with_ev (void *ptr)
260 CrossThreadPool::flush_pending ()
263 bool did_release = false;
265 DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 has %3 pending free entries waiting, status size %4 free %5 used %6\n", pthread_name(), name(), pending.read_space(),
266 total(), available(), used()));
268 while (pending.read (&ptr, 1) == 1) {
269 DEBUG_TRACE (DEBUG::Pool, string_compose ("%1 %2 pushes back a pending free list entry before allocating\n", pthread_name(), name()));
270 free_list.write (&ptr, 1);
275 DEBUG_TRACE (DEBUG::Pool, string_compose ("Pool size: %1 free %2 used %3 pending now %4\n", total(), available(), used(), pending_size()));
280 CrossThreadPool::alloc ()
282 /* process anything waiting to be deleted (i.e. moved back to the free list) */
284 /* now allocate from the potentially larger free list */
285 return Pool::alloc ();
289 CrossThreadPool::push (void* t)
291 pending.write (&t, 1);
294 /** @return true if there is nothing in this pool */
296 CrossThreadPool::empty ()
298 return (free_list.write_space() == pending.read_space());