summaryrefslogtreecommitdiff
path: root/kernel/ringbuffer.h
blob: 9610150e554086b0587f887817bc394acc427faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @file
 *
 * FIFO Buffers
 * ============
 *
 * Simple FIRST IN FIRST OUT
 *
 * Requires
 * --------
 *
 * Requires kballoc/kbfree - block allocation
 * 
 * Thread Safety
 * -------------
 *
 *  **NOTE** : If you have more than one producer and one consumer
 *  you will have to do some locking!
 *
 *  ringbuffer_init() and ringbuffer_free() can be logically called only
 *  once anyway (for each ringbuffer). 
 *
 *  The structure is believed to be thread-safe as long as you have
 *  ONLY _ONE_ single producer AND _ONE_ single consumer PER ringbuffer.
 *
 *  With one Consumer and one Producer:
 *
 *  The Consumer can use:
 *  - ringbuffer_has()   - will not give false positives.
 *  - ringbuffer_empty() - will not give false negatives.
 *  - ringbuffer_get()   - acceseses only the head-pointer.
 *
 *  The Producer can use: 
 *  - ringbuffer_not_full() - will not give false positives
 *  - ringbuffer_full()     - will not give false negatives.
 *  - ringbuffer_put()      - accesses only the tail-pointer.
 *
 * Todo
 * ----
 * provide soemthing to read large blocks faster.
 *
 */

#ifndef RINGBUFFER_H
#define RINGBUFFER_H

#include <stdint.h>
#include <stdbool.h>

/** Ringbuffer sturcutre */
typedef struct ringbuffer_struct
{
    uint32_t size;      // size in bytes
    uint32_t head;      // current head idx
    uint32_t tail;      // current tail idx
    uint8_t  *data;     // the data itself
}ringbuffer;

/** Create a new fifo/ringbuffer of given size (in blocks) */
ringbuffer ringbuffer_init(uint32_t blocks); 

/** Deallocate buffer */
void ringbuffer_free(ringbuffer *f);

/** Put one _byte_ into the buffer. */
bool ringbuffer_put(ringbuffer*,uint8_t);

/** Get a single _byte_ from the buffer. */
uint8_t ringbuffer_get(ringbuffer*);

/** Check if the buffer is not empty */
bool ringbuffer_has(ringbuffer*);

/** Check if the buffer is empty */
bool ringbuffer_empty(ringbuffer*);

/** Check if the buffer is full */
bool ringbuffer_full(ringbuffer* f);

/** Check if the buffer is not full */
bool ringbuffer_not_full(ringbuffer* f);

#endif