Skip to content

Commit

Permalink
adjust trivial_task storage size 1) to have a minimum size 2) to be a…
Browse files Browse the repository at this point in the history
… multiple of pointer size
  • Loading branch information
ademakov committed Sep 4, 2018
1 parent 85e93ed commit 47b28b3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions evenk/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ static constexpr std::size_t fptr_align = alignof(void (*)());

namespace detail {

static constexpr std::size_t align_size(std::size_t size)
{
static_assert((fptr_align & (fptr_align - 1)) == 0,
"function pointer alignment is not a power of two");
size = (size + fptr_align - 1) & ~(fptr_align - 1);
return std::max(size, fptr_size);
}

template <typename F>
auto task_invoke(F &&f) -> decltype(std::forward<F>(f)());

Expand Down Expand Up @@ -203,7 +211,7 @@ class trivial_task
public:
using result_type = R;

static constexpr std::size_t memory_size = S;
static constexpr std::size_t memory_size = detail::align_size(S);

constexpr trivial_task() noexcept = default;
constexpr trivial_task(std::nullptr_t) noexcept {}
Expand All @@ -220,6 +228,8 @@ class trivial_task
"a trivial_task target result type mismatch");
static_assert(std::is_trivially_copyable<target_type>::value,
"a trivial_task target is not trivially copyable");
static_assert(std::is_trivially_destructible<target_type>::value,
"a trivial_task target is not trivially destructible");
static_assert(sizeof(target_type) <= sizeof(memory_),
"a trivial_task target size limit is exceeded");

Expand Down Expand Up @@ -272,7 +282,7 @@ class trivial_task
};

template <typename R, std::size_t S = fptr_size, typename A = std::allocator<char>>
class task : private trivial_task<R, std::max(S, fptr_size)>
class task : private trivial_task<R, S>
{
using base = trivial_task<R, S>;

Expand Down

0 comments on commit 47b28b3

Please sign in to comment.