Skip to content

Commit 3d48555

Browse files
authored
Rmw preallocate (#428)
* Proposola of changes for RMW_Preallocate. Related /ros2/rmw#160 Signed-off-by: Gonzalo de Pedro <[email protected]> * Changed RCL interface Signed-off-by: Gonzalo de Pedro <[email protected]> * Updates for allocation in serialize methods. Signed-off-by: Michael Carroll <[email protected]> * Fix tests for new APIs. Signed-off-by: Michael Carroll <[email protected]>
1 parent 4262d4c commit 3d48555

File tree

10 files changed

+63
-29
lines changed

10 files changed

+63
-29
lines changed

rcl/include/rcl/publisher.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ rcl_publisher_init(
150150
const rcl_node_t * node,
151151
const rosidl_message_type_support_t * type_support,
152152
const char * topic_name,
153-
const rcl_publisher_options_t * options);
153+
const rcl_publisher_options_t * options
154+
);
154155

155156
/// Finalize a rcl_publisher_t.
156157
/**
@@ -244,6 +245,7 @@ rcl_publisher_get_default_options(void);
244245
*
245246
* \param[in] publisher handle to the publisher which will do the publishing
246247
* \param[in] ros_message type-erased pointer to the ROS message
248+
* \param[in] allocation structure pointer, used for memory preallocation (may be NULL)
247249
* \return `RCL_RET_OK` if the message was published successfully, or
248250
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
249251
* \return `RCL_RET_PUBLISHER_INVALID` if the publisher is invalid, or
@@ -252,7 +254,11 @@ rcl_publisher_get_default_options(void);
252254
RCL_PUBLIC
253255
RCL_WARN_UNUSED
254256
rcl_ret_t
255-
rcl_publish(const rcl_publisher_t * publisher, const void * ros_message);
257+
rcl_publish(
258+
const rcl_publisher_t * publisher,
259+
const void * ros_message,
260+
rmw_publisher_allocation_t * allocation
261+
);
256262

257263
/// Publish a serialized message on a topic using a publisher.
258264
/**
@@ -279,6 +285,7 @@ rcl_publish(const rcl_publisher_t * publisher, const void * ros_message);
279285
*
280286
* \param[in] publisher handle to the publisher which will do the publishing
281287
* \param[in] serialized_message pointer to the already serialized message in raw form
288+
* \param[in] allocation structure pointer, used for memory preallocation (may be NULL)
282289
* \return `RCL_RET_OK` if the message was published successfully, or
283290
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
284291
* \return `RCL_RET_PUBLISHER_INVALID` if the publisher is invalid, or
@@ -288,7 +295,10 @@ RCL_PUBLIC
288295
RCL_WARN_UNUSED
289296
rcl_ret_t
290297
rcl_publish_serialized_message(
291-
const rcl_publisher_t * publisher, const rcl_serialized_message_t * serialized_message);
298+
const rcl_publisher_t * publisher,
299+
const rcl_serialized_message_t * serialized_message,
300+
rmw_publisher_allocation_t * allocation
301+
);
292302

293303
/// Get the topic name for the publisher.
294304
/**

rcl/include/rcl/subscription.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ rcl_subscription_init(
153153
const rcl_node_t * node,
154154
const rosidl_message_type_support_t * type_support,
155155
const char * topic_name,
156-
const rcl_subscription_options_t * options);
156+
const rcl_subscription_options_t * options
157+
);
157158

158159
/// Finalize a rcl_subscription_t.
159160
/**
@@ -246,6 +247,7 @@ rcl_subscription_get_default_options(void);
246247
* \param[in] subscription the handle to the subscription from which to take
247248
* \param[inout] ros_message type-erased ptr to a allocated ROS message
248249
* \param[out] message_info rmw struct which contains meta-data for the message
250+
* \param[in] allocation structure pointer used for memory preallocation (may be NULL)
249251
* \return `RCL_RET_OK` if the message was published, or
250252
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
251253
* \return `RCL_RET_SUBSCRIPTION_INVALID` if the subscription is invalid, or
@@ -260,7 +262,9 @@ rcl_ret_t
260262
rcl_take(
261263
const rcl_subscription_t * subscription,
262264
void * ros_message,
263-
rmw_message_info_t * message_info);
265+
rmw_message_info_t * message_info,
266+
rmw_subscription_allocation_t * allocation
267+
);
264268

265269
/// Take a serialized raw message from a topic using a rcl subscription.
266270
/**
@@ -289,6 +293,7 @@ rcl_take(
289293
* \param[in] subscription the handle to the subscription from which to take
290294
* \param[inout] serialized_message pointer to a (pre-allocated) serialized message.
291295
* \param[out] message_info rmw struct which contains meta-data for the message
296+
* \param[in] allocation structure pointer used for memory preallocation (may be NULL)
292297
* \return `RCL_RET_OK` if the message was published, or
293298
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
294299
* \return `RCL_RET_SUBSCRIPTION_INVALID` if the subscription is invalid, or
@@ -303,7 +308,8 @@ rcl_ret_t
303308
rcl_take_serialized_message(
304309
const rcl_subscription_t * subscription,
305310
rcl_serialized_message_t * serialized_message,
306-
rmw_message_info_t * message_info);
311+
rmw_message_info_t * message_info,
312+
rmw_subscription_allocation_t * allocation);
307313

308314
/// Get the topic name for the subscription.
309315
/**

rcl/src/rcl/logging_rosout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void rcl_logging_rosout_output_handler(
260260
rosidl_generator_c__String__assign(&log_message->msg, msg_array.buffer);
261261
rosidl_generator_c__String__assign(&log_message->file, location->file_name);
262262
rosidl_generator_c__String__assign(&log_message->function, location->function_name);
263-
status = rcl_publish(&entry.publisher, log_message);
263+
status = rcl_publish(&entry.publisher, log_message, NULL);
264264
if (RCL_RET_OK != status) {
265265
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to publish log message to rosout: ");
266266
RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);

rcl/src/rcl/publisher.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ rcl_publisher_init(
5353
const rcl_node_t * node,
5454
const rosidl_message_type_support_t * type_support,
5555
const char * topic_name,
56-
const rcl_publisher_options_t * options)
56+
const rcl_publisher_options_t * options
57+
)
5758
{
5859
rcl_ret_t fail_ret = RCL_RET_ERROR;
5960

@@ -74,6 +75,8 @@ rcl_publisher_init(
7475
RCL_CHECK_ARGUMENT_FOR_NULL(topic_name, RCL_RET_INVALID_ARGUMENT);
7576
RCUTILS_LOG_DEBUG_NAMED(
7677
ROS_PACKAGE_NAME, "Initializing publisher for topic name '%s'", topic_name);
78+
79+
7780
// Expand the given topic name.
7881
rcutils_allocator_t rcutils_allocator = *allocator; // implicit conversion to rcutils version
7982
rcutils_string_map_t substitutions_map = rcutils_get_zero_initialized_string_map();
@@ -162,6 +165,7 @@ rcl_publisher_init(
162165
sizeof(rcl_publisher_impl_t), allocator->state);
163166
RCL_CHECK_FOR_NULL_WITH_MSG(
164167
publisher->impl, "allocating memory failed", ret = RCL_RET_BAD_ALLOC; goto cleanup);
168+
165169
// Fill out implementation struct.
166170
// rmw handle (create rmw publisher)
167171
// TODO(wjwwood): pass along the allocator to rmw when it supports it
@@ -188,11 +192,13 @@ rcl_publisher_init(
188192
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Publisher initialized");
189193
// context
190194
publisher->impl->context = node->context;
195+
191196
goto cleanup;
192197
fail:
193198
if (publisher->impl) {
194199
allocator->deallocate(publisher->impl, allocator->state);
195200
}
201+
196202
ret = fail_ret;
197203
// Fall through to cleanup
198204
cleanup:
@@ -245,13 +251,16 @@ rcl_publisher_get_default_options()
245251
}
246252

247253
rcl_ret_t
248-
rcl_publish(const rcl_publisher_t * publisher, const void * ros_message)
254+
rcl_publish(
255+
const rcl_publisher_t * publisher,
256+
const void * ros_message,
257+
rmw_publisher_allocation_t * allocation)
249258
{
250259
if (!rcl_publisher_is_valid(publisher)) {
251260
return RCL_RET_PUBLISHER_INVALID; // error already set
252261
}
253262
RCL_CHECK_ARGUMENT_FOR_NULL(ros_message, RCL_RET_INVALID_ARGUMENT);
254-
if (rmw_publish(publisher->impl->rmw_handle, ros_message) != RMW_RET_OK) {
263+
if (rmw_publish(publisher->impl->rmw_handle, ros_message, allocation) != RMW_RET_OK) {
255264
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
256265
return RCL_RET_ERROR;
257266
}
@@ -260,13 +269,16 @@ rcl_publish(const rcl_publisher_t * publisher, const void * ros_message)
260269

261270
rcl_ret_t
262271
rcl_publish_serialized_message(
263-
const rcl_publisher_t * publisher, const rcl_serialized_message_t * serialized_message)
272+
const rcl_publisher_t * publisher,
273+
const rcl_serialized_message_t * serialized_message,
274+
rmw_publisher_allocation_t * allocation)
264275
{
265276
if (!rcl_publisher_is_valid(publisher)) {
266277
return RCL_RET_PUBLISHER_INVALID; // error already set
267278
}
268279
RCL_CHECK_ARGUMENT_FOR_NULL(serialized_message, RCL_RET_INVALID_ARGUMENT);
269-
rmw_ret_t ret = rmw_publish_serialized_message(publisher->impl->rmw_handle, serialized_message);
280+
rmw_ret_t ret = rmw_publish_serialized_message(publisher->impl->rmw_handle, serialized_message,
281+
allocation);
270282
if (ret != RMW_RET_OK) {
271283
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
272284
if (ret == RMW_RET_BAD_ALLOC) {

rcl/src/rcl/subscription.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ rcl_subscription_init(
4949
const rcl_node_t * node,
5050
const rosidl_message_type_support_t * type_support,
5151
const char * topic_name,
52-
const rcl_subscription_options_t * options)
52+
const rcl_subscription_options_t * options
53+
)
5354
{
5455
rcl_ret_t fail_ret = RCL_RET_ERROR;
5556

@@ -235,7 +236,9 @@ rcl_ret_t
235236
rcl_take(
236237
const rcl_subscription_t * subscription,
237238
void * ros_message,
238-
rmw_message_info_t * message_info)
239+
rmw_message_info_t * message_info,
240+
rmw_subscription_allocation_t * allocation
241+
)
239242
{
240243
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Subscription taking message");
241244
if (!rcl_subscription_is_valid(subscription)) {
@@ -249,7 +252,8 @@ rcl_take(
249252
// Call rmw_take_with_info.
250253
bool taken = false;
251254
rmw_ret_t ret =
252-
rmw_take_with_info(subscription->impl->rmw_handle, ros_message, &taken, message_info_local);
255+
rmw_take_with_info(subscription->impl->rmw_handle, ros_message, &taken,
256+
message_info_local, allocation);
253257
if (ret != RMW_RET_OK) {
254258
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
255259
if (RMW_RET_BAD_ALLOC == ret) {
@@ -269,7 +273,9 @@ rcl_ret_t
269273
rcl_take_serialized_message(
270274
const rcl_subscription_t * subscription,
271275
rcl_serialized_message_t * serialized_message,
272-
rmw_message_info_t * message_info)
276+
rmw_message_info_t * message_info,
277+
rmw_subscription_allocation_t * allocation
278+
)
273279
{
274280
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Subscription taking serialized message");
275281
if (!rcl_subscription_is_valid(subscription)) {
@@ -282,7 +288,7 @@ rcl_take_serialized_message(
282288
// Call rmw_take_with_info.
283289
bool taken = false;
284290
rmw_ret_t ret = rmw_take_serialized_message_with_info(
285-
subscription->impl->rmw_handle, serialized_message, &taken, message_info_local);
291+
subscription->impl->rmw_handle, serialized_message, &taken, message_info_local, allocation);
286292
if (ret != RMW_RET_OK) {
287293
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
288294
if (RMW_RET_BAD_ALLOC == ret) {

rcl/test/rcl/test_publisher.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ TEST_F(CLASSNAME(TestPublisherFixture, RMW_IMPLEMENTATION), test_publisher_nomin
9393
test_msgs__msg__BasicTypes msg;
9494
test_msgs__msg__BasicTypes__init(&msg);
9595
msg.int64_value = 42;
96-
ret = rcl_publish(&publisher, &msg);
96+
ret = rcl_publish(&publisher, &msg, nullptr);
9797
test_msgs__msg__BasicTypes__fini(&msg);
9898
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
9999
}
@@ -116,7 +116,7 @@ TEST_F(CLASSNAME(TestPublisherFixture, RMW_IMPLEMENTATION), test_publisher_nomin
116116
test_msgs__msg__Strings msg;
117117
test_msgs__msg__Strings__init(&msg);
118118
ASSERT_TRUE(rosidl_generator_c__String__assign(&msg.string_value, "testing"));
119-
ret = rcl_publish(&publisher, &msg);
119+
ret = rcl_publish(&publisher, &msg, nullptr);
120120
test_msgs__msg__Strings__fini(&msg);
121121
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
122122
}
@@ -160,14 +160,14 @@ TEST_F(CLASSNAME(TestPublisherFixture, RMW_IMPLEMENTATION), test_publishers_diff
160160
test_msgs__msg__BasicTypes msg_int;
161161
test_msgs__msg__BasicTypes__init(&msg_int);
162162
msg_int.int64_value = 42;
163-
ret = rcl_publish(&publisher, &msg_int);
163+
ret = rcl_publish(&publisher, &msg_int, nullptr);
164164
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
165165
test_msgs__msg__BasicTypes__fini(&msg_int);
166166

167167
test_msgs__msg__Strings msg_string;
168168
test_msgs__msg__Strings__init(&msg_string);
169169
ASSERT_TRUE(rosidl_generator_c__String__assign(&msg_string.string_value, "testing"));
170-
ret = rcl_publish(&publisher_in_namespace, &msg_string);
170+
ret = rcl_publish(&publisher_in_namespace, &msg_string, nullptr);
171171
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
172172
}
173173

rcl/test/rcl/test_subscription.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
163163
test_msgs__msg__BasicTypes msg;
164164
test_msgs__msg__BasicTypes__init(&msg);
165165
msg.int64_value = 42;
166-
ret = rcl_publish(&publisher, &msg);
166+
ret = rcl_publish(&publisher, &msg, nullptr);
167167
test_msgs__msg__BasicTypes__fini(&msg);
168168
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
169169
}
@@ -176,7 +176,7 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
176176
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
177177
test_msgs__msg__BasicTypes__fini(&msg);
178178
});
179-
ret = rcl_take(&subscription, &msg, nullptr);
179+
ret = rcl_take(&subscription, &msg, nullptr, nullptr);
180180
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
181181
ASSERT_EQ(42, msg.int64_value);
182182
}
@@ -214,7 +214,7 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
214214
test_msgs__msg__Strings msg;
215215
test_msgs__msg__Strings__init(&msg);
216216
ASSERT_TRUE(rosidl_generator_c__String__assign(&msg.string_value, test_string));
217-
ret = rcl_publish(&publisher, &msg);
217+
ret = rcl_publish(&publisher, &msg, nullptr);
218218
test_msgs__msg__Strings__fini(&msg);
219219
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
220220
}
@@ -227,7 +227,7 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
227227
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
228228
test_msgs__msg__Strings__fini(&msg);
229229
});
230-
ret = rcl_take(&subscription, &msg, nullptr);
230+
ret = rcl_take(&subscription, &msg, nullptr, nullptr);
231231
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
232232
ASSERT_EQ(std::string(test_string), std::string(msg.string_value.data, msg.string_value.size));
233233
}

rcl_action/src/rcl_action/action_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ rcl_action_take_cancel_response(
390390
RCL_CHECK_ARGUMENT_FOR_NULL(ros_ ## Type, RCL_RET_INVALID_ARGUMENT); \
391391
rmw_message_info_t message_info; /* ignored */ \
392392
rcl_ret_t ret = rcl_take( \
393-
&action_client->impl->Type ## _subscription, ros_ ## Type, &message_info); \
393+
&action_client->impl->Type ## _subscription, ros_ ## Type, &message_info, NULL); \
394394
if (RCL_RET_OK != ret) { \
395395
if (RCL_RET_SUBSCRIPTION_TAKE_FAILED == ret) { \
396396
return RCL_RET_ACTION_CLIENT_TAKE_FAILED; \

rcl_action/src/rcl_action/action_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ rcl_action_publish_feedback(
491491
}
492492
RCL_CHECK_ARGUMENT_FOR_NULL(ros_feedback, RCL_RET_INVALID_ARGUMENT);
493493

494-
rcl_ret_t ret = rcl_publish(&action_server->impl->feedback_publisher, ros_feedback);
494+
rcl_ret_t ret = rcl_publish(&action_server->impl->feedback_publisher, ros_feedback, NULL);
495495
if (RCL_RET_OK != ret) {
496496
return RCL_RET_ERROR; // error already set
497497
}
@@ -559,7 +559,7 @@ rcl_action_publish_status(
559559
}
560560
RCL_CHECK_ARGUMENT_FOR_NULL(status_message, RCL_RET_INVALID_ARGUMENT);
561561

562-
rcl_ret_t ret = rcl_publish(&action_server->impl->status_publisher, status_message);
562+
rcl_ret_t ret = rcl_publish(&action_server->impl->status_publisher, status_message, NULL);
563563
if (RCL_RET_OK != ret) {
564564
return RCL_RET_ERROR; // error already set
565565
}

rcl_lifecycle/src/com_interface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ rcl_lifecycle_com_interface_publish_notification(
255255
msg.goal_state.id = goal->id;
256256
rosidl_generator_c__String__assign(&msg.goal_state.label, goal->label);
257257

258-
return rcl_publish(&com_interface->pub_transition_event, &msg);
258+
return rcl_publish(&com_interface->pub_transition_event, &msg, NULL);
259259
}
260260

261261
#ifdef __cplusplus

0 commit comments

Comments
 (0)