Skip to content

Commit

Permalink
addrset: use bool and add nonnull annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Boasson <[email protected]>
  • Loading branch information
eboasson committed Oct 23, 2023
1 parent fe2b252 commit 1a84eb4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 73 deletions.
6 changes: 4 additions & 2 deletions src/core/ddsi/include/dds/ddsi/ddsi_addrset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
91 changes: 58 additions & 33 deletions src/core/ddsi/src/ddsi__addrset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;


/**
Expand 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 */

Expand Down
67 changes: 29 additions & 38 deletions src/core/ddsi/src/ddsi_addrset.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ 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 &&
loc->port == DDSI_LOCATOR_PORT_INVALID &&
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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 =
Expand All @@ -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;
Expand Down

0 comments on commit 1a84eb4

Please sign in to comment.