From 2f15111ea4ddf85c00032ca1dcd105428c25a8b4 Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Tue, 17 Oct 2023 13:57:26 +0200 Subject: [PATCH] addrset: use bool and add nonnull annotations Signed-off-by: Erik Boasson --- src/core/ddsi/include/dds/ddsi/ddsi_addrset.h | 6 +- src/core/ddsi/src/ddsi__addrset.h | 91 ++++++++++++------- src/core/ddsi/src/ddsi_addrset.c | 67 ++++++-------- 3 files changed, 91 insertions(+), 73 deletions(-) diff --git a/src/core/ddsi/include/dds/ddsi/ddsi_addrset.h b/src/core/ddsi/include/dds/ddsi/ddsi_addrset.h index 1ba9e9e45c..3dddd95adf 100644 --- a/src/core/ddsi/include/dds/ddsi/ddsi_addrset.h +++ b/src/core/ddsi/include/dds/ddsi/ddsi_addrset.h @@ -25,10 +25,12 @@ struct ddsi_addrset; typedef void (*ddsi_addrset_forall_fun_t) (const ddsi_xlocator_t *loc, void *arg); /** @component locators */ -int ddsi_addrset_empty (const struct ddsi_addrset *as); +bool ddsi_addrset_empty (const struct ddsi_addrset *as) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_addrset_forall (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg); +void ddsi_addrset_forall (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg) + ddsrt_nonnull ((1,2)); #if defined (__cplusplus) } diff --git a/src/core/ddsi/src/ddsi__addrset.h b/src/core/ddsi/src/ddsi__addrset.h index a2ef14e92d..5b23931615 100644 --- a/src/core/ddsi/src/ddsi__addrset.h +++ b/src/core/ddsi/src/ddsi__addrset.h @@ -35,7 +35,8 @@ struct ddsi_addrset { typedef ssize_t (*ddsi_addrset_forone_fun_t) (const ddsi_xlocator_t *loc, void *arg); /** @component locators */ -struct ddsi_addrset *ddsi_new_addrset (void); +struct ddsi_addrset *ddsi_new_addrset (void) + ddsrt_attribute_warn_unused_result; /** @component locators */ struct ddsi_addrset *ddsi_ref_addrset (struct ddsi_addrset *as); @@ -44,35 +45,40 @@ struct ddsi_addrset *ddsi_ref_addrset (struct ddsi_addrset *as); void ddsi_unref_addrset (struct ddsi_addrset *as); /** @component locators */ -void ddsi_add_locator_to_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_locator_t *loc); +void ddsi_add_locator_to_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_locator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_add_xlocator_to_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_xlocator_t *loc); +void ddsi_add_xlocator_to_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_xlocator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_remove_from_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_xlocator_t *loc); +void ddsi_remove_from_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_xlocator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_purge (struct ddsi_addrset *as); +int ddsi_compare_locators (const ddsi_locator_t *a, const ddsi_locator_t *b) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_compare_locators (const ddsi_locator_t *a, const ddsi_locator_t *b); - -/** @component locators */ -int ddsi_compare_xlocators (const ddsi_xlocator_t *a, const ddsi_xlocator_t *b); +int ddsi_compare_xlocators (const ddsi_xlocator_t *a, const ddsi_xlocator_t *b) + ddsrt_nonnull_all; /* These lock ASADD, then lock/unlock AS any number of times, then unlock ASADD */ /** @component locators */ -void ddsi_copy_addrset_into_addrset_uc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd); +void ddsi_copy_addrset_into_addrset_uc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_copy_addrset_into_addrset_mc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd); +void ddsi_copy_addrset_into_addrset_mc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_copy_addrset_into_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd); +void ddsi_copy_addrset_into_addrset (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd) + ddsrt_nonnull_all; /** @component locators */ size_t ddsi_addrset_count (const struct ddsi_addrset *as); @@ -84,37 +90,47 @@ size_t ddsi_addrset_count_uc (const struct ddsi_addrset *as); size_t ddsi_addrset_count_mc (const struct ddsi_addrset *as); /** @component locators */ -int ddsi_addrset_empty_uc (const struct ddsi_addrset *as); +bool ddsi_addrset_empty_uc (const struct ddsi_addrset *as) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_empty_mc (const struct ddsi_addrset *as); +bool ddsi_addrset_empty_mc (const struct ddsi_addrset *as) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_addrset_any_uc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst); +void ddsi_addrset_any_uc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_any_mc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst); +bool ddsi_addrset_any_mc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_addrset_any_uc_else_mc_nofail (const struct ddsi_addrset *as, ddsi_xlocator_t *dst); +void ddsi_addrset_any_uc_else_mc_nofail (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_contains_non_psmx_uc (const struct ddsi_addrset *as); +bool ddsi_addrset_contains_non_psmx_uc (const struct ddsi_addrset *as) + ddsrt_nonnull_all; /* Keeps AS locked */ /** @component locators */ -int ddsi_addrset_forone (struct ddsi_addrset *as, ddsi_addrset_forone_fun_t f, void *arg); +int ddsi_addrset_forone (struct ddsi_addrset *as, ddsi_addrset_forone_fun_t f, void *arg) + ddsrt_nonnull ((1, 2)); /** @component locators */ -size_t ddsi_addrset_forall_count (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg); +size_t ddsi_addrset_forall_count (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg) + ddsrt_nonnull ((1, 2)); /** @component locators */ -size_t ddsi_addrset_forall_uc_count (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg); +size_t ddsi_addrset_forall_uc_count (struct ddsi_addrset *as, ddsi_addrset_forall_fun_t f, void *arg) + ddsrt_nonnull ((1, 2)); /** @component locators */ -void ddsi_log_addrset (struct ddsi_domaingv *gv, uint32_t tf, const char *prefix, const struct ddsi_addrset *as); +void ddsi_log_addrset (struct ddsi_domaingv *gv, uint32_t tf, const char *prefix, const struct ddsi_addrset *as) + ddsrt_nonnull_all; /** @@ -123,39 +139,48 @@ void ddsi_log_addrset (struct ddsi_domaingv *gv, uint32_t tf, const char *prefix * * @param a Address set * @param b Address set to compare with - * @return int + * @return bool */ -int ddsi_addrset_eq_onesidederr (const struct ddsi_addrset *a, const struct ddsi_addrset *b); +bool ddsi_addrset_eq_onesidederr (const struct ddsi_addrset *a, const struct ddsi_addrset *b); /** @component locators */ -int ddsi_is_unspec_locator (const ddsi_locator_t *loc); +bool ddsi_is_unspec_locator (const ddsi_locator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_is_unspec_xlocator (const ddsi_xlocator_t *loc); +bool ddsi_is_unspec_xlocator (const ddsi_xlocator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_set_unspec_locator (ddsi_locator_t *loc); +void ddsi_set_unspec_locator (ddsi_locator_t *loc) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_set_unspec_xlocator (ddsi_xlocator_t *loc); +void ddsi_set_unspec_xlocator (ddsi_xlocator_t *loc) + ddsrt_nonnull_all; #ifdef DDS_HAS_SSM /** @component locators */ -int ddsi_addrset_contains_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as); +bool ddsi_addrset_contains_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_any_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst); +bool ddsi_addrset_any_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) + ddsrt_nonnull_all; /** @component locators */ -int ddsi_addrset_any_non_ssm_mc (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst); +bool ddsi_addrset_any_non_ssm_mc (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_copy_addrset_into_addrset_no_ssm_mc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd); +void ddsi_copy_addrset_into_addrset_no_ssm_mc (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd) + ddsrt_nonnull_all; /** @component locators */ -void ddsi_copy_addrset_into_addrset_no_ssm (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd); +void ddsi_copy_addrset_into_addrset_no_ssm (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const struct ddsi_addrset *asadd) + ddsrt_nonnull_all; #endif /* DDS_HAS_SSM */ diff --git a/src/core/ddsi/src/ddsi_addrset.c b/src/core/ddsi/src/ddsi_addrset.c index fc9b414100..c68d678794 100644 --- a/src/core/ddsi/src/ddsi_addrset.c +++ b/src/core/ddsi/src/ddsi_addrset.c @@ -122,7 +122,7 @@ void ddsi_set_unspec_xlocator (ddsi_xlocator_t *loc) ddsi_set_unspec_locator (&loc->c); } -int ddsi_is_unspec_locator (const ddsi_locator_t *loc) +bool ddsi_is_unspec_locator (const ddsi_locator_t *loc) { static const ddsi_locator_t zloc = { .kind = 0 }; return (loc->kind == DDSI_LOCATOR_KIND_INVALID && @@ -130,13 +130,13 @@ int ddsi_is_unspec_locator (const ddsi_locator_t *loc) memcmp (&zloc.address, loc->address, sizeof (zloc.address)) == 0); } -int ddsi_is_unspec_xlocator (const ddsi_xlocator_t *loc) +bool ddsi_is_unspec_xlocator (const ddsi_xlocator_t *loc) { return ddsi_is_unspec_locator (&loc->c); } #ifdef DDS_HAS_SSM -int ddsi_addrset_contains_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as) +bool ddsi_addrset_contains_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as) { struct ddsi_addrset_node *n; ddsrt_avl_citer_t it; @@ -146,14 +146,14 @@ int ddsi_addrset_contains_ssm (const struct ddsi_domaingv *gv, const struct ddsi if (ddsi_is_ssm_mcaddr (gv, &n->loc.c)) { UNLOCK (as); - return 1; + return true; } } UNLOCK (as); - return 0; + return false; } -int ddsi_addrset_any_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) +bool ddsi_addrset_any_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) { struct ddsi_addrset_node *n; ddsrt_avl_citer_t it; @@ -164,14 +164,14 @@ int ddsi_addrset_any_ssm (const struct ddsi_domaingv *gv, const struct ddsi_addr { *dst = n->loc; UNLOCK (as); - return 1; + return true; } } UNLOCK (as); - return 0; + return false; } -int ddsi_addrset_any_non_ssm_mc (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) +bool ddsi_addrset_any_non_ssm_mc (const struct ddsi_domaingv *gv, const struct ddsi_addrset *as, ddsi_xlocator_t *dst) { struct ddsi_addrset_node *n; ddsrt_avl_citer_t it; @@ -182,23 +182,14 @@ int ddsi_addrset_any_non_ssm_mc (const struct ddsi_domaingv *gv, const struct dd { *dst = n->loc; UNLOCK (as); - return 1; + return true; } } UNLOCK (as); - return 0; + return false; } #endif -int ddsi_addrset_purge (struct ddsi_addrset *as) -{ - LOCK (as); - ddsrt_avl_cfree (&addrset_treedef, &as->ucaddrs, ddsrt_free); - ddsrt_avl_cfree (&addrset_treedef, &as->mcaddrs, ddsrt_free); - UNLOCK (as); - return 0; -} - static void add_xlocator_to_addrset_impl (const struct ddsi_domaingv *gv, struct ddsi_addrset *as, const ddsi_xlocator_t *loc) { assert (!ddsi_is_unspec_locator (&loc->c)); @@ -374,43 +365,43 @@ size_t ddsi_addrset_count_mc (const struct ddsi_addrset *as) } } -int ddsi_addrset_empty_uc (const struct ddsi_addrset *as) +bool ddsi_addrset_empty_uc (const struct ddsi_addrset *as) { - int isempty; + bool isempty; LOCK (as); isempty = ddsrt_avl_cis_empty (&as->ucaddrs); UNLOCK (as); return isempty; } -int ddsi_addrset_empty_mc (const struct ddsi_addrset *as) +bool ddsi_addrset_empty_mc (const struct ddsi_addrset *as) { - int isempty; + bool isempty; LOCK (as); isempty = ddsrt_avl_cis_empty (&as->mcaddrs); UNLOCK (as); return isempty; } -int ddsi_addrset_empty (const struct ddsi_addrset *as) +bool ddsi_addrset_empty (const struct ddsi_addrset *as) { - int isempty; + bool isempty; LOCK (as); isempty = ddsrt_avl_cis_empty (&as->ucaddrs) && ddsrt_avl_cis_empty (&as->mcaddrs); UNLOCK (as); return isempty; } -int ddsi_addrset_contains_non_psmx_uc (const struct ddsi_addrset *as) +bool ddsi_addrset_contains_non_psmx_uc (const struct ddsi_addrset *as) { - int have_non_psmx_uc = 0; + bool have_non_psmx_uc = false; LOCK (as); ddsrt_avl_citer_t it; for (const struct ddsi_addrset_node *n = ddsrt_avl_citer_first (&addrset_treedef, &as->ucaddrs, &it); n; n = ddsrt_avl_citer_next (&it)) { if (n->loc.c.kind != DDSI_LOCATOR_KIND_PSMX) { - have_non_psmx_uc = 1; + have_non_psmx_uc = true; break; } } @@ -442,24 +433,24 @@ void ddsi_addrset_any_uc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) done: UNLOCK (as); // SPDP, SEDP processing check the address sets and reject any remote entity that doesn't - // have a non-PSMX unicast address, making this unreachable. + // have a non-PSMX unicast address assert (dst->c.kind != DDSI_LOCATOR_KIND_INVALID); } -int ddsi_addrset_any_mc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) +bool ddsi_addrset_any_mc (const struct ddsi_addrset *as, ddsi_xlocator_t *dst) { LOCK (as); if (ddsrt_avl_cis_empty (&as->mcaddrs)) { UNLOCK (as); - return 0; + return false; } else { const struct ddsi_addrset_node *n = ddsrt_avl_croot_non_empty (&addrset_treedef, &as->mcaddrs); *dst = n->loc; UNLOCK (as); - return 1; + return true; } } @@ -572,14 +563,14 @@ static int addrset_eq_onesidederr1 (const ddsrt_avl_ctree_t *at, const ddsrt_avl } } -int ddsi_addrset_eq_onesidederr (const struct ddsi_addrset *a, const struct ddsi_addrset *b) +bool ddsi_addrset_eq_onesidederr (const struct ddsi_addrset *a, const struct ddsi_addrset *b) { - int iseq; if (a == b) - return 1; + return true; if (a == NULL || b == NULL) - return 0; + return false; LOCK (a); + bool iseq; if (TRYLOCK (b)) { iseq = @@ -593,7 +584,7 @@ int ddsi_addrset_eq_onesidederr (const struct ddsi_addrset *a, const struct ddsi just decide it isn't worth the bother. Which it isn't because it doesn't have to be an exact check on equality. A possible improvement would be to use an rwlock. */ - iseq = 0; + iseq = false; } UNLOCK (a); return iseq;