-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabuffer.h
155 lines (128 loc) · 3.85 KB
/
zabuffer.h
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
#ifndef ZA_BUFFER_H__
#define ZA_BUFFER_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define ZA_BUFFER_DECL_ARRAY(name, elements, size) \
static uint8_t name##_data[elements][size]; \
static struct za_buffer name[elements];
#define ZA_BUFFER_INIT_ARRAY(name, elements, size) \
za_buffer_init_array(name, name##_data, elements, size)
#define ZA_BUFFER_DECL(name, size) \
static uint8_t name##_data[size]; \
static struct za_buffer name = { name##_data, name##_data, size, 0, false };
/**
* Create buffers with list
* - data: NAME_data[LIST_SIZE][DATA_SIZE]
* - buffer: NAME_buffer
* - list: NAME
*/
#define ZA_BUFFER_LIST_DECL(name, list_size, data_size) \
static uint8_t name##_data[list_size][data_size]; \
static struct za_buffer name##_buffer[list_size]; \
static struct za_buffer_list name
#define ZA_BUFFER_LIST_INIT(name, list_size, data_size) \
za_buffer_list_init(&name, list_size, name##_buffer, &name##_data, data_size)
/**
* Buffer
*/
struct za_buffer {
uint8_t *data;
uint8_t *cur;
size_t size;
size_t used;
bool in_use;
};
/**
* List of za_buffer with locking
*/
struct za_buffer_list {
struct za_buffer *buffer;
size_t size;
};
/**
* Initialize buffer, it will not flush the buffer. Please use za_buffer_flush.
*/
void za_buffer_init(struct za_buffer *b, void *data, size_t size);
void za_buffer_init_array(struct za_buffer *a, void *data, size_t elements, size_t size);
void za_buffer_destroy(struct za_buffer *b);
bool za_buffer_is_full(const struct za_buffer *b);
/**
* Reset buffer to begin
* * Set cur member
* * Reset used member
*/
void za_buffer_reset(struct za_buffer *b);
/**
* Flush buffer data to zero and reset
*/
void za_buffer_flush(struct za_buffer *b);
/**
* Append byte to buffer
*/
bool za_buffer_write_u8(struct za_buffer *b, uint8_t c);
/**
* Append buffer to buffer
*/
size_t za_buffer_write_data(struct za_buffer *b, const void *buf, size_t len);
/**
* Set cursor to next byte
*/
bool za_buffer_next(struct za_buffer *b);
/**
* Get size in used bytes
*/
size_t za_buffer_size_inuse(const struct za_buffer *b);
/**
* Get amount of free bytes
*/
size_t za_buffer_size_free(const struct za_buffer *b);
/**
* Memory copy buf with n size into b->data + offset, when
* it won't fit then no bytes are written because memcpy truncating
* bytes is not normal behavior
* @return Size of written bytes (0 if it exceeds b->data + b->size)
*/
size_t za_buffer_memcpy(const struct za_buffer *b, size_t offset, const void *buf, size_t n);
/**
* Search for pattern in memory and return at occurrence
* @param l Memory block
* @param l_len Memory block length
* @param s Pattern to search for
* @param s_len Pattern length
*/
const void *za_buffer_memmem(const void *l, size_t l_len, const void *s, size_t s_len);
/**
* Append buffer to another
* Deep copies src->used bytes starting from offset 0 into dst starting from current offset
* the data must fit else no copy will occur
* @param dst Destination buffer
* @param src Source buffer
*/
int za_buffer_append(struct za_buffer *dst, struct za_buffer *src);
/**
* Initialize list of buffers<br>
* Example:
* - LIST_SIZE: 10
* - OBJ_SIZE: 256
* - l: za_buffer list[LIST_SIZE]
* - l_n: LIST_SIZE
* - data: uint8_t list_data[LIST_SIZE][OBJ_SIZE]
* - data_n: OBJ_SIZE
* @param list List of buffer objects
* @param list_n Amount of items in list
* @param buffer Buffer
* @param data Data block, MUST have a size of l_n * data_n
* @param data_n Data block object size
*/
void za_buffer_list_init(struct za_buffer_list *list, size_t list_n, struct za_buffer *buffer,
void *data, size_t data_n);
struct za_buffer *za_buffer_list_get(struct za_buffer_list *list);
void za_buffer_list_return(struct za_buffer_list *list, struct za_buffer **b);
#ifdef __cplusplus
}
#endif
#endif /** ZA_BUFFER_H__ */