Skip to content

Commit 632e515

Browse files
committed
Implement allocator refactor for type support structs
Signed-off-by: methylDragon <[email protected]>
1 parent 2ab3722 commit 632e515

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

rmw/include/rmw/dynamic_message_type_support.h

+45-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,50 @@ extern "C"
3636
/// Interfaces for runtime interface reflection
3737

3838
// RUNTIME INTERFACE REFLECTION TYPE SUPPORT =======================================================
39-
/// Get dynamic type message typesupport with bound message description
39+
/// Initialize dynamic type message typesupport, binding message description
40+
/**
41+
* NOTE: Take note of the ownership rules for the returned struct and the `description` argument!
42+
*
43+
* If the user passes a NULL description, it is deferred instead, the middleware is responsibile
44+
* for populating the fields on type discovery!!!
45+
*
46+
* Ownership:
47+
* - The `rosidl_message_type_support_t *` returned from this function has different ownership
48+
* rules compared to the statically allocated `rosidl_message_type_support_t` structs from
49+
* code-generated types!
50+
* - The caller is responsible for deallocating the returned pointer
51+
*
52+
* <hr>
53+
* Attribute | Adherence
54+
* ------------------ | -------------
55+
* Allocates Memory | Yes
56+
* Thread-Safe | No
57+
* Uses Atomics | No
58+
* Lock-Free | Yes
59+
*/
60+
RMW_PUBLIC
61+
RMW_WARN_UNUSED
62+
rmw_ret_t
63+
rmw_dynamic_message_type_support_handle_init(
64+
rosidl_dynamic_typesupport_serialization_support_t * serialization_support,
65+
bool middleware_supports_type_discovery,
66+
const rosidl_type_hash_t * type_hash,
67+
const rosidl_runtime_c__type_description__TypeDescription * type_description,
68+
const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources,
69+
rcutils_allocator_t * allocator,
70+
rosidl_message_type_support_t * ts); // OUT
71+
72+
/// Finalize a rosidl_message_type_support_t obtained with
73+
/// `rmw_dynamic_message_type_support_handle_create()`, which has dynamically allocated members
74+
///
75+
/// NOTE: Using this on a statically allocated typesupport will cause undefined behavior!
76+
/// (Static memory will get freed in that case.)
77+
RMW_PUBLIC
78+
RMW_WARN_UNUSED
79+
rmw_ret_t
80+
rmw_dynamic_message_type_support_handle_fini(rosidl_message_type_support_t * ts);
81+
82+
/// Create dynamic type message typesupport with bound message description
4083
/**
4184
* NOTE: Take note of the ownership rules for the returned struct and the `description` argument!
4285
*
@@ -66,6 +109,7 @@ rmw_dynamic_message_type_support_handle_create(
66109
const rosidl_type_hash_t * type_hash,
67110
const rosidl_runtime_c__type_description__TypeDescription * type_description,
68111
const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources,
112+
rcutils_allocator_t * allocator,
69113
rosidl_message_type_support_t ** ts); // OUT
70114

71115
/// Destroy a rosidl_message_type_support_t obtained with

rmw/src/dynamic_message_type_support.c

+51-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,60 @@ extern "C"
3333

3434
// NOTE(methylDragon): How do we test this? It depends on specific serialization support. Do I just
3535
// use the FastRTPS support then?
36+
rmw_ret_t
37+
rmw_dynamic_message_type_support_handle_init(
38+
rosidl_dynamic_typesupport_serialization_support_t * serialization_support,
39+
bool middleware_supports_type_discovery,
40+
const rosidl_type_hash_t * type_hash,
41+
const rosidl_runtime_c__type_description__TypeDescription * type_description,
42+
const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources,
43+
rcutils_allocator_t * allocator,
44+
rosidl_message_type_support_t * ts)
45+
{
46+
if (!middleware_supports_type_discovery && type_description == NULL) {
47+
RMW_SET_ERROR_MSG(
48+
"Middleware does not support type discovery. Deferred dynamic type message type support will "
49+
"never be populated. You must provide a type description.");
50+
return RMW_RET_INVALID_ARGUMENT;
51+
}
52+
// TODO(methylDragon): Remove if and when the deferred description path is supported
53+
if (type_description == NULL) {
54+
RMW_SET_ERROR_MSG(
55+
"Deferred type description is not currently supported. You must provide a type description.");
56+
return RMW_RET_INVALID_ARGUMENT;
57+
}
58+
59+
RMW_CHECK_ARGUMENT_FOR_NULL(serialization_support, RMW_RET_INVALID_ARGUMENT);
60+
RMW_CHECK_ARGUMENT_FOR_NULL(type_hash, RMW_RET_INVALID_ARGUMENT);
61+
RMW_CHECK_ARGUMENT_FOR_NULL(type_description, RMW_RET_INVALID_ARGUMENT);
62+
RMW_CHECK_ARGUMENT_FOR_NULL(allocator, RMW_RET_INVALID_ARGUMENT);
63+
RMW_CHECK_ARGUMENT_FOR_NULL(ts, RMW_RET_INVALID_ARGUMENT);
64+
65+
// NOTE(methylDragon): Not supported for now
66+
// RMW_CHECK_ARGUMENT_FOR_NULL(type_description_sources, RMW_RET_INVALID_ARGUMENT);
67+
68+
return rmw_convert_rcutils_ret_to_rmw_ret(
69+
rosidl_dynamic_message_type_support_handle_init(
70+
serialization_support, type_hash, type_description, type_description_sources, allocator, ts));
71+
}
72+
73+
rmw_ret_t
74+
rmw_dynamic_message_type_support_handle_fini(rosidl_message_type_support_t * ts)
75+
{
76+
if (!ts) {
77+
return RCUTILS_RET_OK;
78+
}
79+
return rmw_convert_rcutils_ret_to_rmw_ret(rosidl_dynamic_message_type_support_handle_fini(ts));
80+
}
3681

37-
/// Create a rosidl_message_type_support_t from a TypeDescription message
3882
rmw_ret_t
3983
rmw_dynamic_message_type_support_handle_create(
4084
rosidl_dynamic_typesupport_serialization_support_t * serialization_support,
4185
bool middleware_supports_type_discovery,
4286
const rosidl_type_hash_t * type_hash,
4387
const rosidl_runtime_c__type_description__TypeDescription * type_description,
4488
const rosidl_runtime_c__type_description__TypeSource__Sequence * type_description_sources,
89+
rcutils_allocator_t * allocator,
4590
rosidl_message_type_support_t ** ts)
4691
{
4792
if (!middleware_supports_type_discovery && type_description == NULL) {
@@ -60,20 +105,23 @@ rmw_dynamic_message_type_support_handle_create(
60105
RMW_CHECK_ARGUMENT_FOR_NULL(serialization_support, RMW_RET_INVALID_ARGUMENT);
61106
RMW_CHECK_ARGUMENT_FOR_NULL(type_hash, RMW_RET_INVALID_ARGUMENT);
62107
RMW_CHECK_ARGUMENT_FOR_NULL(type_description, RMW_RET_INVALID_ARGUMENT);
108+
RMW_CHECK_ARGUMENT_FOR_NULL(allocator, RMW_RET_INVALID_ARGUMENT);
63109
RMW_CHECK_ARGUMENT_FOR_NULL(ts, RMW_RET_INVALID_ARGUMENT);
64110

65111
// NOTE(methylDragon): Not supported for now
66112
// RMW_CHECK_ARGUMENT_FOR_NULL(type_description_sources, RMW_RET_INVALID_ARGUMENT);
67113

68114
return rmw_convert_rcutils_ret_to_rmw_ret(
69115
rosidl_dynamic_message_type_support_handle_create(
70-
serialization_support, type_hash, type_description, type_description_sources, ts));
116+
serialization_support, type_hash, type_description, type_description_sources, allocator, ts));
71117
}
72118

73119
rmw_ret_t
74120
rmw_dynamic_message_type_support_handle_destroy(rosidl_message_type_support_t * ts)
75121
{
76-
RCUTILS_CHECK_ARGUMENT_FOR_NULL(ts, RMW_RET_INVALID_ARGUMENT);
122+
if (!ts) {
123+
return RCUTILS_RET_OK;
124+
}
77125
return rmw_convert_rcutils_ret_to_rmw_ret(rosidl_dynamic_message_type_support_handle_destroy(ts));
78126
}
79127

0 commit comments

Comments
 (0)