-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
72 lines (57 loc) · 1.25 KB
/
queue.c
File metadata and controls
72 lines (57 loc) · 1.25 KB
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
#include <string.h>
#include <stdint.h>
#include "queue.h"
void queue_init(queue_type *q, uint8_t *buf, uint16_t itemsize, uint16_t itemcount)
{
q->itemsize = itemsize;
q->itemcount = itemcount;
q->buf = (void*)buf;
q->head = 0;
q->tail = 0;
return;
}
int queue_put(queue_type *q, void *data)
{
int ret = 1;
if (0 == queue_is_full(q)) {
memcpy(q->buf + q->itemsize * q->head, data, q->itemsize);
q->head++;
if(q->head == q->itemcount) {
q->head = 0;
}
ret = 0;
}
return ret;
}
int queue_get(queue_type *q, void *data)
{
int ret = 1;
if (0 == queue_is_empty(q)) {
memcpy(data, q->buf + q->itemsize * q->tail, q->itemsize);
q->tail++;
if(q->tail == q->itemcount) {
q->tail = 0;
}
ret = 0;
}
return ret;
}
int qeuue_get_size(queue_type *q)
{
int size;
if (q->head >= q->tail) {
size = q->head - q->tail;
} else {
size = q->itemcount - q->tail + q->head;
}
return size;
}
int queue_peek(queue_type *q, void *data)
{
int ret = 1;
if (0 == queue_is_empty(q)) {
memcpy(data, q->buf + q->itemsize * q->tail, q->itemsize);
ret = 0;
}
return ret;
}