From 75c6adfae4a1842511e8b62150cc18ffb391efa7 Mon Sep 17 00:00:00 2001 From: Nicholas Ormrod Date: Thu, 30 Oct 2014 10:18:27 -0700 Subject: [PATCH] Move constructors should be noexcept Summary: The compiler is able to make some nice optimizations when it knows that move constructors are noexcept. (In particular, it helps a lot inside of std::vector if you don't need to worry about the possibility of exception during reallocation.) Some move constructors in folly are obviously moveable: change them. Test Plan: unit tests Reviewed By: delong.j@fb.com Subscribers: trunkagent, sdwilsh, njormrod, folly-diffs@ FB internal diff: D1644296 Tasks: 5486739 Signature: t1:1644296:1414618605:d9a0db5193c82650b96e9c62b019d5da218b15c5 --- folly/Benchmark.h | 2 +- folly/File.cpp | 4 ++-- folly/File.h | 4 ++-- folly/MemoryMapping.cpp | 6 +++--- folly/MemoryMapping.h | 6 +++--- folly/ScopeGuard.h | 2 +- folly/ThreadLocal.h | 2 +- folly/io/IOBufQueue.cpp | 2 +- folly/io/IOBufQueue.h | 2 +- folly/test/ApplyTupleTest.cpp | 4 ++-- folly/test/LazyTest.cpp | 2 +- folly/test/small_vector_test.cpp | 2 +- 12 files changed, 19 insertions(+), 19 deletions(-) diff --git a/folly/Benchmark.h b/folly/Benchmark.h index 4fd28b48..3460a79d 100644 --- a/folly/Benchmark.h +++ b/folly/Benchmark.h @@ -116,7 +116,7 @@ struct BenchmarkSuspender { } BenchmarkSuspender(const BenchmarkSuspender &) = delete; - BenchmarkSuspender(BenchmarkSuspender && rhs) { + BenchmarkSuspender(BenchmarkSuspender && rhs) noexcept { start = rhs.start; rhs.start.tv_nsec = rhs.start.tv_sec = 0; } diff --git a/folly/File.cpp b/folly/File.cpp index 12c4902f..2f239c2a 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -52,7 +52,7 @@ File::File(const char* name, int flags, mode_t mode) ownsFd_ = true; } -File::File(File&& other) +File::File(File&& other) noexcept : fd_(other.fd_) , ownsFd_(other.ownsFd_) { other.release(); @@ -80,7 +80,7 @@ File::~File() { return File(fd, true); } -int File::release() { +int File::release() noexcept { int released = fd_; fd_ = -1; ownsFd_ = false; diff --git a/folly/File.h b/folly/File.h index 2183609b..4a25e1c6 100644 --- a/folly/File.h +++ b/folly/File.h @@ -87,7 +87,7 @@ class File { * Returns and releases the file descriptor; no longer owned by this File. * Returns -1 if the File object didn't wrap a file. */ - int release(); + int release() noexcept; /** * Swap this File with another. @@ -95,7 +95,7 @@ class File { void swap(File& other); // movable - File(File&&); + File(File&&) noexcept; File& operator=(File&&); // FLOCK (INTERPROCESS) LOCKS diff --git a/folly/MemoryMapping.cpp b/folly/MemoryMapping.cpp index 4e78b560..855d4af6 100644 --- a/folly/MemoryMapping.cpp +++ b/folly/MemoryMapping.cpp @@ -38,7 +38,7 @@ DEFINE_int64(mlock_chunk_size, 1 << 20, // 1MB namespace folly { -MemoryMapping::MemoryMapping(MemoryMapping&& other) { +MemoryMapping::MemoryMapping(MemoryMapping&& other) noexcept { swap(other); } @@ -298,7 +298,7 @@ MemoryMapping& MemoryMapping::operator=(MemoryMapping other) { return *this; } -void MemoryMapping::swap(MemoryMapping& other) { +void MemoryMapping::swap(MemoryMapping& other) noexcept { using std::swap; swap(this->file_, other.file_); swap(this->mapStart_, other.mapStart_); @@ -308,7 +308,7 @@ void MemoryMapping::swap(MemoryMapping& other) { swap(this->data_, other.data_); } -void swap(MemoryMapping& a, MemoryMapping& b) { a.swap(b); } +void swap(MemoryMapping& a, MemoryMapping& b) noexcept { a.swap(b); } void alignedForwardMemcpy(void* dst, const void* src, size_t size) { assert(reinterpret_cast(src) % alignof(unsigned long) == 0); diff --git a/folly/MemoryMapping.h b/folly/MemoryMapping.h index 19ca243e..d404a785 100644 --- a/folly/MemoryMapping.h +++ b/folly/MemoryMapping.h @@ -127,13 +127,13 @@ class MemoryMapping : boost::noncopyable { off_t length=-1, Options options=Options()); - MemoryMapping(MemoryMapping&&); + MemoryMapping(MemoryMapping&&) noexcept; ~MemoryMapping(); MemoryMapping& operator=(MemoryMapping); - void swap(MemoryMapping& other); + void swap(MemoryMapping& other) noexcept; /** * Lock the pages in memory @@ -229,7 +229,7 @@ class MemoryMapping : boost::noncopyable { MutableByteRange data_; }; -void swap(MemoryMapping&, MemoryMapping&); +void swap(MemoryMapping&, MemoryMapping&) noexcept; /** * A special case of memcpy() that always copies memory forwards. diff --git a/folly/ScopeGuard.h b/folly/ScopeGuard.h index 9a65811d..348a5933 100644 --- a/folly/ScopeGuard.h +++ b/folly/ScopeGuard.h @@ -77,7 +77,7 @@ class ScopeGuardImplBase { ScopeGuardImplBase() : dismissed_(false) {} - ScopeGuardImplBase(ScopeGuardImplBase&& other) + ScopeGuardImplBase(ScopeGuardImplBase&& other) noexcept : dismissed_(other.dismissed_) { other.dismissed_ = true; } diff --git a/folly/ThreadLocal.h b/folly/ThreadLocal.h index ad111168..78a26979 100644 --- a/folly/ThreadLocal.h +++ b/folly/ThreadLocal.h @@ -137,7 +137,7 @@ class ThreadLocalPtr { public: ThreadLocalPtr() : id_(threadlocal_detail::StaticMeta::create()) { } - ThreadLocalPtr(ThreadLocalPtr&& other) : id_(other.id_) { + ThreadLocalPtr(ThreadLocalPtr&& other) noexcept : id_(other.id_) { other.id_ = 0; } diff --git a/folly/io/IOBufQueue.cpp b/folly/io/IOBufQueue.cpp index 8f40d079..23e721fa 100644 --- a/folly/io/IOBufQueue.cpp +++ b/folly/io/IOBufQueue.cpp @@ -71,7 +71,7 @@ IOBufQueue::IOBufQueue(const Options& options) chainLength_(0) { } -IOBufQueue::IOBufQueue(IOBufQueue&& other) +IOBufQueue::IOBufQueue(IOBufQueue&& other) noexcept : options_(other.options_), chainLength_(other.chainLength_), head_(std::move(other.head_)) { diff --git a/folly/io/IOBufQueue.h b/folly/io/IOBufQueue.h index 59fdc940..cbc11376 100644 --- a/folly/io/IOBufQueue.h +++ b/folly/io/IOBufQueue.h @@ -262,7 +262,7 @@ class IOBufQueue { void clear(); /** Movable */ - IOBufQueue(IOBufQueue&&); + IOBufQueue(IOBufQueue&&) noexcept; IOBufQueue& operator=(IOBufQueue&&); private: diff --git a/folly/test/ApplyTupleTest.cpp b/folly/test/ApplyTupleTest.cpp index bf82a9cd..be48939f 100644 --- a/folly/test/ApplyTupleTest.cpp +++ b/folly/test/ApplyTupleTest.cpp @@ -73,7 +73,7 @@ std::function makeFunc() { } struct GuardObjBase { - GuardObjBase(GuardObjBase&&) {} + GuardObjBase(GuardObjBase&&) noexcept {} GuardObjBase() {} GuardObjBase(GuardObjBase const&) = delete; GuardObjBase& operator=(GuardObjBase const&) = delete; @@ -115,7 +115,7 @@ guard(F&& f, Args&&... args) { struct Mover { Mover() {} - Mover(Mover&&) {} + Mover(Mover&&) noexcept {} Mover(const Mover&) = delete; Mover& operator=(const Mover&) = delete; }; diff --git a/folly/test/LazyTest.cpp b/folly/test/LazyTest.cpp index d2947de2..6d3eed06 100644 --- a/folly/test/LazyTest.cpp +++ b/folly/test/LazyTest.cpp @@ -72,7 +72,7 @@ TEST(Lazy, Map) { struct CopyCount { CopyCount() {} CopyCount(const CopyCount&) { ++count; } - CopyCount(CopyCount&&) {} + CopyCount(CopyCount&&) noexcept {} static int count; diff --git a/folly/test/small_vector_test.cpp b/folly/test/small_vector_test.cpp index 89362d38..f80a4605 100644 --- a/folly/test/small_vector_test.cpp +++ b/folly/test/small_vector_test.cpp @@ -136,7 +136,7 @@ struct NoncopyableCounter { ~NoncopyableCounter() { --alive; } - NoncopyableCounter(NoncopyableCounter&&) { ++alive; } + NoncopyableCounter(NoncopyableCounter&&) noexcept { ++alive; } NoncopyableCounter(NoncopyableCounter const&) = delete; NoncopyableCounter& operator=(NoncopyableCounter const&) const = delete; NoncopyableCounter& operator=(NoncopyableCounter&&) { return *this; } -- 2.34.1