Skip to content

Commit

Permalink
Dynamic Subscription (BONUS: Allocators): rosidl (#737)
Browse files Browse the repository at this point in the history
* Add zero init for message type support handle

Signed-off-by: methylDragon <methylDragon@gmail.com>

* Fix memory leaks in type description utils

Signed-off-by: methylDragon <methylDragon@gmail.com>

* Refactor serialization support to use allocators and refs

Signed-off-by: methylDragon <methylDragon@gmail.com>

* Check if allocator is valid

Signed-off-by: methylDragon <methylDragon@gmail.com>

---------

Signed-off-by: methylDragon <methylDragon@gmail.com>
  • Loading branch information
methylDragon authored Apr 11, 2023
1 parent 6da8660 commit 7583b95
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions rosidl_runtime_c/src/message_type_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
#include <stdio.h>
#include <string.h>

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)
{
Expand Down
21 changes: 20 additions & 1 deletion rosidl_runtime_c/src/type_description_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7583b95

Please sign in to comment.