-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresizablebuffer.c
More file actions
374 lines (313 loc) · 10.9 KB
/
resizablebuffer.c
File metadata and controls
374 lines (313 loc) · 10.9 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
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
/**
* MIT License
*
* Copyright (c) 2023 Alex Chen
*
* 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.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "resizablebuffer.h"
#include "bufferqueue.h"
/* default block size of the resizable buffer. */
#define RBUF_DEF_BLOCK_SIZE 512
/* default maximum size of the resizable buffer. */
#define RBUF_DEF_SIZE_MAX 1024
/* context of the resizable buffer. */
struct _rbuf_ctx {
bque_ctx *bque;
struct _rbuf_ctx_conf {
/* block size of the resizable buffer. */
rbuf_u32 block_size;
/* maximum size of the resizable buffer,
when it is 0, it means no limit. */
rbuf_u32 size_max;
} conf;
struct _rbuf_ctx_cache {
rbuf_u32 block_num;
rbuf_u32 buff_cap;
rbuf_u32 buff_size;
rbuf_u32 last_block_buff_size;
} cache;
};
/**
* @brief create a resizable buffer.
*
* @param ctx the address of the context pointer.
* @param conf configuration pointer.
*/
rbuf_res rbuf_new(rbuf_ctx **ctx, rbuf_conf *conf) {
rbuf_ctx *alloc_ctx;
bque_ctx *mod_bque_ctx;
bque_conf mod_bque_conf;
bque_res mod_bque_res;
RBUF_ASSERT(ctx != NULL);
mod_bque_conf.buff_num_max = 0;
mod_bque_conf.buff_size_max = 0;
mod_bque_res = bque_new(&mod_bque_ctx, &mod_bque_conf);
if (mod_bque_res != BQUE_OK) {
if (mod_bque_res == BQUE_ERR_NO_MEM) {
return RBUF_ERR_NO_MEM;
} else {
return RBUF_ERR;
}
}
alloc_ctx = (rbuf_ctx *)malloc(sizeof(rbuf_ctx));
if (alloc_ctx == NULL) {
bque_del(mod_bque_ctx);
return RBUF_ERR_NO_MEM;
}
memset(alloc_ctx, 0, sizeof(rbuf_ctx));
alloc_ctx->bque = mod_bque_ctx;
if (conf != NULL) {
alloc_ctx->conf.block_size = conf->block_size;
alloc_ctx->conf.size_max = conf->size_max;
} else {
alloc_ctx->conf.block_size = RBUF_DEF_BLOCK_SIZE;
alloc_ctx->conf.size_max = RBUF_DEF_SIZE_MAX;
}
*ctx = alloc_ctx;
return RBUF_OK;
}
/**
* @brief delete the resizable buffer.
*
* @param ctx context pointer.
*/
rbuf_res rbuf_del(rbuf_ctx *ctx) {
RBUF_ASSERT(ctx != NULL);
bque_del(ctx->bque);
free(ctx);
return RBUF_OK;
}
/**
* @brief get the status of the resizable buffer.
*
* @param ctx context pointer.
* @param stat status pointer.
*/
rbuf_res rbuf_status(rbuf_ctx *ctx, rbuf_stat *stat) {
RBUF_ASSERT(ctx != NULL);
RBUF_ASSERT(stat != NULL);
stat->block_num = ctx->cache.block_num;
stat->buff_size = ctx->cache.buff_size;
return RBUF_OK;
}
/**
* @brief resize the buffer size of the resizable buffer.
*
* @param ctx context pointer.
* @param size the new size.
*/
rbuf_res rbuf_resize(rbuf_ctx *ctx, rbuf_u32 size) {
rbuf_u32 new_block_num;
rbuf_u32 block_num_diff;
rbuf_u32 new_block_num_quotient;
rbuf_u32 new_block_num_remainder;
bque_res mod_bque_res;
RBUF_ASSERT(ctx != NULL);
/* check whether the size is too large. */
if (ctx->conf.size_max != 0 &&
size > ctx->conf.size_max) {
return RBUF_ERR_BAD_SIZE;
}
new_block_num_quotient = size / ctx->conf.block_size;
new_block_num_remainder = size % ctx->conf.block_size;
new_block_num = new_block_num_quotient + ((new_block_num_remainder != 0) ? 1 : 0);
if (new_block_num > ctx->cache.block_num) {
block_num_diff = new_block_num - ctx->cache.block_num;
for (rbuf_u32 i = 0; i < block_num_diff; i++) {
mod_bque_res = bque_enqueue(ctx->bque, NULL, ctx->conf.block_size);
if (mod_bque_res != BQUE_OK) {
if (mod_bque_res == BQUE_ERR_NO_MEM) {
return RBUF_ERR_NO_MEM;
} else {
return RBUF_ERR;
}
}
}
ctx->cache.block_num = new_block_num;
ctx->cache.buff_cap = ctx->conf.block_size * new_block_num;
} else if (new_block_num < ctx->cache.block_num) {
block_num_diff = ctx->cache.block_num - new_block_num;
for (rbuf_u32 i = 0; i < block_num_diff; i++) {
mod_bque_res = bque_forfeit(ctx->bque, NULL, NULL);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
}
ctx->cache.block_num = new_block_num;
ctx->cache.buff_cap = ctx->conf.block_size * new_block_num;
}
/* update the buffer size of the whole resizable buffer. */
ctx->cache.buff_size = size;
/* update the buffer size of the last block. */
if (new_block_num_quotient != 0 &&
new_block_num_remainder == 0) {
ctx->cache.last_block_buff_size = ctx->conf.block_size;
} else {
ctx->cache.last_block_buff_size = new_block_num_remainder;
}
return RBUF_OK;
}
/**
* @brief copy external data into the resizable buffer.
*
* @param ctx context pointer.
* @param buff external buffer pointer.
* @param offs offset indicating where to start copying in the resizable buffer.
* @param size data copying size;
*/
rbuf_res rbuf_copy_from(rbuf_ctx *ctx, const void *buff, rbuf_u32 offs, rbuf_u32 size) {
rbuf_u32 new_size;
rbuf_u32 block_offs;
rbuf_u32 starting_block_idx;
rbuf_u32 ending_block_idx;
bque_buff mod_bque_buff;
bque_res mod_bque_res;
rbuf_res res;
RBUF_ASSERT(ctx != NULL);
RBUF_ASSERT(buff != NULL);
new_size = offs + size;
/* if the new buffer size is greater than
the buffer size, resize the buffer. */
if (new_size > ctx->cache.buff_size) {
res = rbuf_resize(ctx, new_size);
if (res != RBUF_OK) {
return res;
}
}
starting_block_idx = offs / ctx->conf.block_size;
ending_block_idx = new_size / ctx->conf.block_size -
((new_size % ctx->conf.block_size == 0) ? 1 : 0);
if (starting_block_idx == ending_block_idx) {
mod_bque_res = bque_item(ctx->bque, starting_block_idx, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
block_offs = offs % ctx->conf.block_size;
memcpy(mod_bque_buff.ptr + block_offs, buff, size);
} else {
rbuf_u32 buff_offs;
rbuf_u32 rest_size;
rbuf_u32 curt_size;
mod_bque_res = bque_item(ctx->bque, starting_block_idx, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
block_offs = offs % ctx->conf.block_size;
curt_size = ctx->conf.block_size - block_offs;
memcpy(mod_bque_buff.ptr + block_offs, buff, curt_size);
buff_offs = curt_size;
rest_size = size - curt_size;
for (bque_u32 i = starting_block_idx + 1; i < ending_block_idx + 1; i++) {
mod_bque_res = bque_item(ctx->bque, (bque_s32)i, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
if (rest_size > ctx->conf.block_size) {
curt_size = ctx->conf.block_size;
} else {
curt_size = rest_size;
}
memcpy(mod_bque_buff.ptr, (rbuf_u8 *)buff + buff_offs, curt_size);
buff_offs += curt_size;
rest_size -= curt_size;
}
}
return RBUF_OK;
}
/**
* @brief append external data to the end of the resizable buffer.
*
* @param ctx context pointer.
* @param buff external buffer pointer.
* @param size data appending size;
*/
rbuf_res rbuf_append(rbuf_ctx *ctx, const void *buff, rbuf_u32 size) {
rbuf_res res;
RBUF_ASSERT(ctx != NULL);
RBUF_ASSERT(buff != NULL);
res = rbuf_copy_from(ctx, buff, ctx->cache.buff_size, size);
return res;
}
/**
* @brief copy data in the resizable buffer into the external buffer.
*
* @param ctx context pointer.
* @param buff external buffer pointer.
* @param offs offset indicating where to start copying in the resizable buffer.
* @param size data copying size;
*/
rbuf_res rbuf_copy_to(rbuf_ctx *ctx, void *buff, rbuf_u32 offs, rbuf_u32 size) {
rbuf_u32 ending_byte_no;
rbuf_u32 block_offs;
rbuf_u32 starting_block_idx;
rbuf_u32 ending_block_idx;
bque_buff mod_bque_buff;
bque_res mod_bque_res;
RBUF_ASSERT(ctx != NULL);
RBUF_ASSERT(buff != NULL);
if (offs > ctx->cache.buff_size) {
return RBUF_ERR_BAD_OFFS;
}
ending_byte_no = offs + size;
if (ending_byte_no > ctx->cache.buff_size) {
return RBUF_ERR_BAD_SIZE;
}
starting_block_idx = offs / ctx->conf.block_size;
ending_block_idx = ending_byte_no / ctx->conf.block_size;
if (starting_block_idx == ending_block_idx) {
mod_bque_res = bque_item(ctx->bque, starting_block_idx, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
block_offs = offs % ctx->conf.block_size;
memcpy(buff, mod_bque_buff.ptr + block_offs, size);
} else {
rbuf_u32 buff_offs;
rbuf_u32 rest_size;
rbuf_u32 curt_size;
mod_bque_res = bque_item(ctx->bque, starting_block_idx, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
block_offs = offs % ctx->conf.block_size;
curt_size = ctx->conf.block_size - block_offs;
memcpy(buff, mod_bque_buff.ptr + block_offs, curt_size);
buff_offs = curt_size;
rest_size = size - curt_size;
for (bque_u32 i = starting_block_idx + 1; i < ending_block_idx + 1; i++) {
mod_bque_res = bque_item(ctx->bque, (bque_s32)i, &mod_bque_buff);
if (mod_bque_res != BQUE_OK) {
return RBUF_ERR;
}
if (rest_size > ctx->conf.block_size) {
curt_size = ctx->conf.block_size;
} else {
curt_size = rest_size;
}
memcpy((rbuf_u8 *)buff + buff_offs, mod_bque_buff.ptr, curt_size);
buff_offs += curt_size;
rest_size -= curt_size;
}
}
return RBUF_OK;
}