Skip to contentSkip to conversation

block/vhdx: Small cleanups

PATCH Applied Max Reitz 2 participants 5 messages Dec 18, 2015 – Feb 23, 2016 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover block/vhdx: Small cleanups

Just some bits I discovered while going through the Coverity report.

(The issue reported by Coverity actually was not an issue.)

Max Reitz (2):
vhdx: DIV_ROUND_UP() in vhdx_calc_bat_entries()
vhdx: Simplify vhdx_set_shift_bits()

 block/vhdx.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
Signature · 1 line
2.6.4
author
#1 lore raw

PATCH 1/2 vhdx: DIV_ROUND_UP() in vhdx_calc_bat_entries()

We have DIV_ROUND_UP(), so we can use it to produce more easily readable code. It may be slower than the bit shifting currently performed (because it actually performs a division), but since vhdx_calc_bat_entries() is never used in a hot path, this is completely fine.

---
 block/vhdx.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
block/vhdx.c +2 -8
index 2fe9a5e..3955f7f 100644
@@ -856,14 +856,8 @@ static void vhdx_calc_bat_entries(BDRVVHDXState *s)
 {
     uint32_t data_blocks_cnt, bitmap_blocks_cnt;
 
-    data_blocks_cnt = s->virtual_disk_size >> s->block_size_bits;
-    if (s->virtual_disk_size - (data_blocks_cnt << s->block_size_bits)) {
-        data_blocks_cnt++;
-    }
-    bitmap_blocks_cnt = data_blocks_cnt >> s->chunk_ratio_bits;
-    if (data_blocks_cnt - (bitmap_blocks_cnt << s->chunk_ratio_bits)) {
-        bitmap_blocks_cnt++;
-    }
+    data_blocks_cnt = DIV_ROUND_UP(s->virtual_disk_size, s->block_size);
+    bitmap_blocks_cnt = DIV_ROUND_UP(data_blocks_cnt, s->chunk_ratio);
 
     if (s->parent_entries) {
         s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1);
Signature · 1 line
2.6.4
author
#2 lore raw

PATCH 2/2 vhdx: Simplify vhdx_set_shift_bits()

In reply to Max Reitz

For values which are powers of two (and we do assume all of these to be), sizeof(x) * 8 - 1 - clz(x) == ctz(x). Therefore, use ctz().

---
 block/vhdx.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
block/vhdx.c +4 -4
index 3955f7f..475f602 100644
@@ -263,10 +263,10 @@ static void vhdx_region_unregister_all(BDRVVHDXState *s)
 
 static void vhdx_set_shift_bits(BDRVVHDXState *s)
 {
-    s->logical_sector_size_bits = 31 - clz32(s->logical_sector_size);
-    s->sectors_per_block_bits =   31 - clz32(s->sectors_per_block);
-    s->chunk_ratio_bits =         63 - clz64(s->chunk_ratio);
-    s->block_size_bits =          31 - clz32(s->block_size);
+    s->logical_sector_size_bits = ctz32(s->logical_sector_size);
+    s->sectors_per_block_bits =   ctz32(s->sectors_per_block);
+    s->chunk_ratio_bits =         ctz64(s->chunk_ratio);
+    s->block_size_bits =          ctz32(s->block_size);
 }
 
 /*
Signature · 1 line
2.6.4
author
#3 lore raw
In reply to Max Reitz
Quoted 11 lines · Max Reitz

Just some bits I discovered while going through the Coverity report.

(The issue reported by Coverity actually was not an issue.)

Max Reitz (2):
vhdx: DIV_ROUND_UP() in vhdx_calc_bat_entries()
vhdx: Simplify vhdx_set_shift_bits()

 block/vhdx.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

Ping

Applied
#4 lore raw
Quoted 6 lines · Max Reitz
Quoted 5 lines · Max Reitz

Just some bits I discovered while going through the Coverity report.

(The issue reported by Coverity actually was not an issue.)

Quoted 9 lines
Quoted 6 lines

Max Reitz (2):
vhdx: DIV_ROUND_UP() in vhdx_calc_bat_entries()
vhdx: Simplify vhdx_set_shift_bits()

 block/vhdx.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

Ping

Thanks, applied to my block branch

Jeff