mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] kfifo: roundup fifo size to next power of two
Date: Fri, 21 Sep 2018 13:14:05 +0200	[thread overview]
Message-ID: <20180921111405.26959-1-s.hauer@pengutronix.de> (raw)

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>
---
 lib/kfifo.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

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;
-- 
2.19.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2018-09-21 11:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180921111405.26959-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox