Skip to content

Commit

Permalink
Fully qualified node name is computed once and stored.
Browse files Browse the repository at this point in the history
  • Loading branch information
rarvolt authored and tfoote committed Feb 2, 2019
1 parent 586ae66 commit 114bc52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion rcl/include/rcl/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ rcl_node_get_namespace(const rcl_node_t * node);
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
Expand Down
27 changes: 10 additions & 17 deletions rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct rcl_node_impl_t
rmw_node_t * rmw_node_handle;
rcl_guard_condition_t * graph_guard_condition;
const char * logger_name;
const char * fq_name;
} rcl_node_impl_t;


Expand Down Expand Up @@ -284,6 +285,7 @@ rcl_node_init(
node->impl->rmw_node_handle = NULL;
node->impl->graph_guard_condition = NULL;
node->impl->logger_name = NULL;
node->impl->fq_name = NULL;
node->impl->options = rcl_node_get_default_options();
node->context = context;
// Initialize node impl.
Expand Down Expand Up @@ -319,6 +321,9 @@ rcl_node_init(
local_namespace_ = remapped_namespace;
}

// compute fully qualified name of the node
node->impl->fq_name = rcutils_format_string(*allocator, "%s/%s", local_namespace_, name);

// node logger name
node->impl->logger_name = rcl_create_node_logger_name(name, local_namespace_, allocator);
RCL_CHECK_FOR_NULL_WITH_MSG(
Expand Down Expand Up @@ -440,6 +445,9 @@ rcl_node_init(
ROS_PACKAGE_NAME, "Failed to fini publisher for node: %i", ret);
allocator->deallocate((char *)node->impl->logger_name, allocator->state);
}
if (node->impl->fq_name) {
allocator->deallocate((char *)node->impl->fq_name, allocator->state);
}
if (node->impl->rmw_node_handle) {
ret = rmw_destroy_node(node->impl->rmw_node_handle);
if (ret != RMW_RET_OK) {
Expand Down Expand Up @@ -514,6 +522,7 @@ rcl_node_fini(rcl_node_t * node)
allocator.deallocate(node->impl->graph_guard_condition, allocator.state);
// assuming that allocate and deallocate are ok since they are checked in init
allocator.deallocate((char *)node->impl->logger_name, allocator.state);
allocator.deallocate((char *)node->impl->fq_name, allocator.state);
if (NULL != node->impl->options.arguments.impl) {
rcl_ret_t ret = rcl_arguments_fini(&(node->impl->options.arguments));
if (ret != RCL_RET_OK) {
Expand Down Expand Up @@ -609,23 +618,7 @@ rcl_node_get_fully_qualified_name(const rcl_node_t * node)
if (!rcl_node_is_valid_except_context(node)) {
return NULL; // error already set
}

const char * name = rcl_node_get_name(node);
const char * ns = rcl_node_get_namespace(node);
char * fq_name = NULL;

if ('/' == ns[strlen(ns) - 1]) {
fq_name = (char *)calloc(strlen(ns) + strlen(name), sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, name);
}
else {
fq_name = (char *)calloc(strlen(ns) + strlen(name) + 1, sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, "/");
strcat(fq_name, name);
}
return fq_name;
return node->impl->fq_name;
}

const rcl_node_options_t *
Expand Down
6 changes: 3 additions & 3 deletions rcl/test/rcl/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_STREQ(fq_name, actual_fq_node_name);
free((char *)actual_fq_node_name);
}
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
EXPECT_NO_MEMORY_OPERATIONS({
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
});
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_EQ(std::string(fq_name), std::string(actual_fq_node_name));
free((char *)actual_fq_node_name);
}
// Test rcl_node_get_logger_name().
const char * actual_node_logger_name;
Expand Down

0 comments on commit 114bc52

Please sign in to comment.