Skip to contentSkip to conversation

is_power_of_2 in kernel/kfifo

PATCH Superseded Vignesh Babu BM 3 participants 3 messages Feb 21, 2007 Kernel janitors

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 is_power_of_2 in kernel/kfifo

Replacing (n & (n-1)) in the context of power of 2 checks with is_power_of_2

---

kernel/kfifo.c +2 -1
index 5d1d907..eeb5e7e 100644
@@ -24,6 +24,7 @@
 #include <linux/slab.h>
 #include <linux/err.h>
 #include <linux/kfifo.h>
+#include <linux/log2.h>
 
 /**
  * kfifo_init - allocates a new FIFO using a preallocated buffer
@@ -41,7 +42,7 @@ struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
 	struct kfifo *fifo;
 
 	/* size must be a power of 2 */
-	BUG_ON(size & (size - 1));
+	BUG_ON(!is_power_of_2(size));
 
 	fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
 	if (!fifo)
Signature · 8 lines
Regards,  
Vignesh Babu BM  
_____________________________________________________________  
"Why is it that every time I'm with you, makes me believe in magic?"
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
#1 lore raw
Quoted 19 lines · Vignesh Babu BM

Replacing (n & (n-1)) in the context of power of 2 checks with is_power_of_2

---

kernel/kfifo.c +1 -0
index 5d1d907..eeb5e7e 100644
@@ -24,6 +24,7 @@
 #include <linux/slab.h>
 #include <linux/err.h>
 #include <linux/kfifo.h>
+#include <linux/log2.h>
 
 /**
  * kfifo_init - allocates a new FIFO using a preallocated buffer

kernel/kfifo.c struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size, · line 48

Quoted 8 lines
kernel/kfifo.c +1 -1
@@ -41,7 +42,7 @@ struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
 	struct kfifo *fifo;
 
 	/* size must be a power of 2 */
-	BUG_ON(size & (size - 1));
+	BUG_ON(!is_power_of_2(size));
 
 	fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
 	if (!fifo)

Could you also remove the comment? (since it is now obvious by the code)

Richard Knutsson

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

Acked-by on v1 1/1
#2 lore raw
In reply to Vignesh Babu BM
Quoted 5 lines · Richard Knutsson
Quoted 4 lines · Vignesh Babu BM

Replacing (n & (n-1)) in the context of power of 2 checks with is_power_of_2

[...]

Quoted 2 lines
Quoted 2 lines
kernel/kfifo.c +1 -1
-	BUG_ON(size & (size - 1));
+	BUG_ON(!is_power_of_2(size));

[...]

Quoted 1 line

Could you also remove the comment? (since it is now obvious by the code)

Thanks,

Signature · 6 lines
Stelian Pop <[email protected]>

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors