Skip to content

Commit 749e22a

Browse files
committed
utils/annotate: abstract-out info attr operations
Signed-off-by: Brice Goglin <[email protected]>
1 parent a03b7f3 commit 749e22a

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

utils/hwloc/hwloc-annotate.c

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -79,53 +79,59 @@ static char *distances_transform_removeobj = NULL;
7979
static char *distances_transform_replace_oldtype = NULL;
8080
static char *distances_transform_replace_newtype = NULL;
8181

82+
static void apply_infos(struct hwloc_infos_s *infos)
83+
{
84+
unsigned i, j;
85+
if (clearinfos) {
86+
/* this may be considered dangerous, applications should not modify objects directly */
87+
for(i=0; i<infos->count; i++) {
88+
struct hwloc_info_s *info = &infos->array[i];
89+
free(info->name);
90+
free(info->value);
91+
}
92+
free(infos->array);
93+
infos->array = NULL;
94+
infos->count = 0;
95+
infos->allocated = 0;
96+
}
97+
if (infoname) {
98+
if (replaceinfos) {
99+
/* this may be considered dangerous, applications should not modify objects directly */
100+
for(i=0, j=0; i<infos->count; i++) {
101+
struct hwloc_info_s *info = &infos->array[i];
102+
if (!strcmp(infoname, info->name)) {
103+
/* remove info */
104+
free(info->name);
105+
info->name = NULL;
106+
free(info->value);
107+
} else {
108+
if (i != j) {
109+
/* shift info to where it belongs */
110+
infos->array[j].name = info->name;
111+
infos->array[j].value = info->value;
112+
}
113+
j++;
114+
}
115+
}
116+
infos->count = j;
117+
if (!j) {
118+
free(infos->array);
119+
infos->array = NULL;
120+
infos->allocated = 0;
121+
}
122+
}
123+
if (infovalue)
124+
hwloc_modify_infos(infos, HWLOC_MODIFY_INFOS_OP_ADD, infoname, infovalue);
125+
}
126+
}
127+
82128
static void apply(hwloc_topology_t topology, hwloc_obj_t obj)
83129
{
84-
unsigned i,j;
85-
if (clearinfos) {
86-
/* this may be considered dangerous, applications should not modify objects directly */
87-
for(i=0; i<obj->infos.count; i++) {
88-
struct hwloc_info_s *info = &obj->infos.array[i];
89-
free(info->name);
90-
free(info->value);
91-
}
92-
free(obj->infos.array);
93-
obj->infos.array = NULL;
94-
obj->infos.count = 0;
95-
obj->infos.allocated = 0;
96-
}
97130
if (clearuserdata) {
98131
hwloc_utils_userdata_free(obj);
99132
}
100-
if (infoname) {
101-
if (replaceinfos) {
102-
/* this may be considered dangerous, applications should not modify objects directly */
103-
for(i=0, j=0; i<obj->infos.count; i++) {
104-
struct hwloc_info_s *info = &obj->infos.array[i];
105-
if (!strcmp(infoname, info->name)) {
106-
/* remove info */
107-
free(info->name);
108-
info->name = NULL;
109-
free(info->value);
110-
} else {
111-
if (i != j) {
112-
/* shift info to where it belongs */
113-
obj->infos.array[j].name = info->name;
114-
obj->infos.array[j].value = info->value;
115-
}
116-
j++;
117-
}
118-
}
119-
obj->infos.count = j;
120-
if (!j) {
121-
free(obj->infos.array);
122-
obj->infos.array = NULL;
123-
obj->infos.allocated = 0;
124-
}
125-
}
126-
if (infovalue)
127-
hwloc_obj_add_info(obj, infoname, infovalue);
128-
}
133+
if (infoname || clearinfos)
134+
apply_infos(&obj->infos);
129135
if (subtype) {
130136
if (obj->subtype)
131137
free(obj->subtype);

0 commit comments

Comments
 (0)