- Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtiny_queue.h
More file actions
Latest commit
103 lines (87 loc) · 2.74 KB
/
Copy pathtiny_queue.h
File metadata and controls
103 lines (87 loc) · 2.74 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*!
* @file
* @brief FIFO queue for arbitrarily-sized elements.
*/
#ifndeftiny_queue_h
#definetiny_queue_h
#include<stddef.h>
#include<stdint.h>
#include"tiny_ring_buffer.h"
typedefstruct {
uint16_telement_count;
tiny_ring_buffer_tring_buffer;
} tiny_queue_t;
typedefstruct
{
unsignedoffset;
constuint8_t*buffer;
unsignedbuffer_capacity;
} tiny_queue_raw_element_iterator_t;
/*!
* Initialize a queue with the provided buffer.
*/
voidtiny_queue_init(
tiny_queue_t*self,
void*buffer,
unsignedbuffer_size);
/*!
* Add an element to the queue. Returns true if the element was successfully enqueued, false otherwise
*/
booltiny_queue_enqueue(tiny_queue_t*self, constvoid*element, uint16_tsize);
/*!
* Remove an element from the queue and copy it into the provided element buffer.
* @pre The queue is not empty
*/
voidtiny_queue_dequeue(tiny_queue_t*self, void*element, uint16_t*size);
/*!
* Similar to Dequeue but the element is not retrieved.
* @pre The queue is not empty
*/
voidtiny_queue_discard(tiny_queue_t*self);
/*!
* Peeks an element from the queue without removing it and copies it into the provided element buffer.
* @pre 0 <= index < The count of elements in the queue
*/
voidtiny_queue_peek(tiny_queue_t*self, void*element, uint16_t*size, uint16_tindex);
/*!
* Peek part of an element. Only copies out up to the specified number of bytes.
* @pre 0 <= index < The count of elements in the queue
*/
voidtiny_queue_peek_partial(tiny_queue_t*self, void*element, uint16_tsize, uint16_toffset, uint16_telement_index);
/*!
* Peek part of an element. Only copies out up to the specified number of bytes.
* @pre 0 <= index < The count of elements in the queue
*/
voidtiny_queue_peek_size(tiny_queue_t*self, uint16_t*size, uint16_tindex);
/*!
* Get the number of elements in the queue.
*/
uint16_ttiny_queue_count(tiny_queue_t*self);
/*!
* Get an iterator over the raw bytes of an element. This allows for optimized access to
* an element that cannot be read out of the queue in its entirety.
*/
tiny_queue_raw_element_iterator_ttiny_queue_raw_element_iterator(
tiny_queue_t*instance,
uint16_tindex);
/*!
* View the next byte in the element without advancing the iterator.
*/
staticinlineuint8_ttiny_queue_raw_element_iterator_peek(
tiny_queue_raw_element_iterator_t*instance)
{
returninstance->buffer[instance->offset];
}
/*!
* @warning This does no bounds checking. The client must ensure that the iterator is not
* advanced past the end of the buffer.
*/
staticinlinevoidtiny_queue_raw_element_iterator_advance(
tiny_queue_raw_element_iterator_t*instance)
{
instance->offset++;
if(instance->offset >= instance->buffer_capacity) {
instance->offset=0;
}
}
#endif