Skip to content

Commit

Permalink
feat: move file buffers to viet (skyrim-multiplayer#2028)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Jun 9, 2024
1 parent 0ee0d16 commit 148bce0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 38 deletions.
2 changes: 2 additions & 0 deletions libespm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ add_library(libespm ALIAS espm)
find_path(SPARSEPP_INCLUDE_DIR NAMES spp.h PATH_SUFFIXES sparsepp)
get_filename_component(SPARSEPP_INCLUDE_DIR ${SPARSEPP_INCLUDE_DIR} DIRECTORY)
target_include_directories(espm PUBLIC ${SPARSEPP_INCLUDE_DIR})

target_link_libraries(espm PUBLIC viet)
4 changes: 2 additions & 2 deletions libespm/include/libespm/Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class Loader
std::vector<fs::path> MakeFilePaths(const fs::path& dataDir,
const std::vector<fs::path>& fileNames);

std::unique_ptr<IBuffer> MakeBuffer(const fs::path& filePath) const;
std::unique_ptr<Viet::IBuffer> MakeBuffer(const fs::path& filePath) const;

struct Entry
{
std::unique_ptr<IBuffer> buffer;
std::unique_ptr<Viet::IBuffer> buffer;
std::unique_ptr<Browser> browser;

uintmax_t size = 0;
Expand Down
11 changes: 6 additions & 5 deletions libespm/src/Loader.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "libespm/Loader.h"

#include "libespm/AllocatedBuffer.h"
#include "libespm/MappedBuffer.h"
#include "AllocatedBuffer.h"
#include "MappedBuffer.h"
#include "libespm/Utils.h"

namespace espm {
Expand Down Expand Up @@ -87,13 +87,14 @@ std::vector<fs::path> Loader::MakeFilePaths(
return res;
}

std::unique_ptr<IBuffer> Loader::MakeBuffer(const fs::path& filePath) const
std::unique_ptr<Viet::IBuffer> Loader::MakeBuffer(
const fs::path& filePath) const
{
switch (bufferType) {
case BufferType::AllocatedBuffer:
return std::make_unique<AllocatedBuffer>(filePath);
return std::make_unique<Viet::AllocatedBuffer>(filePath);
case BufferType::MappedBuffer:
return std::make_unique<MappedBuffer>(filePath);
return std::make_unique<Viet::MappedBuffer>(filePath);
default:
throw std::runtime_error("[espm] unhandled buffer type");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include <filesystem>
#include <vector>

#include "IBuffer.h"
#include "Loader.h"

namespace espm {
namespace Viet {

class AllocatedBuffer : public IBuffer
{
Expand All @@ -20,4 +20,4 @@ class AllocatedBuffer : public IBuffer
std::vector<char> data;
};

} // namespace espm
}
4 changes: 2 additions & 2 deletions libespm/include/libespm/IBuffer.h → viet/include/IBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cstddef>

namespace espm {
namespace Viet {

class IBuffer
{
Expand All @@ -13,4 +13,4 @@ class IBuffer
virtual size_t GetLength() const = 0;
};

} // namespace espm
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
#endif

#include "IBuffer.h"
#include "Loader.h"

namespace espm {
namespace Viet {

class MappedBuffer : public IBuffer
{
public:
MappedBuffer(const fs::path& path);
MappedBuffer(const std::filesystem::path& path);

~MappedBuffer();

Expand All @@ -34,4 +33,4 @@ class MappedBuffer : public IBuffer
size_t size_;
};

} // namespace espm
}
11 changes: 6 additions & 5 deletions libespm/src/AllocatedBuffer.cpp → viet/src/AllocatedBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "libespm/AllocatedBuffer.h"
#include "AllocatedBuffer.h"

namespace espm {
#include <fstream>

namespace Viet {

AllocatedBuffer::AllocatedBuffer(const std::filesystem::path& path)
: data()
Expand All @@ -10,7 +12,7 @@ AllocatedBuffer::AllocatedBuffer(const std::filesystem::path& path)

std::ifstream f(path.string(), std::ios::binary);
if (!f.read(data.data(), size)) {
throw std::runtime_error("[espm] can't read " + path.string());
throw std::runtime_error("[AllocatedBuffer] can't read " + path.string());
}
}

Expand All @@ -23,5 +25,4 @@ size_t AllocatedBuffer::GetLength() const
{
return data.size();
}

} // namespace espm
}
38 changes: 21 additions & 17 deletions libespm/src/MappedBuffer.cpp → viet/src/MappedBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
#include "libespm/MappedBuffer.h"
#include "MappedBuffer.h"

#include <iostream>
#include <stdexcept>

#ifndef WIN32
# include <fcntl.h>
# include <sys/mman.h>
# include <unistd.h>
#endif

namespace espm {
namespace Viet {

MappedBuffer::MappedBuffer(const fs::path& path)
MappedBuffer::MappedBuffer(const std::filesystem::path& path)
{
size_ = fs::file_size(path);
size_ = std::filesystem::file_size(path);
#ifdef WIN32
fileHandle_ = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
if (!fileHandle_) {
throw std::system_error(GetLastError(), std::system_category(),
"[espm] CreateFileW failed for " + path.string());
"[MappedBuffer] CreateFileW failed for " +
path.string());
}

mapHandle_ = CreateFileMapping(fileHandle_, NULL, PAGE_READONLY, 0, 0, NULL);
if (!mapHandle_) {
throw std::system_error(GetLastError(), std::system_category(),
"[espm] CreateFileMapping failed for " +
"[MappedBuffer] CreateFileMapping failed for " +
path.string());
}

viewPtr_ = MapViewOfFile(mapHandle_, FILE_MAP_READ, 0, 0, 0);
if (!mapHandle_) {
throw std::system_error(GetLastError(), std::system_category(),
"[espm] CreateFileMapping failed for " +
"[MappedBuffer] CreateFileMapping failed for " +
path.string());
}
data_ = static_cast<char*>(viewPtr_);
#else
fd_ = open(path.c_str(), O_RDONLY);
if (fd_ == -1) {
throw std::system_error(errno, std::system_category(),
"[espm] open failed for " + path.string());
"[MappedBuffer] open failed for " + path.string());
}
const auto mmapResult = mmap(NULL, size_, PROT_READ, MAP_SHARED, fd_, 0);
if (mmapResult == MAP_FAILED) {
throw std::system_error(errno, std::system_category(),
"[espm] mmap failed for " + path.string());
"[MappedBuffer] mmap failed for " + path.string());
}
data_ = static_cast<char*>(mmapResult);
#endif
Expand All @@ -56,39 +58,41 @@ MappedBuffer::~MappedBuffer()
if (viewPtr_) {
bool result = UnmapViewOfFile(viewPtr_);
if (!result) {
std::cerr << "[espm] can't unmap file, error=" << GetLastError()
std::cerr << "[MappedBuffer] can't unmap file, error=" << GetLastError()
<< std::endl;
std::terminate();
}
}
if (mapHandle_) {
bool result = CloseHandle(mapHandle_);
if (!result) {
std::cerr << "[espm] can't close map handle, error=" << GetLastError()
<< std::endl;
std::cerr << "[MappedBuffer] can't close map handle, error="
<< GetLastError() << std::endl;
std::terminate();
}
}
if (fileHandle_) {
bool result = CloseHandle(fileHandle_);
if (!result) {
std::cerr << "[espm] can't close file handle, error=" << GetLastError()
<< std::endl;
std::cerr << "[MappedBuffer] can't close file handle, error="
<< GetLastError() << std::endl;
std::terminate();
}
}
#else
if (data_) {
int result = munmap(data_, size_);
if (result == -1) {
std::cerr << "[espm] can't unmap file, errno=" << errno << std::endl;
std::cerr << "[MappedBuffer] can't unmap file, errno=" << errno
<< std::endl;
std::terminate();
}
}
if (fd_ != -1) {
int result = close(fd_);
if (result == -1) {
std::cerr << "[espm] can't close file, errno=" << errno << std::endl;
std::cerr << "[MappedBuffer] can't close file, errno=" << errno
<< std::endl;
std::terminate();
}
}
Expand All @@ -105,4 +109,4 @@ size_t MappedBuffer::GetLength() const
return size_;
}

} // namespace espm
}

0 comments on commit 148bce0

Please sign in to comment.