-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmpsock_buffer.c
274 lines (236 loc) · 6.86 KB
/
mpsock_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
#include "mpsock_buffer.h"
/*
* debug function
*/
void print_ringbuffer(mpsock_buffer *buffer)
{
size_t end = buffer->buffer_end - buffer->buffer_start;
size_t start = 0;
size_t head = buffer->head - buffer->buffer_start;
size_t tail = buffer->tail - buffer->buffer_start;
LOG_DEBUG("%sbuffer_start=%zu, buffer_end=%zu, head=%zu, tail=%zu",FUN_EVENT,start,end,head,tail);
}
/*
* verify if there is enough space to store size in the buffer
*/
int has_enough_memory(mpsock_buffer *buffer, size_t size)
{
if(buffer->tail > buffer->head)
{
return ((buffer->buffer_end - buffer->tail) + (buffer->head - buffer->buffer_start) > size);
}
else if(buffer->tail < buffer->head)
{
return (buffer->head - buffer->tail > size);
}
else
{
return (buffer->buffer_end - buffer->buffer_start > size);
}
}
/*
* verify that we have enough memory allocated
*/
int has_enough_allocated(mpsock_buffer *buffer, size_t size)
{
if(buffer->tail > buffer->head)
{
return ((buffer->tail - buffer->head) >= size);
}
else if(buffer->tail < buffer->head)
{
return (((buffer->buffer_end - buffer->head) + (buffer->tail - buffer->buffer_start)) >= size);
}
else
{
return (size == 0);
}
}
/*
* returns a pointer to memory in buffer with given size
* return NULL if not enough space is available
*/
void *allocate_buffer_memory(mpsock_buffer *buffer, size_t size)
{
LOG_DEBUG("%sallocate_buffer_memory(%zu)",FUN_EVENT,size);
if(USE_ASSERTS) assert(size > 0);
if(USE_ASSERTS) assert(buffer->head <= buffer->buffer_end && buffer->head >= buffer->buffer_start);
if(USE_ASSERTS) assert(buffer->tail <= buffer->buffer_end && buffer->tail >= buffer->buffer_start);
if(USE_ASSERTS) assert(has_enough_memory(buffer,size));
//if(!has_enough_memory(buffer,size))
//{
// // we do not have enough free space for this operation
// return NULL;
//}
// return value
void *ret = buffer->tail;
// advance tail by size
if(buffer->tail+size <= buffer->buffer_end)
{
if(buffer->tail < buffer->head)
{
// no wrap around -> simply add
buffer->tail += size;
if(USE_ASSERTS) assert(buffer->tail < buffer->head);
}
else
{
// no wrap around -> simply add
buffer->tail += size;
if(USE_ASSERTS) assert(buffer->tail > buffer->head);
}
}
else
{
// we wrap around the end
buffer->tail = buffer->buffer_start + (size - (buffer->buffer_end - buffer->tail));
if(USE_ASSERTS) assert(buffer->tail < buffer->head);
}
print_ringbuffer(buffer);
if(USE_ASSERTS) assert(buffer->tail != buffer->head);
if(USE_ASSERTS) assert(buffer->tail <= buffer->buffer_end && buffer->tail >= buffer->buffer_start);
if(USE_ASSERTS) assert(buffer->head <= buffer->buffer_end && buffer->head >= buffer->buffer_start);
return ret;
}
/*
* free given size from head in given buffer
*/
void free_buffer_memory(mpsock_buffer *buffer, size_t size)
{
LOG_DEBUG("%sfree_buffer_memory(%zu)",FUN_EVENT,size);
if(USE_ASSERTS) assert(buffer->head <= buffer->buffer_end && buffer->head >= buffer->buffer_start);
if(USE_ASSERTS) assert(buffer->tail <= buffer->buffer_end && buffer->tail >= buffer->buffer_start);
if(USE_ASSERTS) assert(has_enough_allocated(buffer,size));
if(buffer->buffer_end - buffer->head >= size)
{
if(buffer->tail < buffer->head)
{
// no wrap around -> simply add
buffer->head += size;
if(USE_ASSERTS) assert(buffer->head >= buffer->tail);
}
else
{
// no wrap around -> simply add
buffer->head += size;
if(USE_ASSERTS) assert(buffer->head <= buffer->tail);
}
}
else
{
// we wrap around the end
buffer->head = buffer->buffer_start + (size - (buffer->buffer_end - buffer->head));
if(USE_ASSERTS) assert(buffer->head <= buffer->tail);
}
print_ringbuffer(buffer);
if(USE_ASSERTS) assert(buffer->tail <= buffer->buffer_end && buffer->tail >= buffer->buffer_start);
if(USE_ASSERTS) assert(buffer->head <= buffer->buffer_end && buffer->head >= buffer->buffer_start);
}
mpsock_buffer *create_buffer(size_t size)
{
mpsock_buffer *buf = (mpsock_buffer*)malloc(sizeof(mpsock_buffer));
buf->buffer_start = (void*)malloc(size);
buf->buffer_end = buf->buffer_start + size;
buf->head = buf->buffer_start;
buf->tail = buf->head;
buf->free_chunk_structures = NULL;
return buf;
}
void store_in_buffer(mpsock_buffer *buffer, void *dst, const void *orig, size_t offset, size_t len)
{
// determine real destination in buffer
void *real_dst = dst+offset;
if(real_dst > buffer->buffer_end)
{
// wrap around
real_dst = buffer->buffer_start + (offset - (buffer->buffer_end - dst));
}
if(USE_ASSERTS) assert(real_dst <= buffer->buffer_end && real_dst >= buffer->buffer_start);
// store data
if(real_dst + len <= buffer->buffer_end)
{
// no wrap
memcpy(real_dst,orig,len);
}
else
{
// we wrap
memcpy(real_dst,orig,buffer->buffer_end - real_dst);
memcpy(buffer->buffer_start, orig + (buffer->buffer_end - real_dst),len - (buffer->buffer_end - real_dst));
}
}
void read_from_buffer(mpsock_buffer *buffer, void *dst, void *orig, size_t offset, size_t len)
{
// determine real origin in buffer
void *real_orig = orig+offset;
if(real_orig > buffer->buffer_end)
{
// wrap around
real_orig = buffer->buffer_start + (offset - (buffer->buffer_end - orig));
}
if(USE_ASSERTS) assert(real_orig <= buffer->buffer_end && real_orig >= buffer->buffer_start);
// store data
if(real_orig + len <= buffer->buffer_end)
{
// no wrap
memcpy(dst,real_orig,len);
}
else
{
// we wrap
memcpy(dst,real_orig,buffer->buffer_end - real_orig);
memcpy(dst+(buffer->buffer_end - real_orig), buffer->buffer_start,len - (buffer->buffer_end - real_orig));
}
}
mpsock_chunk *allocate_chunk_in_buffer(mpsock_buffer *buffer, size_t start_byte, size_t size)
{
LOG_DEBUG("%sallocate_chunk",FUN_EVENT);
mpsock_chunk *chunk;
if(buffer->free_chunk_structures == NULL)
{
LOG_DEBUG("%sno reusable chunk found -> create new chunk",COND_EVENT);
// create the chunk
chunk = (mpsock_chunk*)malloc(sizeof(mpsock_chunk));
}
else
{
LOG_DEBUG("%sreuse old chunk",COND_EVENT);
// get the chunk from the list
chunk = buffer->free_chunk_structures;
buffer->free_chunk_structures = buffer->free_chunk_structures->next;
}
// init values
chunk->next = NULL;
chunk->start_byte = start_byte;
chunk->data_size = size;
chunk->buffer_size = size;
chunk->pos_save = 0;
chunk->pos_read = 0;
// allocate the buffer
chunk->buffer = allocate_buffer_memory(buffer, size);
return chunk;
}
void free_chunk_in_buffer(mpsock_buffer *buffer, mpsock_chunk *chunk)
{
LOG_DEBUG("%sfree_chunk",FUN_EVENT);
// free buffer memory
free_buffer_memory(buffer, chunk->buffer_size);
// reinit
chunk->next = NULL;
// append to free structure list
mpsock_chunk *l_chunk = buffer->free_chunk_structures;
if(l_chunk == NULL)
{
buffer->free_chunk_structures = chunk;
}
else
{
mpsock_chunk *last_chunk;
while(l_chunk != NULL)
{
last_chunk = l_chunk;
l_chunk = l_chunk->next;
}
last_chunk->next = chunk;
}
}