-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
405 lines (367 loc) · 8.86 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
#include "containers.h"
struct _StreamBuffer {
size_t Size;
size_t Cursor;
unsigned Flags;
ContainerAllocator *Allocator;
char *Data;
} ;
#define SB_READ_FLAG 1
#define SB_WRITE_FLAG 2
#define SB_APPEND_FLAG 4
#define SB_FIXED 8
#define FCLOSE_DESTROYS 8
static int Finalize(StreamBuffer *b);
static StreamBuffer *CreateWithAllocator(size_t size,const ContainerAllocator *Allocator)
{
StreamBuffer *result;
if (size == 0)
size = 1024;
result = Allocator->malloc(sizeof(StreamBuffer));
if (result == NULL) {
iError.RaiseError("iStreamBuffer.Create",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
memset(result,0,sizeof(StreamBuffer));
result->Allocator = (ContainerAllocator *)Allocator;
result->Data = Allocator->malloc(size);
if (result->Data == NULL) {
Allocator->free(result);
return NULL;
}
result->Size = size;
memset(result->Data,0,size);
return result;
}
static StreamBuffer *Create(size_t size)
{
return CreateWithAllocator(size,CurrentAllocator);
}
static StreamBuffer *CreateFromFile(const char *FileName)
{
FILE *f = fopen(FileName,"rb");
StreamBuffer *result = NULL;
size_t siz;
if (f == NULL) {
iError.RaiseError("iBuffer.CreateFromFile",CONTAINER_ERROR_NOENT,FileName);
return NULL;
}
else if (fseek(f, 0, SEEK_END)) {
goto err;
}
siz = ftell(f);
if ((int)siz < 0) goto err;
fseek(f,0,SEEK_SET);
result = Create(siz+1);
if (result == NULL) goto err;
if (siz != fread(result->Data,1,siz,f)) {
iError.RaiseError("iBuffer.CreateFromFile",CONTAINER_ERROR_FILE_READ);
Finalize(result);
result = NULL;
}
err:
fclose(f);
return result;
}
static int enlargeBuffer(StreamBuffer *b,size_t chunkSize)
{
char *p;
size_t newSiz;
if (chunkSize < b->Size/2)
newSiz = b->Size/2;
else newSiz = chunkSize;
p = b->Allocator->realloc(b->Data,b->Size+newSiz);
if (p == NULL)
return CONTAINER_ERROR_NOMEMORY;
b->Data = p;
b->Size += newSiz;
return 1;
}
static size_t Write(StreamBuffer *b,void *data, size_t siz)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.Write",CONTAINER_ERROR_BADARG);
return 0;
}
if ((b->Cursor + siz) >= b->Size) {
int r = enlargeBuffer(b,b->Size+siz);
if (r < 0)
return 0;
}
memcpy(b->Data+b->Cursor,data,siz);
b->Cursor += siz;
return siz;
}
static size_t Read(StreamBuffer *b, void *data, size_t siz)
{
size_t len;
if (b == NULL || data == NULL || siz == 0) {
iError.RaiseError("iStreamBuffer.Read",CONTAINER_ERROR_BADARG);
return 0;
}
if (b->Cursor >= b->Size)
return 0;
len = siz;
if (b->Size - b->Cursor < len)
len = b->Size - b->Cursor;
memcpy(data,b->Data+b->Cursor,len);
b->Cursor += len;
return len;
}
static int SetPosition(StreamBuffer *b,size_t pos)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.SetPosition",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (pos > b->Size)
pos = b->Size;
b->Cursor = pos;
return 1;
}
static size_t GetPosition(const StreamBuffer *b)
{
if (b == NULL)
return 0;
return b->Cursor;
}
static size_t StreamBufferSize(const StreamBuffer *b)
{
if (b == NULL)
return sizeof(StreamBuffer);
return b->Size;
}
static int Clear(StreamBuffer *b)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.Clear",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
memset(b->Data,0,b->Size);
b->Cursor = 0;
return 1;
}
static int Finalize(StreamBuffer *b)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.Finalize",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
b->Allocator->free(b->Data);
b->Allocator->free(b);
return 1;
}
static char *GetData(const StreamBuffer *b)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.Finalize",CONTAINER_ERROR_BADARG);
return NULL;
}
return b->Data;
}
static int Resize(StreamBuffer *b,size_t newSize)
{
char *tmp;
if (b == NULL) {
iError.RaiseError("iStreamBuffer.Resize",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (newSize == b->Size)
return 0;
tmp = b->Allocator->realloc(b->Data,newSize);
if (tmp == NULL) {
iError.RaiseError("iStreamBuffer.Resize",CONTAINER_ERROR_NOMEMORY);
return CONTAINER_ERROR_NOMEMORY;
}
b->Data = tmp;
b->Size = newSize;
return 1;
}
static int ReadFromFile(StreamBuffer *b,FILE *infile)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.ReadFromFile",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
b->Cursor = 0;
return fread(b->Data,1,b->Size,infile);
}
static int WriteToFile(StreamBuffer *b,FILE *outfile)
{
if (b == NULL) {
iError.RaiseError("iStreamBuffer.WriteToFile",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
b->Cursor = 0;
return fwrite(b->Data,1,b->Size,outfile);
}
StreamBufferInterface iStreamBuffer = {
Create,
CreateWithAllocator,
CreateFromFile,
Read,
Write,
SetPosition,
GetPosition,
GetData,
StreamBufferSize,
Clear,
Finalize,
Resize,
ReadFromFile,
WriteToFile,
};
/* --------------------------------------------------------------------------
Circular buffers
------------------------------------------------------------------------- */
struct _CircularBuffer {
size_t maxCount;
size_t ElementSize;
size_t head;
size_t tail;
ContainerAllocator *Allocator;
DestructorFunction DestructorFn;
unsigned char *data;
};
static DestructorFunction SetDestructor(CircularBuffer *cb,DestructorFunction fn)
{
DestructorFunction oldfn;
if (cb == NULL)
return NULL;
oldfn = cb->DestructorFn;
if (fn)
cb->DestructorFn = fn;
return oldfn;
}
static size_t Size(const CircularBuffer *cb)
{
if (cb == NULL)
return sizeof(CircularBuffer);
return cb->head - cb->tail;
}
static int Add( CircularBuffer * b,const void *data_element)
{
unsigned char *ring_data = NULL;
int result = 1;
if (b == NULL || data_element == NULL) {
iError.RaiseError("iCircularBuffer.Add",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (b->maxCount == (b->head - b->tail)) {
b->head = 0;
result = 0;
}
ring_data = b->data + ((b->head % b->maxCount) * b->ElementSize);
memcpy(ring_data,data_element,b->ElementSize);
b->head++;
return result;
}
static int PopFront(CircularBuffer *b,void *result)
{
void *data;
if (b == NULL) {
iError.RaiseError("iCircularBuffer.PopFront",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (b->head == b->tail)
return 0;
data = &(b->data[(b->tail % b->maxCount) * b->ElementSize]);
b->tail++;
if (result)
memcpy(result,data,b->ElementSize);
return 1;
}
static int PeekFront(CircularBuffer *b,void *result)
{
void *data;
if (b == NULL || result == NULL) {
iError.RaiseError("iCircularBuffer.PeekFront",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (b->head == b->tail)
return 0;
data = &(b->data[(b->tail % b->maxCount) * b->ElementSize]);
memcpy(result,data,b->ElementSize);
return 1;
}
static CircularBuffer *CreateCBWithAllocator(size_t sizElement,size_t sizeBuffer,const ContainerAllocator *allocator)
{
CircularBuffer *result;
if (sizElement == 0 || sizeBuffer == 0 || allocator == NULL) {
iError.RaiseError("iCircularBuffer.Create",CONTAINER_ERROR_BADARG);
return NULL;
}
result = allocator->malloc(sizeof(CircularBuffer));
if (result == NULL) {
iError.RaiseError("iCircularBuffer.Create",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
memset(result,0,sizeof(CircularBuffer));
result->maxCount = sizeBuffer;
result->ElementSize = sizElement;
result->Allocator = (ContainerAllocator *)allocator;
sizElement = sizElement*sizeBuffer; /* Here we should test for overflow */
result->data = allocator->malloc(sizElement);
if (result->data == NULL) {
allocator->free(result);
iError.RaiseError("iCircularBuffer.Create",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
return result;
}
static CircularBuffer *CreateCB(size_t sizElement,size_t sizeBuffer)
{
return CreateCBWithAllocator(sizElement, sizeBuffer, CurrentAllocator);
}
static int CBClear(CircularBuffer *cb)
{
unsigned char *p;
size_t i;
if (cb == NULL) {
iError.RaiseError("iCircularBuffer.Clear",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (cb->head == cb->tail)
return 0;
p = cb->data;
if (cb->DestructorFn) {
for (i=cb->tail; i<cb->head;i++) {
cb->DestructorFn(p);
p += cb->ElementSize;
}
}
memset(p,0,cb->ElementSize*(cb->head-cb->tail));
cb->head = cb->tail = 0;
return 1;
}
static int CBFinalize(CircularBuffer *cb)
{
if (cb == NULL) {
iError.RaiseError("iCircularBuffer.Finalize",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
CBClear(cb);
cb->Allocator->free(cb->data);
cb->Allocator->free(cb);
return 1;
}
static size_t Sizeof(const CircularBuffer *cb)
{
size_t result = sizeof(CircularBuffer);
if (cb == NULL)
return result;
result += (cb->head - cb->tail)*cb->ElementSize;
return result;
}
CircularBufferInterface iCircularBuffer = {
Size,
Add,
PopFront,
PeekFront,
CreateCBWithAllocator,
CreateCB,
CBClear,
CBFinalize,
Sizeof,
SetDestructor,
};