summaryrefslogtreecommitdiff
path: root/kernel/ringbuffer.h
blob: f68f7660a6e9f86d01231b368ae48e354f65fe94 (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
/**
 * @file
 *
 * FIFO Buffers
 * ============
 *
 * Simple FIRST IN FIRST OUT
 *
 * Requires
 * --------
 * Requires kballoc/kbfree - block allocation
 * 
 * Thread
 * ------
 * This is __not__ threadsafe. It is your job to lock accesses to
 * reads/writes.
 *
 * 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;
    uint32_t front;
    uint32_t back;
    uint8_t  *data;
}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 _char_ into buffer. Returns true on success (i.e. buffer not full) */
bool	ringbuffer_put(ringbuffer*,uint8_t);

/** Get a single _char_ from the buffer,
 *  Return value __0__ might indicate that the buffer is empty.
 *  check with _ringbuffer_has()_ to be sure.
 */
uint8_t ringbuffer_get(ringbuffer*);

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

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

#endif