Skip to content

Commit cdf3380

Browse files
committed
API: return meaningful positive values from modify_infos()
To tell whether things got added/replaced/removed. Signed-off-by: Brice Goglin <[email protected]>
1 parent 090d23a commit cdf3380

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

hwloc/topology.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ int hwloc__add_info(struct hwloc_infos_s *infos, const char *name, const char *v
519519
if (!array[count].value)
520520
goto out_with_name;
521521
infos->count = count+1;
522-
return 0;
522+
return 1;
523523

524524
out_with_name:
525525
free(array[count].name);
@@ -582,15 +582,15 @@ int hwloc__replace_infos(struct hwloc_infos_s *infos,
582582
if (found) {
583583
if (found > 1)
584584
infos->count -= found-1;
585-
return 0;
585+
return 1+found;
586586
} else {
587587
/* no match, just add */
588588
return hwloc__add_info(infos, name, value);
589589
}
590590
}
591591

592592
static int hwloc__remove_infos(struct hwloc_infos_s *infos,
593-
const char *name, const char *value)
593+
const char *name, const char *value)
594594
{
595595
struct hwloc_info_s *array = infos->array;
596596
unsigned count = infos->count;
@@ -611,7 +611,7 @@ static int hwloc__remove_infos(struct hwloc_infos_s *infos,
611611
}
612612
}
613613
infos->count -= found;
614-
return 0;
614+
return found;
615615
}
616616

617617
int hwloc_modify_infos(struct hwloc_infos_s *infos, unsigned long op, const char *name, const char *value)

include/hwloc.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,10 @@ hwloc_get_info_by_name(struct hwloc_infos_s *infos, const char *name) __hwloc_at
11691169
* \p name and/or \p value may be non \c NULL to specify which pair(s) to remove.
11701170
* If both \p name and \p value are \c NULL, all pairs are removed.
11711171
*
1172-
* \return \c 0 on success, \c -1 on error.
1172+
* \return a positive value if some info attributes were added/modified/removed
1173+
* (see the documentation of each operation).
1174+
* \return \c 0 if the request was valid but no change was applied.
1175+
* \return \c -1 on error.
11731176
*
11741177
* \note If \p value contains some non-printable characters, they will
11751178
* be dropped when exporting to XML, see hwloc_topology_export_xml() in hwloc/export.h.
@@ -1181,12 +1184,15 @@ HWLOC_DECLSPEC int hwloc_modify_infos(struct hwloc_infos_s *infos,
11811184
/** \brief Operations given to hwloc_modify_infos(). */
11821185
enum hwloc_modify_infos_op_e {
11831186
/** \brief Add a new info attribute with the given name and value.
1187+
* \return \c 1 if the pair was successfully added.
11841188
* \hideinitializer
11851189
*/
11861190
HWLOC_MODIFY_INFOS_OP_ADD = 1UL<<0,
11871191

11881192
/** \brief Add a new info attribute with the given name and value
11891193
* only if that pair doesn't exist yet.
1194+
* \return \c 1 if the pair was successfully added.
1195+
* \return \c 0 if the pair already existed.
11901196
* \hideinitializer
11911197
*/
11921198
HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE = 1UL<<1,
@@ -1195,12 +1201,16 @@ enum hwloc_modify_infos_op_e {
11951201
* with a single attribute with the given name and value.
11961202
* If no existing pair matches, add a new one.
11971203
* If multiple pairs match, only one remains.
1204+
* \return \c 1 if the pair was added.
1205+
* \return \c N+1 if N existing pairs were replaced by one.
11981206
* \hideinitializer
11991207
*/
12001208
HWLOC_MODIFY_INFOS_OP_REPLACE = 1UL<<2,
12011209

12021210
/** \brief Remove existing info attributes that matches the given
12031211
* name and/or value if not \c NULL.
1212+
* \return \c N if N existing pairs were removed.
1213+
* \return \c 0 if no matching pair was found and removed.
12041214
* \hideinitializer
12051215
*/
12061216
HWLOC_MODIFY_INFOS_OP_REMOVE = 1UL<<3

tests/hwloc/infos.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,61 +48,61 @@ int main(void)
4848
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NAME2); /* no match */
4949
assert(!err);
5050
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NULL); /* remove all */
51-
assert(!err);
51+
assert(err >= 2);
5252
assert(obj->infos.count == 0);
5353

5454
/* invalid add */
5555
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, NULL, "");
5656
assert(err == -1);
5757
/* 9 interleaved duplicates */
5858
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo1");
59-
assert(!err);
59+
assert(err == 1);
6060
assert(obj->infos.count == 1);
6161
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo1");
62-
assert(!err);
62+
assert(err == 1);
6363
assert(obj->infos.count == 2);
6464
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo1");
65-
assert(!err);
65+
assert(err == 1);
6666
assert(obj->infos.count == 3);
6767
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo2");
68-
assert(!err);
68+
assert(err == 1);
6969
assert(obj->infos.count == 4);
7070
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo2");
71-
assert(!err);
71+
assert(err == 1);
7272
assert(obj->infos.count == 5);
7373
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo2");
74-
assert(!err);
74+
assert(err == 1);
7575
assert(obj->infos.count == 6);
7676
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo3");
77-
assert(!err);
77+
assert(err == 1);
7878
assert(obj->infos.count == 7);
7979
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo3");
80-
assert(!err);
80+
assert(err == 1);
8181
assert(obj->infos.count == 8);
8282
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo3");
83-
assert(!err);
83+
assert(err == 1);
8484
assert(obj->infos.count == 9);
8585

8686
/* invalid replace */
8787
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "", NULL);
8888
assert(err == -1);
8989
/* replace the third set of duplicates */
9090
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin3", "foo4");
91-
assert(!err);
91+
assert(err == 3+1);
9292
assert(obj->infos.count == 7);
9393
assert(!strcmp(obj->infos.array[2].name, "coin3"));
9494
assert(!strcmp(obj->infos.array[2].value, "foo4"));
9595
/* remove second set of duplicates */
9696
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin2", NULL);
97-
assert(!err);
97+
assert(err == 3);
9898
assert(obj->infos.count == 4);
9999
/* remove second instance of first duplicates */
100100
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin1", "foo2");
101-
assert(!err);
101+
assert(err == 1);
102102
assert(obj->infos.count == 3);
103103
/* replace reminder of the first set of duplicates */
104104
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin1", "foo5");
105-
assert(!err);
105+
assert(err == 2+1);
106106
assert(obj->infos.count == 2);
107107
assert(!strcmp(obj->infos.array[0].name, "coin1"));
108108
assert(!strcmp(obj->infos.array[0].value, "foo5"));
@@ -112,10 +112,10 @@ int main(void)
112112

113113
/* check add_unique */
114114
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo5");
115-
assert(!err);
115+
assert(err == 0);
116116
assert(obj->infos.count == 2);
117117
err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo4");
118-
assert(!err);
118+
assert(err == 1);
119119
assert(obj->infos.count == 3);
120120
assert(!strcmp(obj->infos.array[2].name, "coin1"));
121121
assert(!strcmp(obj->infos.array[2].value, "foo4"));

tests/hwloc/shmem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ int main(int argc, char *argv[])
265265
err = hwloc_topology_load(orig);
266266
assert(!err);
267267
err = hwloc_obj_add_info(hwloc_get_root_obj(orig), "ShmemSyntheticWithDistances", "1");
268-
assert(!err);
268+
assert(err == 1);
269269

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

0 commit comments

Comments
 (0)