summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2018-09-21 10:26:51 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2018-09-21 13:13:59 +0200
commit16a3b9f241861aaf7ddd6f284e111aad291c508f (patch)
treecb25c5ddb652bde1bcb566208e07a40dbcb30aee
parent5e131c5905265c2060836864557fe51198627dea (diff)
downloadbarebox-16a3b9f241861aaf7ddd6f284e111aad291c508f.tar.gz
barebox-16a3b9f241861aaf7ddd6f284e111aad291c508f.tar.xz
kfifo: roundup fifo size to next power of two
Comments in include/kfifo.h state that the FIFO size will be rounded up to the next power of two, but so far we haven't actually done this, probably because we didn't have roundup_pow_of_two() back then when kfifo support was added. Fix that now and do what the comments state. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--lib/kfifo.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/kfifo.c b/lib/kfifo.c
index 307dae1441..fa22feb7e0 100644
--- a/lib/kfifo.c
+++ b/lib/kfifo.c
@@ -18,6 +18,7 @@
#include <malloc.h>
#include <kfifo.h>
#include <errno.h>
+#include <linux/log2.h>
/**
* kfifo_init - allocates a new FIFO using a preallocated buffer
@@ -49,6 +50,15 @@ struct kfifo *kfifo_alloc(unsigned int size)
unsigned char *buffer;
struct kfifo *fifo;
+ /*
+ * round up to the next power of 2, since our 'let the indices
+ * wrap' tachnique works only in this case.
+ */
+ if (size & (size - 1)) {
+ BUG_ON(size > 0x80000000);
+ size = roundup_pow_of_two(size);
+ }
+
buffer = malloc(size);
if (!buffer)
return NULL;