edaf0907e8ea06c523314ffe3603981bf186ed95
[ardour.git] / libs / ardour / buffer_manager.cc
1 /*
2     Copyright (C) 2010 Paul 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 */
19
20 #include <iostream>
21 #include "ardour/buffer_manager.h"
22 #include "ardour/thread_buffers.h"
23
24 using namespace ARDOUR;
25 using namespace PBD;
26
27 RingBufferNPT<ThreadBuffers*>* BufferManager::thread_buffers = 0;
28
29 void
30 BufferManager::init (uint32_t size)
31 {
32         thread_buffers = new ThreadBufferFIFO (size+1); // must be one larger than requested
33
34         /* and populate with actual ThreadBuffers 
35          */
36
37         for (uint32_t n = 0; n < size; ++n) {        
38                 ThreadBuffers* ts = new ThreadBuffers;
39                 thread_buffers->write (&ts, 1);
40         }
41 }
42
43 ThreadBuffers*
44 BufferManager::get_thread_buffers ()
45 {
46         ThreadBuffers* tbp;
47
48         if (thread_buffers->read (&tbp, 1) == 1) {
49                 return tbp;
50         }
51
52         return 0;
53 }
54
55 void
56 BufferManager::put_thread_buffers (ThreadBuffers* tbp)
57 {
58         thread_buffers->write (&tbp, 1);
59 }
60
61 void
62 BufferManager::ensure_buffers (ChanCount howmany)
63 {
64         /* this is protected by the audioengine's process lock: we do not  */
65
66         for (uint32_t n = 0; n < thread_buffers->bufsize() - 1; ++n) {
67                 thread_buffers->buffer()[n]->ensure_buffers (howmany);
68         }
69 }