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