-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from wheremyfoodat/rhappy
Stuff
- Loading branch information
Showing
13 changed files
with
236 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <system_error> | ||
|
||
#include "helpers.hpp" | ||
#include "mio/mio.hpp" | ||
|
||
// Minimal RAII wrapper over memory mapped files | ||
|
||
class MemoryMappedFile { | ||
std::filesystem::path filePath = ""; // path of our file | ||
mio::mmap_sink map; // mmap sink for our file | ||
|
||
u8* pointer = nullptr; // Pointer to the contents of the memory mapped file | ||
bool opened = false; | ||
|
||
public: | ||
bool exists() const { return opened; } | ||
u8* data() const { return pointer; } | ||
|
||
std::error_code flush(); | ||
MemoryMappedFile(); | ||
MemoryMappedFile(const std::filesystem::path& path); | ||
|
||
~MemoryMappedFile(); | ||
// Returns true on success | ||
bool open(const std::filesystem::path& path); | ||
void close(); | ||
|
||
// TODO: For memory-mapped output files we'll need some more stuff such as a constructor that takes path/size/shouldCreate as parameters | ||
|
||
u8& operator[](size_t index) { return pointer[index]; } | ||
const u8& operator[](size_t index) const { return pointer[index]; } | ||
|
||
auto begin() { return map.begin(); } | ||
auto end() { return map.end(); } | ||
auto cbegin() { return map.cbegin(); } | ||
auto cend() { return map.cend(); } | ||
|
||
mio::mmap_sink& getSink() { return map; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "memory_mapped_file.hpp" | ||
|
||
MemoryMappedFile::MemoryMappedFile() : opened(false), filePath(""), pointer(nullptr) {} | ||
MemoryMappedFile::MemoryMappedFile(const std::filesystem::path& path) { open(path); } | ||
MemoryMappedFile::~MemoryMappedFile() { close(); } | ||
|
||
// TODO: This should probably also return the error one way or another eventually | ||
bool MemoryMappedFile::open(const std::filesystem::path& path) { | ||
std::error_code error; | ||
map = mio::make_mmap_sink(path.string(), 0, mio::map_entire_file, error); | ||
|
||
if (error) { | ||
opened = false; | ||
return false; | ||
} | ||
|
||
filePath = path; | ||
pointer = (u8*)map.data(); | ||
opened = true; | ||
return true; | ||
} | ||
|
||
void MemoryMappedFile::close() { | ||
if (opened) { | ||
opened = false; | ||
pointer = nullptr; // Set the pointer to nullptr to avoid errors related to lingering pointers | ||
|
||
map.unmap(); | ||
} | ||
} | ||
|
||
std::error_code MemoryMappedFile::flush() { | ||
std::error_code ret; | ||
map.sync(ret); | ||
|
||
return ret; | ||
} |
Oops, something went wrong.