Skip to content

Latest commit

 

History

History
49 lines (41 loc) · 1.3 KB

README.md

File metadata and controls

49 lines (41 loc) · 1.3 KB

kfifo

kfifo is a ring queue inspired by linux kernel. It is simple and efficient that can be used in embedded systems.

Features

  • Ultra Fast
  • Lock Free
  • Thread Safe (Single Producer With Single Consumer)
  • Supports C99 And UP

Usage

kfifo.h should be dropped into an existing project and compiled along with it. The library provides some macros for using. Talking is cheap, show you a sample following:

#include <assert.h>
#include <stdlib.h>

#include "kfifo.h"

int main(void) {
    _KFIFO(kfifo, int) kfifo;
    KFIFO_INIT(&kfifo, 1000);
    assert(KFIFO_CAPACITY(&kfifo) == 1023);
    do {
        int* n = calloc(1, sizeof(int));
        *n = 1;
        KFIFO_ENQUEUE(&kfifo, n);
    } while(!KFIFO_FULL(&kfifo));
    assert(KFIFO_LENGTH(&kfifo) == 1023);
    do {
        int* nn;
        KFIFO_DEQUEUE(&kfifo, nn);
        assert(*nn == 1);
        free(nn);
    } while(!KFIFO_EMPTY(&kfifo));
    assert(KFIFO_LENGTH(&kfifo) == 0);
    KFIFO_FREE(&kfifo);
    return 0;
}