[PATCH 2/7] kfifo: change kfifo_init to work with a preallocated fifo

Sascha Hauer s.hauer at pengutronix.de
Thu Jan 26 08:26:48 EST 2012


kfifo currently only works with dynamically allocated fifos.
Change the currently unused kfifo_init to take a preallocated
fifo. This allows for statically initialized fifos.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 include/kfifo.h |    2 +-
 lib/kfifo.c     |   23 +++++++++--------------
 2 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/include/kfifo.h b/include/kfifo.h
index 3eb03cb..2987e0b 100644
--- a/include/kfifo.h
+++ b/include/kfifo.h
@@ -28,7 +28,7 @@ struct kfifo {
 	unsigned int out;	/* data is extracted from off. (out % size) */
 };
 
-struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size);
+void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size);
 struct kfifo *kfifo_alloc(unsigned int size);
 void kfifo_free(struct kfifo *fifo);
 
diff --git a/lib/kfifo.c b/lib/kfifo.c
index 27d44e9..a2f3727 100644
--- a/lib/kfifo.c
+++ b/lib/kfifo.c
@@ -34,19 +34,11 @@
  * Do NOT pass the kfifo to kfifo_free() after use! Simply free the
  * &struct kfifo with free().
  */
-struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size)
+void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size)
 {
-	struct kfifo *fifo;
-
-	fifo = malloc(sizeof(struct kfifo));
-	if (!fifo)
-		return NULL;
-
 	fifo->buffer = buffer;
 	fifo->size = size;
 	fifo->in = fifo->out = 0;
-
-	return fifo;
 }
 
 /**
@@ -60,18 +52,21 @@ struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size)
 struct kfifo *kfifo_alloc(unsigned int size)
 {
 	unsigned char *buffer;
-	struct kfifo *ret;
+	struct kfifo *fifo;
 
 	buffer = malloc(size);
 	if (!buffer)
 		return NULL;
 
-	ret = kfifo_init(buffer, size);
-
-	if (!ret)
+	fifo = malloc(sizeof(struct kfifo));
+	if (!fifo) {
 		free(buffer);
+		return NULL;
+	}
+
+	kfifo_init(fifo, buffer, size);
 
-	return ret;
+	return fifo;
 }
 
 /**
-- 
1.7.8.3




More information about the barebox mailing list