-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_buffer.h
More file actions
237 lines (206 loc) · 6.89 KB
/
c_buffer.h
File metadata and controls
237 lines (206 loc) · 6.89 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @file: c_buffer.h
* @author: Lucas Wennerholm <lucas.wennerholm@gmail.com>
* @brief: Header file for a circular buffer
*
* @license: MIT License
*
* Copyright (c) 2024 Lucas Wennerholm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef C_BUFFER_H
#define C_BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
// The available number of bytes in the C buffer will be one less than the size of the array
#define C_BUFFER_ARRAY_OVERHEAD 1
#ifndef C_BUFFER_FAST_UNSAFE
#define C_BUFFER_FAST_UNSAFE (1)
#endif /* C_BUFFER_FAST_UNSAFE */
/**
* This module manages connections data streams.
*/
typedef enum {
C_BUFFER_WRAPED = 1,
C_BUFFER_SUCCESS = 0,
C_BUFFER_NULL_ERROR = -301,
C_BUFFER_INSUFFICIENT = -302,
C_BUFFER_MISMATCH = -303,
} cBufferErr_t;
typedef struct {
uint8_t *data;
size_t size;
uint32_t head;
uint32_t tail;
} cBuffer_t;
/**
* Initialize the buffer
* Note: The available size in the buffer will be one less than input array
* Input: Pointer to buffer instance
* Input: Pointer to data array
* Input: Size of the data array
* Returns: cBufferErr_t
*/
int32_t cBufferInit(cBuffer_t *inst, uint8_t *buffer, size_t buffer_size);
/**
* Check if the buffer is empty
* Input: Pointer to buffer instance
* Returns: cBufferErr_t or 1 if empty
*/
int32_t cBufferEmpty(cBuffer_t *inst);
/**
* Check if the buffer is full
* Input: Pointer to buffer instance
* Returns: cBufferErr_t or 1 if full
*/
int32_t cBufferFull(cBuffer_t *inst);
/**
* Get number of bytes available for read
* Input: Pointer to buffer instance
* Returns: Number of bytes available or cBufferErr_t
*/
int32_t cBufferAvailableForRead(cBuffer_t* inst);
/**
* Get number of bytes available for write
* Input: Pointer to buffer instance
* Returns: Number of bytes available or cBufferErr_t
*/
int32_t cBufferAvailableForWrite(cBuffer_t* inst);
/**
* Write the new data at the start of the buffer
* Input: Pointer to buffer instance
* Input: Pointer to data to write
* Input: Size of data to write
* Returns: cBufferErr_t or num bytes written
*/
int32_t cBufferPrepend(cBuffer_t *inst, uint8_t *data, size_t data_size);
/**
* Write a uint32 at the start of the buffer in big endian format
* Input: Pointer to buffer instance
* Input: Data to write
* Returns: cBufferErr_t or num bytes written
*/
int32_t cBufferPrependUint32(cBuffer_t *inst, uint32_t data);
/**
* Write a uint16 at the start of the buffer in big endian format
* Input: Pointer to buffer instance
* Input: Data to write
* Returns: cBufferErr_t or num bytes written
*/
int32_t cBufferPrependUint16(cBuffer_t *inst, uint16_t data);
/**
* Write a single byte at the start of the buffer
* Input: Pointer to buffer instance
* Input: Byte to write
* Returns: cBufferErr_t or 1 if one byte was written
*/
int32_t cBufferPrependByte(cBuffer_t *inst, uint8_t data);
/**
* Write the new data at the end of the buffer
* Input: Pointer to buffer instance
* Input: Pointer to data to write
* Input: Size of data to write
* Returns: cBufferErr_t or 1 if full
*/
int32_t cBufferAppend(cBuffer_t *inst, uint8_t *data, size_t data_size);
/**
* Write the new data at the end of the buffer
* Input: Pointer to buffer instance
* Input: Byte to write
* Returns: cBufferErr_t or 1 if one byte was written
*/
int32_t cBufferAppendByte(cBuffer_t *inst, uint8_t data);
/**
* Read data from the buffer
* Input: Pointer to buffer instance
* Input: Pointer to data to read into
* Input: Maximum amount amount of data that can be read
* Returns: cBufferErr_t or num of bytes read
*/
int32_t cBufferReadAll(cBuffer_t *inst, uint8_t *data, size_t max_read_size);
/**
* Read the next byte from the buffer
* Note: Using this function on emtpy buffers will return 0
* Input: Pointer to buffer instance
* Returns: the next data in the buffer
*/
uint8_t cBufferReadByte(cBuffer_t *inst);
/**
* Read the next byte from the buffer
* Note: Using this function on emtpy buffers will return 0
* Input: Pointer to buffer instance
* Input: Pointer to data to read into
* Input: Number of bytes to read
* Returns: cBufferErr_t or num of bytes read
*/
int32_t cBufferReadBytes(cBuffer_t *inst, uint8_t *data, size_t read_size);
/**
* Clear a buffer, this resets the head and tail to first element of buffer
* Input: Pointer to buffer instance
* Returns: cBufferErr_t
*/
int32_t cBufferClear(cBuffer_t *inst);
/**
* Rotate the buffer to make sure the data is available in continous memory
* Input: Pointer to buffer instance
* Returns: cBufferErr_t
*/
int32_t cBufferContiguate(cBuffer_t* inst);
/**
* Check if the data is available in continous memory
* Input: Pointer to buffer instance
* Returns: cBufferErr_t, SUCCESS indicates that the data is contigous
*/
int32_t cBufferIsContigous(cBuffer_t* inst);
/**
* Get pointer to the current first element in the buffer
* Input: Pointer to buffer instance
* Returns: Pointer to element or null if invalid or wrap in buffer
*/
uint8_t *cBufferGetReadPointer(cBuffer_t* inst);
/**
* Get pointer to the current next write element in the buffer
* Note: There is no garantuee that the data is continous
* Input: Pointer to buffer instance
* Returns: Pointer to element
*/
uint8_t *cBufferGetWritePointer(cBuffer_t* inst);
/**
* Increment the amount of data in the buffer without writing anything to the buffer
* Note: There is no garantuee that the data is continous
* Input: Pointer to buffer instance
* Input: Number of bytes to append to head
* Returns: cBufferErr_t
*/
int32_t cBufferEmptyWrite(cBuffer_t* inst, size_t num_bytes);
/**
* Decrement the amount of data in the buffer without copying any data
* Input: Pointer to buffer instance
* Input: Number of bytes to remove from to tail
* Returns: cBufferErr_t
*/
int32_t cBufferEmptyRead(cBuffer_t* inst, size_t num_bytes);
#ifdef __cplusplus
}
#endif
#endif /* C_BUFFER_H */