Commit b75576a1 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/system/

Summary: [Folly] Apply `clang-format` to `folly/system/`.

Reviewed By: igorsugak

Differential Revision: D9625730

fbshipit-source-id: 88f93231f77497d933233d4267c6983558baeb25
parent c5b81a8d
...@@ -45,7 +45,9 @@ static constexpr ssize_t kDefaultMlockChunkSize = ...@@ -45,7 +45,9 @@ static constexpr ssize_t kDefaultMlockChunkSize =
#endif // _MSC_VER #endif // _MSC_VER
; ;
DEFINE_int64(mlock_chunk_size, kDefaultMlockChunkSize, DEFINE_int64(
mlock_chunk_size,
kDefaultMlockChunkSize,
"Maximum bytes to mlock/munlock/munmap at once " "Maximum bytes to mlock/munlock/munmap at once "
"(will be rounded up to PAGESIZE). Ignored if negative."); "(will be rounded up to PAGESIZE). Ignored if negative.");
...@@ -59,24 +61,33 @@ MemoryMapping::MemoryMapping(MemoryMapping&& other) noexcept { ...@@ -59,24 +61,33 @@ MemoryMapping::MemoryMapping(MemoryMapping&& other) noexcept {
swap(other); swap(other);
} }
MemoryMapping::MemoryMapping(File file, off_t offset, off_t length, MemoryMapping::MemoryMapping(
File file,
off_t offset,
off_t length,
Options options) Options options)
: file_(std::move(file)), : file_(std::move(file)), options_(std::move(options)) {
options_(std::move(options)) {
CHECK(file_); CHECK(file_);
init(offset, length); init(offset, length);
} }
MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length, MemoryMapping::MemoryMapping(
const char* name,
off_t offset,
off_t length,
Options options) Options options)
: MemoryMapping(File(name, options.writable ? O_RDWR : O_RDONLY), : MemoryMapping(
File(name, options.writable ? O_RDWR : O_RDONLY),
offset, offset,
length, length,
options) { } options) {}
MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length, MemoryMapping::MemoryMapping(
int fd,
off_t offset,
off_t length,
Options options) Options options)
: MemoryMapping(File(fd), offset, length, options) { } : MemoryMapping(File(fd), offset, length, options) {}
MemoryMapping::MemoryMapping(AnonymousType, off_t length, Options options) MemoryMapping::MemoryMapping(AnonymousType, off_t length, Options options)
: options_(std::move(options)) { : options_(std::move(options)) {
...@@ -187,15 +198,15 @@ void MemoryMapping::init(off_t offset, off_t length) { ...@@ -187,15 +198,15 @@ void MemoryMapping::init(off_t offset, off_t length) {
// The standard doesn't actually require PROT_NONE to be zero... // The standard doesn't actually require PROT_NONE to be zero...
int prot = PROT_NONE; int prot = PROT_NONE;
if (options_.readable || options_.writable) { if (options_.readable || options_.writable) {
prot = ((options_.readable ? PROT_READ : 0) | prot =
((options_.readable ? PROT_READ : 0) |
(options_.writable ? PROT_WRITE : 0)); (options_.writable ? PROT_WRITE : 0));
} }
unsigned char* start = static_cast<unsigned char*>(mmap( unsigned char* start = static_cast<unsigned char*>(mmap(
options_.address, size_t(mapLength_), prot, flags, file_.fd(), offset)); options_.address, size_t(mapLength_), prot, flags, file_.fd(), offset));
PCHECK(start != MAP_FAILED) PCHECK(start != MAP_FAILED)
<< " offset=" << offset << " offset=" << offset << " length=" << mapLength_;
<< " length=" << mapLength_;
mapStart_ = start; mapStart_ = start;
data_.reset(start + skipStart, size_t(length)); data_.reset(start + skipStart, size_t(length));
} }
...@@ -224,8 +235,11 @@ off_t memOpChunkSize(off_t length, off_t pageSize) { ...@@ -224,8 +235,11 @@ off_t memOpChunkSize(off_t length, off_t pageSize) {
* - success: true + amountSucceeded == bufSize (op success on whole buffer) * - success: true + amountSucceeded == bufSize (op success on whole buffer)
* - failure: false + amountSucceeded == nr bytes on which op succeeded. * - failure: false + amountSucceeded == nr bytes on which op succeeded.
*/ */
bool memOpInChunks(std::function<int(void*, size_t)> op, bool memOpInChunks(
void* mem, size_t bufSize, off_t pageSize, std::function<int(void*, size_t)> op,
void* mem,
size_t bufSize,
off_t pageSize,
size_t& amountSucceeded) { size_t& amountSucceeded) {
// Linux' unmap/mlock/munlock take a kernel semaphore and block other threads // Linux' unmap/mlock/munlock take a kernel semaphore and block other threads
// from doing other memory operations. If the size of the buffer is big the // from doing other memory operations. If the size of the buffer is big the
...@@ -275,7 +289,11 @@ bool MemoryMapping::mlock(LockMode lock) { ...@@ -275,7 +289,11 @@ bool MemoryMapping::mlock(LockMode lock) {
} }
// only part of the buffer was mlocked, unlock it back // only part of the buffer was mlocked, unlock it back
if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, options_.pageSize, if (!memOpInChunks(
::munlock,
mapStart_,
amountSucceeded,
options_.pageSize,
amountSucceeded)) { amountSucceeded)) {
PLOG(WARNING) << "munlock()"; PLOG(WARNING) << "munlock()";
} }
...@@ -317,8 +335,8 @@ MemoryMapping::~MemoryMapping() { ...@@ -317,8 +335,8 @@ MemoryMapping::~MemoryMapping() {
size_t(mapLength_), size_t(mapLength_),
options_.pageSize, options_.pageSize,
amountSucceeded)) { amountSucceeded)) {
PLOG(FATAL) << folly::format("munmap({}) failed at {}", PLOG(FATAL) << folly::format(
mapLength_, amountSucceeded); "munmap({}) failed at {}", mapLength_, amountSucceeded);
} }
} }
} }
...@@ -329,8 +347,7 @@ void MemoryMapping::advise(int advice) const { ...@@ -329,8 +347,7 @@ void MemoryMapping::advise(int advice) const {
void MemoryMapping::advise(int advice, size_t offset, size_t length) const { void MemoryMapping::advise(int advice, size_t offset, size_t length) const {
CHECK_LE(offset + length, size_t(mapLength_)) CHECK_LE(offset + length, size_t(mapLength_))
<< " offset: " << offset << " offset: " << offset << " length: " << length
<< " length: " << length
<< " mapLength_: " << mapLength_; << " mapLength_: " << mapLength_;
// Include the entire start page: round down to page boundary. // Include the entire start page: round down to page boundary.
...@@ -366,7 +383,9 @@ void MemoryMapping::swap(MemoryMapping& other) noexcept { ...@@ -366,7 +383,9 @@ void MemoryMapping::swap(MemoryMapping& other) noexcept {
swap(this->data_, other.data_); swap(this->data_, other.data_);
} }
void swap(MemoryMapping& a, MemoryMapping& b) noexcept { a.swap(b); } void swap(MemoryMapping& a, MemoryMapping& b) noexcept {
a.swap(b);
}
void alignedForwardMemcpy(void* dst, const void* src, size_t size) { void alignedForwardMemcpy(void* dst, const void* src, size_t size) {
assert(reinterpret_cast<uintptr_t>(src) % alignof(unsigned long) == 0); assert(reinterpret_cast<uintptr_t>(src) % alignof(unsigned long) == 0);
...@@ -399,7 +418,8 @@ void mmapFileCopy(const char* src, const char* dest, mode_t mode) { ...@@ -399,7 +418,8 @@ void mmapFileCopy(const char* src, const char* dest, mode_t mode) {
off_t(srcMap.range().size()), off_t(srcMap.range().size()),
MemoryMapping::writable()); MemoryMapping::writable());
alignedForwardMemcpy(destMap.writableRange().data(), alignedForwardMemcpy(
destMap.writableRange().data(),
srcMap.range().data(), srcMap.range().data(),
srcMap.range().size()); srcMap.range().size());
} }
......
...@@ -38,7 +38,7 @@ class MemoryMapping : boost::noncopyable { ...@@ -38,7 +38,7 @@ class MemoryMapping : boost::noncopyable {
*/ */
enum class LockMode { enum class LockMode {
TRY_LOCK, TRY_LOCK,
MUST_LOCK MUST_LOCK,
}; };
/** /**
* Map a portion of the file indicated by filename in memory, causing a CHECK * Map a portion of the file indicated by filename in memory, causing a CHECK
...@@ -55,12 +55,30 @@ class MemoryMapping : boost::noncopyable { ...@@ -55,12 +55,30 @@ class MemoryMapping : boost::noncopyable {
Options() {} Options() {}
// Convenience methods; return *this for chaining. // Convenience methods; return *this for chaining.
Options& setPageSize(off_t v) { pageSize = v; return *this; } Options& setPageSize(off_t v) {
Options& setShared(bool v) { shared = v; return *this; } pageSize = v;
Options& setPrefault(bool v) { prefault = v; return *this; } return *this;
Options& setReadable(bool v) { readable = v; return *this; } }
Options& setWritable(bool v) { writable = v; return *this; } Options& setShared(bool v) {
Options& setGrow(bool v) { grow = v; return *this; } shared = v;
return *this;
}
Options& setPrefault(bool v) {
prefault = v;
return *this;
}
Options& setReadable(bool v) {
readable = v;
return *this;
}
Options& setWritable(bool v) {
writable = v;
return *this;
}
Options& setGrow(bool v) {
grow = v;
return *this;
}
// Page size. 0 = use appropriate page size. // Page size. 0 = use appropriate page size.
// (On Linux, we use a huge page size if the file is on a hugetlbfs // (On Linux, we use a huge page size if the file is on a hugetlbfs
...@@ -103,28 +121,31 @@ class MemoryMapping : boost::noncopyable { ...@@ -103,28 +121,31 @@ class MemoryMapping : boost::noncopyable {
} }
enum AnonymousType { enum AnonymousType {
kAnonymous kAnonymous,
}; };
/** /**
* Create an anonymous mapping. * Create an anonymous mapping.
*/ */
MemoryMapping(AnonymousType, off_t length, Options options=Options()); MemoryMapping(AnonymousType, off_t length, Options options = Options());
explicit MemoryMapping(File file, explicit MemoryMapping(
off_t offset=0, File file,
off_t length=-1, off_t offset = 0,
Options options=Options()); off_t length = -1,
Options options = Options());
explicit MemoryMapping(const char* name,
off_t offset=0, explicit MemoryMapping(
off_t length=-1, const char* name,
Options options=Options()); off_t offset = 0,
off_t length = -1,
explicit MemoryMapping(int fd, Options options = Options());
off_t offset=0,
off_t length=-1, explicit MemoryMapping(
Options options=Options()); int fd,
off_t offset = 0,
off_t length = -1,
Options options = Options());
MemoryMapping(MemoryMapping&&) noexcept; MemoryMapping(MemoryMapping&&) noexcept;
...@@ -165,9 +186,8 @@ class MemoryMapping : boost::noncopyable { ...@@ -165,9 +186,8 @@ class MemoryMapping : boost::noncopyable {
template <class T> template <class T>
Range<const T*> asRange() const { Range<const T*> asRange() const {
size_t count = data_.size() / sizeof(T); size_t count = data_.size() / sizeof(T);
return Range<const T*>(static_cast<const T*>( return Range<const T*>(
static_cast<const void*>(data_.data())), static_cast<const T*>(static_cast<const void*>(data_.data())), count);
count);
} }
/** /**
...@@ -185,9 +205,7 @@ class MemoryMapping : boost::noncopyable { ...@@ -185,9 +205,7 @@ class MemoryMapping : boost::noncopyable {
Range<T*> asWritableRange() const { Range<T*> asWritableRange() const {
DCHECK(options_.writable); // you'll segfault anyway... DCHECK(options_.writable); // you'll segfault anyway...
size_t count = data_.size() / sizeof(T); size_t count = data_.size() / sizeof(T);
return Range<T*>(static_cast<T*>( return Range<T*>(static_cast<T*>(static_cast<void*>(data_.data())), count);
static_cast<void*>(data_.data())),
count);
} }
/** /**
...@@ -210,7 +228,9 @@ class MemoryMapping : boost::noncopyable { ...@@ -210,7 +228,9 @@ class MemoryMapping : boost::noncopyable {
return locked_; return locked_;
} }
int fd() const { return file_.fd(); } int fd() const {
return file_.fd();
}
private: private:
MemoryMapping(); MemoryMapping();
......
...@@ -80,6 +80,7 @@ constexpr detail::ShellCmdFormat operator"" _shellify( ...@@ -80,6 +80,7 @@ constexpr detail::ShellCmdFormat operator"" _shellify(
* from namespace `folly::shell_literals` because that requires that the format * from namespace `folly::shell_literals` because that requires that the format
* string is a compile-time constant which can be inspected during code reviews * string is a compile-time constant which can be inspected during code reviews
*/ */
// clang-format off
template <typename... Arguments> template <typename... Arguments>
[[deprecated( [[deprecated(
"Use `\"command {} {} ...\"_shellify(argument1, argument2 ...)` from " "Use `\"command {} {} ...\"_shellify(argument1, argument2 ...)` from "
...@@ -89,5 +90,6 @@ std::vector<std::string> shellify( ...@@ -89,5 +90,6 @@ std::vector<std::string> shellify(
Arguments&&... arguments) { Arguments&&... arguments) {
return detail::shellify(format, std::forward<Arguments>(arguments)...); return detail::shellify(format, std::forward<Arguments>(arguments)...);
} }
// clang-format on
} // namespace folly } // namespace folly
...@@ -39,7 +39,9 @@ TEST(ThreadName, getCurrentThreadName) { ...@@ -39,7 +39,9 @@ TEST(ThreadName, getCurrentThreadName) {
EXPECT_EQ(kThreadName.toString(), *getCurrentThreadName()); EXPECT_EQ(kThreadName.toString(), *getCurrentThreadName());
} }
}); });
SCOPE_EXIT { th.join(); }; SCOPE_EXIT {
th.join();
};
} }
#if FOLLY_HAVE_PTHREAD #if FOLLY_HAVE_PTHREAD
...@@ -52,9 +54,13 @@ TEST(ThreadName, setThreadName_other_pthread) { ...@@ -52,9 +54,13 @@ TEST(ThreadName, setThreadName_other_pthread) {
handle_set.post(); handle_set.post();
let_thread_end.wait(); let_thread_end.wait();
}); });
SCOPE_EXIT { th.join(); }; SCOPE_EXIT {
th.join();
};
handle_set.wait(); handle_set.wait();
SCOPE_EXIT { let_thread_end.post(); }; SCOPE_EXIT {
let_thread_end.post();
};
EXPECT_EQ( EXPECT_EQ(
expectedSetOtherThreadNameResult, setThreadName(handle, kThreadName)); expectedSetOtherThreadNameResult, setThreadName(handle, kThreadName));
} }
...@@ -62,11 +68,13 @@ TEST(ThreadName, setThreadName_other_pthread) { ...@@ -62,11 +68,13 @@ TEST(ThreadName, setThreadName_other_pthread) {
TEST(ThreadName, setThreadName_other_id) { TEST(ThreadName, setThreadName_other_id) {
Baton<> let_thread_end; Baton<> let_thread_end;
thread th([&] { thread th([&] { let_thread_end.wait(); });
let_thread_end.wait(); SCOPE_EXIT {
}); th.join();
SCOPE_EXIT { th.join(); }; };
SCOPE_EXIT { let_thread_end.post(); }; SCOPE_EXIT {
let_thread_end.post();
};
EXPECT_EQ( EXPECT_EQ(
expectedSetOtherThreadNameResult, expectedSetOtherThreadNameResult,
setThreadName(th.get_id(), kThreadName)); setThreadName(th.get_id(), kThreadName));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment