pulling trunk
[ardour.git] / libs / pbd3 / pool.cc
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     $Id$
19 */
20
21 #include <iostream>
22 #include <vector>
23
24 #include <pbd/pool.h>
25 #include <pbd/error.h>
26
27 using namespace std;
28
29 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
30 {
31         _name = n;
32
33         free_list = new RingBuffer<void*> (nitems);
34
35         /* since some overloaded ::operator new() might use this,
36            its important that we use a "lower level" allocator to
37            get more space.  
38         */
39
40         block = malloc (nitems * item_size);
41
42         void **ptrlist = (void **) malloc (sizeof (void *)  * nitems);
43
44         for (unsigned long i = 0; i < nitems; i++) {
45                 ptrlist[i] = static_cast<void *> (static_cast<char*>(block) + (i * item_size));
46         }
47
48         free_list->write (ptrlist, nitems);
49
50         free (ptrlist);
51 }
52
53 Pool::~Pool ()
54 {
55         free (block);
56 }
57
58 void *
59 Pool::alloc ()
60 {
61         void *ptr;
62
63 //      cerr << _name << " pool " << " alloc, thread = " << pthread_name() << " space = " << free_list->read_space() << endl;
64
65         if (free_list->read (&ptr, 1) < 1) {
66                 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
67                 /*NOTREACHED*/
68                 return 0;
69         } else {
70                 return ptr;
71         }
72 };
73
74 void            
75 Pool::release (void *ptr)
76 {
77         free_list->write (&ptr, 1);
78 //      cerr << _name << ": release, now has " << free_list->read_space() << endl;
79 }
80
81 /*---------------------------------------------*/
82
83 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems) 
84         : Pool (n, isize, nitems),
85         m_lock(0)
86 {
87 }
88
89 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
90 {
91     if(m_lock) delete m_lock;
92 }
93
94 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems) 
95         : Pool (n, isize, nitems),
96     m_lock(0)
97 {
98 }
99
100 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
101 {
102     if(m_lock) delete m_lock;
103 }
104
105 void*
106 MultiAllocSingleReleasePool::alloc ()
107 {
108         void *ptr;
109     if(!m_lock) {
110         m_lock = new Glib::Mutex();
111         // umm, I'm not sure that this doesn't also allocate memory.
112         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
113     }
114     
115     Glib::Mutex::Lock guard(*m_lock);
116         ptr = Pool::alloc ();
117         return ptr;
118 }
119
120 void
121 MultiAllocSingleReleasePool::release (void* ptr)
122 {
123         Pool::release (ptr);
124 }
125
126 void*
127 SingleAllocMultiReleasePool::alloc ()
128 {
129         return Pool::alloc ();
130 }
131
132 void
133 SingleAllocMultiReleasePool::release (void* ptr)
134 {
135     if(!m_lock) {
136         m_lock = new Glib::Mutex();
137         // umm, I'm not sure that this doesn't also allocate memory.
138         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
139     }
140     Glib::Mutex::Lock guard(*m_lock);
141         Pool::release (ptr);
142 }
143