Skip to contentSkip to conversation

tools: allocate bitmaps in units of unsigned long

PATCH In review Olaf Hering 2 participants 8 messages Dec 9, 2020 – Dec 15, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tools: allocate bitmaps in units of unsigned long

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.

---
 tools/libs/ctrl/xc_bitops.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/libs/ctrl/xc_bitops.h +4 -1
index 3d3a09772a..d6c5ea5138 100644
@@ -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)
#1 lore raw

PATCH 2/3 tools: remove unused ORDER_LONG

There are no users left, xenpaging has its own variant.
The last user was removed with commit 11d0044a168994de85b9b328452292852aedc871

---
 tools/libs/ctrl/xc_bitops.h | 2 --
 1 file changed, 2 deletions(-)
tools/libs/ctrl/xc_bitops.h +0 -2
index d6c5ea5138..f0bac4a071 100644
@@ -6,9 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-/* Needed by several includees, but no longer used for bitops. */
 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
-#define ORDER_LONG (sizeof(unsigned long) == 4 ? 5 : 6)
 
 #define BITMAP_ENTRY(_nr,_bmap) ((_bmap))[(_nr) / 8]
 #define BITMAP_SHIFT(_nr) ((_nr) % 8)
#3 lore raw

PATCH 3/3 tools: add API to work with sevaral bits at once

In reply to Olaf Hering

Introduce new API to test if a fixed number of bits is clear or set, and clear or set them all at once.

The caller has to make sure the input bitnumber is a multiply of BITS_PER_LONG.

This API avoids the loop over each bit in a known range just to see if all of them are either clear or set.

---
 tools/libs/ctrl/xc_bitops.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
tools/libs/ctrl/xc_bitops.h +25 -0
index f0bac4a071..92f38872fb 100644
@@ -77,4 +77,29 @@ static inline void bitmap_or(void *_dst, const void *_other,
         dst[i] |= other[i];
 }
 
+static inline int test_bit_long_set(unsigned long nr_base, const void *_addr)
+{
+    const unsigned long *addr = _addr;
+    unsigned long val = addr[nr_base / BITS_PER_LONG];
+    return val == ~0;
+}
+
+static inline int test_bit_long_clear(unsigned long nr_base, const void *_addr)
+{
+    const unsigned long *addr = _addr;
+    unsigned long val = addr[nr_base / BITS_PER_LONG];
+    return val == 0;
+}
+
+static inline void clear_bit_long(unsigned long nr_base, void *_addr)
+{
+    unsigned long *addr = _addr;
+    addr[nr_base / BITS_PER_LONG] = 0;
+}
+
+static inline void set_bit_long(unsigned long nr_base, void *_addr)
+{
+    unsigned long *addr = _addr;
+    addr[nr_base / BITS_PER_LONG] = ~0;
+}
 #endif  /* XC_BITOPS_H */
#4 lore raw

Re: [PATCH v1 3/3] tools: add API to work with sevaral bits at once

Quoted 9 lines · Olaf Hering

Introduce new API to test if a fixed number of bits is clear or set, and clear or set them all at once.

The caller has to make sure the input bitnumber is a multiply of BITS_PER_LONG.

This API avoids the loop over each bit in a known range just to see if all of them are either clear or set.

I would rather these be introduced along side their callers.

tools/libs/ctrl/xc_bitops.h static inline void bitmap_or(void *_dst, const void *_other, · line 80

Quoted 13 lines
---
 tools/libs/ctrl/xc_bitops.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
tools/libs/ctrl/xc_bitops.h +1 -0
index f0bac4a071..92f38872fb 100644
@@ -77,4 +77,29 @@ static inline void bitmap_or(void *_dst, const void *_other,
         dst[i] |= other[i];
 }
 
+static inline int test_bit_long_set(unsigned long nr_base, const void *_addr)

What's wrong with requiring the input addr be const unsigned long *?

#5 lore raw

Re: [PATCH v1 3/3] tools: add API to work with sevaral bits at once

Am Tue, 15 Dec 2020 16:22:44 +0000
schrieb Wei Liu <[email protected]>:

Quoted 1 line

What's wrong with requiring the input addr be const unsigned long *?

Probably nothing. In the end I just borrowed the prototypes from the other functions in this file.

I will resend with this change once I have the consumers ready.

Olaf

#6 lore raw

Re: [PATCH v1 3/3] tools: add API to work with sevaral bits at once

Quoted 8 lines · Olaf Hering

Am Tue, 15 Dec 2020 16:22:44 +0000
schrieb Wei Liu <[email protected]>:

Quoted 1 line

What's wrong with requiring the input addr be const unsigned long *?

Probably nothing. In the end I just borrowed the prototypes from the other functions in this file.

I will resend with this change once I have the consumers ready.

Okay.

I will push the first two shortly.

Wei.

Quoted 2 lines

Olaf

Acked-by on v1 1/3
#7 lore raw
In reply to Olaf Hering
Quoted 7 lines · Olaf Hering

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.

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

Quoted 20 lines
---
 tools/libs/ctrl/xc_bitops.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/libs/ctrl/xc_bitops.h +4 -1
index 3d3a09772a..d6c5ea5138 100644
@@ -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)