Skip to content

Commit

Permalink
Fix convert nullptr to pointer of lambda type
Browse files Browse the repository at this point in the history
Signed-off-by: owentou <[email protected]>
  • Loading branch information
owent committed Oct 26, 2021
1 parent 5894bf9 commit 2f5350b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 44 deletions.
7 changes: 4 additions & 3 deletions include/libcopp/coroutine/coroutine_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ class coroutine_context : public coroutine_context_base {
* @param private_buffer_size size of private buffer
* @return COPP_EC_SUCCESS or error code
*/
static LIBCOPP_COPP_API int create(coroutine_context *p, callback_t &runner, const stack_context &callee_stack,
static LIBCOPP_COPP_API int create(coroutine_context *p, callback_t &&runner, const stack_context &callee_stack,
size_t coroutine_size, size_t private_buffer_size) LIBCOPP_MACRO_NOEXCEPT;

template <typename TRunner>
static LIBCOPP_COPP_API_HEAD_ONLY int create(coroutine_context *p, TRunner *runner, const stack_context &callee_stack,
size_t coroutine_size,
size_t private_buffer_size) LIBCOPP_MACRO_NOEXCEPT {
return create(p, std::bind(&TRunner::operator(), runner, std::placeholders::_1), callee_stack, coroutine_size,
private_buffer_size);
return create(
p, [runner](void *private_data) { return (*runner)(private_data); }, callee_stack, coroutine_size,
private_buffer_size);
}

/**
Expand Down
14 changes: 6 additions & 8 deletions include/libcopp/coroutine/coroutine_context_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class coroutine_context_container : public coroutine_context {
}

// after this call runner will be unavailable
callback_t callback(std::move(runner));
if (coroutine_context::create(ret.get(), callback, ret->callee_stack_, coroutine_size, private_buffer_size) < 0) {
if (coroutine_context::create(ret.get(), std::move(runner), ret->callee_stack_, coroutine_size,
private_buffer_size) < 0) {
ret.reset();
}

Expand All @@ -114,9 +114,8 @@ class coroutine_context_container : public coroutine_context {
return create(callback_t(), alloc, stack_size, private_buffer_size, coroutine_size);
}

using runner_fn_t = int (TRunner::*)(void *);
runner_fn_t fn = &TRunner::operator();
return create(std::bind(fn, runner, std::placeholders::_1), alloc, stack_size, private_buffer_size, coroutine_size);
return create([runner](void *private_data) { return (*runner)(private_data); }, alloc, stack_size,
private_buffer_size, coroutine_size);
}

static inline ptr_t create(int (*fn)(void *), allocator_type &alloc, size_t stack_size = 0,
Expand All @@ -137,9 +136,8 @@ class coroutine_context_container : public coroutine_context {
template <class TRunner>
static inline ptr_t create(TRunner *runner, size_t stack_size = 0, size_t private_buffer_size = 0,
size_t coroutine_size = 0) LIBCOPP_MACRO_NOEXCEPT {
using runner_fn_t = int (TRunner::*)(void *);
runner_fn_t fn = &TRunner::operator();
return create(std::bind(fn, runner, std::placeholders::_1), stack_size, private_buffer_size, coroutine_size);
return create([runner](void *private_data) { return (*runner)(private_data); }, stack_size, private_buffer_size,
coroutine_size);
}

static inline ptr_t create(int (*fn)(void *), size_t stack_size = 0, size_t private_buffer_size = 0,
Expand Down
7 changes: 4 additions & 3 deletions include/libcopp/coroutine/coroutine_context_fiber.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class coroutine_context_fiber : public coroutine_context_base {
* @param private_buffer_size size of private buffer
* @return COPP_EC_SUCCESS or error code
*/
static LIBCOPP_COPP_API int create(coroutine_context_fiber *p, callback_t &runner, const stack_context &callee_stack,
static LIBCOPP_COPP_API int create(coroutine_context_fiber *p, callback_t &&runner, const stack_context &callee_stack,
size_t coroutine_size, size_t private_buffer_size,
size_t stack_reserve_size_of_fiber = 0) LIBCOPP_MACRO_NOEXCEPT;

Expand All @@ -85,8 +85,9 @@ class coroutine_context_fiber : public coroutine_context_base {
const stack_context &callee_stack, size_t coroutine_size,
size_t private_buffer_size,
size_t stack_reserve_size_of_fiber = 0) LIBCOPP_MACRO_NOEXCEPT {
return create(p, std::bind(&TRunner::operator(), runner, std::placeholders::_1), callee_stack, coroutine_size,
private_buffer_size, stack_reserve_size_of_fiber);
return create(
p, [runner](void *private_data) { return (*runner)(private_data); }, callee_stack, coroutine_size,
private_buffer_size, stack_reserve_size_of_fiber);
}

/**
Expand Down
15 changes: 6 additions & 9 deletions include/libcopp/coroutine/coroutine_context_fiber_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ class coroutine_context_fiber_container : public coroutine_context_fiber {

// after this call runner will be unavailable
// stack_sz is used for stack reserve size of fiber
callback_t callback(std::move(runner));
if (coroutine_context_fiber::create(ret.get(), callback, ret->callee_stack_, coroutine_size, private_buffer_size,
stack_sz) < 0) {
if (coroutine_context_fiber::create(ret.get(), std::move(runner), ret->callee_stack_, coroutine_size,
private_buffer_size, stack_sz) < 0) {
ret.reset();
}

Expand All @@ -115,9 +114,8 @@ class coroutine_context_fiber_container : public coroutine_context_fiber {
return create(callback_t(), alloc, stack_size, private_buffer_size, coroutine_size);
}

using runner_fn_t = int (TRunner::*)(void *);
runner_fn_t fn = &TRunner::operator();
return create(std::bind(fn, runner, std::placeholders::_1), alloc, stack_size, private_buffer_size, coroutine_size);
return create([runner](void *private_data) { return (*runner)(private_data); }, alloc, stack_size,
private_buffer_size, coroutine_size);
}

static inline ptr_t create(int (*fn)(void *), allocator_type &alloc, size_t stack_size = 0,
Expand All @@ -138,9 +136,8 @@ class coroutine_context_fiber_container : public coroutine_context_fiber {
template <class TRunner>
static inline ptr_t create(TRunner *runner, size_t stack_size = 0, size_t private_buffer_size = 0,
size_t coroutine_size = 0) LIBCOPP_MACRO_NOEXCEPT {
using runner_fn_t = int (TRunner::*)(void *);
runner_fn_t fn = &TRunner::operator();
return create(std::bind(fn, runner, std::placeholders::_1), stack_size, private_buffer_size, coroutine_size);
return create([runner](void *private_data) { return (*runner)(private_data); }, stack_size, private_buffer_size,
coroutine_size);
}

static inline ptr_t create(int (*fn)(void *), size_t stack_size = 0, size_t private_buffer_size = 0,
Expand Down
7 changes: 2 additions & 5 deletions include/libcotask/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class LIBCOPP_COTASK_API_HEAD_ONLY task : public impl::task_impl {
}

typename coroutine_t::ptr_t coroutine =
coroutine_t::create(reinterpret_cast<a_t *>(nullptr), alloc, stack_size,
coroutine_t::create(coroutine_t::callback_t(), alloc, stack_size,
sizeof(impl::task_impl *) + private_buffer_size, action_size + task_size);
if (!coroutine) {
return ptr_t();
Expand All @@ -122,11 +122,8 @@ class LIBCOPP_COTASK_API_HEAD_ONLY task : public impl::task_impl {
return ret;
}

using a_t_fn_t = int (a_t::*)(void *);
a_t_fn_t a_t_fn = &a_t::operator();

// redirect runner
coroutine->set_runner(std::move(std::bind(a_t_fn, action, std::placeholders::_1)));
coroutine->set_runner([action](void *private_data) { return (*action)(private_data); });

ret->action_destroy_fn_ = get_placement_destroy(action);
ret->_set_action(action);
Expand Down
2 changes: 1 addition & 1 deletion src/libcopp/coroutine/coroutine_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ LIBCOPP_COPP_API coroutine_context::coroutine_context() LIBCOPP_MACRO_NOEXCEPT :

LIBCOPP_COPP_API coroutine_context::~coroutine_context() {}

LIBCOPP_COPP_API int coroutine_context::create(coroutine_context *p, callback_t &runner,
LIBCOPP_COPP_API int coroutine_context::create(coroutine_context *p, callback_t &&runner,
const stack_context &callee_stack, size_t coroutine_size,
size_t private_buffer_size) LIBCOPP_MACRO_NOEXCEPT {
if (nullptr == p) {
Expand Down
2 changes: 1 addition & 1 deletion src/libcopp/coroutine/coroutine_context_fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ LIBCOPP_COPP_API coroutine_context_fiber::~coroutine_context_fiber() {
}
}

LIBCOPP_COPP_API int coroutine_context_fiber::create(coroutine_context_fiber *p, callback_t &runner,
LIBCOPP_COPP_API int coroutine_context_fiber::create(coroutine_context_fiber *p, callback_t &&runner,
const stack_context &callee_stack, size_t coroutine_size,
size_t private_buffer_size,
size_t stack_reserve_size_of_fiber) LIBCOPP_MACRO_NOEXCEPT {
Expand Down
20 changes: 7 additions & 13 deletions test/case/coroutine_context_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,65 +156,59 @@ CASE_TEST(coroutine, coroutine_context_create_failed) {
copp::coroutine_context *placement_new_addr = reinterpret_cast<copp::coroutine_context *>(stack_buff + 112 * 1024);
test_context_base_foo_runner runner;
{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
callee_stack.size = 120 * 1024;

CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(nullptr, callback, callee_stack, 4096, 4096));
copp::coroutine_context::create(nullptr, std::move(runner), callee_stack, 4096, 4096));
}

{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
callee_stack.size = 120 * 1024;

CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4095));
copp::coroutine_context::create(placement_new_addr, std::move(runner), callee_stack, 4096, 4095));
}

{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
callee_stack.size = 120 * 1024;

CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4095, 4096));
copp::coroutine_context::create(placement_new_addr, std::move(runner), callee_stack, 4095, 4096));
}

{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = nullptr;
callee_stack.size = 120 * 1024;

CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4096));
copp::coroutine_context::create(placement_new_addr, std::move(runner), callee_stack, 4096, 4096));
}

{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
callee_stack.size = 8192;

CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4096));
copp::coroutine_context::create(placement_new_addr, std::move(runner), callee_stack, 4096, 4096));
}

{
copp::coroutine_context::callback_t callback(std::move(runner));
copp::stack_context callee_stack;
callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
callee_stack.size = 120 * 1024;

copp::coroutine_context *placement_invalid_addr =
reinterpret_cast<copp::coroutine_context *>(stack_buff + 116 * 1024);
CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR,
copp::coroutine_context::create(placement_invalid_addr, callback, callee_stack, 4096, 4096));
CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(placement_invalid_addr, std::move(runner),
callee_stack, 4096, 4096));
}

delete[] stack_buff;
Expand Down
3 changes: 2 additions & 1 deletion test/case/coroutine_context_this_coroutine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ CASE_TEST(this_context, get_coroutine) {
for (int i = 0; i < 5; ++i) {
co_arr[i] = co_type::create(&runners[i], 128 * 1024);

th_pool.push_back(std::thread(std::bind(test_this_context_thread_func, co_arr[i], &runners[i])));
th_pool.push_back(
std::thread([&co_arr, &runners, i]() { test_this_context_thread_func(co_arr[i], &runners[i]); }));
}

for (std::thread &th : th_pool) {
Expand Down

0 comments on commit 2f5350b

Please sign in to comment.