diff mbox series

[v1,1/3] tools: allocate bitmaps in units of unsigned long

Message ID 20201209155452.28376-1-olaf@aepfle.de (mailing list archive)
State New, archived
Headers show
Series [v1,1/3] tools: allocate bitmaps in units of unsigned long | expand

Commit Message

Olaf Hering Dec. 9, 2020, 3:54 p.m. UTC
Allocate enough memory so that the returned pointer can be safely
accessed as an array of unsigned long.

The actual bitmap size in units of bytes, as returned by bitmap_size,
remains unchanged.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
---
 tools/libs/ctrl/xc_bitops.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Wei Liu Dec. 15, 2020, 4:11 p.m. UTC | #1
On Wed, Dec 09, 2020 at 04:54:49PM +0100, Olaf Hering wrote:
> Allocate enough memory so that the returned pointer can be safely
> accessed as an array of unsigned long.
> 
> The actual bitmap size in units of bytes, as returned by bitmap_size,
> remains unchanged.
> 
> Signed-off-by: Olaf Hering <olaf@aepfle.de>

Acked-by: Wei Liu <wl@xen.org>

I can see where you're coming from. This (internal) API's returned
pointer is being assigned to unsigned long *.

> ---
>  tools/libs/ctrl/xc_bitops.h | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/libs/ctrl/xc_bitops.h b/tools/libs/ctrl/xc_bitops.h
> index 3d3a09772a..d6c5ea5138 100644
> --- a/tools/libs/ctrl/xc_bitops.h
> +++ b/tools/libs/ctrl/xc_bitops.h
> @@ -21,7 +21,10 @@ static inline unsigned long bitmap_size(unsigned long nr_bits)
>  
>  static inline void *bitmap_alloc(unsigned long nr_bits)
>  {
> -    return calloc(1, bitmap_size(nr_bits));
> +    unsigned long longs;
> +
> +    longs = (nr_bits + BITS_PER_LONG - 1) / BITS_PER_LONG;
> +    return calloc(longs, sizeof(unsigned long));
>  }
>  
>  static inline void bitmap_set(void *addr, unsigned long nr_bits)
diff mbox series

Patch

diff --git a/tools/libs/ctrl/xc_bitops.h b/tools/libs/ctrl/xc_bitops.h
index 3d3a09772a..d6c5ea5138 100644
--- a/tools/libs/ctrl/xc_bitops.h
+++ b/tools/libs/ctrl/xc_bitops.h
@@ -21,7 +21,10 @@  static inline unsigned long bitmap_size(unsigned long nr_bits)
 
 static inline void *bitmap_alloc(unsigned long nr_bits)
 {
-    return calloc(1, bitmap_size(nr_bits));
+    unsigned long longs;
+
+    longs = (nr_bits + BITS_PER_LONG - 1) / BITS_PER_LONG;
+    return calloc(longs, sizeof(unsigned long));
 }
 
 static inline void bitmap_set(void *addr, unsigned long nr_bits)