Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion src/common/mapped_file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "mapped_file.h"

#ifdef _WIN32

#include "utils.h" // For DBG
#include <iostream>

Expand Down Expand Up @@ -47,4 +49,85 @@ std::span<const uint8_t> MappedFile::get_data() const {
return { static_cast<const uint8_t*>(m_pMappedData), m_file_size };
}

#endif // _WIN32
#else // !_WIN32

#include "utils.h"

#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

MappedFile::MappedFile(const std::string& path) {
m_fd = ::open(path.c_str(), O_RDONLY);
if (m_fd < 0) {
std::cerr << "Error: Could not open file " << path << ": " << std::strerror(errno) << std::endl;
return;
}

struct stat st {};
if (::fstat(m_fd, &st) != 0) {
std::cerr << "Error: Could not stat file: " << std::strerror(errno) << std::endl;
::close(m_fd);
m_fd = -1;
return;
}

if (!S_ISREG(st.st_mode)) {
std::cerr << "Error: Not a regular file." << std::endl;
::close(m_fd);
m_fd = -1;
return;
}

m_file_size = static_cast<size_t>(st.st_size);
DBG("[IO] File size: ", m_file_size, " bytes");

if (m_file_size == 0) {
m_pMappedData = nullptr;
DBG("[IO] Empty file; no mmap");
return;
}

m_pMappedData = ::mmap(nullptr, m_file_size, PROT_READ, MAP_PRIVATE, m_fd, 0);
if (m_pMappedData == MAP_FAILED) {
std::cerr << "Error: Could not mmap file: " << std::strerror(errno) << std::endl;
m_pMappedData = nullptr;
::close(m_fd);
m_fd = -1;
return;
}

DBG("[IO] Mapped view @ ", m_pMappedData, " size=", m_file_size, " bytes");
}

MappedFile::~MappedFile() {
if (m_pMappedData && m_pMappedData != MAP_FAILED && m_file_size > 0) {
::munmap(m_pMappedData, m_file_size);
}
if (m_fd >= 0) {
::close(m_fd);
}
}

bool MappedFile::is_valid() const {
if (m_fd < 0) {
return false;
}
if (m_file_size == 0) {
return true;
}
return m_pMappedData != nullptr && m_pMappedData != MAP_FAILED;
}

std::span<const uint8_t> MappedFile::get_data() const {
if (!is_valid() || m_file_size == 0) {
return {};
}
return { static_cast<const uint8_t*>(m_pMappedData), m_file_size };
}

#endif // _WIN32
18 changes: 12 additions & 6 deletions src/common/mapped_file.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
#pragma once
#ifdef _WIN32
#include <string>
#include <span>

#include <cstdint>
#include <span>
#include <string>

#ifdef _WIN32
#include <windows.h>
#endif

class MappedFile {
public:
MappedFile(const std::string& path);
~MappedFile();

// Disable copy/move for simplicity
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;

bool is_valid() const;
std::span<const uint8_t> get_data() const;

private:
#ifdef _WIN32
HANDLE m_hFile = INVALID_HANDLE_VALUE;
HANDLE m_hMapping = NULL;
LPCVOID m_pMappedData = NULL;
size_t m_file_size = 0;
#else
int m_fd = -1;
void* m_pMappedData = nullptr;
size_t m_file_size = 0;
#endif
};

#endif // _WIN32