Skip to content

Commit

Permalink
Initialize the NULL strucutre with static value. (#378)
Browse files Browse the repository at this point in the history
* Initialize the NULL strucutre with static value.

Signed-off-by: Tomoya Fujita <[email protected]>

* use static const for xxx_get_zero_initialized_yyy functions.

Signed-off-by: Tomoya Fujita <[email protected]>

* address review comment.

Signed-off-by: Tomoya Fujita <[email protected]>

---------

Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya authored Sep 16, 2024
1 parent ac4f9af commit f4105b1
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 59 deletions.
6 changes: 2 additions & 4 deletions rmw/src/discovery_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
rmw_discovery_options_t
rmw_get_zero_initialized_discovery_options(void)
{
rmw_discovery_options_t result = (rmw_discovery_options_t) {
.automatic_discovery_range = RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET,
.static_peers_count = 0,
}; // NOLINT(readability/braces): false positive
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_discovery_options_t result;
return result;
}

Expand Down
6 changes: 4 additions & 2 deletions rmw/src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ extern "C" {
rmw_event_t
rmw_get_zero_initialized_event(void)
{
const rmw_event_t event = {
// TODO(@fujitatomoya): This is not exatly zero initialized structure.
/// We should introduce xxx_get_default_event to return the default values.
static const rmw_event_t event = {
.implementation_identifier = NULL,
.data = NULL,
.event_type = RMW_EVENT_INVALID
}; // NOLINT(readability/braces): false positive
};
return event;
}

Expand Down
21 changes: 12 additions & 9 deletions rmw/src/init_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ extern "C"
rmw_init_options_t
rmw_get_zero_initialized_init_options(void)
{
return (const rmw_init_options_t) {
.domain_id = RMW_DEFAULT_DOMAIN_ID,
.discovery_options = rmw_get_zero_initialized_discovery_options(),
.implementation_identifier = NULL,
.impl = NULL,
.instance_id = 0,
.enclave = NULL,
.security_options = rmw_get_default_security_options(),
}; // NOLINT(readability/braces): false positive
// TODO(@fujitatomoya): This is not exatly zero initialized structure.
/// We should introduce xxx_get_default_init_optionst to return the default values.
static const rmw_init_options_t init_option = {
.domain_id = RMW_DEFAULT_DOMAIN_ID,
.discovery_options = {RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET, 0},
.implementation_identifier = NULL,
.impl = NULL,
.instance_id = 0,
.enclave = NULL,
.security_options = {RMW_SECURITY_ENFORCEMENT_PERMISSIVE, NULL},
};
return init_option;
}

#ifdef __cplusplus
Expand Down
18 changes: 4 additions & 14 deletions rmw/src/message_sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
rmw_message_sequence_t
rmw_get_zero_initialized_message_sequence(void)
{
static rmw_message_sequence_t message_sequence = {
.data = NULL,
.size = 0u,
.capacity = 0u,
.allocator = NULL
};

// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_message_sequence_t message_sequence;
return message_sequence;
}

Expand Down Expand Up @@ -75,13 +70,8 @@ rmw_message_sequence_fini(rmw_message_sequence_t * sequence)
rmw_message_info_sequence_t
rmw_get_zero_initialized_message_info_sequence(void)
{
static rmw_message_info_sequence_t message_info_sequence = {
.data = NULL,
.size = 0u,
.capacity = 0u,
.allocator = NULL
};

// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_message_info_sequence_t message_info_sequence;
return message_info_sequence;
}

Expand Down
7 changes: 2 additions & 5 deletions rmw/src/names_and_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
rmw_names_and_types_t
rmw_get_zero_initialized_names_and_types(void)
{
static rmw_names_and_types_t zero = {
.names = {0, NULL, {NULL, NULL, NULL, NULL, NULL}},
.types = NULL,
};
zero.names = rcutils_get_zero_initialized_string_array();
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_names_and_types_t zero;
return zero;
}

Expand Down
3 changes: 2 additions & 1 deletion rmw/src/network_flow_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
rmw_network_flow_endpoint_t
rmw_get_zero_initialized_network_flow_endpoint(void)
{
rmw_network_flow_endpoint_t network_flow_endpoint = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_network_flow_endpoint_t network_flow_endpoint;
return network_flow_endpoint;
}

Expand Down
3 changes: 2 additions & 1 deletion rmw/src/network_flow_endpoint_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
rmw_network_flow_endpoint_array_t
rmw_get_zero_initialized_network_flow_endpoint_array(void)
{
rmw_network_flow_endpoint_array_t network_flow_endpoint_array = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_network_flow_endpoint_array_t network_flow_endpoint_array;
return network_flow_endpoint_array;
}

Expand Down
5 changes: 3 additions & 2 deletions rmw/src/security_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
rmw_security_options_t
rmw_get_zero_initialized_security_options(void)
{
rmw_security_options_t zero_initialized_options = {0, NULL};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_security_options_t zero_initialized_options;
return zero_initialized_options;
}

rmw_security_options_t
rmw_get_default_security_options(void)
{
rmw_security_options_t default_options = {
static const rmw_security_options_t default_options = {
RMW_SECURITY_ENFORCEMENT_PERMISSIVE,
NULL,
};
Expand Down
7 changes: 3 additions & 4 deletions rmw/src/subscription_content_filter_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
rmw_subscription_content_filter_options_t
rmw_get_zero_initialized_content_filter_options(void)
{
return (const rmw_subscription_content_filter_options_t) {
.filter_expression = NULL,
.expression_parameters = rcutils_get_zero_initialized_string_array()
}; // NOLINT(readability/braces): false positive
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_subscription_content_filter_options_t zero;
return zero;
}

rmw_ret_t
Expand Down
10 changes: 2 additions & 8 deletions rmw/src/topic_endpoint_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
rmw_topic_endpoint_info_t
rmw_get_zero_initialized_topic_endpoint_info(void)
{
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif
rmw_topic_endpoint_info_t zero = {0};
#ifdef __clang__
# pragma clang diagnostic pop
#endif
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_topic_endpoint_info_t zero;
return zero;
}

Expand Down
10 changes: 2 additions & 8 deletions rmw/src/topic_endpoint_info_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@
rmw_topic_endpoint_info_array_t
rmw_get_zero_initialized_topic_endpoint_info_array(void)
{
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif
rmw_topic_endpoint_info_array_t zero = {0};
#ifdef __clang__
# pragma clang diagnostic pop
#endif
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_topic_endpoint_info_array_t zero;
return zero;
}

Expand Down
3 changes: 2 additions & 1 deletion rmw/src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RMW_WARN_UNUSED
rmw_message_info_t
rmw_get_zero_initialized_message_info(void)
{
rmw_message_info_t zero_initialized_message_info = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_message_info_t zero_initialized_message_info;
return zero_initialized_message_info;
}

0 comments on commit f4105b1

Please sign in to comment.