-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
462 lines (373 loc) · 9.17 KB
/
buffer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 - 2022 TheVice
*
*/
#include "stdc_secure_api.h"
#include "buffer.h"
#if !defined(_WIN32) && defined(NDEBUG) && defined(__ALPINE_PREVENT_ILLEGAL_INSTRUCTION__)
#include "common.h"
#endif
#include <stdlib.h>
#include <string.h>
struct buffer
{
void* data;
ptrdiff_t size;
ptrdiff_t capacity;
};
static struct buffer pool;
static uint8_t is_pool_init = 0;
#if defined(_WIN64) || defined(__amd64) || defined(__x86_64)
static const ptrdiff_t maximum_capacity = (ptrdiff_t)(INT32_MAX) + 1;
#else
static const ptrdiff_t maximum_capacity = 1073741824;
#endif
ptrdiff_t buffer_calculate_capacity(const ptrdiff_t size)
{
ptrdiff_t capacity = 2;
while (capacity < size && capacity < maximum_capacity)
{
capacity = capacity << 1;
}
return capacity < maximum_capacity ? capacity : maximum_capacity;
}
struct buffer* buffer_to_real_buffer(const void* the_buffer)
{
void* real_buffer = NULL;
ptrdiff_t i = 0;
if (NULL == the_buffer)
{
return real_buffer;
}
while (NULL != (real_buffer = buffer_buffer_data(&pool, i++)))
{
if (the_buffer == real_buffer)
{
break;
}
}
return (struct buffer*)real_buffer;
}
uint8_t buffer_size_of()
{
return (uint8_t)sizeof(struct buffer);
}
uint8_t buffer_init(void* the_buffer, uint8_t size_of_buffer)
{
if (NULL == the_buffer || size_of_buffer < sizeof(struct buffer))
{
return 0;
}
memset(the_buffer, 0, sizeof(struct buffer));
return 1;
}
ptrdiff_t buffer_size(const void* the_buffer)
{
return NULL == the_buffer ? 0 : ((const struct buffer*)the_buffer)->size;
}
uint8_t buffer_resize(void* the_buffer_, ptrdiff_t size)
{
struct buffer* the_buffer = (struct buffer*)the_buffer_;
if (NULL == the_buffer || size < 0 || the_buffer->size < 0)
{
return 0;
}
if (the_buffer->capacity < size)
{
const ptrdiff_t capacity = buffer_calculate_capacity(size);
if (capacity < size)
{
return 0;
}
if (NULL != the_buffer->data)
{
free(the_buffer->data);
the_buffer->data = NULL;
the_buffer->size = 0;
the_buffer->capacity = 0;
}
the_buffer->data = malloc(capacity);
if (NULL == the_buffer->data)
{
return 0;
}
the_buffer->capacity = capacity;
}
the_buffer->size = size;
return 1;
}
void buffer_release(void* the_buffer_)
{
struct buffer* the_buffer = (struct buffer*)the_buffer_;
if (NULL == the_buffer)
{
return;
}
if (NULL != the_buffer->data)
{
free(the_buffer->data);
the_buffer->data = NULL;
}
the_buffer->size = 0;
the_buffer->capacity = 0;
}
void buffer_release_inner_buffers(void* the_buffer)
{
ptrdiff_t i = 0;
void* inner_buffer = NULL;
while (NULL != (inner_buffer = buffer_buffer_data((struct buffer*)the_buffer, i++)))
{
buffer_release(inner_buffer);
}
}
void buffer_release_with_inner_buffers(void* the_buffer)
{
buffer_release_inner_buffers(the_buffer);
buffer_release(the_buffer);
}
uint8_t buffer_append(void* the_buffer_, const void* data, ptrdiff_t size)
{
struct buffer* the_buffer = (struct buffer*)the_buffer_;
if (NULL == the_buffer || size < 0 || the_buffer->size < 0)
{
return 0;
}
if (!size)
{
return 1;
}
if (the_buffer->capacity - the_buffer->size < size)
{
if ((maximum_capacity - the_buffer->capacity) < size)
{
return 0;
}
const ptrdiff_t new_size = the_buffer->capacity + size;
const ptrdiff_t capacity = buffer_calculate_capacity(new_size);
if (capacity < new_size)
{
return 0;
}
void* new_data = malloc(capacity);
if (NULL == new_data)
{
return 0;
}
if (0 < the_buffer->size && NULL != the_buffer->data)
{
#if __STDC_LIB_EXT1__
if (0 != memcpy_s(new_data, capacity, the_buffer->data, the_buffer->size))
{
free(new_data);
new_data = NULL;
return 0;
}
#else
memcpy(new_data, the_buffer->data, the_buffer->size);
#endif
}
if (NULL != the_buffer->data)
{
free(the_buffer->data);
}
the_buffer->data = new_data;
the_buffer->capacity = capacity;
}
if (NULL != data)
{
#if !defined(_WIN32) && defined(NDEBUG) && defined(__ALPINE_PREVENT_ILLEGAL_INSTRUCTION__)
uint8_t* dst = (uint8_t*)the_buffer->data + the_buffer->size;
MEM_CPY(dst, data, size);
#else
#if __STDC_LIB_EXT1__
if (0 != memcpy_s((uint8_t*)the_buffer->data + the_buffer->size,
the_buffer->capacity - the_buffer->size, data, size))
{
return 0;
}
#else
memcpy((uint8_t*)the_buffer->data + the_buffer->size, data, size);
#endif
#endif
}
the_buffer->size += size;
return 1;
}
uint8_t buffer_append_char(void* the_buffer, const char* data, ptrdiff_t data_count)
{
return buffer_append(the_buffer, (const void*)data, sizeof(char) * data_count);
}
uint8_t buffer_append_wchar_t(void* the_buffer, const wchar_t* data, ptrdiff_t data_count)
{
return buffer_append(the_buffer, (const void*)data, sizeof(wchar_t) * data_count);
}
uint8_t buffer_append_buffer(void* the_buffer, const void* data, ptrdiff_t data_count)
{
return buffer_append(the_buffer, (const void*)data, sizeof(struct buffer) * data_count);
}
uint8_t buffer_append_data_from_buffer(void* the_buffer, const void* the_source_buffer)
{
struct buffer* the_source_buffer_ = (struct buffer*)the_source_buffer;
return NULL == the_source_buffer_ ?
0 : buffer_append(the_buffer, the_source_buffer_->data, the_source_buffer_->size);
}
void* buffer_data(const void* the_buffer_, ptrdiff_t index)
{
struct buffer* the_buffer = (struct buffer*)the_buffer_;
if (NULL == the_buffer ||
NULL == the_buffer->data ||
index < 0 ||
the_buffer->size <= index)
{
return NULL;
}
return (void*)((uint8_t*)the_buffer->data + index);
}
char* buffer_char_data(const void* the_buffer, ptrdiff_t data_position)
{
return (char*)buffer_data(the_buffer, sizeof(char) * data_position);
}
uint8_t* buffer_uint8_t_data(const void* the_buffer, ptrdiff_t data_position)
{
return (uint8_t*)buffer_data(the_buffer, sizeof(uint8_t) * data_position);
}
wchar_t* buffer_wchar_t_data(const void* the_buffer, ptrdiff_t data_position)
{
return (wchar_t*)buffer_data(the_buffer, sizeof(wchar_t) * data_position);
}
uint16_t* buffer_uint16_t_data(const void* the_buffer, ptrdiff_t data_position)
{
return (uint16_t*)buffer_data(the_buffer, sizeof(uint16_t) * data_position);
}
uint32_t* buffer_uint32_t_data(const void* the_buffer, ptrdiff_t data_position)
{
return (uint32_t*)buffer_data(the_buffer, sizeof(uint32_t) * data_position);
}
void* buffer_buffer_data(const void* the_buffer, ptrdiff_t data_position)
{
return buffer_data(the_buffer, sizeof(struct buffer) * data_position);
}
uint8_t buffer_push_back(void* the_buffer, uint8_t data)
{
return buffer_append(the_buffer, &data, 1);
}
uint8_t buffer_push_back_uint16_t(void* the_buffer, uint16_t data)
{
return buffer_append(the_buffer, (const void*)&data, sizeof(uint16_t));
}
uint8_t buffer_push_back_uint32_t(void* the_buffer, uint32_t data)
{
return buffer_append(the_buffer, (const void*)&data, sizeof(uint32_t));
}
uint8_t buffer_shrink_to_fit(void* the_buffer_)
{
struct buffer* the_buffer = (struct buffer*)the_buffer_;
if (NULL == the_buffer || the_buffer->size < 0 || the_buffer->capacity < 512)
{
return 0;
}
const ptrdiff_t capacity = buffer_calculate_capacity(the_buffer->size);
if (capacity < the_buffer->capacity)
{
void* new_data = realloc(the_buffer->data, capacity);
if (NULL == new_data)
{
return 0;
}
free(the_buffer->data);
the_buffer->data = new_data;
the_buffer->capacity = capacity;
return 1;
}
return 0;
}
uint8_t buffer_init_pool(void** the_buffer)
{
static const ptrdiff_t pool_limit = (ptrdiff_t)(sizeof(struct buffer) * UINT8_MAX);
struct buffer* current_buffer = NULL;
struct buffer* candidate_buffer = NULL;
ptrdiff_t i = 0;
ptrdiff_t capacity = 0;
if (NULL == the_buffer)
{
return 0;
}
if (!is_pool_init)
{
if (!buffer_init(&pool, sizeof(struct buffer)) ||
!buffer_append(&pool, NULL, pool_limit) ||
!buffer_resize(&pool, 0))
{
buffer_release(&pool);
return 0;
}
is_pool_init = 1;
}
while (NULL != (current_buffer = buffer_buffer_data(&pool, i++)))
{
if (buffer_size(current_buffer) < 0 && capacity < current_buffer->capacity)
{
capacity = current_buffer->capacity;
candidate_buffer = current_buffer;
}
}
if (NULL != candidate_buffer)
{
candidate_buffer->size = 0;
*the_buffer = candidate_buffer;
}
else if (buffer_size(&pool) < pool_limit)
{
struct buffer new_buffer;
if (!buffer_init(&new_buffer, sizeof(struct buffer)) ||
!buffer_append_buffer(&pool, &new_buffer, 1))
{
return 0;
}
else
{
*the_buffer = buffer_buffer_data(&pool, i - 1);
}
}
else
{
return 0;
}
return NULL != *the_buffer;
}
uint8_t buffer_return_to_pool(void* the_buffer)
{
struct buffer* real_buffer = buffer_to_real_buffer(the_buffer);
if (NULL == real_buffer)
{
return 0;
}
real_buffer->size = -1;
return 1;
}
uint8_t buffer_release_pool()
{
if (is_pool_init)
{
void* current_buffer = NULL;
ptrdiff_t i = 0;
uint8_t pool_is_free = 1;
while (NULL != (current_buffer = buffer_buffer_data(&pool, i++)))
{
if (pool_is_free && !(buffer_size(current_buffer) < 0))
{
pool_is_free = 0;
}
buffer_release(current_buffer);
}
buffer_release(&pool);
return pool_is_free;
}
else
{
is_pool_init = buffer_init(&pool, sizeof(struct buffer));
}
return is_pool_init;
}