-
Notifications
You must be signed in to change notification settings - Fork 1
/
nw_buf.c
260 lines (228 loc) · 5.92 KB
/
nw_buf.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Description: network buf manager
* History: [email protected], 2016/03/16, create
*/
# include <errno.h>
# include <string.h>
# include "nw_buf.h"
# define NW_BUF_POOL_INIT_SIZE 64
# define NW_BUF_POOL_MAX_SIZE 65535
# define NW_CACHE_INIT_SIZE 64
# define NW_CACHE_MAX_SIZE 65535
size_t nw_buf_size(nw_buf *buf)
{
return buf->wpos - buf->rpos;
}
size_t nw_buf_avail(nw_buf *buf)
{
return buf->size - buf->wpos;
}
size_t nw_buf_write(nw_buf *buf, const void *data, size_t len)
{
size_t available = buf->size - buf->wpos;
size_t wlen = len > available ? available : len;
memcpy(buf->data + buf->wpos, data, wlen);
buf->wpos += wlen;
return wlen;
}
void nw_buf_shift(nw_buf *buf)
{
if (buf->rpos == buf->wpos) {
buf->rpos = buf->wpos = 0;
} else if (buf->rpos != 0) {
memmove(buf->data, buf->data + buf->rpos, buf->wpos - buf->rpos);
buf->wpos -= buf->rpos;
buf->rpos = 0;
}
}
nw_buf_pool *nw_buf_pool_create(uint32_t size)
{
nw_buf_pool *pool = malloc(sizeof(nw_buf_pool));
if (pool == NULL)
return NULL;
pool->size = size;
pool->used = 0;
pool->free = 0;
pool->free_total = NW_BUF_POOL_INIT_SIZE;
pool->free_arr = malloc(pool->free_total * sizeof(nw_buf *));
if (pool->free_arr == NULL) {
free(pool);
return NULL;
}
return pool;
}
nw_buf *nw_buf_alloc(nw_buf_pool *pool)
{
if (pool->free) {
nw_buf *buf = pool->free_arr[--pool->free];
buf->size = pool->size;
buf->rpos = 0;
buf->wpos = 0;
buf->next = NULL;
return buf;
}
nw_buf *buf = malloc(sizeof(nw_buf) + pool->size);
if (buf == NULL)
return NULL;
buf->size = pool->size;
buf->rpos = 0;
buf->wpos = 0;
buf->next = NULL;
return buf;
}
void nw_buf_free(nw_buf_pool *pool, nw_buf *buf)
{
if (pool->free < pool->free_total) {
pool->free_arr[pool->free++] = buf;
} else if (pool->free_total < NW_BUF_POOL_MAX_SIZE) {
uint32_t new_free_total = pool->free_total * 2;
void *new_arr = realloc(pool->free_arr, new_free_total * sizeof(nw_buf *));
if (new_arr) {
pool->free_total = new_free_total;
pool->free_arr = new_arr;
pool->free_arr[pool->free++] = buf;
} else {
free(buf);
}
} else {
free(buf);
}
}
void nw_buf_pool_release(nw_buf_pool *pool)
{
for (uint32_t i = 0; i < pool->free; ++i) {
free(pool->free_arr[i]);
}
free(pool->free_arr);
free(pool);
}
nw_buf_list *nw_buf_list_create(nw_buf_pool *pool, uint32_t limit)
{
nw_buf_list *list = malloc(sizeof(nw_buf_list));
if (list == NULL)
return NULL;
list->pool = pool;
list->count = 0;
list->limit = limit;
list->head = NULL;
list->tail = NULL;
return list;
}
size_t nw_buf_list_write(nw_buf_list *list, const void *data, size_t len)
{
const void *pos = data;
size_t left = len;
if (list->tail && nw_buf_avail(list->tail)) {
size_t ret = nw_buf_write(list->tail, pos, left);
left -= ret;
pos += ret;
}
while (left) {
if (list->limit && list->count >= list->limit)
return len - left;
nw_buf *buf = nw_buf_alloc(list->pool);
if (buf == NULL)
return len - left;
if (list->head == NULL)
list->head = buf;
if (list->tail != NULL)
list->tail->next = buf;
list->tail = buf;
list->count++;
size_t ret = nw_buf_write(list->tail, pos, left);
left -= ret;
pos += ret;
}
return len;
}
size_t nw_buf_list_append(nw_buf_list *list, const void *data, size_t len)
{
if (list->limit && list->count >= list->limit)
return 0;
nw_buf *buf = nw_buf_alloc(list->pool);
if (buf == NULL)
return 0;
if (len > buf->size) {
nw_buf_free(list->pool, buf);
return 0;
}
nw_buf_write(buf, data, len);
if (list->head == NULL)
list->head = buf;
if (list->tail != NULL)
list->tail->next = buf;
list->tail = buf;
list->count++;
return len;
}
void nw_buf_list_shift(nw_buf_list *list)
{
if (list->head) {
nw_buf *tmp = list->head;
list->head = tmp->next;
if (list->head == NULL) {
list->tail = NULL;
}
list->count--;
nw_buf_free(list->pool, tmp);
}
}
void nw_buf_list_release(nw_buf_list *list)
{
nw_buf *curr = list->head;
nw_buf *next = NULL;
while (curr) {
next = curr->next;
nw_buf_free(list->pool, curr);
curr = next;
}
free(list);
}
nw_cache *nw_cache_create(uint32_t size)
{
nw_cache *cache = malloc(sizeof(nw_cache));
if (cache == NULL)
return NULL;
cache->size = size;
cache->used = 0;
cache->free = 0;
cache->free_total = NW_CACHE_INIT_SIZE;
cache->free_arr = malloc(cache->free_total * sizeof(void *));
if (cache->free_arr == NULL) {
free(cache);
return NULL;
}
return cache;
}
void *nw_cache_alloc(nw_cache *cache)
{
if (cache->free)
return cache->free_arr[--cache->free];
return malloc(cache->size);
}
void nw_cache_free(nw_cache *cache, void *obj)
{
if (cache->free < cache->free_total) {
cache->free_arr[cache->free++] = obj;
} else if (cache->free_total < NW_CACHE_MAX_SIZE) {
uint32_t new_free_total = cache->free_total * 2;
void *new_arr = realloc(cache->free_arr, new_free_total * sizeof(void *));
if (new_arr) {
cache->free_total = new_free_total;
cache->free_arr = new_arr;
cache->free_arr[cache->free++] = obj;
} else {
free(obj);
}
} else {
free(obj);
}
}
void nw_cache_release(nw_cache *cache)
{
for (uint32_t i = 0; i < cache->free; ++i) {
free(cache->free_arr[i]);
}
free(cache->free_arr);
free(cache);
}