merge from 2.0-ongoing @ 3581
[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 {
34         _name = n;
35
36         free_list = new RingBuffer<void*> (nitems);
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
53         free (ptrlist);
54 }
55
56 Pool::~Pool ()
57 {
58         free (block);
59 }
60
61 void *
62 Pool::alloc ()
63 {
64         void *ptr;
65
66 //      cerr << _name << " pool " << " alloc, thread = " << pthread_name() << " space = " << free_list->read_space() << endl;
67
68         if (free_list->read (&ptr, 1) < 1) {
69                 fatal << "CRITICAL: " << _name << " POOL OUT OF MEMORY - RECOMPILE WITH LARGER SIZE!!" << endmsg;
70                 /*NOTREACHED*/
71                 return 0;
72         } else {
73                 return ptr;
74         }
75 }
76
77 void            
78 Pool::release (void *ptr)
79 {
80         free_list->write (&ptr, 1);
81 //      cerr << _name << ": release, now has " << free_list->read_space() << endl;
82 }
83
84 /*---------------------------------------------*/
85
86 MultiAllocSingleReleasePool::MultiAllocSingleReleasePool (string n, unsigned long isize, unsigned long nitems) 
87         : Pool (n, isize, nitems),
88         m_lock(0)
89 {
90 }
91
92 MultiAllocSingleReleasePool::~MultiAllocSingleReleasePool ()
93 {
94     if(m_lock) delete m_lock;
95 }
96
97 SingleAllocMultiReleasePool::SingleAllocMultiReleasePool (string n, unsigned long isize, unsigned long nitems) 
98         : Pool (n, isize, nitems),
99     m_lock(0)
100 {
101 }
102
103 SingleAllocMultiReleasePool::~SingleAllocMultiReleasePool ()
104 {
105     if(m_lock) delete m_lock;
106 }
107
108 void*
109 MultiAllocSingleReleasePool::alloc ()
110 {
111         void *ptr;
112     if(!m_lock) {
113         m_lock = new Glib::Mutex();
114         // umm, I'm not sure that this doesn't also allocate memory.
115         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
116     }
117     
118     Glib::Mutex::Lock guard(*m_lock);
119         ptr = Pool::alloc ();
120         return ptr;
121 }
122
123 void
124 MultiAllocSingleReleasePool::release (void* ptr)
125 {
126         Pool::release (ptr);
127 }
128
129 void*
130 SingleAllocMultiReleasePool::alloc ()
131 {
132         return Pool::alloc ();
133 }
134
135 void
136 SingleAllocMultiReleasePool::release (void* ptr)
137 {
138     if(!m_lock) {
139         m_lock = new Glib::Mutex();
140         // umm, I'm not sure that this doesn't also allocate memory.
141         if(!m_lock) error << "cannot create Glib::Mutex in pool.cc" << endmsg;
142     }
143     Glib::Mutex::Lock guard(*m_lock);
144         Pool::release (ptr);
145 }
146