-
Notifications
You must be signed in to change notification settings - Fork 28
/
queue.h
50 lines (38 loc) · 805 Bytes
/
queue.h
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
#ifndef queue_h
#define queue_h
#include <stdint.h>
#include "util.h"
typedef struct
{
char *data;
int size;
int in;
int out;
} queue_t;
void queue_new(queue_t *queue, int size, char *buffer);
attr_inline attr_pure char queue_empty(const queue_t *queue)
{
return(queue->in == queue->out);
}
attr_inline attr_pure char queue_full(const queue_t *queue)
{
return(((queue->in + 1) % queue->size) == queue->out);
}
attr_inline void queue_flush(queue_t *queue)
{
queue->in = 0;
queue->out = 0;
}
attr_inline void queue_push(queue_t *queue, char data)
{
queue->data[queue->in] = data;
queue->in = (queue->in + 1) % queue->size;
}
attr_inline char queue_pop(queue_t *queue)
{
char data;
data = queue->data[queue->out];
queue->out = (queue->out + 1) % queue->size;
return(data);
}
#endif