Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save allocator for RCL_CLOCK_UNINITIALIZED clock. #623

Merged
merged 5 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions rcl/src/rcl/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ rcl_get_system_time(void * data, rcl_time_point_value_t * current_time)

// Internal method for zeroing values on init, assumes clock is valid
static void
rcl_init_generic_clock(rcl_clock_t * clock)
rcl_init_generic_clock(rcl_clock_t * clock, rcl_allocator_t * allocator)
{
clock->type = RCL_CLOCK_UNINITIALIZED;
clock->jump_callbacks = NULL;
clock->num_jump_callbacks = 0u;
clock->get_now = NULL;
clock->data = NULL;
clock->allocator = *allocator;
}

// The function used to get the current ros time.
Expand Down Expand Up @@ -91,7 +92,7 @@ rcl_clock_init(
switch (clock_type) {
case RCL_CLOCK_UNINITIALIZED:
RCL_CHECK_ARGUMENT_FOR_NULL(clock, RCL_RET_INVALID_ARGUMENT);
rcl_init_generic_clock(clock);
rcl_init_generic_clock(clock, allocator);
return RCL_RET_OK;
case RCL_ROS_TIME:
return rcl_ros_clock_init(clock, allocator);
Expand Down Expand Up @@ -144,7 +145,7 @@ rcl_ros_clock_init(
{
RCL_CHECK_ARGUMENT_FOR_NULL(clock, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(allocator, RCL_RET_INVALID_ARGUMENT);
rcl_init_generic_clock(clock);
rcl_init_generic_clock(clock, allocator);
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
clock->data = allocator->allocate(sizeof(rcl_ros_clock_storage_t), allocator->state);
if (NULL == clock->data) {
RCL_SET_ERROR_MSG("allocating memory failed");
Expand Down Expand Up @@ -186,7 +187,7 @@ rcl_steady_clock_init(
{
RCL_CHECK_ARGUMENT_FOR_NULL(clock, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(allocator, RCL_RET_INVALID_ARGUMENT);
rcl_init_generic_clock(clock);
rcl_init_generic_clock(clock, allocator);
clock->get_now = rcl_get_steady_time;
clock->type = RCL_STEADY_TIME;
clock->allocator = *allocator;
Expand All @@ -213,7 +214,7 @@ rcl_system_clock_init(
{
RCL_CHECK_ARGUMENT_FOR_NULL(clock, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(allocator, RCL_RET_INVALID_ARGUMENT);
rcl_init_generic_clock(clock);
rcl_init_generic_clock(clock, allocator);
clock->get_now = rcl_get_system_time;
clock->type = RCL_SYSTEM_TIME;
clock->allocator = *allocator;
Expand Down
21 changes: 19 additions & 2 deletions rcl/test/rcl/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,22 @@ TEST(CLASSNAME(rcl_time, RMW_IMPLEMENTATION), specific_clock_instantiation) {
rcl_allocator_t allocator = rcl_get_default_allocator();
{
rcl_clock_t uninitialized_clock;
rcl_ret_t ret = rcl_clock_init(
RCL_CLOCK_UNINITIALIZED, &uninitialized_clock, &allocator);
rcl_ret_t ret = rcl_clock_init(RCL_CLOCK_UNINITIALIZED, &uninitialized_clock, &allocator);
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
EXPECT_EQ(uninitialized_clock.type, RCL_CLOCK_UNINITIALIZED) <<
"Expected time source of type RCL_CLOCK_UNINITIALIZED";
ret = rcl_clock_fini(&uninitialized_clock);
EXPECT_EQ(ret, RCL_RET_INVALID_ARGUMENT) << rcl_get_error_string().str;
rcl_reset_error();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok, but it still seems unrelated to the PR.
You can add a RCUTILS_CHECK_ALLOCATOR_IS_VALID check after initializing the clock, to test this modification.

Copy link
Contributor Author

@Blast545 Blast545 Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in ce8d867

EXPECT_EQ(
rcl_ros_clock_fini(&uninitialized_clock), RCL_RET_ERROR) << rcl_get_error_string().str;
rcl_reset_error();
EXPECT_EQ(
rcl_steady_clock_fini(&uninitialized_clock), RCL_RET_ERROR) << rcl_get_error_string().str;
rcl_reset_error();
EXPECT_EQ(
rcl_system_clock_fini(&uninitialized_clock), RCL_RET_ERROR) << rcl_get_error_string().str;
rcl_reset_error();
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
}
{
rcl_clock_t ros_clock;
Expand Down Expand Up @@ -296,6 +307,12 @@ TEST(CLASSNAME(rcl_time, RMW_IMPLEMENTATION), specific_clock_instantiation) {
ret = rcl_clock_fini(&steady_clock);
EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str;
}
{
rcl_clock_t fail_clock;
rcl_clock_type_t undefined_type = (rcl_clock_type_t) 130;
rcl_ret_t ret = rcl_clock_init(undefined_type, &fail_clock, &allocator);
EXPECT_EQ(ret, RCL_RET_INVALID_ARGUMENT) << rcl_get_error_string().str;
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
}
}

TEST(CLASSNAME(rcl_time, RMW_IMPLEMENTATION), rcl_time_difference) {
Expand Down