Merged revisions 6292,6294-6295,6311,6314 via svnmerge from
[ardour.git] / libs / pbd / 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 <cstdlib>
22 #include <iostream>
23 #include <vector>
24 #include <cstdlib>
25
26 #include "pbd/pool.h"
27 #include "pbd/error.h"
28
29 using namespace std;
30 using namespace PBD;
31
32 Pool::Pool (string n, unsigned long item_size, unsigned long nitems)
33         : free_list (nitems)
34         , _name (n)
35 {
36         _name = n;
37
38         /* since some overloaded ::operator new() might use this,
39            its important that we use a "lower level" allocator to
40            get more space.  
41         */
42
43         block = malloc (nitems * item_size);
44
45         void **ptrlist = (void **) malloc (sizeof (void *)  * nitems);
46
47         for (unsigned long i = 0; i < nitems; i++) {
48                 ptrlist[i] = static_cast<void *> (static_cast<char*>(block) + (i * item_size));
49         }
50
51         free_list.write (ptrlist, nitems);
52         free (ptrlist);
53 }
54
55 Pool::~Pool ()
56 {
57         free (block);
58 }
59
60 void *
61 Pool::alloc ()
62 {
63         void *ptr;
64
65 //      cerr << _name << " pool " << " alloc, thread = " << pthread_name() << " space = " << free_list->read_space() << endl;
66
67         if (free_list.read (&ptr, 1) < 1) {
68                 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
69                 /*NOTREACHED*/
70                 return 0;
71         } else {
72                 return ptr;
73         }
74 }
75
76 void            
77 Pool::release (void *ptr)
78 {
79         free_list.write (&ptr, 1);
80 //      cerr << _name << ": release, now has " << free_list->read_space() << endl;
81 }
82
83 /*---------------------------------------------*/
84
85 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems) 
86         : Pool (n, isize, nitems),
87         m_lock(0)
88 {
89 }
90
91 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
92 {
93     delete m_lock;
94 }
95
96 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems) 
97         : Pool (n, isize, nitems),
98     m_lock(0)
99 {
100 }
101
102 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
103 {
104     delete m_lock;
105 }
106
107 void*
108 MultiAllocSingleReleasePool::alloc ()
109 {
110         void *ptr;
111     if(!m_lock) {
112         m_lock = new Glib::Mutex();
113         // umm, I'm not sure that this doesn't also allocate memory.
114         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
115     }
116     
117     Glib::Mutex::Lock guard(*m_lock);
118         ptr = Pool::alloc ();
119         return ptr;
120 }
121
122 void
123 MultiAllocSingleReleasePool::release (void* ptr)
124 {
125         Pool::release (ptr);
126 }
127
128 void*
129 SingleAllocMultiReleasePool::alloc ()
130 {
131         return Pool::alloc ();
132 }
133
134 void
135 SingleAllocMultiReleasePool::release (void* ptr)
136 {
137     if(!m_lock) {
138         m_lock = new Glib::Mutex();
139         // umm, I'm not sure that this doesn't also allocate memory.
140         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
141     }
142     Glib::Mutex::Lock guard(*m_lock);
143         Pool::release (ptr);
144 }
145
146 /*-------------------------------------------------------*/
147
148 static void 
149 free_per_thread_pool (void* ptr)
150 {
151         Pool* pptr = static_cast<Pool*>(ptr);
152         delete pptr;
153 }
154  
155 PerThreadPool::PerThreadPool ()
156 {
157         {
158                 /* for some reason this appears necessary to get glib's thread private stuff to work */
159                 GPrivate* key;
160                 key = g_private_new (NULL);
161         }
162
163         _key = g_private_new (free_per_thread_pool);
164 }
165
166 void
167 PerThreadPool::create_per_thread_pool (string n, unsigned long isize, unsigned long nitems)
168 {
169         Pool* p = new CrossThreadPool (n, isize, nitems);
170         g_private_set (_key, p);
171 }
172
173 CrossThreadPool*
174 PerThreadPool::per_thread_pool ()
175 {
176         CrossThreadPool* p = static_cast<CrossThreadPool*> (g_private_get (_key));
177         if (!p) {
178                 fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_self() << endmsg;
179                 /*NOTREACHED*/
180         }
181         return p;
182 }
183
184 CrossThreadPool::CrossThreadPool  (string n, unsigned long isize, unsigned long nitems)
185         : Pool (n, isize, nitems)
186         , pending (nitems)
187 {
188         
189 }
190
191 void*
192 CrossThreadPool::alloc () 
193 {
194         void* ptr;
195         while (pending.read (&ptr, 1) == 1) {
196                 free_list.write (&ptr, 1);
197         }
198         return Pool::alloc ();
199 }
200
201 void
202 CrossThreadPool::push (void* t) 
203 {
204         pending.write (&t, 1);
205 }