Skip to content

Commit 114bc52

Browse files
rarvolttfoote
authored andcommitted
Fully qualified node name is computed once and stored.
1 parent 586ae66 commit 114bc52

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

rcl/include/rcl/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ rcl_node_get_namespace(const rcl_node_t * node);
362362
* <hr>
363363
* Attribute | Adherence
364364
* ------------------ | -------------
365-
* Allocates Memory | Yes
365+
* Allocates Memory | No
366366
* Thread-Safe | No
367367
* Uses Atomics | No
368368
* Lock-Free | Yes

rcl/src/rcl/node.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct rcl_node_impl_t
5959
rmw_node_t * rmw_node_handle;
6060
rcl_guard_condition_t * graph_guard_condition;
6161
const char * logger_name;
62+
const char * fq_name;
6263
} rcl_node_impl_t;
6364

6465

@@ -284,6 +285,7 @@ rcl_node_init(
284285
node->impl->rmw_node_handle = NULL;
285286
node->impl->graph_guard_condition = NULL;
286287
node->impl->logger_name = NULL;
288+
node->impl->fq_name = NULL;
287289
node->impl->options = rcl_node_get_default_options();
288290
node->context = context;
289291
// Initialize node impl.
@@ -319,6 +321,9 @@ rcl_node_init(
319321
local_namespace_ = remapped_namespace;
320322
}
321323

324+
// compute fully qualified name of the node
325+
node->impl->fq_name = rcutils_format_string(*allocator, "%s/%s", local_namespace_, name);
326+
322327
// node logger name
323328
node->impl->logger_name = rcl_create_node_logger_name(name, local_namespace_, allocator);
324329
RCL_CHECK_FOR_NULL_WITH_MSG(
@@ -440,6 +445,9 @@ rcl_node_init(
440445
ROS_PACKAGE_NAME, "Failed to fini publisher for node: %i", ret);
441446
allocator->deallocate((char *)node->impl->logger_name, allocator->state);
442447
}
448+
if (node->impl->fq_name) {
449+
allocator->deallocate((char *)node->impl->fq_name, allocator->state);
450+
}
443451
if (node->impl->rmw_node_handle) {
444452
ret = rmw_destroy_node(node->impl->rmw_node_handle);
445453
if (ret != RMW_RET_OK) {
@@ -514,6 +522,7 @@ rcl_node_fini(rcl_node_t * node)
514522
allocator.deallocate(node->impl->graph_guard_condition, allocator.state);
515523
// assuming that allocate and deallocate are ok since they are checked in init
516524
allocator.deallocate((char *)node->impl->logger_name, allocator.state);
525+
allocator.deallocate((char *)node->impl->fq_name, allocator.state);
517526
if (NULL != node->impl->options.arguments.impl) {
518527
rcl_ret_t ret = rcl_arguments_fini(&(node->impl->options.arguments));
519528
if (ret != RCL_RET_OK) {
@@ -609,23 +618,7 @@ rcl_node_get_fully_qualified_name(const rcl_node_t * node)
609618
if (!rcl_node_is_valid_except_context(node)) {
610619
return NULL; // error already set
611620
}
612-
613-
const char * name = rcl_node_get_name(node);
614-
const char * ns = rcl_node_get_namespace(node);
615-
char * fq_name = NULL;
616-
617-
if ('/' == ns[strlen(ns) - 1]) {
618-
fq_name = (char *)calloc(strlen(ns) + strlen(name), sizeof(char));
619-
strcpy(fq_name, ns);
620-
strcat(fq_name, name);
621-
}
622-
else {
623-
fq_name = (char *)calloc(strlen(ns) + strlen(name) + 1, sizeof(char));
624-
strcpy(fq_name, ns);
625-
strcat(fq_name, "/");
626-
strcat(fq_name, name);
627-
}
628-
return fq_name;
621+
return node->impl->fq_name;
629622
}
630623

631624
const rcl_node_options_t *

rcl/test/rcl/test_node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
218218
EXPECT_TRUE(actual_fq_node_name ? true : false);
219219
if (actual_fq_node_name) {
220220
EXPECT_STREQ(fq_name, actual_fq_node_name);
221-
free((char *)actual_fq_node_name);
222221
}
223222
rcl_reset_error();
224-
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
223+
EXPECT_NO_MEMORY_OPERATIONS({
224+
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
225+
});
225226
EXPECT_TRUE(actual_fq_node_name ? true : false);
226227
if (actual_fq_node_name) {
227228
EXPECT_EQ(std::string(fq_name), std::string(actual_fq_node_name));
228-
free((char *)actual_fq_node_name);
229229
}
230230
// Test rcl_node_get_logger_name().
231231
const char * actual_node_logger_name;

0 commit comments

Comments
 (0)