From: Lee Schermerhorn <lee.schermerhorn@hp.com>
To: Cliff Wickman <cpi@sgi.com>
Cc: Kornilios Kourtis <kkourt@cslab.ece.ntua.gr>,
	Brice Goglin <Brice.Goglin@inria.fr>, <linux-numa@vger.kernel.org>,
	<eric.whitney@hp.com>
Date:	Tue, 28 Apr 2009 12:36:44 -0400

Subject: [PATCH 4/8] numactl/libnuma - return freeable bitmasks

List-ID: <linux-numa.vger.kernel.org>

[PATCH 04/08] - Return freeable masks

Against:  numactl-2.0.3-rc2

The V2+ libnuma API provides functions to free masks [bit, cpu, node]
returned by some functions.  [It should probably be more specific
about it being the caller's responsibility to free such masks.]
However, in some cases, the library returns pointers to libnuma
internal masks, such as numa_no_nodes.

Instead, return pointer to freshly allocated mask that the caller can
free without affecting the library's internal operation.

Kornilious replaced my bogus structure assignments with calls to
copy_bitmask_to_bitmask()   --lts

 libnuma.c |   18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Index: numactl-dev/libnuma.c
===================================================================
--- numactl-dev.orig/libnuma.c
+++ numactl-dev/libnuma.c
@@ -904,9 +904,9 @@ numa_get_interleave_mask_v2(void)
 
 	bmp = numa_allocate_nodemask();
 	getpol(&oldpolicy, bmp);
-	if (oldpolicy == MPOL_INTERLEAVE)
-		return bmp;
-	return numa_no_nodes_ptr;
+	if (oldpolicy != MPOL_INTERLEAVE)
+		copy_bitmask_to_bitmask(numa_no_nodes_ptr, bmp);
+	return bmp;
 } 
 __asm__(".symver numa_get_interleave_mask_v2,numa_get_interleave_mask@@libnuma_1.2");
 
@@ -1063,9 +1063,9 @@ numa_get_membind_v2(void)
 
 	bmp = numa_allocate_nodemask();
 	getpol(&oldpolicy, bmp);
-	if (oldpolicy == MPOL_BIND)
-		return bmp;
-	return numa_all_nodes_ptr;
+	if (oldpolicy != MPOL_BIND)
+		copy_bitmask_to_bitmask(numa_all_nodes_ptr, bmp);
+	return bmp;
 } 
 __asm__(".symver numa_get_membind_v2,numa_get_membind@@libnuma_1.2");
 
@@ -1667,8 +1667,10 @@ numa_parse_nodestring(char *s)
 
 	mask = numa_allocate_nodemask();
 
-	if (s[0] == 0)
-		return numa_no_nodes_ptr;
+	if (s[0] == 0){
+		copy_bitmask_to_bitmask(numa_no_nodes_ptr, mask);
+		return mask; /* return freeable mask */
+	}
 	if (*s == '!') {
 		invert = 1;
 		s++;
