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.
25 #include <pbd/error.h>
30 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
34 free_list = new RingBuffer<void*> (nitems);
36 /* since some overloaded ::operator new() might use this,
37 its important that we use a "lower level" allocator to
41 block = malloc (nitems * item_size);
43 void **ptrlist = (void **) malloc (sizeof (void *) * nitems);
45 for (unsigned long i = 0; i < nitems; i++) {
46 ptrlist[i] = static_cast<void *> (static_cast<char*>(block) + (i * item_size));
49 free_list->write (ptrlist, nitems);
64 // cerr << _name << " pool " << " alloc, thread = " << pthread_name() << " space = " << free_list->read_space() << endl;
66 if (free_list->read (&ptr, 1) < 1) {
67 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
76 Pool::release (void *ptr)
78 free_list->write (&ptr, 1);
79 // cerr << _name << ": release, now has " << free_list->read_space() << endl;
82 /*---------------------------------------------*/
84 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems)
85 : Pool (n, isize, nitems),
90 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
92 if(m_lock) delete m_lock;
95 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems)
96 : Pool (n, isize, nitems),
101 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
103 if(m_lock) delete m_lock;
107 MultiAllocSingleReleasePool::alloc ()
111 m_lock = new Glib::Mutex();
112 // umm, I'm not sure that this doesn't also allocate memory.
113 if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
116 Glib::Mutex::Lock guard(*m_lock);
117 ptr = Pool::alloc ();
122 MultiAllocSingleReleasePool::release (void* ptr)
128 SingleAllocMultiReleasePool::alloc ()
130 return Pool::alloc ();
134 SingleAllocMultiReleasePool::release (void* ptr)
137 m_lock = new Glib::Mutex();
138 // umm, I'm not sure that this doesn't also allocate memory.
139 if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
141 Glib::Mutex::Lock guard(*m_lock);