add left/right side trim cursors and use them for region trimming, as appropriate
[ardour.git] / libs / pbd / file_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 <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <cassert>
26 #include <iostream>
27 #include <cstdio>
28 #include "pbd/compose.h"
29 #include "pbd/file_manager.h"
30 #include "pbd/debug.h"
31
32 using namespace std;
33 using namespace PBD;
34
35 FileManager* FileDescriptor::_manager;
36
37 FileManager::FileManager ()
38         : _open (0)
39 {
40         struct rlimit rl;
41         int const r = getrlimit (RLIMIT_NOFILE, &rl);
42         
43         /* XXX: this is a bit arbitrary */
44         if (r == 0) {
45                 _max_open = rl.rlim_cur - 64;
46         } else {
47                 _max_open = 256;
48         }
49
50         DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
51 }
52
53 void
54 FileManager::add (FileDescriptor* d)
55 {
56         Glib::Mutex::Lock lm (_mutex);
57         _files.push_back (d);
58 }
59
60 /** @return true on error, otherwise false */
61 bool
62 FileManager::allocate (FileDescriptor* d)
63 {
64         Glib::Mutex::Lock lm (_mutex);
65
66         if (!d->is_open()) {
67                 
68                 /* this file needs to be opened */
69                 
70                 if (_open == _max_open) {
71
72                         /* We already have the maximum allowed number of files opened, so we must try to close one.
73                            Find the unallocated, open file with the lowest last_used time.
74                         */
75
76                         double lowest_last_used = DBL_MAX;
77                         list<FileDescriptor*>::iterator oldest = _files.end ();
78
79                         for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
80                                 if ((*i)->is_open() && (*i)->_refcount == 0) {
81                                         if ((*i)->_last_used < lowest_last_used) {
82                                                 lowest_last_used = (*i)->_last_used;
83                                                 oldest = i;
84                                         }
85                                 }
86                         }
87
88                         if (oldest == _files.end()) {
89                                 /* no unallocated and open files exist, so there's nothing we can do */
90                                 return true;
91                         }
92
93                         close (*oldest);
94                         DEBUG_TRACE (
95                                 DEBUG::FileManager,
96                                 string_compose (
97                                         "closed file for %1 to release file handle; now have %2 of %3 open\n",
98                                         (*oldest)->_name, _open, _max_open
99                                         )
100                                 );
101                 }
102
103                 if (d->open ()) {
104                         DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_name));
105                         return true;
106                 }
107
108                 _open++;
109
110                 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_name, _open, _max_open));
111         }
112
113         struct timespec t;
114         clock_gettime (CLOCK_MONOTONIC, &t);
115         d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
116
117         d->_refcount++;
118         
119         return false;
120 }
121
122 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
123 void
124 FileManager::release (FileDescriptor* d)
125 {
126         Glib::Mutex::Lock lm (_mutex);
127
128         d->_refcount--;
129         assert (d->_refcount >= 0);
130 }
131
132 /** Remove a file from our lists.  It will be closed if it is currently open. */
133 void
134 FileManager::remove (FileDescriptor* d)
135 {
136         Glib::Mutex::Lock lm (_mutex);
137
138         if (d->is_open ()) {
139                 close (d);
140                 DEBUG_TRACE (
141                         DEBUG::FileManager,
142                         string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_name, _open, _max_open)
143                         );
144         }
145
146         _files.remove (d);
147 }
148
149 void
150 FileManager::close (FileDescriptor* d)
151 {
152         /* we must have a lock on our mutex */
153
154         d->close ();
155         d->Closed (); /* EMIT SIGNAL */
156         _open--;
157 }
158
159 FileDescriptor::FileDescriptor (string const & n, bool w)
160         : _refcount (0)
161         , _last_used (0)
162         , _name (n)
163         , _writeable (w)
164 {
165
166 }
167
168 FileManager*
169 FileDescriptor::manager ()
170 {
171         if (_manager == 0) {
172                 _manager = new FileManager;
173         }
174
175         return _manager;
176 }
177
178 /** Release a previously allocated handle to this file */
179 void
180 FileDescriptor::release ()
181 {
182         manager()->release (this);
183 }
184
185
186
187 /** @param n Filename.
188  *  @param w true to open writeable, otherwise false.
189  *  @param m Open mode for the file.
190  */
191
192 FdFileDescriptor::FdFileDescriptor (string const & n, bool w, mode_t m)
193         : FileDescriptor (n, w)
194         , _fd (-1)
195         , _mode (m)
196 {
197         manager()->add (this);
198 }
199
200 FdFileDescriptor::~FdFileDescriptor ()
201 {
202         manager()->remove (this);
203 }
204
205 bool
206 FdFileDescriptor::is_open () const
207 {
208         /* we must have a lock on the FileManager's mutex */
209
210         return _fd != -1;
211 }
212
213 bool
214 FdFileDescriptor::open ()
215 {
216         /* we must have a lock on the FileManager's mutex */
217         
218         _fd = ::open (_name.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
219         return (_fd == -1);
220 }
221
222 void
223 FdFileDescriptor::close ()
224 {
225         /* we must have a lock on the FileManager's mutex */
226
227         ::close (_fd);
228         _fd = -1;
229 }
230
231 /** @return fd, or -1 on error */
232 int
233 FdFileDescriptor::allocate ()
234 {
235         bool const f = manager()->allocate (this);
236         if (f) {
237                 return -1;
238         }
239
240         /* this is ok thread-wise because allocate () has incremented
241            the Descriptor's refcount, so the file will not be closed
242         */
243         return _fd;
244 }
245
246
247 /** @param n Filename.
248  *  @param w true to open writeable, otherwise false.
249  */
250
251 StdioFileDescriptor::StdioFileDescriptor (string const & n, std::string const & m)
252         : FileDescriptor (n, false)
253         , _file (0)
254         , _mode (m)
255 {
256         manager()->add (this);
257 }
258
259 StdioFileDescriptor::~StdioFileDescriptor ()
260 {
261         manager()->remove (this);
262 }
263
264 bool
265 StdioFileDescriptor::is_open () const
266 {
267         /* we must have a lock on the FileManager's mutex */
268
269         return _file != 0;
270 }
271
272 bool
273 StdioFileDescriptor::open ()
274 {
275         /* we must have a lock on the FileManager's mutex */
276         
277         _file = fopen (_name.c_str(), _mode.c_str());
278         return (_file == 0);
279 }
280
281 void
282 StdioFileDescriptor::close ()
283 {
284         /* we must have a lock on the FileManager's mutex */
285
286         fclose (_file);
287         _file = 0;
288 }
289
290 /** @return FILE*, or 0 on error */
291 FILE*
292 StdioFileDescriptor::allocate ()
293 {
294         bool const f = manager()->allocate (this);
295         if (f) {
296                 return 0;
297         }
298
299         /* this is ok thread-wise because allocate () has incremented
300            the Descriptor's refcount, so the file will not be closed
301         */
302         return _file;
303 }