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 @@ 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_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; + } - return ret; + kfifo_init(fifo, buffer, size); + + return fifo; } /**