VLCB SDK
An opinionated SDK for VLCB protocol
Loading...
Searching...
No Matches
packet_buf.c
Go to the documentation of this file.
2
3#include <assert.h>
4#include <stdbool.h>
5#include <string.h>
6
7void vlcb_net_packetbuf_Init(VlcbPacketBuf *const c, size_t maxlen,
8 size_t bucket_size) {
9 assert(c != NULL && maxlen > 0 && bucket_size > 0);
10 c->tail = 0;
11 c->head = 0;
12 c->bucket_size = bucket_size;
13 c->maxlen = maxlen;
14}
15
17 assert(c != NULL);
18
19 c->head = 0;
20 c->tail = 0;
21}
22
24 assert(c != NULL);
25
26 return c->head == c->tail;
27}
28
30 assert(c != NULL);
31
32 size_t head = c->head + 1;
33 if (head == c->maxlen) {
34 head = 0;
35 }
36
37 return head == c->tail;
38}
39
41 assert(c != NULL);
42
43 size_t size = c->maxlen;
44
46 if (c->head >= c->tail) {
47 size = c->head - c->tail;
48 } else {
49 size = c->maxlen + c->head - c->tail;
50 }
51 }
52
53 return size;
54}
55
57 assert(c != NULL);
58
59 return c->bucket_size;
60}
61
62int vlcb_net_packetbuf_Push(VlcbPacketBuf *const c, const void *data) {
63 assert(c != NULL && data != NULL);
64
65 int next;
66
67 next = c->head + 1;
68
69 if (next >= c->maxlen) {
70 next = 0;
71 }
72
73 if (next == c->tail) {
74 return -1;
75 }
76
77 memcpy(&c->buffer[c->head], data, c->bucket_size);
78 c->head = next;
79 return 0;
80}
81
82int vlcb_net_packetbuf_Pop(VlcbPacketBuf *const c, void *data) {
83 assert(c != NULL && data != NULL);
84 int next;
85
87 return -1;
88 }
89
90 next = c->tail + 1;
91 if (next >= c->maxlen) {
92 next = 0;
93 }
94
95 memcpy(data, &c->buffer[c->tail], c->bucket_size);
96 c->tail = next;
97 return 0;
98}
int vlcb_net_packetbuf_Pop(VlcbPacketBuf *const c, void *data)
Definition packet_buf.c:82
bool vlcb_net_packetbuf_IsEmpty(VlcbPacketBuf *const c)
Definition packet_buf.c:23
size_t vlcb_net_packetbuf_Capacity(VlcbPacketBuf *const c)
Definition packet_buf.c:40
bool vlcb_net_packetbuf_IsFull(VlcbPacketBuf *const c)
Definition packet_buf.c:29
size_t vlcb_net_packetbuf_BucketSize(VlcbPacketBuf *const c)
Definition packet_buf.c:56
void vlcb_net_packetbuf_Init(VlcbPacketBuf *const c, size_t maxlen, size_t bucket_size)
Definition packet_buf.c:7
void vlcb_net_packetbuf_Reset(VlcbPacketBuf *const c)
Definition packet_buf.c:16
int vlcb_net_packetbuf_Push(VlcbPacketBuf *const c, const void *data)
Definition packet_buf.c:62
size_t tail
Definition packet_buf.h:9
uint8_t buffer[]
Definition packet_buf.h:12
size_t maxlen
Definition packet_buf.h:10
size_t head
Definition packet_buf.h:8
size_t bucket_size
Definition packet_buf.h:11