Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from lat9nq/mmap-fix
Browse files Browse the repository at this point in the history
stingray, mapped_file: Various fixes
  • Loading branch information
Xaymar authored Apr 13, 2024
2 parents d0e579e + 94338e7 commit ad4ecf6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
13 changes: 8 additions & 5 deletions source/mapped_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
#define NOMINMAX
#include <Windows.h>
#else
#include <cerrno>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif

mapped_file::mapped_file() : _path(), _file(), _map(), _ptr() {}

mapped_file::mapped_file(std::filesystem::path path) : _path(path)
mapped_file::mapped_file(const std::filesystem::path& path) : _path(path)
{
#ifdef WIN32
_file.reset(CreateFileA(reinterpret_cast<LPCSTR>(path.generic_string().c_str()), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL), [](void* p) { CloseHandle(p); });
Expand All @@ -40,14 +41,16 @@ mapped_file::mapped_file(std::filesystem::path path) : _path(path)
throw std::runtime_error("MapViewOfFile failed.");
}
#else
_file.reset(reinterpret_cast<void*>(open(path.generic_string().c_str(), O_NOATIME | O_RDONLY)), [](void* p) { close(reinterpret_cast<size_t>(p)); });
int fd = open(path.generic_string().c_str(), O_NOATIME | O_RDONLY);
int err = errno;
_file.reset(reinterpret_cast<void*>(fd), [](void* p) { close(reinterpret_cast<size_t>(p)); });
if (reinterpret_cast<size_t>(_file.get()) == -1) {
throw std::runtime_error("open failed.");
throw std::runtime_error(std::string{"open failed: "} + std::to_string(err));
}

size_t size = std::filesystem::file_size(path);
_map.reset(reinterpret_cast<void*>(mmap(nullptr, size, PROT_READ, MAP_NORESERVE, reinterpret_cast<size_t>(_file.get()), 0)), [&size](void* p) { munmap(p, size); });
if (!_map) {
_map.reset(reinterpret_cast<void*>(mmap(nullptr, size, PROT_READ, MAP_NORESERVE | MAP_PRIVATE, reinterpret_cast<size_t>(_file.get()), 0)), [&size](void* p) { munmap(p, size); });
if (_map.get() == reinterpret_cast<void*>(-1)) {
throw std::runtime_error("mmap failed.");
}

Expand Down
2 changes: 1 addition & 1 deletion source/mapped_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class mapped_file {

public:
mapped_file();
mapped_file(std::filesystem::path path);
mapped_file(const std::filesystem::path& path);

~mapped_file();

Expand Down
3 changes: 1 addition & 2 deletions source/stingray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "stingray.hpp"
#include "endian.h"

stingray::hash_t& stingray::hash_t::operator=(stingray::hash_t const& other)
{
Expand Down Expand Up @@ -61,7 +60,7 @@ stingray::hash_t::operator uint64_t() const noexcept

stingray::hash_t::operator stingray::thin_hash_t() const noexcept
{
return {(_value) >> 32};
return static_cast<uint32_t>((_value) >> 32);
}

stingray::hash_t::operator uint32_t() const noexcept
Expand Down
3 changes: 1 addition & 2 deletions source/stingray_unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once
#include <cinttypes>
#include <cstddef>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "stingray.hpp"
#include "stingray_data.hpp"

Expand Down

0 comments on commit ad4ecf6

Please sign in to comment.