From 7583b95e13b9b0ca22f4f166da732ac73eace945 Mon Sep 17 00:00:00 2001 From: methylDragon Date: Tue, 11 Apr 2023 06:02:17 +0000 Subject: [PATCH] Dynamic Subscription (BONUS: Allocators): rosidl (#737) * Add zero init for message type support handle Signed-off-by: methylDragon * Fix memory leaks in type description utils Signed-off-by: methylDragon * Refactor serialization support to use allocators and refs Signed-off-by: methylDragon * Check if allocator is valid Signed-off-by: methylDragon --------- Signed-off-by: methylDragon --- .../message_type_support_struct.h | 4 ++++ rosidl_runtime_c/src/message_type_support.c | 13 ++++++++++++ rosidl_runtime_c/src/type_description_utils.c | 21 ++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/rosidl_runtime_c/include/rosidl_runtime_c/message_type_support_struct.h b/rosidl_runtime_c/include/rosidl_runtime_c/message_type_support_struct.h index 08a254f6b..071cac27d 100644 --- a/rosidl_runtime_c/include/rosidl_runtime_c/message_type_support_struct.h +++ b/rosidl_runtime_c/include/rosidl_runtime_c/message_type_support_struct.h @@ -57,6 +57,10 @@ struct rosidl_message_type_support_t rosidl_message_get_type_description_sources_function get_type_description_sources_func; }; +/// Return a rosidl_message_type_support_t struct with members set to `NULL`. +ROSIDL_GENERATOR_C_PUBLIC +rosidl_message_type_support_t rosidl_get_zero_initialized_message_type_support_handle(void); + /// Get the message type support handle specific to this identifier. /** * The handle's message typesupport identifier function is returned or if the parameters are NULL diff --git a/rosidl_runtime_c/src/message_type_support.c b/rosidl_runtime_c/src/message_type_support.c index 439e332cf..08d89ca1d 100644 --- a/rosidl_runtime_c/src/message_type_support.c +++ b/rosidl_runtime_c/src/message_type_support.c @@ -18,6 +18,19 @@ #include #include +rosidl_message_type_support_t rosidl_get_zero_initialized_message_type_support_handle(void) +{ + static rosidl_message_type_support_t null_message_type_support = { + .typesupport_identifier = NULL, + .data = NULL, + .func = NULL, + .get_type_hash_func = NULL, + .get_type_description_func = NULL, + .get_type_description_sources_func = NULL + }; + return null_message_type_support; +} + const rosidl_message_type_support_t * get_message_typesupport_handle( const rosidl_message_type_support_t * handle, const char * identifier) { diff --git a/rosidl_runtime_c/src/type_description_utils.c b/rosidl_runtime_c/src/type_description_utils.c index 4e620f119..2f6fbe180 100644 --- a/rosidl_runtime_c/src/type_description_utils.c +++ b/rosidl_runtime_c/src/type_description_utils.c @@ -112,6 +112,10 @@ rosidl_runtime_c_type_description_utils_get_field_map( { RCUTILS_CHECK_ARGUMENT_FOR_NULL(individual_description, RCUTILS_RET_INVALID_ARGUMENT); RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT); + if (!rcutils_allocator_is_valid(allocator)) { + RCUTILS_SET_ERROR_MSG("allocator is invalid"); + return RCUTILS_RET_INVALID_ARGUMENT; + } RCUTILS_CHECK_ARGUMENT_FOR_NULL(hash_map, RCUTILS_RET_INVALID_ARGUMENT); if (*hash_map != NULL) { 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( { RCUTILS_CHECK_ARGUMENT_FOR_NULL(referenced_types, RCUTILS_RET_INVALID_ARGUMENT); RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT); + if (!rcutils_allocator_is_valid(allocator)) { + RCUTILS_SET_ERROR_MSG("allocator is invalid"); + return RCUTILS_RET_INVALID_ARGUMENT; + } RCUTILS_CHECK_ARGUMENT_FOR_NULL(hash_map, RCUTILS_RET_INVALID_ARGUMENT); if (*hash_map != NULL) { 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 RCUTILS_CHECK_ARGUMENT_FOR_NULL(main_type_description, RCUTILS_RET_INVALID_ARGUMENT); HASH_MAP_VALIDATE_HASH_MAP(referenced_types_map); RCUTILS_CHECK_ARGUMENT_FOR_NULL(allocator, RCUTILS_RET_INVALID_ARGUMENT); + if (!rcutils_allocator_is_valid(allocator)) { + RCUTILS_SET_ERROR_MSG("allocator is invalid"); + return RCUTILS_RET_INVALID_ARGUMENT; + } RCUTILS_CHECK_ARGUMENT_FOR_NULL(seen_map, RCUTILS_RET_INVALID_ARGUMENT); // 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( goto end; } + ret = rcutils_hash_map_fini(hash_map); + if (ret != RCUTILS_RET_OK) { + RCUTILS_SET_ERROR_MSG("Could not finalize hash map"); + return ret; + } + allocator.deallocate(hash_map, allocator.state); + return RCUTILS_RET_OK; end: @@ -853,7 +872,7 @@ rosidl_runtime_c_type_description_utils_type_description_is_valid( goto end_sequence; } - return RCUTILS_RET_OK; + ret = RCUTILS_RET_OK; end_sequence: rosidl_runtime_c__type_description__IndividualTypeDescription__Sequence__destroy(sorted_sequence);