-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
executable file
·76 lines (65 loc) · 1.38 KB
/
queue.c
File metadata and controls
executable file
·76 lines (65 loc) · 1.38 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
72
73
74
75
76
#include "queue.h"
#include "mem.h"
#include "osapi.h"
#include "c_types.h"
#include "espmissingincludes.h"
struct queue {
uint8_t head;
uint8_t tail;
uint8_t size;
uint8_t elem_size;
uint8_t capacity;
void *data;
};
ICACHE_FLASH_ATTR struct queue *queue_create(uint8_t elem_size,
uint8_t capacity)
{
struct queue *ptr;
void *data;
ptr = (struct queue *) os_malloc(sizeof(struct queue));
if (!ptr)
return NULL;
data = os_malloc(elem_size * capacity);
if (!data)
return NULL;
ptr->head = 0;
ptr->tail = 0;
ptr->size = 0;
ptr->elem_size = elem_size;
ptr->capacity = capacity;
ptr->data = data;
return ptr;
}
ICACHE_FLASH_ATTR void queue_push(struct queue *q, const void *elem)
{
os_memcpy(q->data + q->head * q->elem_size, elem, q->elem_size);
if (++(q->head) == q->capacity)
q->head = 0;
q->size++;
}
ICACHE_FLASH_ATTR void queue_pop(struct queue *q, void *elem)
{
os_memcpy(elem, q->data + q->tail * q->elem_size, q->elem_size);
if (++(q->tail) == q->capacity)
q->tail = 0;
q->size--;
}
ICACHE_FLASH_ATTR bool queue_is_empty(const struct queue *q)
{
return q->size == 0;
}
ICACHE_FLASH_ATTR bool queue_is_full(const struct queue * q)
{
return q->size == q->capacity;
}
ICACHE_FLASH_ATTR void queue_clear(struct queue *q)
{
q->head = 0;
q->tail = 0;
q->size = 0;
}
ICACHE_FLASH_ATTR void queue_destroy(struct queue *q)
{
os_free(q->data);
os_free(q);
}