Skip to content

Commit 7583b95

Browse files
authored
Dynamic Subscription (BONUS: Allocators): rosidl (#737)
* Add zero init for message type support handle Signed-off-by: methylDragon <[email protected]> * Fix memory leaks in type description utils Signed-off-by: methylDragon <[email protected]> * Refactor serialization support to use allocators and refs Signed-off-by: methylDragon <[email protected]> * Check if allocator is valid Signed-off-by: methylDragon <[email protected]> --------- Signed-off-by: methylDragon <[email protected]>
1 parent 6da8660 commit 7583b95

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

rosidl_runtime_c/include/rosidl_runtime_c/message_type_support_struct.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ struct rosidl_message_type_support_t
5757
rosidl_message_get_type_description_sources_function get_type_description_sources_func;
5858
};
5959

60+
/// Return a rosidl_message_type_support_t struct with members set to `NULL`.
61+
ROSIDL_GENERATOR_C_PUBLIC
62+
rosidl_message_type_support_t rosidl_get_zero_initialized_message_type_support_handle(void);
63+
6064
/// Get the message type support handle specific to this identifier.
6165
/**
6266
* The handle's message typesupport identifier function is returned or if the parameters are NULL

rosidl_runtime_c/src/message_type_support.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
#include <stdio.h>
1919
#include <string.h>
2020

21+
rosidl_message_type_support_t rosidl_get_zero_initialized_message_type_support_handle(void)
22+
{
23+
static rosidl_message_type_support_t null_message_type_support = {
24+
.typesupport_identifier = NULL,
25+
.data = NULL,
26+
.func = NULL,
27+
.get_type_hash_func = NULL,
28+
.get_type_description_func = NULL,
29+
.get_type_description_sources_func = NULL
30+
};
31+
return null_message_type_support;
32+
}
33+
2134
const rosidl_message_type_support_t * get_message_typesupport_handle(
2235
const rosidl_message_type_support_t * handle, const char * identifier)
2336
{

rosidl_runtime_c/src/type_description_utils.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ rosidl_runtime_c_type_description_utils_get_field_map(
112112
{
113113
RCUTILS_CHECK_ARGUMENT_FOR_NULL(individual_description, RCUTILS_RET_INVALID_ARGUMENT);
114114
RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT);
115+
if (!rcutils_allocator_is_valid(allocator)) {
116+
RCUTILS_SET_ERROR_MSG("allocator is invalid");
117+
return RCUTILS_RET_INVALID_ARGUMENT;
118+
}
115119
RCUTILS_CHECK_ARGUMENT_FOR_NULL(hash_map, RCUTILS_RET_INVALID_ARGUMENT);
116120
if (*hash_map != NULL) {
117121
RCUTILS_SET_ERROR_MSG("'hash_map' output argument is not pointing to null");
@@ -176,6 +180,10 @@ rosidl_runtime_c_type_description_utils_get_referenced_type_description_map(
176180
{
177181
RCUTILS_CHECK_ARGUMENT_FOR_NULL(referenced_types, RCUTILS_RET_INVALID_ARGUMENT);
178182
RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT);
183+
if (!rcutils_allocator_is_valid(allocator)) {
184+
RCUTILS_SET_ERROR_MSG("allocator is invalid");
185+
return RCUTILS_RET_INVALID_ARGUMENT;
186+
}
179187
RCUTILS_CHECK_ARGUMENT_FOR_NULL(hash_map, RCUTILS_RET_INVALID_ARGUMENT);
180188
if (*hash_map != NULL) {
181189
RCUTILS_SET_ERROR_MSG("'hash_map' output argument is not pointing to null");
@@ -277,6 +285,10 @@ rosidl_runtime_c_type_description_utils_get_necessary_referenced_type_descriptio
277285
RCUTILS_CHECK_ARGUMENT_FOR_NULL(main_type_description, RCUTILS_RET_INVALID_ARGUMENT);
278286
HASH_MAP_VALIDATE_HASH_MAP(referenced_types_map);
279287
RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT);
288+
if (!rcutils_allocator_is_valid(allocator)) {
289+
RCUTILS_SET_ERROR_MSG("allocator is invalid");
290+
return RCUTILS_RET_INVALID_ARGUMENT;
291+
}
280292
RCUTILS_CHECK_ARGUMENT_FOR_NULL(seen_map, RCUTILS_RET_INVALID_ARGUMENT);
281293

282294
// Only true for the top level call, so we can determine when to finalize the map
@@ -693,6 +705,13 @@ rosidl_runtime_c_type_description_utils_individual_type_description_is_valid(
693705
goto end;
694706
}
695707

708+
ret = rcutils_hash_map_fini(hash_map);
709+
if (ret != RCUTILS_RET_OK) {
710+
RCUTILS_SET_ERROR_MSG("Could not finalize hash map");
711+
return ret;
712+
}
713+
allocator.deallocate(hash_map, allocator.state);
714+
696715
return RCUTILS_RET_OK;
697716

698717
end:
@@ -853,7 +872,7 @@ rosidl_runtime_c_type_description_utils_type_description_is_valid(
853872
goto end_sequence;
854873
}
855874

856-
return RCUTILS_RET_OK;
875+
ret = RCUTILS_RET_OK;
857876

858877
end_sequence:
859878
rosidl_runtime_c__type_description__IndividualTypeDescription__Sequence__destroy(sorted_sequence);

0 commit comments

Comments
 (0)