Skip to contentSkip to conversation

bufio: round up block size to power of 2

PATCH Superseded Michael Chang 2 participants 3 messages Apr 20, 2018 – Apr 24, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bufio: round up block size to power of 2

Rounding up the bufio->block_size to meet power of 2 to facilitate next_buf calcuation in grub_bufio_read.

---
 grub-core/io/bufio.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/io/bufio.c +8 -0
index 22438277d..4d66b65a3 100644
@@ -61,6 +61,14 @@ grub_bufio_open (grub_file_t io, int size)
     size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
             io->size);
 
+  /* round up size to power of 2 */
+  if (size & (size - 1))
+    {
+      int round_up;
+      for (round_up = 1; round_up < size; round_up <<= 1);
+      size = round_up;
+    }
+
   bufio = grub_zalloc (sizeof (struct grub_bufio) + size);
   if (! bufio)
     {
Signature · 1 line
2.13.6
#1 lore raw
Quoted 13 lines · Michael Chang

Rounding up the bufio->block_size to meet power of 2 to facilitate next_buf calcuation in grub_bufio_read.

---
 grub-core/io/bufio.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/io/bufio.c +0 -0
index 22438277d..4d66b65a3 100644

grub-core/io/bufio.c grub_bufio_open (grub_file_t io, int size) · line 64

Quoted 4 lines
grub-core/io/bufio.c +1 -0
@@ -61,6 +61,14 @@ grub_bufio_open (grub_file_t io, int size)
     size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
             io->size);
 
+  /* round up size to power of 2 */

May I ask you to say why here?

grub-core/io/bufio.c grub_bufio_open (grub_file_t io, int size) · line 70

Quoted 6 lines
grub-core/io/bufio.c +6 -0
+  if (size & (size - 1))
+    {
+      int round_up;
+      for (round_up = 1; round_up < size; round_up <<= 1);
+      size = round_up;
+    }

Please use this (stolen from linux/drivers/md/raid5.c):
while (size & (size - 1))

    size = (size | (size - 1)) + 1;

Daniel

#2 lore raw
Quoted 17 lines · Daniel Kiper
Quoted 16 lines · Michael Chang

Rounding up the bufio->block_size to meet power of 2 to facilitate next_buf calcuation in grub_bufio_read.

---
 grub-core/io/bufio.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/io/bufio.c +0 -0
index 22438277d..4d66b65a3 100644
@@ -61,6 +61,14 @@ grub_bufio_open (grub_file_t io, int size)
     size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
             io->size);
 
Quoted 3 lines

grub-core/io/bufio.c grub_bufio_open (grub_file_t io, int size) · line 64

Quoted 1 line
grub-core/io/bufio.c +1 -0
+  /* round up size to power of 2 */

May I ask you to say why here?

OK.

Quoted 11 lines

grub-core/io/bufio.c grub_bufio_open (grub_file_t io, int size) · line 70

Quoted 7 lines
grub-core/io/bufio.c +6 -0
+  if (size & (size - 1))
+    {
+      int round_up;
+      for (round_up = 1; round_up < size; round_up <<= 1);
+      size = round_up;
+    }

Please use this (stolen from linux/drivers/md/raid5.c):
while (size & (size - 1))

    size = (size | (size - 1)) + 1;

Cool. They will be added in upcoming V2 patch.

Thanks,
Michael

Quoted 2 lines

Daniel