Commit f73c7249 authored by Eric Niebler's avatar Eric Niebler Committed by Facebook Github Bot 8

use std::forward instead of std::move on objects whose types have been...

use std::forward instead of std::move on objects whose types have been deduced; don't take the sizeof incomplete types

Summary: Pretty sure std::forward is needed here instead of std::move. If you trace the call chain to see where the types of the objects come from, you'll see they can be deduced to be lvalues, so std::forward is the right choice. Also, moved some dicey looking code that appeared to be taking the size of some incomkplete types.

Reviewed By: spacedentist

Differential Revision: D3214199

fb-gh-sync-id: 778190ffb25a648b839760a3dddfad8dc6d41c88
fbshipit-source-id: 778190ffb25a648b839760a3dddfad8dc6d41c88
parent 95e7a3ba
...@@ -336,20 +336,13 @@ struct IsRvalueRefTry<folly::Try<T>&&> { static const bool value = true; }; ...@@ -336,20 +336,13 @@ struct IsRvalueRefTry<folly::Try<T>&&> { static const bool value = true; };
template <typename F, typename G> template <typename F, typename G>
struct FiberManager::AddTaskFinallyHelper { struct FiberManager::AddTaskFinallyHelper {
class Func; class Func;
class Finally;
typedef typename std::result_of<F()>::type Result; typedef typename std::result_of<F()>::type Result;
static constexpr bool allocateInBuffer =
sizeof(Func) + sizeof(Finally) <= Fiber::kUserBufferSize;
class Finally { class Finally {
public: public:
Finally(G&& finally, Finally(G finally, FiberManager& fm)
FiberManager& fm) : : finally_(std::move(finally)), fm_(fm) {}
finally_(std::forward<G>(finally)),
fm_(fm) {
}
void operator()() { void operator()() {
try { try {
...@@ -376,8 +369,8 @@ struct FiberManager::AddTaskFinallyHelper { ...@@ -376,8 +369,8 @@ struct FiberManager::AddTaskFinallyHelper {
class Func { class Func {
public: public:
Func(F&& func, Finally& finally) : Func(F func, Finally& finally)
func_(std::move(func)), result_(finally.result_) {} : func_(std::move(func)), result_(finally.result_) {}
void operator()() { void operator()() {
result_ = folly::makeTryWith(std::move(func_)); result_ = folly::makeTryWith(std::move(func_));
...@@ -393,6 +386,9 @@ struct FiberManager::AddTaskFinallyHelper { ...@@ -393,6 +386,9 @@ struct FiberManager::AddTaskFinallyHelper {
F func_; F func_;
folly::Optional<folly::Try<Result>>& result_; folly::Optional<folly::Try<Result>>& result_;
}; };
static constexpr bool allocateInBuffer =
sizeof(Func) + sizeof(Finally) <= Fiber::kUserBufferSize;
}; };
template <typename F, typename G> template <typename F, typename G>
...@@ -414,7 +410,10 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { ...@@ -414,7 +410,10 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) {
auto fiber = getFiber(); auto fiber = getFiber();
initLocalData(*fiber); initLocalData(*fiber);
typedef AddTaskFinallyHelper<F,G> Helper; typedef AddTaskFinallyHelper<
typename std::decay<F>::type,
typename std::decay<G>::type>
Helper;
if (Helper::allocateInBuffer) { if (Helper::allocateInBuffer) {
auto funcLoc = static_cast<typename Helper::Func*>( auto funcLoc = static_cast<typename Helper::Func*>(
...@@ -423,13 +422,14 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { ...@@ -423,13 +422,14 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) {
static_cast<void*>(funcLoc + 1)); static_cast<void*>(funcLoc + 1));
new (finallyLoc) typename Helper::Finally(std::forward<G>(finally), *this); new (finallyLoc) typename Helper::Finally(std::forward<G>(finally), *this);
new (funcLoc) typename Helper::Func(std::move(func), *finallyLoc); new (funcLoc) typename Helper::Func(std::forward<F>(func), *finallyLoc);
fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc)); fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc));
} else { } else {
auto finallyLoc = auto finallyLoc =
new typename Helper::Finally(std::forward<G>(finally), *this); new typename Helper::Finally(std::forward<G>(finally), *this);
auto funcLoc = new typename Helper::Func(std::move(func), *finallyLoc); auto funcLoc =
new typename Helper::Func(std::forward<F>(func), *finallyLoc);
fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc)); fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc));
} }
......
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