HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_view_posix_impl.hpp
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#include "file_view.hpp"
6#include "../utility/utility.hpp"
7#include "../macros.hpp"
8#include <mutex>
9#include <sys/mman.h>
10
11
12
13namespace hi::inline v1 {
14
15file_view::file_view(std::shared_ptr<file_mapping> const &_file_mapping_object, std::size_t offset, std::size_t size) :
17{
18 if (size == 0) {
19 size = _file_mapping_object->size - _offset;
20 }
21 hi_assert(_offset + size <= _file_mapping_object->size);
22
23 int prot;
24 if (accessMode() >= (AccessMode::Read | AccessMode::Write)) {
25 prot = PROT_WRITE | PROT_READ;
26 } else if (accessMode() >= AccessMode::Read) {
27 prot = PROT_READ;
28 } else {
29 throw io_error("{}: Illegal access mode write-only when viewing file.", location());
30 }
31
32 int flags = MAP_SHARED;
33
34 void *data;
35 if ((data = ::mmap(0, size, prot, flags, _file_mapping_object->file->fileHandle, _offset)) == MAP_FAILED) {
36 throw io_error("{}: Could not map view of file. '{}'", location(), get_last_error_message());
37 }
38
39 auto *bytes_ptr = new void_span(data, size);
40 _bytes = std::shared_ptr<void_span>(bytes_ptr, file_view::unmap);
41}
42
43file_view::file_view(std::filesystem::path const &path, AccessMode accessMode, std::size_t offset, std::size_t size) :
44 file_view(findOrCreateFileMappingObject(path, accessMode, offset + size), offset, size)
45{
46}
47
48file_view::file_view(file_view const &other) noexcept :
49 _file_mapping_object(other._file_mapping_object), _bytes(other._bytes), _offset(other._offset)
50{
51 hi_assert(&other != this);
52}
53
54file_view &file_view::operator=(file_view const &other) noexcept
55{
56 hi_return_on_self_assignment(other);
57 _file_mapping_object = other._file_mapping_object;
58 _offset = other._offset;
59 _bytes = other._bytes;
60 return *this;
61}
62
63file_view::file_view(file_view &&other) noexcept :
64 _file_mapping_object(std::move(other._file_mapping_object)), _bytes(std::move(other._bytes)), _offset(other._offset)
65{
66 hi_assert(&other != this);
67}
68
69file_view &file_view::operator=(file_view &&other) noexcept
70{
71 hi_return_on_self_assignment(other);
72 _file_mapping_object = std::move(other._file_mapping_object);
73 _offset = other._offset;
74 _bytes = std::move(other._bytes);
75 return *this;
76}
77
78void file_view::unmap(void_span *bytes) noexcept
79{
80 if (bytes != nullptr) {
81 if (!bytes->empty()) {
82 if (!munmap(bytes->data(), bytes->size())) {
83 hi_log_error("Could not munmap view on file '{}'", get_last_error_message());
84 }
85 }
86 delete bytes;
87 }
88}
89
90void file_view::flush(void *base, std::size_t size)
91{
92 int flags = MS_SYNC;
93 if (!msync(base, size, flags)) {
94 throw io_error("{}: Could not flush file '{}'", location(), get_last_error_message());
95 }
96}
97
98} // namespace hi::inline v1
Defines the file_view class.
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
std::string get_last_error_message(uint32_t error_code)
Get the error message from an error code.
Definition exception_win32_impl.hpp:22
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T move(T... args)