split out sndfile manager code into its own file; move a couple of utility functions...
[ardour2.git] / libs / pbd / file_manager.cc
blob1ababb79bbdbbd413781168679aac9a5114cb499
1 /*
2 Copyright (C) 2010 Paul Davis
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.
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.
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.
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"
32 using namespace std;
33 using namespace PBD;
35 FileManager* FileDescriptor::_manager;
37 FileManager::FileManager ()
38 : _open (0)
40 struct rlimit rl;
41 int const r = getrlimit (RLIMIT_NOFILE, &rl);
43 /* XXX: this is a bit arbitrary */
44 if (r == 0) {
45 _max_open = rl.rlim_cur - 64;
46 } else {
47 _max_open = 256;
50 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
53 void
54 FileManager::add (FileDescriptor* d)
56 Glib::Mutex::Lock lm (_mutex);
57 _files.push_back (d);
60 /** @return true on error, otherwise false */
61 bool
62 FileManager::allocate (FileDescriptor* d)
64 Glib::Mutex::Lock lm (_mutex);
66 if (!d->is_open()) {
68 /* this file needs to be opened */
70 if (_open == _max_open) {
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.
76 double lowest_last_used = DBL_MAX;
77 list<FileDescriptor*>::iterator oldest = _files.end ();
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;
88 if (oldest == _files.end()) {
89 /* no unallocated and open files exist, so there's nothing we can do */
90 return true;
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
103 if (d->open ()) {
104 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_name));
105 return true;
108 _open++;
110 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_name, _open, _max_open));
113 struct timespec t;
114 clock_gettime (CLOCK_MONOTONIC, &t);
115 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
117 d->_refcount++;
119 return false;
122 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
123 void
124 FileManager::release (FileDescriptor* d)
126 Glib::Mutex::Lock lm (_mutex);
128 d->_refcount--;
129 assert (d->_refcount >= 0);
132 /** Remove a file from our lists. It will be closed if it is currently open. */
133 void
134 FileManager::remove (FileDescriptor* d)
136 Glib::Mutex::Lock lm (_mutex);
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)
146 _files.remove (d);
149 void
150 FileManager::close (FileDescriptor* d)
152 /* we must have a lock on our mutex */
154 d->close ();
155 d->Closed (); /* EMIT SIGNAL */
156 _open--;
159 FileDescriptor::FileDescriptor (string const & n, bool w)
160 : _refcount (0)
161 , _last_used (0)
162 , _name (n)
163 , _writeable (w)
168 FileManager*
169 FileDescriptor::manager ()
171 if (_manager == 0) {
172 _manager = new FileManager;
175 return _manager;
178 /** Release a previously allocated handle to this file */
179 void
180 FileDescriptor::release ()
182 manager()->release (this);
187 /** @param n Filename.
188 * @param w true to open writeable, otherwise false.
189 * @param m Open mode for the file.
192 FdFileDescriptor::FdFileDescriptor (string const & n, bool w, mode_t m)
193 : FileDescriptor (n, w)
194 , _fd (-1)
195 , _mode (m)
197 manager()->add (this);
200 FdFileDescriptor::~FdFileDescriptor ()
202 manager()->remove (this);
205 bool
206 FdFileDescriptor::is_open () const
208 /* we must have a lock on the FileManager's mutex */
210 return _fd != -1;
213 bool
214 FdFileDescriptor::open ()
216 /* we must have a lock on the FileManager's mutex */
218 _fd = ::open (_name.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
219 return (_fd == -1);
222 void
223 FdFileDescriptor::close ()
225 /* we must have a lock on the FileManager's mutex */
227 ::close (_fd);
228 _fd = -1;
231 /** @return fd, or -1 on error */
233 FdFileDescriptor::allocate ()
235 bool const f = manager()->allocate (this);
236 if (f) {
237 return -1;
240 /* this is ok thread-wise because allocate () has incremented
241 the Descriptor's refcount, so the file will not be closed
243 return _fd;
247 /** @param n Filename.
248 * @param w true to open writeable, otherwise false.
251 StdioFileDescriptor::StdioFileDescriptor (string const & n, std::string const & m)
252 : FileDescriptor (n, false)
253 , _file (0)
254 , _mode (m)
256 manager()->add (this);
259 StdioFileDescriptor::~StdioFileDescriptor ()
261 manager()->remove (this);
264 bool
265 StdioFileDescriptor::is_open () const
267 /* we must have a lock on the FileManager's mutex */
269 return _file != 0;
272 bool
273 StdioFileDescriptor::open ()
275 /* we must have a lock on the FileManager's mutex */
277 _file = fopen (_name.c_str(), _mode.c_str());
278 return (_file == 0);
281 void
282 StdioFileDescriptor::close ()
284 /* we must have a lock on the FileManager's mutex */
286 fclose (_file);
287 _file = 0;
290 /** @return FILE*, or 0 on error */
291 FILE*
292 StdioFileDescriptor::allocate ()
294 bool const f = manager()->allocate (this);
295 if (f) {
296 return 0;
299 /* this is ok thread-wise because allocate () has incremented
300 the Descriptor's refcount, so the file will not be closed
302 return _file;