Skip to content

Commit 5aa875e

Browse files
committed
hwloc/linux-libnuma.h: handle bitmap realloc errors
And document them in the API, as well as the other functions that can or cannot fail. Refs #578. Signed-off-by: Brice Goglin <[email protected]>
1 parent 4519fab commit 5aa875e

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

include/hwloc/linux-libnuma.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright © 2009 CNRS
3-
* Copyright © 2009-2017 Inria. All rights reserved.
3+
* Copyright © 2009-2023 Inria. All rights reserved.
44
* Copyright © 2009-2010, 2012 Université Bordeaux
55
* See COPYING in top-level directory.
66
*/
@@ -50,6 +50,8 @@ extern "C" {
5050
* This function may be used before calling set_mempolicy, mbind, migrate_pages
5151
* or any other function that takes an array of unsigned long and a maximal
5252
* node number as input parameter.
53+
*
54+
* \return 0.
5355
*/
5456
static __hwloc_inline int
5557
hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
@@ -84,6 +86,8 @@ hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpus
8486
* This function may be used before calling set_mempolicy, mbind, migrate_pages
8587
* or any other function that takes an array of unsigned long and a maximal
8688
* node number as input parameter.
89+
*
90+
* \return 0.
8791
*/
8892
static __hwloc_inline int
8993
hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset,
@@ -119,6 +123,9 @@ hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nod
119123
* This function may be used after calling get_mempolicy or any other function
120124
* that takes an array of unsigned long as output parameter (and possibly
121125
* a maximal node number as input parameter).
126+
*
127+
* \return 0 on success.
128+
* \return -1 on error, for instance if failing an internal reallocation.
122129
*/
123130
static __hwloc_inline int
124131
hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
@@ -130,7 +137,8 @@ hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t
130137
while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
131138
if (node->os_index < maxnode
132139
&& (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
133-
hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
140+
if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
141+
return -1;
134142
return 0;
135143
}
136144

@@ -142,6 +150,9 @@ hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t
142150
* This function may be used after calling get_mempolicy or any other function
143151
* that takes an array of unsigned long as output parameter (and possibly
144152
* a maximal node number as input parameter).
153+
*
154+
* \return 0 on success.
155+
* \return -1 with errno set to \c ENOMEM if some internal reallocation failed.
145156
*/
146157
static __hwloc_inline int
147158
hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
@@ -153,7 +164,8 @@ hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset
153164
while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
154165
if (node->os_index < maxnode
155166
&& (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
156-
hwloc_bitmap_set(nodeset, node->os_index);
167+
if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
168+
return -1;
157169
return 0;
158170
}
159171

@@ -184,7 +196,7 @@ hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset
184196
* This function may be used before calling many numa_ functions
185197
* that use a struct bitmask as an input parameter.
186198
*
187-
* \return newly allocated struct bitmask.
199+
* \return newly allocated struct bitmask, or \c NULL on error.
188200
*/
189201
static __hwloc_inline struct bitmask *
190202
hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
@@ -209,7 +221,7 @@ hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpu
209221
* This function may be used before calling many numa_ functions
210222
* that use a struct bitmask as an input parameter.
211223
*
212-
* \return newly allocated struct bitmask.
224+
* \return newly allocated struct bitmask, or \c NULL on error.
213225
*/
214226
static __hwloc_inline struct bitmask *
215227
hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
@@ -231,6 +243,9 @@ hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_no
231243
*
232244
* This function may be used after calling many numa_ functions
233245
* that use a struct bitmask as an output parameter.
246+
*
247+
* \return 0 on success.
248+
* \return -1 with errno set to \c ENOMEM if some internal reallocation failed.
234249
*/
235250
static __hwloc_inline int
236251
hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
@@ -241,14 +256,18 @@ hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_
241256
hwloc_bitmap_zero(cpuset);
242257
while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
243258
if (numa_bitmask_isbitset(bitmask, node->os_index))
244-
hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
259+
if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
260+
return -1;
245261
return 0;
246262
}
247263

248264
/** \brief Convert libnuma bitmask \p bitmask into hwloc NUMA node set \p nodeset
249265
*
250266
* This function may be used after calling many numa_ functions
251267
* that use a struct bitmask as an output parameter.
268+
*
269+
* \return 0 on success.
270+
* \return -1 with errno set to \c ENOMEM if some internal reallocation failed.
252271
*/
253272
static __hwloc_inline int
254273
hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
@@ -259,7 +278,8 @@ hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodese
259278
hwloc_bitmap_zero(nodeset);
260279
while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
261280
if (numa_bitmask_isbitset(bitmask, node->os_index))
262-
hwloc_bitmap_set(nodeset, node->os_index);
281+
if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
282+
return -1;
263283
return 0;
264284
}
265285

0 commit comments

Comments
 (0)