extensions to mmap.cpp, some macro cleanups
parent
884136580d
commit
5b0c09dbd0
|
@ -31,12 +31,12 @@ MTS_NAMESPACE_BEGIN
|
||||||
*/
|
*/
|
||||||
class MTS_EXPORT_CORE MemoryMappedFile : public Object {
|
class MTS_EXPORT_CORE MemoryMappedFile : public Object {
|
||||||
public:
|
public:
|
||||||
/// Map the specified file into memory
|
|
||||||
MemoryMappedFile(const fs::path &filename);
|
|
||||||
|
|
||||||
/// Create a new memory-mapped file of the specified size
|
/// Create a new memory-mapped file of the specified size
|
||||||
MemoryMappedFile(const fs::path &filename, size_t size);
|
MemoryMappedFile(const fs::path &filename, size_t size);
|
||||||
|
|
||||||
|
/// Map the specified file into memory
|
||||||
|
MemoryMappedFile(const fs::path &filename, bool readOnly = true);
|
||||||
|
|
||||||
/// Return a pointer to the file contents in memory
|
/// Return a pointer to the file contents in memory
|
||||||
void *getData();
|
void *getData();
|
||||||
|
|
||||||
|
@ -46,6 +46,17 @@ public:
|
||||||
/// Return the size of the mapped region
|
/// Return the size of the mapped region
|
||||||
size_t getSize() const;
|
size_t getSize() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Resize the memory-mapped file
|
||||||
|
*
|
||||||
|
* This involves remapping the file, which will
|
||||||
|
* generally change the pointer obtained via getData()
|
||||||
|
*/
|
||||||
|
void resize(size_t size);
|
||||||
|
|
||||||
|
/// Return whether the mapped memory region is read-only
|
||||||
|
bool isReadOnly() const;
|
||||||
|
|
||||||
/// Return a string representation
|
/// Return a string representation
|
||||||
std::string toString() const;
|
std::string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,9 @@
|
||||||
|
|
||||||
#include <mitsuba/core/appender.h>
|
#include <mitsuba/core/appender.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#ifdef WIN32
|
|
||||||
#include <io.h>
|
#if defined(__WINDOWS__)
|
||||||
|
# include <io.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MTS_NAMESPACE_BEGIN
|
MTS_NAMESPACE_BEGIN
|
||||||
|
@ -110,7 +111,7 @@ UnbufferedAppender::UnbufferedAppender(int fd)
|
||||||
|
|
||||||
void UnbufferedAppender::append(ELogLevel level, const std::string &text) {
|
void UnbufferedAppender::append(ELogLevel level, const std::string &text) {
|
||||||
std::string value = text + std::string("\n");
|
std::string value = text + std::string("\n");
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
write(m_fd, value.c_str(), (unsigned int) value.length());
|
write(m_fd, value.c_str(), (unsigned int) value.length());
|
||||||
#else
|
#else
|
||||||
if (write(m_fd, value.c_str(), value.length()) != (ssize_t) value.length())
|
if (write(m_fd, value.c_str(), value.length()) != (ssize_t) value.length())
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include <mitsuba/core/fstream.h>
|
#include <mitsuba/core/fstream.h>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
|
|
||||||
#if !defined(WIN32)
|
#if !defined(__WINDOWS__)
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
#else
|
#else
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
@ -30,7 +30,7 @@ MTS_NAMESPACE_BEGIN
|
||||||
|
|
||||||
struct FileStream::FileStreamPrivate
|
struct FileStream::FileStreamPrivate
|
||||||
{
|
{
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
HANDLE file;
|
HANDLE file;
|
||||||
#else
|
#else
|
||||||
FILE* file;
|
FILE* file;
|
||||||
|
@ -80,7 +80,7 @@ void FileStream::open(const fs::path &path, EFileMode mode) {
|
||||||
d->write = true;
|
d->write = true;
|
||||||
d->read = true;
|
d->read = true;
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
DWORD dwDesiredAccess = GENERIC_READ;
|
DWORD dwDesiredAccess = GENERIC_READ;
|
||||||
DWORD dwCreationDisposition = OPEN_EXISTING;
|
DWORD dwCreationDisposition = OPEN_EXISTING;
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ void FileStream::close() {
|
||||||
AssertEx(d->file != 0, "No file is currently open");
|
AssertEx(d->file != 0, "No file is currently open");
|
||||||
Log(ETrace, "Closing \"%s\"", d->path.string().c_str());
|
Log(ETrace, "Closing \"%s\"", d->path.string().c_str());
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
if (!CloseHandle(d->file)) {
|
if (!CloseHandle(d->file)) {
|
||||||
Log(EError, "Error while trying to close file \"%s\": %s",
|
Log(EError, "Error while trying to close file \"%s\": %s",
|
||||||
d->path.string().c_str(), lastErrorText().c_str());
|
d->path.string().c_str(), lastErrorText().c_str());
|
||||||
|
@ -190,7 +190,7 @@ void FileStream::remove() {
|
||||||
void FileStream::seek(size_t pos) {
|
void FileStream::seek(size_t pos) {
|
||||||
AssertEx(d->file != 0, "No file is currently open");
|
AssertEx(d->file != 0, "No file is currently open");
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
LARGE_INTEGER fpos;
|
LARGE_INTEGER fpos;
|
||||||
fpos.QuadPart = pos;
|
fpos.QuadPart = pos;
|
||||||
if (SetFilePointerEx(d->file, fpos, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
if (SetFilePointerEx(d->file, fpos, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
||||||
|
@ -207,7 +207,7 @@ void FileStream::seek(size_t pos) {
|
||||||
|
|
||||||
size_t FileStream::getPos() const {
|
size_t FileStream::getPos() const {
|
||||||
AssertEx(d->file != 0, "No file is currently open");
|
AssertEx(d->file != 0, "No file is currently open");
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
DWORD pos = SetFilePointer(d->file, 0, 0, FILE_CURRENT);
|
DWORD pos = SetFilePointer(d->file, 0, 0, FILE_CURRENT);
|
||||||
if (pos == INVALID_SET_FILE_POINTER) {
|
if (pos == INVALID_SET_FILE_POINTER) {
|
||||||
Log(EError, "Error while looking up the position in file \"%s\": %s",
|
Log(EError, "Error while looking up the position in file \"%s\": %s",
|
||||||
|
@ -228,7 +228,7 @@ size_t FileStream::getPos() const {
|
||||||
size_t FileStream::getSize() const {
|
size_t FileStream::getSize() const {
|
||||||
AssertEx(d->file != 0, "No file is currently open");
|
AssertEx(d->file != 0, "No file is currently open");
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
LARGE_INTEGER result;
|
LARGE_INTEGER result;
|
||||||
if (GetFileSizeEx(d->file, &result) == 0) {
|
if (GetFileSizeEx(d->file, &result) == 0) {
|
||||||
Log(EError, "Error while getting the file size of \"%s\": %s",
|
Log(EError, "Error while getting the file size of \"%s\": %s",
|
||||||
|
@ -260,7 +260,7 @@ void FileStream::truncate(size_t size) {
|
||||||
if (pos > size)
|
if (pos > size)
|
||||||
pos = size;
|
pos = size;
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
seek(size);
|
seek(size);
|
||||||
if (!SetEndOfFile(d->file)) {
|
if (!SetEndOfFile(d->file)) {
|
||||||
Log(EError, "Error while truncating file \"%s\": %s",
|
Log(EError, "Error while truncating file \"%s\": %s",
|
||||||
|
@ -281,7 +281,7 @@ void FileStream::truncate(size_t size) {
|
||||||
void FileStream::flush() {
|
void FileStream::flush() {
|
||||||
AssertEx(d->file != 0, "No file is currently open");
|
AssertEx(d->file != 0, "No file is currently open");
|
||||||
AssertEx(d->write, "File is not open with write access");
|
AssertEx(d->write, "File is not open with write access");
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
if (!FlushFileBuffers(d->file)) {
|
if (!FlushFileBuffers(d->file)) {
|
||||||
Log(EError, "Error while flusing the buffers of \"%s\": %s",
|
Log(EError, "Error while flusing the buffers of \"%s\": %s",
|
||||||
d->path.string().c_str(), lastErrorText().c_str());
|
d->path.string().c_str(), lastErrorText().c_str());
|
||||||
|
@ -300,7 +300,7 @@ void FileStream::read(void *pPtr, size_t size) {
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return;
|
return;
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
DWORD lpNumberOfBytesRead;
|
DWORD lpNumberOfBytesRead;
|
||||||
if (!ReadFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesRead, 0)) {
|
if (!ReadFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesRead, 0)) {
|
||||||
Log(EError, "Error while reading from file \"%s\": %s",
|
Log(EError, "Error while reading from file \"%s\": %s",
|
||||||
|
@ -329,7 +329,7 @@ void FileStream::write(const void *pPtr, size_t size) {
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(__WINDOWS__)
|
||||||
DWORD lpNumberOfBytesWritten;
|
DWORD lpNumberOfBytesWritten;
|
||||||
if (!WriteFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesWritten, 0)) {
|
if (!WriteFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesWritten, 0)) {
|
||||||
Log(EError, "Error while writing to file \"%s\": %s",
|
Log(EError, "Error while writing to file \"%s\": %s",
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
#if defined(__OSX__)
|
#if defined(__OSX__)
|
||||||
# include <sys/sysctl.h>
|
# include <sys/sysctl.h>
|
||||||
#elif defined(WIN32)
|
#elif defined(__WINDOWS__)
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ void Logger::log(ELogLevel level, const Class *theClass,
|
||||||
char tmp[512], *msg = tmp;
|
char tmp[512], *msg = tmp;
|
||||||
va_list iterator;
|
va_list iterator;
|
||||||
|
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
va_start(iterator, fmt);
|
va_start(iterator, fmt);
|
||||||
size_t size = _vscprintf(fmt, iterator) + 1;
|
size_t size = _vscprintf(fmt, iterator) + 1;
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ void Logger::log(ELogLevel level, const Class *theClass,
|
||||||
|
|
||||||
if (runningInDebugger)
|
if (runningInDebugger)
|
||||||
__asm__ ("int $3");
|
__asm__ ("int $3");
|
||||||
#elif defined(WIN32)
|
#elif defined(__WINDOWS__)
|
||||||
if (IsDebuggerPresent())
|
if (IsDebuggerPresent())
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,130 +3,153 @@
|
||||||
#if defined(__LINUX__) || defined(__OSX__)
|
#if defined(__LINUX__) || defined(__OSX__)
|
||||||
# include <sys/mman.h>
|
# include <sys/mman.h>
|
||||||
# include <fcntl.h>
|
# include <fcntl.h>
|
||||||
#elif defined(WIN32)
|
#elif defined(__WINDOWS__)
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MTS_NAMESPACE_BEGIN
|
MTS_NAMESPACE_BEGIN
|
||||||
|
|
||||||
struct MemoryMappedFile::MemoryMappedFilePrivate
|
struct MemoryMappedFile::MemoryMappedFilePrivate {
|
||||||
{
|
|
||||||
fs::path filename;
|
fs::path filename;
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
HANDLE file;
|
HANDLE file;
|
||||||
HANDLE fileMapping;
|
HANDLE fileMapping;
|
||||||
#endif
|
#endif
|
||||||
size_t size;
|
size_t size;
|
||||||
void *data;
|
void *data;
|
||||||
|
bool readOnly;
|
||||||
|
|
||||||
MemoryMappedFilePrivate(const fs::path & f, size_t s = 0) :
|
MemoryMappedFilePrivate(const fs::path & f, size_t s = 0)
|
||||||
filename(f), size(s), data(NULL) {}
|
: filename(f), size(s), data(NULL), readOnly(false) {}
|
||||||
|
|
||||||
|
void create() {
|
||||||
|
#if defined(__LINUX__) || defined(__OSX__)
|
||||||
|
int fd = open(filename.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0664);
|
||||||
|
if (fd == -1)
|
||||||
|
Log(EError, "Could not open \"%s\"!", filename.string().c_str());
|
||||||
|
int result = lseek(fd, size-1, SEEK_SET);
|
||||||
|
if (result == -1)
|
||||||
|
Log(EError, "Could not set file size of \"%s\"!", filename.string().c_str());
|
||||||
|
result = write(fd, "", 1);
|
||||||
|
if (result != 1)
|
||||||
|
Log(EError, "Could not write to \"%s\"!", filename.string().c_str());
|
||||||
|
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (data == NULL)
|
||||||
|
Log(EError, "Could not map \"%s\" to memory!", filename.string().c_str());
|
||||||
|
if (close(fd) != 0)
|
||||||
|
Log(EError, "close(): unable to close file!");
|
||||||
|
#elif defined(___WINDOWS__)
|
||||||
|
file = CreateFile(filename.string().c_str(), GENERIC_WRITE | GENERIC_READ,
|
||||||
|
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
|
Log(EError, "Could not open \"%s\": %s", filename.string().c_str(),
|
||||||
|
lastErrorText().c_str());
|
||||||
|
fileMapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0,
|
||||||
|
static_cast<DWORD>(size), NULL);
|
||||||
|
if (fileMapping == NULL)
|
||||||
|
Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s",
|
||||||
|
filename.string().c_str(), lastErrorText().c_str());
|
||||||
|
data = (void *) MapViewOfFile(fileMapping, FILE_MAP_WRITE, 0, 0, 0);
|
||||||
|
if (data == NULL)
|
||||||
|
Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s",
|
||||||
|
filename.string().c_str(), lastErrorText().c_str());
|
||||||
|
#endif
|
||||||
|
readOnly = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void map() {
|
||||||
|
if (!fs::exists(filename))
|
||||||
|
Log(EError, "The file \"%s\" does not exist!", filename.string().c_str());
|
||||||
|
size = (size_t) fs::file_size(filename);
|
||||||
|
#if defined(__LINUX__) || defined(__OSX__)
|
||||||
|
int fd = open(filename.string().c_str(), readOnly ? O_RDONLY : O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
Log(EError, "Could not open \"%s\"!", filename.string().c_str());
|
||||||
|
data = mmap(NULL, size, PROT_READ | (readOnly ? 0 : PROT_WRITE), MAP_SHARED, fd, 0);
|
||||||
|
if (data == NULL)
|
||||||
|
Log(EError, "Could not map \"%s\" to memory!", filename.string().c_str());
|
||||||
|
if (close(fd) != 0)
|
||||||
|
Log(EError, "close(): unable to close file!");
|
||||||
|
#elif defined(__WINDOWS__)
|
||||||
|
file = CreateFile(filename.string().c_str(), GENERIC_READ | (readOnly ? 0 : GENERIC_WRITE),
|
||||||
|
FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
||||||
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
|
Log(EError, "Could not open \"%s\": %s", filename.string().c_str(),
|
||||||
|
lastErrorText().c_str());
|
||||||
|
fileMapping = CreateFileMapping(file, NULL, readOnly ? PAGE_READONLY : PAGE_READWRITE, 0, 0, NULL);
|
||||||
|
if (fileMapping == NULL)
|
||||||
|
Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s",
|
||||||
|
filename.string().c_str(), lastErrorText().c_str());
|
||||||
|
data = (void *) MapViewOfFile(fileMapping, readOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
|
||||||
|
if (data == NULL)
|
||||||
|
Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s",
|
||||||
|
filename.string().c_str(), lastErrorText().c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void unmap() {
|
||||||
|
SLog(ETrace, "Unmapping \"%s\" from memory",
|
||||||
|
filename.string().c_str());
|
||||||
|
#if defined(__LINUX__) || defined(__OSX__)
|
||||||
|
int retval = munmap(data, size);
|
||||||
|
if (retval != 0)
|
||||||
|
Log(EError, "munmap(): unable to unmap memory!");
|
||||||
|
#elif defined(__WINDOWS__)
|
||||||
|
if (!UnmapViewOfFile(data))
|
||||||
|
Log(EError, "UnmapViewOfFile(): unable to unmap memory: %s", lastErrorText().c_str());
|
||||||
|
if (!CloseHandle(fileMapping))
|
||||||
|
Log(EError, "CloseHandle(): unable to close file mapping: %s", lastErrorText().c_str());
|
||||||
|
if (!CloseHandle(file))
|
||||||
|
Log(EError, "CloseHandle(): unable to close file: %s", lastErrorText().c_str());
|
||||||
|
#endif
|
||||||
|
data = NULL;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
MemoryMappedFile::MemoryMappedFile(const fs::path &filename, size_t size)
|
MemoryMappedFile::MemoryMappedFile(const fs::path &filename, size_t size)
|
||||||
: d(new MemoryMappedFilePrivate(filename, size)) {
|
: d(new MemoryMappedFilePrivate(filename, size)) {
|
||||||
Log(ETrace, "Creating memory-mapped file \"%s\" (%s)..",
|
SLog(ETrace, "Creating memory-mapped file \"%s\" (%s)..",
|
||||||
filename.filename().string().c_str(), memString(d->size).c_str());
|
filename.filename().string().c_str(), memString(d->size).c_str());
|
||||||
#if defined(__LINUX__) || defined(__OSX__)
|
d->create();
|
||||||
int fd = open(filename.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0664);
|
|
||||||
if (fd == -1)
|
|
||||||
Log(EError, "Could not open \"%s\"!", d->filename.string().c_str());
|
|
||||||
int result = lseek(fd, size-1, SEEK_SET);
|
|
||||||
if (result == -1)
|
|
||||||
Log(EError, "Could not set file size of \"%s\"!", d->filename.string().c_str());
|
|
||||||
result = write(fd, "", 1);
|
|
||||||
if (result != 1)
|
|
||||||
Log(EError, "Could not write to \"%s\"!", d->filename.string().c_str());
|
|
||||||
d->data = mmap(NULL, d->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
||||||
if (d->data == NULL)
|
|
||||||
Log(EError, "Could not map \"%s\" to memory!", d->filename.string().c_str());
|
|
||||||
if (close(fd) != 0)
|
|
||||||
Log(EError, "close(): unable to close file!");
|
|
||||||
#elif defined(_WIN32)
|
|
||||||
d->file = CreateFile(filename.string().c_str(), GENERIC_WRITE | GENERIC_READ,
|
|
||||||
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
||||||
if (d->file == INVALID_HANDLE_VALUE)
|
|
||||||
Log(EError, "Could not open \"%s\": %s", d->filename.string().c_str(),
|
|
||||||
lastErrorText().c_str());
|
|
||||||
d->fileMapping = CreateFileMapping(d->file, NULL, PAGE_READWRITE, 0,
|
|
||||||
static_cast<DWORD>(size), NULL);
|
|
||||||
if (d->fileMapping == NULL)
|
|
||||||
Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s",
|
|
||||||
d->filename.string().c_str(), lastErrorText().c_str());
|
|
||||||
d->data = (void *) MapViewOfFile(d->fileMapping, FILE_MAP_WRITE, 0, 0, 0);
|
|
||||||
if (d->data == NULL)
|
|
||||||
Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s",
|
|
||||||
d->filename.string().c_str(), lastErrorText().c_str());
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MemoryMappedFile::MemoryMappedFile(const fs::path &filename)
|
MemoryMappedFile::MemoryMappedFile(const fs::path &filename, bool readOnly)
|
||||||
: d(new MemoryMappedFilePrivate(filename)) {
|
: d(new MemoryMappedFilePrivate(filename)) {
|
||||||
if (!fs::exists(filename))
|
d->readOnly = readOnly;
|
||||||
Log(EError, "The file \"%s\" does not exist!", filename.string().c_str());
|
d->map();
|
||||||
d->size = (size_t) fs::file_size(filename);
|
Log(ETrace, "Mapped \"%s\" into memory (%s)..",
|
||||||
Log(ETrace, "Mapping \"%s\" into memory (%s)..",
|
filename.filename().string().c_str(), memString(d->size).c_str());
|
||||||
filename.filename().string().c_str(), memString(d->size).c_str());
|
|
||||||
#if defined(__LINUX__) || defined(__OSX__)
|
|
||||||
int fd = open(filename.string().c_str(), O_RDONLY);
|
|
||||||
if (fd == -1)
|
|
||||||
Log(EError, "Could not open \"%s\"!", d->filename.string().c_str());
|
|
||||||
d->data = mmap(NULL, d->size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (d->data == NULL)
|
|
||||||
Log(EError, "Could not map \"%s\" to memory!", d->filename.string().c_str());
|
|
||||||
if (close(fd) != 0)
|
|
||||||
Log(EError, "close(): unable to close file!");
|
|
||||||
#elif defined(WIN32)
|
|
||||||
d->file = CreateFile(filename.string().c_str(), GENERIC_READ,
|
|
||||||
FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
|
||||||
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
||||||
if (d->file == INVALID_HANDLE_VALUE)
|
|
||||||
Log(EError, "Could not open \"%s\": %s", d->filename.string().c_str(),
|
|
||||||
lastErrorText().c_str());
|
|
||||||
d->fileMapping = CreateFileMapping(d->file, NULL, PAGE_READONLY, 0, 0, NULL);
|
|
||||||
if (d->fileMapping == NULL)
|
|
||||||
Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s",
|
|
||||||
d->filename.string().c_str(), lastErrorText().c_str());
|
|
||||||
d->data = (void *) MapViewOfFile(d->fileMapping, FILE_MAP_READ, 0, 0, 0);
|
|
||||||
if (d->data == NULL)
|
|
||||||
Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s",
|
|
||||||
d->filename.string().c_str(), lastErrorText().c_str());
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryMappedFile::~MemoryMappedFile() {
|
MemoryMappedFile::~MemoryMappedFile() {
|
||||||
if (d->data != NULL) {
|
if (d->data) {
|
||||||
Log(ETrace, "Unmapping \"%s\" from memory",
|
try {
|
||||||
d->filename.string().c_str());
|
d->unmap();
|
||||||
|
} catch (std::exception &e) {
|
||||||
#if defined(__LINUX__) || defined(__OSX__)
|
/* Don't throw exceptions from a constructor */
|
||||||
int retval = munmap(d->data, d->size);
|
Log(EWarn, "%s", e.what());
|
||||||
if (retval != 0)
|
}
|
||||||
Log(EWarn, "munmap(): unable to unmap memory!");
|
|
||||||
#elif defined(WIN32)
|
|
||||||
if (!UnmapViewOfFile(d->data)) {
|
|
||||||
Log(EWarn, "UnmapViewOfFile(): unable to unmap memory: %s", lastErrorText().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!CloseHandle(d->fileMapping)) {
|
|
||||||
Log(EWarn, "CloseHandle(): unable to close file mapping: %s", lastErrorText().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!CloseHandle(d->file)) {
|
|
||||||
Log(EWarn, "CloseHandle(): unable to close file: %s", lastErrorText().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void * MemoryMappedFile::getData() {
|
void MemoryMappedFile::resize(size_t size) {
|
||||||
|
if (!d->data)
|
||||||
|
Log(EError, "Internal error in MemoryMappedFile::resize()!");
|
||||||
|
d->unmap();
|
||||||
|
fs::resize_file(d->filename, size);
|
||||||
|
d->size = size;
|
||||||
|
d->map();
|
||||||
|
}
|
||||||
|
|
||||||
|
void *MemoryMappedFile::getData() {
|
||||||
return d->data;
|
return d->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a pointer to the file contents in memory (const version)
|
/// Return a pointer to the file contents in memory (const version)
|
||||||
const void * MemoryMappedFile::getData() const {
|
const void *MemoryMappedFile::getData() const {
|
||||||
return d->data;
|
return d->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,10 +157,14 @@ size_t MemoryMappedFile::getSize() const {
|
||||||
return d->size;
|
return d->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool MemoryMappedFile::isReadOnly() const {
|
||||||
|
return d->readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
std::string MemoryMappedFile::toString() const {
|
std::string MemoryMappedFile::toString() const {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "MemoryMappedFile[filename=\""
|
oss << "MemoryMappedFile[filename=\""
|
||||||
<< d->filename.string() << "\", size="
|
<< d->filename.string() << "\", size="
|
||||||
<< memString(d->size) << "]";
|
<< memString(d->size) << "]";
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
#include <mitsuba/core/cobject.h>
|
#include <mitsuba/core/cobject.h>
|
||||||
#include <mitsuba/core/version.h>
|
#include <mitsuba/core/version.h>
|
||||||
|
|
||||||
#if !defined(WIN32)
|
#if !defined(__WINDOWS__)
|
||||||
# include <dlfcn.h>
|
# include <dlfcn.h>
|
||||||
#else
|
#else
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
@ -43,7 +43,7 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Plugin::PluginPrivate {
|
struct Plugin::PluginPrivate {
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
HMODULE handle;
|
HMODULE handle;
|
||||||
#else
|
#else
|
||||||
void *handle;
|
void *handle;
|
||||||
|
@ -61,7 +61,7 @@ struct Plugin::PluginPrivate {
|
||||||
|
|
||||||
Plugin::Plugin(const std::string &shortName, const fs::path &path)
|
Plugin::Plugin(const std::string &shortName, const fs::path &path)
|
||||||
: d(new PluginPrivate(shortName, path)) {
|
: d(new PluginPrivate(shortName, path)) {
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
d->handle = LoadLibraryW(path.c_str());
|
d->handle = LoadLibraryW(path.c_str());
|
||||||
if (!d->handle) {
|
if (!d->handle) {
|
||||||
SLog(EError, "Error while loading plugin \"%s\": %s",
|
SLog(EError, "Error while loading plugin \"%s\": %s",
|
||||||
|
@ -77,7 +77,7 @@ Plugin::Plugin(const std::string &shortName, const fs::path &path)
|
||||||
try {
|
try {
|
||||||
d->getDescription = (GetDescriptionFunc) getSymbol("GetDescription");
|
d->getDescription = (GetDescriptionFunc) getSymbol("GetDescription");
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
FreeLibrary(d->handle);
|
FreeLibrary(d->handle);
|
||||||
#else
|
#else
|
||||||
dlclose(d->handle);
|
dlclose(d->handle);
|
||||||
|
@ -102,7 +102,7 @@ Plugin::Plugin(const std::string &shortName, const fs::path &path)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Plugin::hasSymbol(const std::string &sym) const {
|
bool Plugin::hasSymbol(const std::string &sym) const {
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
void *ptr = GetProcAddress(d->handle, sym.c_str());
|
void *ptr = GetProcAddress(d->handle, sym.c_str());
|
||||||
#else
|
#else
|
||||||
void *ptr = dlsym(d->handle, sym.c_str());
|
void *ptr = dlsym(d->handle, sym.c_str());
|
||||||
|
@ -111,7 +111,7 @@ bool Plugin::hasSymbol(const std::string &sym) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Plugin::getSymbol(const std::string &sym) {
|
void *Plugin::getSymbol(const std::string &sym) {
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
void *data = GetProcAddress(d->handle, sym.c_str());
|
void *data = GetProcAddress(d->handle, sym.c_str());
|
||||||
if (!data) {
|
if (!data) {
|
||||||
SLog(EError, "Could not resolve symbol \"%s\" in \"%s\": %s",
|
SLog(EError, "Could not resolve symbol \"%s\" in \"%s\": %s",
|
||||||
|
@ -152,7 +152,7 @@ const std::string& Plugin::getShortName() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
Plugin::~Plugin() {
|
Plugin::~Plugin() {
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
FreeLibrary(d->handle);
|
FreeLibrary(d->handle);
|
||||||
#else
|
#else
|
||||||
dlclose(d->handle);
|
dlclose(d->handle);
|
||||||
|
@ -226,7 +226,7 @@ void PluginManager::ensurePluginLoaded(const std::string &name) {
|
||||||
|
|
||||||
/* Build the full plugin file name */
|
/* Build the full plugin file name */
|
||||||
fs::path shortName = fs::path("plugins") / name;
|
fs::path shortName = fs::path("plugins") / name;
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
shortName.replace_extension(".dll");
|
shortName.replace_extension(".dll");
|
||||||
#elif defined(__OSX__)
|
#elif defined(__OSX__)
|
||||||
shortName.replace_extension(".dylib");
|
shortName.replace_extension(".dylib");
|
||||||
|
|
|
@ -473,7 +473,7 @@ void Random::State::init_by_array(const uint32_t *init_key, int key_length) {
|
||||||
Random::Random() : mt(NULL) {
|
Random::Random() : mt(NULL) {
|
||||||
mt = (State *) allocAligned(sizeof(State));
|
mt = (State *) allocAligned(sizeof(State));
|
||||||
Assert(mt != NULL);
|
Assert(mt != NULL);
|
||||||
#if defined(_WIN32)
|
#if defined(___WINDOWS__)
|
||||||
seed();
|
seed();
|
||||||
#else
|
#else
|
||||||
#if 0
|
#if 0
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include <mitsuba/core/sshstream.h>
|
#include <mitsuba/core/sshstream.h>
|
||||||
#include <mitsuba/core/statistics.h>
|
#include <mitsuba/core/statistics.h>
|
||||||
|
|
||||||
#if !defined(WIN32)
|
#if !defined(__WINDOWS__)
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
#else
|
#else
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
@ -33,7 +33,7 @@ struct SSHStream::SSHStreamPrivate
|
||||||
const std::string userName, hostName;
|
const std::string userName, hostName;
|
||||||
const int port, timeout;
|
const int port, timeout;
|
||||||
size_t received, sent;
|
size_t received, sent;
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
HANDLE childInRd, childInWr;
|
HANDLE childInRd, childInWr;
|
||||||
HANDLE childOutRd, childOutWr;
|
HANDLE childOutRd, childOutWr;
|
||||||
#else
|
#else
|
||||||
|
@ -44,7 +44,7 @@ struct SSHStream::SSHStreamPrivate
|
||||||
SSHStreamPrivate(const std::string& uname, const std::string& hname,
|
SSHStreamPrivate(const std::string& uname, const std::string& hname,
|
||||||
int p, int tm) :
|
int p, int tm) :
|
||||||
userName(uname), hostName(hname), port(p), timeout(tm), received(0), sent(0),
|
userName(uname), hostName(hname), port(p), timeout(tm), received(0), sent(0),
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
childInRd(0), childInWr(0), childOutRd(0), childOutWr(0)
|
childInRd(0), childInWr(0), childOutRd(0), childOutWr(0)
|
||||||
#else
|
#else
|
||||||
infd(-1), outfd(-1), input(0), output(0)
|
infd(-1), outfd(-1), input(0), output(0)
|
||||||
|
@ -60,7 +60,7 @@ SSHStream::SSHStream(const std::string &userName,
|
||||||
Log(EInfo, "Establishing a SSH connection to \"%s@%s\"",
|
Log(EInfo, "Establishing a SSH connection to \"%s@%s\"",
|
||||||
userName.c_str(), hostName.c_str());
|
userName.c_str(), hostName.c_str());
|
||||||
|
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
/* Inherit pipe handles */
|
/* Inherit pipe handles */
|
||||||
SECURITY_ATTRIBUTES sAttr;
|
SECURITY_ATTRIBUTES sAttr;
|
||||||
sAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
sAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||||
|
@ -173,7 +173,7 @@ SSHStream::SSHStream(const std::string &userName,
|
||||||
|
|
||||||
SSHStream::~SSHStream() {
|
SSHStream::~SSHStream() {
|
||||||
Log(EDebug, "Closing SSH connection");
|
Log(EDebug, "Closing SSH connection");
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
CloseHandle(d->childInWr);
|
CloseHandle(d->childInWr);
|
||||||
CloseHandle(d->childOutRd);
|
CloseHandle(d->childOutRd);
|
||||||
#else
|
#else
|
||||||
|
@ -225,7 +225,7 @@ void SSHStream::truncate(size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSHStream::flush() {
|
void SSHStream::flush() {
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
// No-op
|
// No-op
|
||||||
#else
|
#else
|
||||||
if (fflush(d->output) == EOF)
|
if (fflush(d->output) == EOF)
|
||||||
|
@ -235,7 +235,7 @@ void SSHStream::flush() {
|
||||||
|
|
||||||
void SSHStream::read(void *ptr, size_t size) {
|
void SSHStream::read(void *ptr, size_t size) {
|
||||||
static StatsCounter bytesRcvd("Network", "Bytes received (SSH)");
|
static StatsCounter bytesRcvd("Network", "Bytes received (SSH)");
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
size_t left = size;
|
size_t left = size;
|
||||||
char *data = (char *) ptr;
|
char *data = (char *) ptr;
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
|
@ -260,7 +260,7 @@ void SSHStream::read(void *ptr, size_t size) {
|
||||||
|
|
||||||
void SSHStream::write(const void *ptr, size_t size) {
|
void SSHStream::write(const void *ptr, size_t size) {
|
||||||
static StatsCounter bytesSent("Network", "Bytes sent (SSH)");
|
static StatsCounter bytesSent("Network", "Bytes sent (SSH)");
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
size_t left = size;
|
size_t left = size;
|
||||||
char *data = (char *) ptr;
|
char *data = (char *) ptr;
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include <mitsuba/core/sstream.h>
|
#include <mitsuba/core/sstream.h>
|
||||||
#include <mitsuba/core/statistics.h>
|
#include <mitsuba/core/statistics.h>
|
||||||
|
|
||||||
#if !defined(WIN32)
|
#if !defined(__WINDOWS__)
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <errno.h>
|
# include <errno.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
|
@ -44,7 +44,7 @@ static StatsCounter bytesSent("Network", "Bytes sent");
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
// This function is natively avaiable since Windows Vista
|
// This function is natively avaiable since Windows Vista
|
||||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t len) {
|
const char *inet_ntop(int af, const void *src, char *dst, socklen_t len) {
|
||||||
if (af == AF_INET) {
|
if (af == AF_INET) {
|
||||||
|
@ -148,7 +148,7 @@ SocketStream::SocketStream(const std::string &host, int port)
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketStream::~SocketStream() {
|
SocketStream::~SocketStream() {
|
||||||
#ifdef WIN32
|
#ifdef __WINDOWS__
|
||||||
if (closesocket(m_socket) == SOCKET_ERROR)
|
if (closesocket(m_socket) == SOCKET_ERROR)
|
||||||
handleError("closesocket");
|
handleError("closesocket");
|
||||||
#else
|
#else
|
||||||
|
@ -161,7 +161,7 @@ void SocketStream::read(void *ptr, size_t size) {
|
||||||
const size_t total = size;
|
const size_t total = size;
|
||||||
char *data = (char *) ptr;
|
char *data = (char *) ptr;
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
#if defined(WIN32)
|
#if defined(__WINDOWS__)
|
||||||
int n = recv(m_socket, data, (int) size, 0);
|
int n = recv(m_socket, data, (int) size, 0);
|
||||||
#else
|
#else
|
||||||
int n = recv(m_socket, data, size, 0);
|
int n = recv(m_socket, data, size, 0);
|
||||||
|
@ -190,7 +190,7 @@ void SocketStream::write(const void *ptr, size_t size) {
|
||||||
#if defined(__LINUX__)
|
#if defined(__LINUX__)
|
||||||
/* Linux: Don't send the EPIPE signal when the connection breaks */
|
/* Linux: Don't send the EPIPE signal when the connection breaks */
|
||||||
int n = send(m_socket, data, size, MSG_NOSIGNAL);
|
int n = send(m_socket, data, size, MSG_NOSIGNAL);
|
||||||
#elif defined(WIN32)
|
#elif defined(__WINDOWS__)
|
||||||
int n = send(m_socket, data, (int) size, 0);
|
int n = send(m_socket, data, (int) size, 0);
|
||||||
#else
|
#else
|
||||||
int n = send(m_socket, data, size, 0);
|
int n = send(m_socket, data, size, 0);
|
||||||
|
|
Loading…
Reference in New Issue