-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.c
74 lines (58 loc) · 1.35 KB
/
queue.c
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
#include "queue.h"
#include "error.h"
void queue_init(queue_t *queue, uint8_t *data, uint8_t data_size, uint8_t capacity)
{
queue->data = data;
queue->data_size = data_size;
queue->capacity = capacity;
queue->size = 0;
queue->head = 0;
queue->tail = 0;
}
uint8_t queue_enqueue(queue_t *queue, const uint8_t *data)
{
// Check to make sure that we do not overflow
if (queue->size + 1 > queue->capacity)
{
return E_BUFF_OVERFLOW;
}
// Get the starting index
uint16_t realIndex = queue->head * queue->data_size;
// Iterate the input buffer
for (uint8_t i = 0; i < queue->data_size; i++)
{
queue->data[realIndex] = data[i];
realIndex++;
}
// Calculate the new head
queue->head = (queue->head + 1) % queue->capacity;
queue->size++;
return E_NO_ERROR;
}
uint8_t queue_dequeue(queue_t *queue, uint8_t *data)
{
// Make sure there is something to dequeue
if (queue->size == 0)
{
return E_EMPTY;
}
// Get the starting index
uint16_t realIndex = queue->tail * queue->data_size;
// Iterate the input buffer
for (uint8_t i = 0; i < queue->data_size; i++)
{
data[i] = queue->data[realIndex];
realIndex++;
}
// Calculate the new tail
queue->tail = (queue->tail + 1) % queue->capacity;
queue->size--;
return E_NO_ERROR;
}
uint8_t queue_clear(queue_t *queue)
{
queue->head = 0;
queue->tail = 0;
queue->size = 0;
return E_NO_ERROR;
}