remove file manager LRU cache from code.

This was a very clever attempt to fix a non-problem. If the platform doesn't have enough file descriptors available
then the platform is broken and we're not going to hack around trying to fix it.
This commit is contained in:
Paul Davis 2014-12-10 18:28:55 -05:00
parent 17707b9674
commit 294b99aabf
15 changed files with 96 additions and 793 deletions

View File

@ -32,7 +32,6 @@
#include "ardour/source.h"
#include "ardour/ardour.h"
#include "ardour/readable.h"
#include "pbd/file_manager.h"
#include "pbd/stateful.h"
#include "pbd/xml++.h"
@ -160,7 +159,6 @@ class LIBARDOUR_API AudioSource : virtual public Source,
*/
mutable Glib::Threads::Mutex _peaks_ready_lock;
PBD::FdFileDescriptor* _peakfile_descriptor;
int _peakfile_fd;
framecnt_t peak_leftover_cnt;
framecnt_t peak_leftover_size;

View File

@ -110,8 +110,6 @@ protected:
uint16_t _channel;
bool _within_session;
std::string _origin;
bool _open;
};
} // namespace ARDOUR

View File

@ -77,8 +77,16 @@ public:
void set_path (const std::string& newpath);
private:
int open_for_write ();
bool _open;
Evoral::MusicalTime _last_ev_time_beats;
framepos_t _last_ev_time_frames;
/** end time (start + duration) of last call to read_unlocked */
mutable framepos_t _smf_last_read_end;
/** time (in SMF ticks, 1 tick per _ppqn) of the last event read by read_unlocked */
mutable framepos_t _smf_last_read_time;
int open_for_write ();
framecnt_t read_unlocked (Evoral::EventSink<framepos_t>& dst,
framepos_t position,
framepos_t start,
@ -89,12 +97,6 @@ public:
framepos_t position,
framecnt_t cnt);
Evoral::MusicalTime _last_ev_time_beats;
framepos_t _last_ev_time_frames;
/** end time (start + duration) of last call to read_unlocked */
mutable framepos_t _smf_last_read_end;
/** time (in SMF ticks, 1 tick per _ppqn) of the last event read by read_unlocked */
mutable framepos_t _smf_last_read_time;
};
}; /* namespace ARDOUR */

View File

@ -24,7 +24,6 @@
#include "ardour/audiofilesource.h"
#include "ardour/broadcast_info.h"
#include "pbd/sndfile_manager.h"
namespace ARDOUR {
@ -75,8 +74,6 @@ class LIBARDOUR_API SndFileSource : public AudioFileSource {
static int get_soundfile_info (const std::string& path, SoundFileInfo& _info, std::string& error_msg);
void release_descriptor ();
protected:
void set_path (const std::string& p);
void set_header_timeline_position ();
@ -86,7 +83,7 @@ class LIBARDOUR_API SndFileSource : public AudioFileSource {
framecnt_t write_float (Sample* data, framepos_t pos, framecnt_t cnt);
private:
PBD::SndFileDescriptor* _descriptor;
SNDFILE* _sndfile;
SF_INFO _info;
BroadcastInfo *_broadcast_info;
@ -113,7 +110,6 @@ class LIBARDOUR_API SndFileSource : public AudioFileSource {
framecnt_t nondestructive_write_unlocked (Sample *dst, framecnt_t cnt);
void handle_header_position_change ();
PBD::ScopedConnection header_position_connection;
PBD::ScopedConnection file_manager_connection;
};
} // namespace ARDOUR

View File

@ -71,27 +71,25 @@ bool AudioSource::_build_peakfiles = false;
AudioSource::AudioSource (Session& s, string name)
: Source (s, DataType::AUDIO, name)
, _length (0)
, _peak_byte_max (0)
, _peaks_built (false)
, _peakfile_fd (-1)
, peak_leftover_cnt (0)
, peak_leftover_size (0)
, peak_leftovers (0)
{
_peaks_built = false;
_peak_byte_max = 0;
_peakfile_descriptor = 0;
peak_leftover_cnt = 0;
peak_leftover_size = 0;
peak_leftovers = 0;
}
AudioSource::AudioSource (Session& s, const XMLNode& node)
: Source (s, node)
, _length (0)
, _peak_byte_max (0)
, _peaks_built (false)
, _peakfile_fd (-1)
, peak_leftover_cnt (0)
, peak_leftover_size (0)
, peak_leftovers (0)
{
_peaks_built = false;
_peak_byte_max = 0;
_peakfile_descriptor = 0;
peak_leftover_cnt = 0;
peak_leftover_size = 0;
peak_leftovers = 0;
if (set_state (node, Stateful::loading_state_version)) {
throw failed_constructor();
}
@ -105,7 +103,9 @@ AudioSource::~AudioSource ()
cerr << "AudioSource destroyed with leftover peak data pending" << endl;
}
delete _peakfile_descriptor;
close (_peakfile_fd);
_peakfile_fd = -1;
delete [] peak_leftovers;
}
@ -318,6 +318,13 @@ AudioSource::read_peaks (PeakData *peaks, framecnt_t npeaks, framepos_t start, f
* @param npeaks Number of peaks to write.
*/
struct ScopedFileDescriptor {
ScopedFileDescriptor (int fd) : _fd (fd) {}
~ScopedFileDescriptor() { close (_fd); }
operator int() { return _fd; }
int _fd;
};
int
AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt,
double samples_per_visual_peak, framecnt_t samples_per_file_peak) const
@ -331,9 +338,13 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
ssize_t nread;
framecnt_t zero_fill = 0;
boost::scoped_ptr<FdFileDescriptor> peakfile_descriptor(new FdFileDescriptor (peakpath, false, 0664));
int peakfile_fd = -1;
ScopedFileDescriptor sfd (::open (peakpath.c_str(), O_RDONLY));
if (sfd < 0) {
error << string_compose (_("Cannot open peakfile @ %1 for reading (%2)"), peakpath, strerror (errno)) << endmsg;
return -1;
}
expected_peaks = (cnt / (double) samples_per_file_peak);
scale = npeaks/expected_peaks;
@ -382,21 +393,16 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
ssize_t bytes_to_read = sizeof (PeakData)* npeaks;
/* open, read, close */
if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
return -1;
}
DEBUG_TRACE (DEBUG::Peaks, "DIRECT PEAKS\n");
offset = lseek (peakfile_fd, first_peak_byte, SEEK_SET);
offset = lseek (sfd, first_peak_byte, SEEK_SET);
if (offset != first_peak_byte) {
error << string_compose(_("AudioSource: could not seek to correct location in peak file \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
return -1;
}
nread = ::read (peakfile_fd, peaks, bytes_to_read);
nread = ::read (sfd, peaks, bytes_to_read);
if (nread != bytes_to_read) {
DEBUG_TRACE (DEBUG::Peaks, string_compose ("[%1]: Cannot read peaks from peakfile! (read only %2 not %3 at sample %4 = byte %5 )\n"
@ -449,11 +455,6 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
/* open ... close during out: handling */
if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
return 0;
}
while (nvisual_peaks < npeaks) {
if (i == stored_peaks_read) {
@ -467,16 +468,16 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
, bytes_to_read, start_byte));
off_t offset = lseek (peakfile_fd, start_byte, SEEK_SET);
off_t offset = lseek (sfd, start_byte, SEEK_SET);
if (offset != start_byte) {
error << string_compose(_("AudioSource: could not seek to correct location in peak file \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
return -1;
}
if ((nread = ::read (peakfile_fd, staging.get(), bytes_to_read)) != bytes_to_read) {
if ((nread = ::read (sfd, staging.get(), bytes_to_read)) != bytes_to_read) {
off_t fend = lseek (peakfile_fd, 0, SEEK_END);
off_t fend = lseek (sfd, 0, SEEK_END);
DEBUG_TRACE (DEBUG::Peaks, string_compose ("[%1]: cannot read peak data from peakfile (%2 peaks instead of %3) (%4) at start_byte = %5 _length = %6 versus len = %7 expected maxpeaks = %8 npeaks was %9"
, _name, (nread / sizeof(PeakData)), to_read, g_strerror (errno), start_byte, _length, fend, ((_length - current_frame)/samples_per_file_peak), npeaks));
@ -667,8 +668,7 @@ AudioSource::build_peaks_from_scratch ()
int
AudioSource::prepare_for_peakfile_writes ()
{
_peakfile_descriptor = new FdFileDescriptor (peakpath, true, 0664);
if ((_peakfile_fd = _peakfile_descriptor->allocate()) < 0) {
if ((_peakfile_fd = open (peakpath.c_str(), O_CREAT|O_RDWR, 0664)) < 0) {
error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
return -1;
}
@ -688,8 +688,8 @@ AudioSource::done_with_peakfile_writes (bool done)
PeaksReady (); /* EMIT SIGNAL */
}
delete _peakfile_descriptor;
_peakfile_descriptor = 0;
close (_peakfile_fd);
_peakfile_fd = -1;
}
/** @param first_frame Offset from the source start of the first frame to process */
@ -712,8 +712,10 @@ AudioSource::compute_and_write_peaks (Sample* buf, framecnt_t first_frame, frame
off_t first_peak_byte;
boost::scoped_array<Sample> buf2;
if (_peakfile_descriptor == 0) {
prepare_for_peakfile_writes ();
if (_peakfile_fd < 0) {
if (prepare_for_peakfile_writes ()) {
return -1;
}
}
restart:
@ -887,7 +889,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, framecnt_t first_frame, frame
void
AudioSource::truncate_peakfile ()
{
if (_peakfile_descriptor == 0) {
if (_peakfile_fd < 0) {
error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
<< endmsg;
return;

View File

@ -59,7 +59,6 @@ FileSource::FileSource (Session& session, DataType type, const string& path, con
, _file_is_new (!origin.empty()) // if origin is left unspecified (empty string) then file must exist
, _channel (0)
, _origin (origin)
, _open (false)
{
set_within_session_from_path (path);
}
@ -68,7 +67,6 @@ FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist
: Source (session, node)
, _file_is_new (false)
, _channel (0)
, _open (false)
{
/* this setting of _path is temporary - we expect derived classes
to call ::init() which will actually locate the file

View File

@ -59,6 +59,7 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
, MidiSource(s, path, flags)
, FileSource(s, DataType::MIDI, path, string(), flags)
, Evoral::SMF()
, _open (false)
, _last_ev_time_beats(0.0)
, _last_ev_time_frames(0)
, _smf_last_read_end (0)
@ -94,6 +95,7 @@ SMFSource::SMFSource (Session& s, const string& path)
, MidiSource(s, path, Source::Flag (0))
, FileSource(s, DataType::MIDI, path, string(), Source::Flag (0))
, Evoral::SMF()
, _open (false)
, _last_ev_time_beats(0.0)
, _last_ev_time_frames(0)
, _smf_last_read_end (0)
@ -125,6 +127,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
: Source(s, node)
, MidiSource(s, node)
, FileSource(s, node, must_exist)
, _open (false)
, _last_ev_time_beats(0.0)
, _last_ev_time_frames(0)
, _smf_last_read_end (0)
@ -174,7 +177,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
return;
}
if (open(_path)) {
if (open (_path)) {
throw failed_constructor ();
}

View File

@ -58,7 +58,7 @@ const Source::Flag SndFileSource::default_writable_flags = Source::Flag (
SndFileSource::SndFileSource (Session& s, const XMLNode& node)
: Source(s, node)
, AudioFileSource (s, node)
, _descriptor (0)
, _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@ -82,7 +82,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, int chn, Flag flag
: Source(s, DataType::AUDIO, path, flags)
/* note that the origin of an external file is itself */
, AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
, _descriptor (0)
, _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@ -108,7 +108,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, const string& orig
SampleFormat sfmt, HeaderFormat hf, framecnt_t rate, Flag flags)
: Source(s, DataType::AUDIO, path, flags)
, AudioFileSource (s, path, origin, flags, sfmt, hf)
, _descriptor (0)
, _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@ -194,7 +194,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, int chn)
: Source (s, DataType::AUDIO, path, Flag (0))
/* the final boolean argument is not used, its value is irrelevant. see audiofilesource.h for explanation */
, AudioFileSource (s, path, Flag (0))
, _descriptor (0)
, _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@ -235,17 +235,19 @@ SndFileSource::open ()
{
string path_to_open;
if (_sndfile) {
return 0;
}
#ifdef PLATFORM_WINDOWS
path_to_open = Glib::locale_from_utf8(_path);
#else
path_to_open = _path;
#endif
_descriptor = new SndFileDescriptor (path_to_open.c_str(), writable(), &_info);
_descriptor->Closed.connect_same_thread (file_manager_connection, boost::bind (&SndFileSource::file_closed, this));
SNDFILE* sf = _descriptor->allocate ();
_sndfile = sf_open (path_to_open.c_str(), writable() ? SFM_RDWR : SFM_READ, &_info);
if (sf == 0) {
if (_sndfile == 0) {
char errbuf[1024];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
#ifndef HAVE_COREAUDIO
@ -265,8 +267,8 @@ SndFileSource::open ()
#ifndef HAVE_COREAUDIO
error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, _channel) << endmsg;
#endif
delete _descriptor;
_descriptor = 0;
sf_close (_sndfile);
_sndfile = 0;
return -1;
}
@ -276,7 +278,7 @@ SndFileSource::open ()
_broadcast_info = new BroadcastInfo;
}
bool bwf_info_exists = _broadcast_info->load_from_file (sf);
bool bwf_info_exists = _broadcast_info->load_from_file (_sndfile);
if (_file_is_new && _length == 0 && writable() && !bwf_info_exists) {
/* newly created files will not have a BWF header at this point in time.
@ -305,7 +307,7 @@ SndFileSource::open ()
}
if (writable()) {
sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
sf_command (_sndfile, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
if (_flags & Broadcast) {
@ -316,7 +318,7 @@ SndFileSource::open ()
_broadcast_info->set_from_session (_session, header_position_offset);
_broadcast_info->set_description (string_compose ("BWF %1", _name));
if (!_broadcast_info->write_to_file (sf)) {
if (!_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
path_to_open, _broadcast_info->get_error())
<< endmsg;
@ -326,15 +328,16 @@ SndFileSource::open ()
}
}
}
_descriptor->release ();
_open = true;
return 0;
}
SndFileSource::~SndFileSource ()
{
delete _descriptor;
if (_sndfile) {
sf_close (_sndfile);
_sndfile = 0;
}
delete _broadcast_info;
delete [] xfade_buf;
}
@ -355,15 +358,13 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
uint32_t real_cnt;
framepos_t file_cnt;
if (writable() && !_open) {
if (writable() && !_sndfile) {
/* file has not been opened yet - nothing written to it */
memset (dst, 0, sizeof (Sample) * cnt);
return cnt;
}
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0) {
if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 for reading."), _path) << endmsg;
return 0;
}
@ -396,22 +397,20 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
if (file_cnt) {
if (sf_seek (sf, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
if (sf_seek (_sndfile, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.val().substr (1), errbuf) << endmsg;
_descriptor->release ();
return 0;
}
if (_info.channels == 1) {
framecnt_t ret = sf_read_float (sf, dst, file_cnt);
framecnt_t ret = sf_read_float (_sndfile, dst, file_cnt);
if (ret != file_cnt) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose(_("SndFileSource: @ %1 could not read %2 within %3 (%4) (len = %5, ret was %6)"), start, file_cnt, _name.val().substr (1), errbuf, _length, ret) << endl;
}
_descriptor->release ();
return ret;
}
}
@ -420,7 +419,7 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
Sample* interleave_buf = get_interleave_buffer (real_cnt);
nread = sf_read_float (sf, interleave_buf, real_cnt);
nread = sf_read_float (_sndfile, interleave_buf, real_cnt);
ptr = interleave_buf + _channel;
nread /= _info.channels;
@ -431,14 +430,13 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
ptr += _info.channels;
}
_descriptor->release ();
return nread;
}
framecnt_t
SndFileSource::write_unlocked (Sample *data, framecnt_t cnt)
{
if (!_open && open()) {
if (open()) {
return 0; // failure
}
@ -589,19 +587,12 @@ SndFileSource::flush_header ()
return -1;
}
if (!_open) {
warning << string_compose (_("attempt to flush an un-opened audio file source (%1)"), _path) << endmsg;
return -1;
}
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0) {
if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 to write header"), _path) << endmsg;
return -1;
}
int const r = sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE;
_descriptor->release ();
int const r = sf_command (_sndfile, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE;
return r;
}
@ -609,25 +600,18 @@ SndFileSource::flush_header ()
void
SndFileSource::flush ()
{
if (!_open) {
warning << string_compose (_("attempt to flush an un-opened audio file source (%1)"), _path) << endmsg;
return;
}
if (!writable()) {
warning << string_compose (_("attempt to flush a non-writable audio file source (%1)"), _path) << endmsg;
return;
}
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0) {
if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 to flush contents"), _path) << endmsg;
return;
}
// Hopefully everything OK
sf_write_sync (sf);
_descriptor->release ();
sf_write_sync (_sndfile);
}
int
@ -638,7 +622,7 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
return -1;
}
if (!_open) {
if (!_sndfile) {
warning << string_compose (_("attempt to set BWF info for an un-opened audio file source (%1)"), _path) << endmsg;
return -1;
}
@ -654,9 +638,7 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
set_header_timeline_position ();
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
if (!_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
_path, _broadcast_info->get_error())
<< endmsg;
@ -665,7 +647,6 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
_broadcast_info = 0;
}
_descriptor->release ();
return 0;
}
@ -678,9 +659,7 @@ SndFileSource::set_header_timeline_position ()
_broadcast_info->set_time_reference (_timeline_position);
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
if (_sndfile == 0 || !_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
_path, _broadcast_info->get_error())
<< endmsg;
@ -688,29 +667,22 @@ SndFileSource::set_header_timeline_position ()
delete _broadcast_info;
_broadcast_info = 0;
}
_descriptor->release ();
}
framecnt_t
SndFileSource::write_float (Sample* data, framepos_t frame_pos, framecnt_t cnt)
{
SNDFILE* sf = _descriptor->allocate ();
if (sf == 0 || sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
if (_sndfile == 0 || sf_seek (_sndfile, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose (_("%1: cannot seek to %2 (libsndfile error: %3)"), _path, frame_pos, errbuf) << endmsg;
_descriptor->release ();
return 0;
}
if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {
_descriptor->release ();
if (sf_writef_float (_sndfile, data, cnt) != (ssize_t) cnt) {
return 0;
}
_descriptor->release ();
return cnt;
}
@ -1011,17 +983,5 @@ void
SndFileSource::set_path (const string& p)
{
FileSource::set_path (p);
if (_descriptor) {
_descriptor->set_path (_path);
}
}
void
SndFileSource::release_descriptor ()
{
if (_descriptor) {
_descriptor->release ();
_descriptor = 0;
}
}

View File

@ -25,7 +25,6 @@
#include "evoral/Event.hpp"
#include "evoral/SMF.hpp"
#include "evoral/midi_util.h"
#include "pbd/file_manager.h"
#ifdef COMPILER_MSVC
extern double round(double x);
@ -78,8 +77,7 @@ SMF::seek_to_track(int track)
bool
SMF::test(const std::string& path)
{
PBD::StdioFileDescriptor d (path, "r");
FILE* f = d.allocate ();
FILE* f = fopen (path.c_str(), "r");
if (f == 0) {
return false;
}
@ -110,8 +108,7 @@ SMF::open(const std::string& path, int track) THROW_FILE_ERROR
_file_path = path;
PBD::StdioFileDescriptor d (_file_path, "r");
FILE* f = d.allocate ();
FILE* f = fopen (_file_path.c_str(), "r");
if (f == 0) {
return -1;
}
@ -182,8 +179,7 @@ SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
{
/* put a stub file on disk */
PBD::StdioFileDescriptor d (_file_path, "w+");
FILE* f = d.allocate ();
FILE* f = fopen (_file_path.c_str(), "w+");
if (f == 0) {
return -1;
}
@ -396,8 +392,7 @@ void
SMF::end_write() THROW_FILE_ERROR
{
Glib::Threads::Mutex::Lock lm (_smf_lock);
PBD::StdioFileDescriptor d (_file_path, "w+");
FILE* f = d.allocate ();
FILE* f = fopen (_file_path.c_str(), "w+");
if (f == 0) {
throw FileError (_file_path);
}

View File

@ -1,334 +0,0 @@
/*
Copyright (C) 2010 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cassert>
#include <cstdio>
#include <glib.h>
#include <glib/gstdio.h>
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
#include "pbd/compose.h"
#include "pbd/file_manager.h"
#include "pbd/resource.h"
#include "pbd/debug.h"
using namespace std;
using namespace PBD;
FileManager* FileDescriptor::_manager;
FileManager::FileManager ()
: _open (0)
{
struct ResourceLimit rl;
/* XXX: this is a bit arbitrary */
if (get_resource_limit (OpenFiles, rl)) {
_max_open = rl.current_limit - 64;
} else {
_max_open = 256;
}
DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
}
void
FileManager::add (FileDescriptor* d)
{
Glib::Threads::Mutex::Lock lm (_mutex);
_files.push_back (d);
}
/** @return true on error, otherwise false */
bool
FileManager::allocate (FileDescriptor* d)
{
Glib::Threads::Mutex::Lock lm (_mutex);
if (!d->is_open()) {
/* this file needs to be opened */
if (_open == _max_open) {
/* We already have the maximum allowed number of files opened, so we must try to close one.
Find the unallocated, open file with the lowest last_used time.
*/
double lowest_last_used = DBL_MAX;
list<FileDescriptor*>::iterator oldest = _files.end ();
for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
if ((*i)->is_open() && (*i)->_refcount == 0) {
if ((*i)->_last_used < lowest_last_used) {
lowest_last_used = (*i)->_last_used;
oldest = i;
}
}
}
if (oldest == _files.end()) {
/* no unallocated and open files exist, so there's nothing we can do */
return true;
}
close (*oldest);
DEBUG_TRACE (
DEBUG::FileManager,
string_compose (
"closed file for %1 to release file handle; now have %2 of %3 open\n",
(*oldest)->_path, _open, _max_open
)
);
}
if (d->open ()) {
DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
return true;
}
_open++;
DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
}
#ifdef __APPLE__
d->_last_used = mach_absolute_time();
#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_C_SOURCE >= 199309L)
struct timespec t;
clock_gettime (CLOCK_MONOTONIC, &t);
d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
#else
struct timeval now;
gettimeofday (&now, NULL);
d->_last_used = now.tv_sec + (double) now.tv_usec / 10e6;
#endif
d->_refcount++;
return false;
}
/** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
void
FileManager::release (FileDescriptor* d)
{
Glib::Threads::Mutex::Lock lm (_mutex);
d->_refcount--;
assert (d->_refcount >= 0);
}
/** Remove a file from our lists. It will be closed if it is currently open. */
void
FileManager::remove (FileDescriptor* d)
{
Glib::Threads::Mutex::Lock lm (_mutex);
if (d->is_open ()) {
close (d);
DEBUG_TRACE (
DEBUG::FileManager,
string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
);
}
_files.remove (d);
}
void
FileManager::close (FileDescriptor* d)
{
/* we must have a lock on our mutex */
d->close ();
d->Closed (); /* EMIT SIGNAL */
_open--;
}
FileDescriptor::FileDescriptor (string const & n, bool w)
: _refcount (0)
, _last_used (0)
, _path (n)
, _writeable (w)
{
}
FileManager*
FileDescriptor::manager ()
{
if (_manager == 0) {
_manager = new FileManager;
}
return _manager;
}
/** Release a previously allocated handle to this file */
void
FileDescriptor::release ()
{
manager()->release (this);
}
/** @param file_name Filename.
* @param writeable true to open writeable, otherwise false.
* @param mode Open mode for the file.
*/
FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode)
: FileDescriptor (file_name, writeable)
, _fd (-1)
, _mode (mode)
{
manager()->add (this);
}
FdFileDescriptor::~FdFileDescriptor ()
{
manager()->remove (this);
}
bool
FdFileDescriptor::is_open () const
{
/* we must have a lock on the FileManager's mutex */
return _fd != -1;
}
bool
FdFileDescriptor::open ()
{
/* we must have a lock on the FileManager's mutex */
/* files must be opened with O_BINARY flag on windows
* or it treats the file as a text stream and puts in
* line endings in etc
*/
#ifdef PLATFORM_WINDOWS
#define WRITE_FLAGS O_RDWR | O_CREAT | O_BINARY
#define READ_FLAGS O_RDONLY | O_BINARY
#else
#define WRITE_FLAGS O_RDWR | O_CREAT
#define READ_FLAGS O_RDONLY
#endif
_fd = ::g_open (_path.c_str(), _writeable ? WRITE_FLAGS : READ_FLAGS, _mode);
return (_fd == -1);
}
void
FdFileDescriptor::close ()
{
/* we must have a lock on the FileManager's mutex */
::close (_fd);
_fd = -1;
}
/** @return fd, or -1 on error */
int
FdFileDescriptor::allocate ()
{
bool const f = manager()->allocate (this);
if (f) {
return -1;
}
/* this is ok thread-wise because allocate () has incremented
the Descriptor's refcount, so the file will not be closed
*/
return _fd;
}
void
FileDescriptor::set_path (const string& p)
{
_path = p;
}
/** @param file_name Filename.
* @param mode Mode to pass to fopen.
*/
StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
: FileDescriptor (file_name, false)
, _file (0)
, _mode (mode)
{
manager()->add (this);
}
StdioFileDescriptor::~StdioFileDescriptor ()
{
manager()->remove (this);
}
bool
StdioFileDescriptor::is_open () const
{
/* we must have a lock on the FileManager's mutex */
return _file != 0;
}
bool
StdioFileDescriptor::open ()
{
/* we must have a lock on the FileManager's mutex */
_file = fopen (_path.c_str(), _mode.c_str());
return (_file == 0);
}
void
StdioFileDescriptor::close ()
{
/* we must have a lock on the FileManager's mutex */
fclose (_file);
_file = 0;
}
/** @return FILE*, or 0 on error */
FILE*
StdioFileDescriptor::allocate ()
{
bool const f = manager()->allocate (this);
if (f) {
return 0;
}
/* this is ok thread-wise because allocate () has incremented
the Descriptor's refcount, so the file will not be closed
*/
return _file;
}

View File

@ -50,7 +50,6 @@
#endif
#include "pbd/compose.h"
#include "pbd/file_manager.h"
#include "pbd/file_utils.h"
#include "pbd/debug.h"
#include "pbd/error.h"
@ -282,11 +281,9 @@ copy_file(const std::string & from_path, const std::string & to_path)
{
if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
FdFileDescriptor from_file(from_path, false, 0444);
FdFileDescriptor to_file(to_path, true, 0666);
int fd_from (::open (from_path.c_str(), O_RDONLY));
int fd_to (::open (to_path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666));
int fd_from = from_file.allocate ();
int fd_to = to_file.allocate ();
char buf[4096]; // BUFSIZ ??
ssize_t nread;

View File

@ -1,157 +0,0 @@
/*
Copyright (C) 2010 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __pbd_file_manager_h__
#define __pbd_file_manager_h__
#include <sys/types.h>
#include <string>
#include <map>
#include <list>
#include <glibmm/threads.h>
#include "pbd/libpbd_visibility.h"
#include "pbd/signals.h"
namespace PBD {
class LIBPBD_API FileManager;
/** Parent class for FileDescriptors.
*
* When a subclass is instantiated, the file it describes is added to a
* list. The FileDescriptor can be `allocated', meaning that its
* file will be opened on the filesystem, and can then be `released'.
* FileDescriptors are reference counted as they are allocated and
* released. When a descriptor's refcount is 0, the file on the
* filesystem is eligible to be closed if necessary to free up file
* handles for other files.
*
* The upshot of all this is that Ardour can manage the number of
* open files to stay within limits imposed by the operating system.
*/
class LIBPBD_API FileDescriptor
{
public:
FileDescriptor (std::string const &, bool);
virtual ~FileDescriptor () {}
const std::string& path() const { return _path; }
void release ();
virtual void set_path (const std::string&);
/** Emitted when the file is closed */
PBD::Signal0<void> Closed;
protected:
friend class FileManager;
/* These methods and variables must be called / accessed
with a lock held on the FileManager's mutex
*/
/** @return false on success, true on failure */
virtual bool open () = 0;
virtual void close () = 0;
virtual bool is_open () const = 0;
int _refcount; ///< number of active users of this file
double _last_used; ///< monotonic time that this file was last allocated
std::string _path; ///< file path
bool _writeable; ///< true if it should be opened writeable, otherwise false
FileManager* manager ();
private:
static FileManager* _manager;
};
/** FileDescriptor for a file to be opened using POSIX open */
class LIBPBD_API FdFileDescriptor : public FileDescriptor
{
public:
FdFileDescriptor (std::string const & file_name, bool writeable, mode_t mode);
~FdFileDescriptor ();
int allocate ();
private:
friend class FileManager;
bool open ();
void close ();
bool is_open () const;
int _fd; ///< file descriptor, or -1 if the file is closed
mode_t _mode; ///< mode to use when creating files
};
/** FileDescriptor for a file opened using stdio */
class LIBPBD_API StdioFileDescriptor : public FileDescriptor
{
public:
StdioFileDescriptor (std::string const & file_name, std::string const & mode);
~StdioFileDescriptor ();
FILE* allocate ();
private:
friend class FileManager;
bool open ();
void close ();
bool is_open () const;
FILE* _file;
std::string _mode;
};
/** Class to limit the number of files held open */
class LIBPBD_API FileManager
{
public:
FileManager ();
void add (FileDescriptor *);
void remove (FileDescriptor *);
void release (FileDescriptor *);
bool allocate (FileDescriptor *);
private:
void close (FileDescriptor *);
std::list<FileDescriptor*> _files; ///< files we know about
Glib::Threads::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents
int _open; ///< number of open files
int _max_open; ///< maximum number of open files
};
}
#endif

View File

@ -1,58 +0,0 @@
/*
Copyright (C) 2010 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __pbd_sndfile_manager_h__
#define __pbd_sndfile_manager_h__
#include <sys/types.h>
#include <string>
#include <map>
#include <sndfile.h>
#include <glibmm/threads.h>
#include "pbd/libpbd_visibility.h"
#include "pbd/signals.h"
#include "pbd/file_manager.h"
namespace PBD {
/** FileDescriptor for a file to be opened using libsndfile */
class LIBPBD_API SndFileDescriptor : public FileDescriptor
{
public:
SndFileDescriptor (std::string const & file_name, bool writeable, SF_INFO* info);
~SndFileDescriptor ();
SNDFILE* allocate ();
private:
friend class FileManager;
bool open ();
void close ();
bool is_open () const;
SNDFILE* _sndfile; ///< SNDFILE* pointer, or 0 if the file is closed
SF_INFO* _info; ///< libsndfile's info for this file
};
}
#endif /* __pbd_sndfile_manager_h__ */

View File

@ -1,95 +0,0 @@
/*
Copyright (C) 2010 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file libs/pbd/sndfile_manager.cc
* @brief A FileDescriptor for files opened using libsndfile.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cassert>
#include "pbd/compose.h"
#include "pbd/sndfile_manager.h"
#include "pbd/debug.h"
using namespace std;
using namespace PBD;
/** @param file_name Filename.
* @param writeable true to open writeable, otherwise false.
* @param info SF_INFO for the file.
*/
SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info)
: FileDescriptor (file_name, writeable)
, _sndfile (0)
, _info (info)
{
manager()->add (this);
}
SndFileDescriptor::~SndFileDescriptor ()
{
manager()->remove (this);
}
/** @return SNDFILE*, or 0 on error */
SNDFILE*
SndFileDescriptor::allocate ()
{
bool const f = manager()->allocate (this);
if (f) {
return 0;
}
/* this is ok thread-wise because allocate () has incremented
the Descriptor's refcount, so the file will not be closed
*/
return _sndfile;
}
void
SndFileDescriptor::close ()
{
/* we must have a lock on the FileManager's mutex */
assert (_sndfile);
sf_close (_sndfile);
_sndfile = 0;
}
bool
SndFileDescriptor::is_open () const
{
/* we must have a lock on the FileManager's mutex */
return _sndfile != 0;
}
bool
SndFileDescriptor::open ()
{
/* we must have a lock on the FileManager's mutex */
_sndfile = sf_open (_path.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info);
return (_sndfile == 0);
}

View File

@ -46,7 +46,6 @@ libpbd_sources = [
'epa.cc',
'error.cc',
'ffs.cc',
'file_manager.cc',
'file_utils.cc',
'fpu.cc',
'glib_semaphore.cc',
@ -68,7 +67,6 @@ libpbd_sources = [
'semutils.cc',
'shortpath.cc',
'signals.cc',
'sndfile_manager.cc',
'stacktrace.cc',
'stateful_diff_command.cc',
'stateful.cc',