Skip to content

Commit

Permalink
API: return meaningful positive values from modify_infos()
Browse files Browse the repository at this point in the history
To tell whether things got added/replaced/removed.

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Jul 28, 2023
1 parent 090d23a commit cdf3380
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
8 changes: 4 additions & 4 deletions hwloc/topology.c
Expand Up @@ -519,7 +519,7 @@ int hwloc__add_info(struct hwloc_infos_s *infos, const char *name, const char *v
if (!array[count].value)
goto out_with_name;
infos->count = count+1;
return 0;
return 1;

out_with_name:
free(array[count].name);
Expand Down Expand Up @@ -582,15 +582,15 @@ int hwloc__replace_infos(struct hwloc_infos_s *infos,
if (found) {
if (found > 1)
infos->count -= found-1;
return 0;
return 1+found;
} else {
/* no match, just add */
return hwloc__add_info(infos, name, value);
}
}

static int hwloc__remove_infos(struct hwloc_infos_s *infos,
const char *name, const char *value)
const char *name, const char *value)
{
struct hwloc_info_s *array = infos->array;
unsigned count = infos->count;
Expand All @@ -611,7 +611,7 @@ static int hwloc__remove_infos(struct hwloc_infos_s *infos,
}
}
infos->count -= found;
return 0;
return found;
}

int hwloc_modify_infos(struct hwloc_infos_s *infos, unsigned long op, const char *name, const char *value)
Expand Down
12 changes: 11 additions & 1 deletion include/hwloc.h
Expand Up @@ -1169,7 +1169,10 @@ hwloc_get_info_by_name(struct hwloc_infos_s *infos, const char *name) __hwloc_at
* \p name and/or \p value may be non \c NULL to specify which pair(s) to remove.
* If both \p name and \p value are \c NULL, all pairs are removed.
*
* \return \c 0 on success, \c -1 on error.
* \return a positive value if some info attributes were added/modified/removed
* (see the documentation of each operation).
* \return \c 0 if the request was valid but no change was applied.
* \return \c -1 on error.
*
* \note If \p value contains some non-printable characters, they will
* be dropped when exporting to XML, see hwloc_topology_export_xml() in hwloc/export.h.
Expand All @@ -1181,12 +1184,15 @@ HWLOC_DECLSPEC int hwloc_modify_infos(struct hwloc_infos_s *infos,
/** \brief Operations given to hwloc_modify_infos(). */
enum hwloc_modify_infos_op_e {
/** \brief Add a new info attribute with the given name and value.
* \return \c 1 if the pair was successfully added.
* \hideinitializer
*/
HWLOC_MODIFY_INFOS_OP_ADD = 1UL<<0,

/** \brief Add a new info attribute with the given name and value
* only if that pair doesn't exist yet.
* \return \c 1 if the pair was successfully added.
* \return \c 0 if the pair already existed.
* \hideinitializer
*/
HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE = 1UL<<1,
Expand All @@ -1195,12 +1201,16 @@ enum hwloc_modify_infos_op_e {
* with a single attribute with the given name and value.
* If no existing pair matches, add a new one.
* If multiple pairs match, only one remains.
* \return \c 1 if the pair was added.
* \return \c N+1 if N existing pairs were replaced by one.
* \hideinitializer
*/
HWLOC_MODIFY_INFOS_OP_REPLACE = 1UL<<2,

/** \brief Remove existing info attributes that matches the given
* name and/or value if not \c NULL.
* \return \c N if N existing pairs were removed.
* \return \c 0 if no matching pair was found and removed.
* \hideinitializer
*/
HWLOC_MODIFY_INFOS_OP_REMOVE = 1UL<<3
Expand Down
32 changes: 16 additions & 16 deletions tests/hwloc/infos.c
Expand Up @@ -48,61 +48,61 @@ int main(void)
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NAME2); /* no match */
assert(!err);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NULL); /* remove all */
assert(!err);
assert(err >= 2);
assert(obj->infos.count == 0);

/* invalid add */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, NULL, "");
assert(err == -1);
/* 9 interleaved duplicates */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo1");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 1);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo1");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 2);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo1");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 3);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo2");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 4);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo2");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 5);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo2");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 6);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo3");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 7);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo3");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 8);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo3");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 9);

/* invalid replace */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "", NULL);
assert(err == -1);
/* replace the third set of duplicates */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin3", "foo4");
assert(!err);
assert(err == 3+1);
assert(obj->infos.count == 7);
assert(!strcmp(obj->infos.array[2].name, "coin3"));
assert(!strcmp(obj->infos.array[2].value, "foo4"));
/* remove second set of duplicates */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin2", NULL);
assert(!err);
assert(err == 3);
assert(obj->infos.count == 4);
/* remove second instance of first duplicates */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin1", "foo2");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 3);
/* replace reminder of the first set of duplicates */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin1", "foo5");
assert(!err);
assert(err == 2+1);
assert(obj->infos.count == 2);
assert(!strcmp(obj->infos.array[0].name, "coin1"));
assert(!strcmp(obj->infos.array[0].value, "foo5"));
Expand All @@ -112,10 +112,10 @@ int main(void)

/* check add_unique */
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo5");
assert(!err);
assert(err == 0);
assert(obj->infos.count == 2);
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo4");
assert(!err);
assert(err == 1);
assert(obj->infos.count == 3);
assert(!strcmp(obj->infos.array[2].name, "coin1"));
assert(!strcmp(obj->infos.array[2].value, "foo4"));
Expand Down
2 changes: 1 addition & 1 deletion tests/hwloc/shmem.c
Expand Up @@ -265,7 +265,7 @@ int main(int argc, char *argv[])
err = hwloc_topology_load(orig);
assert(!err);
err = hwloc_obj_add_info(hwloc_get_root_obj(orig), "ShmemSyntheticWithDistances", "1");
assert(!err);
assert(err == 1);

printf("adding distance matrix\n");
for(i=0; i<3; i++) {
Expand Down

0 comments on commit cdf3380

Please sign in to comment.