2019-07-07 02:05:30 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/MemoryMapping.h"
|
|
|
|
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/logging.h"
|
2019-07-07 02:05:30 +02:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// windows,
|
2019-07-21 20:07:07 +02:00
|
|
|
// anonymous maps,
|
2019-07-07 02:05:30 +02:00
|
|
|
// huge pages?
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
#if TD_WINDOWS
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
2019-07-21 20:07:07 +02:00
|
|
|
|
|
|
|
class MemoryMapping::Impl {
|
2019-07-07 02:05:30 +02:00
|
|
|
public:
|
2019-07-21 20:07:07 +02:00
|
|
|
Impl(MutableSlice data, int64 offset) : data_(data), offset_(offset) {
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
Slice as_slice() const {
|
|
|
|
return data_.substr(narrow_cast<size_t>(offset_));
|
|
|
|
}
|
|
|
|
MutableSlice as_mutable_slice() const {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MutableSlice data_;
|
|
|
|
int64 offset_;
|
|
|
|
};
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
static Result<int64> get_page_size() {
|
2019-07-07 02:05:30 +02:00
|
|
|
#if TD_WINDOWS
|
|
|
|
return Status::Error("Unimplemented");
|
|
|
|
#else
|
|
|
|
static Result<int64> page_size = []() -> Result<int64> {
|
|
|
|
auto page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
if (page_size < 0) {
|
|
|
|
return OS_ERROR("Can't load page size from sysconf");
|
|
|
|
}
|
|
|
|
return page_size;
|
|
|
|
}();
|
|
|
|
return page_size.clone();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<MemoryMapping> MemoryMapping::create_anonymous(const MemoryMapping::Options &options) {
|
|
|
|
return Status::Error("Unsupported yet");
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
Result<MemoryMapping> MemoryMapping::create_from_file(const FileFd &file_fd, const MemoryMapping::Options &options) {
|
|
|
|
#if TD_WINDOWS
|
|
|
|
return Status::Error("Unsupported yet");
|
|
|
|
#else
|
|
|
|
if (file_fd.empty()) {
|
|
|
|
return Status::Error("Can't create memory mapping: file is empty");
|
|
|
|
}
|
|
|
|
TRY_RESULT(stat, file_fd.stat());
|
|
|
|
auto fd = file_fd.get_native_fd().fd();
|
|
|
|
auto begin = options.offset;
|
|
|
|
if (begin < 0) {
|
|
|
|
return Status::Error(PSLICE() << "Can't create memory mapping: negative offset " << options.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 end;
|
|
|
|
if (options.size < 0) {
|
|
|
|
end = stat.size_;
|
|
|
|
} else {
|
|
|
|
end = begin + stat.size_;
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
TRY_RESULT(page_size, get_page_size());
|
2019-07-07 02:05:30 +02:00
|
|
|
auto fixed_begin = begin / page_size * page_size;
|
|
|
|
|
|
|
|
auto data_offset = begin - fixed_begin;
|
|
|
|
auto data_size = end - fixed_begin;
|
|
|
|
|
|
|
|
void *data = mmap(nullptr, data_size, PROT_READ, MAP_PRIVATE, fd, fixed_begin);
|
|
|
|
if (data == MAP_FAILED) {
|
|
|
|
return OS_ERROR("mmap call failed");
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
return MemoryMapping(make_unique<Impl>(MutableSlice(static_cast<char *>(data), data_size), data_offset));
|
2019-07-07 02:05:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryMapping::MemoryMapping(MemoryMapping &&other) = default;
|
|
|
|
MemoryMapping &MemoryMapping::operator=(MemoryMapping &&other) = default;
|
|
|
|
MemoryMapping::~MemoryMapping() = default;
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
MemoryMapping::MemoryMapping(unique_ptr<Impl> impl) : impl_(std::move(impl)) {
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
Slice MemoryMapping::as_slice() const {
|
|
|
|
return impl_->as_slice();
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
MutableSlice MemoryMapping::as_mutable_slice() {
|
|
|
|
return impl_->as_mutable_slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|