Remove mmap dependencies with QFile

QFile implements cross platform file mapping and removes this dependency entirely.
Also simplifies the file size and error to string handling logic.
This commit is contained in:
Josh Blum
2017-11-25 15:54:13 -06:00
parent 69e8c1410c
commit 40890e46ab
3 changed files with 11 additions and 36 deletions

View File

@@ -20,12 +20,9 @@
#include "inputsource.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdexcept>
#include <algorithm>
@@ -107,13 +104,12 @@ InputSource::~InputSource()
void InputSource::cleanup()
{
if (mmapData != nullptr) {
munmap(mmapData, fileSize);
inputFile->unmap(mmapData);
mmapData = nullptr;
fileSize = 0;
}
if (inputFile != nullptr) {
fclose(inputFile);
delete inputFile;
inputFile = nullptr;
}
}
@@ -138,28 +134,21 @@ void InputSource::openFile(const char *filename)
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexF32SampleAdapter());
}
errno = 0;
FILE *file = fopen(filename, "rb");
if (file == nullptr) {
std::stringstream ss;
ss << "Error opening file: " << strerror(errno) << " (" << errno << ")";
throw std::runtime_error(ss.str());
std::unique_ptr<QFile> file(new QFile(filename));
if (!file->open(QFile::ReadOnly)) {
throw std::runtime_error(file->errorString().toStdString());
}
struct stat sb;
if (fstat(fileno(file), &sb) != 0)
throw std::runtime_error("Error fstating file");
off_t size = sb.st_size;
auto size = file->size();
sampleCount = size / sampleAdapter->sampleSize();
auto data = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(file), 0);
auto data = file->map(0, size);
if (data == nullptr)
throw std::runtime_error("Error mmapping file");
cleanup();
inputFile = file;
fileSize = size;
inputFile = file.release();
mmapData = data;
invalidate();