Summary:
fix the following error showing up in continuous tests:
```
Makefile:186: Warning: Compiling in debug mode. Don't use the resulting binary in production
port/mmap.cc:46:15: error: first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'rocksdb::MemMapping' [-Werror,-Wnontrivial-memcall]
46 | std::memcpy(this, &other, sizeof(*this));
| ^
port/mmap.cc:46:15: note: explicitly cast the pointer to silence this warning
46 | std::memcpy(this, &other, sizeof(*this));
| ^
| (void*)
1 error generated.
make: *** [Makefile:2580: port/mmap.o] Error 1
make: *** Waiting for unfinished jobs....
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/13864
Test Plan: `make USE_CLANG=1 j=150 check` with 13f054febb/build_tools/build_detect_platform (L61-L70) commented out.
Reviewed By: mszeszko-meta
Differential Revision: D80033441
Pulled By: cbi42
fbshipit-source-id: b2330eea71fe28243236b75128ec6f3f1e971873
98 lines
2.4 KiB
C++
98 lines
2.4 KiB
C++
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "port/mmap.h"
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <new>
|
|
#include <utility>
|
|
|
|
#include "util/hash.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
MemMapping::~MemMapping() {
|
|
#ifdef OS_WIN
|
|
if (addr_ != nullptr) {
|
|
(void)::UnmapViewOfFile(addr_);
|
|
}
|
|
if (page_file_handle_ != NULL) {
|
|
(void)::CloseHandle(page_file_handle_);
|
|
}
|
|
#else // OS_WIN -> !OS_WIN
|
|
if (addr_ != nullptr) {
|
|
auto status = munmap(addr_, length_);
|
|
assert(status == 0);
|
|
if (status != 0) {
|
|
// TODO: handle error?
|
|
}
|
|
}
|
|
#endif // OS_WIN
|
|
}
|
|
|
|
MemMapping::MemMapping(MemMapping&& other) noexcept {
|
|
*this = std::move(other);
|
|
}
|
|
|
|
MemMapping& MemMapping::operator=(MemMapping&& other) noexcept {
|
|
if (&other == this) {
|
|
return *this;
|
|
}
|
|
this->~MemMapping();
|
|
std::memcpy(static_cast<void*>(this), &other, sizeof(*this));
|
|
new (&other) MemMapping();
|
|
return *this;
|
|
}
|
|
|
|
MemMapping MemMapping::AllocateAnonymous(size_t length, bool huge) {
|
|
MemMapping mm;
|
|
mm.length_ = length;
|
|
assert(mm.addr_ == nullptr);
|
|
if (length == 0) {
|
|
// OK to leave addr as nullptr
|
|
return mm;
|
|
}
|
|
int huge_flag = 0;
|
|
#ifdef OS_WIN
|
|
if (huge) {
|
|
#ifdef FILE_MAP_LARGE_PAGES
|
|
huge_flag = FILE_MAP_LARGE_PAGES;
|
|
#endif // FILE_MAP_LARGE_PAGES
|
|
}
|
|
mm.page_file_handle_ = ::CreateFileMapping(
|
|
INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_COMMIT,
|
|
Upper32of64(length), Lower32of64(length), nullptr);
|
|
if (mm.page_file_handle_ == NULL) {
|
|
// Failure
|
|
return mm;
|
|
}
|
|
mm.addr_ = ::MapViewOfFile(mm.page_file_handle_, FILE_MAP_WRITE | huge_flag,
|
|
0, 0, length);
|
|
#else // OS_WIN -> !OS_WIN
|
|
if (huge) {
|
|
#ifdef MAP_HUGETLB
|
|
huge_flag = MAP_HUGETLB;
|
|
#endif // MAP_HUGE_TLB
|
|
}
|
|
mm.addr_ = mmap(nullptr, length, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS | huge_flag, -1, 0);
|
|
if (mm.addr_ == MAP_FAILED) {
|
|
mm.addr_ = nullptr;
|
|
}
|
|
#endif // OS_WIN
|
|
return mm;
|
|
}
|
|
|
|
MemMapping MemMapping::AllocateHuge(size_t length) {
|
|
return AllocateAnonymous(length, /*huge*/ true);
|
|
}
|
|
|
|
MemMapping MemMapping::AllocateLazyZeroed(size_t length) {
|
|
return AllocateAnonymous(length, /*huge*/ false);
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|