source/myhtml/utils/mchar_async.c

filter: Statementstmt 39.7% 150 / 377 Branchbran 25.0% 41 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 35.3% 191 / 541 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/myosi.c ↑ Summary source/myhtml/thread.c »
1 /*
2 Copyright 2015-2016 Alexander Borisov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 #include "myhtml/utils/mchar_async.h"
20  
21 2 mchar_async_t * mchar_async_create(size_t pos_size, size_t size)
22 {
23 2 mchar_async_t *mcobj_async = (mchar_async_t*)calloc(1, sizeof(mchar_async_t));
24
25 2 mchar_async_init(mcobj_async, pos_size, size);
26
27 2 return mcobj_async;
28 }
29  
30 2 void mchar_async_init(mchar_async_t *mchar_async, size_t chunk_len, size_t char_size)
31 {
32 2 ▶ if(char_size < 4096)
Branch: 1 branch
BranchTrueFalseTotal
if(char_size < 4096) 0 2 2
33 0 char_size = 4096;
34
35 2 mchar_async->origin_size = char_size;
36
37 2 mchar_async->chunks_size = chunk_len;
38 2 mchar_async->chunks_pos_size = 1024;
39 2 mchar_async->chunks = (mchar_async_chunk_t**)mycalloc(mchar_async->chunks_pos_size, sizeof(mchar_async_chunk_t*));
40 2 mchar_async->chunks[0] = (mchar_async_chunk_t*)mycalloc(mchar_async->chunks_size, sizeof(mchar_async_chunk_t));
41
42 2 mchar_async_cache_init(&mchar_async->chunk_cache);
43
44 2 mchar_async->nodes_length = 0;
45 2 mchar_async->nodes_size = 64;
46 2 mchar_async->nodes = (mchar_async_node_t*)mycalloc(mchar_async->nodes_size, sizeof(mchar_async_node_t));
47
48 2 mchar_async->nodes_cache_length = 0;
49 2 mchar_async->nodes_cache_size = mchar_async->nodes_size;
50 2 mchar_async->nodes_cache = (size_t*)mymalloc(mchar_async->nodes_cache_size * sizeof(size_t));
51
52 2 mchar_async_clean(mchar_async);
53
54 2 mchar_async->mcsync = mcsync_create();
55 2 }
56  
57 2 void mchar_async_clean(mchar_async_t *mchar_async)
58 {
59 2 mchar_async->chunks_length = 0;
60 2 mchar_async->chunks_pos_length = 1;
61
62 2 mchar_async_cache_clean(&mchar_async->chunk_cache);
63
64 2 ▶ for (size_t node_idx = 0; node_idx < mchar_async->nodes_length; node_idx++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t node_idx = 0; node_idx < mchar_async->nodes_length; node_idx++) 0 2 2
65 {
66 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
67 0 mchar_async_cache_clean(&node->cache);
68
69 0 node->chunk = mchar_async_chunk_malloc(mchar_async, node, mchar_async->origin_size);
70 0 node->chunk->prev = 0;
71 }
72 2 }
73  
74 2 mchar_async_t * mchar_async_destroy(mchar_async_t *mchar_async, int destroy_self)
75 {
76 2 ▶ if(mchar_async == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async == NULL) 0 2 2
77 0 return NULL;
78
79 2 ▶ if(mchar_async->nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes) 2 0 2
80 {
81 5 ▶ for (size_t node_idx = 0; node_idx < mchar_async->nodes_length; node_idx++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t node_idx = 0; node_idx < mchar_async->nodes_length; node_idx++) 3 2 5
82 {
83 3 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
84 3 mchar_async_cache_destroy(&node->cache, false);
85 }
86
87 2 free(mchar_async->nodes);
88 2 mchar_async->nodes = NULL;
89 }
90
91 2 ▶ if(mchar_async->nodes_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache) { 2 0 2
92 2 free(mchar_async->nodes_cache);
93 }
94
95 2 ▶ if(mchar_async->chunks)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks) 2 0 2
96 {
97 4 ▶ for (size_t pos_idx = 0; pos_idx < mchar_async->chunks_pos_length; pos_idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t pos_idx = 0; pos_idx < mchar_async->chunks_pos_length; pos_idx++) { 2 2 4
98 2 ▶ if(mchar_async->chunks[pos_idx])
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[pos_idx]) 2 0 2
99 {
100 194 ▶ for (size_t idx = 0; idx < mchar_async->chunks_size; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t idx = 0; idx < mchar_async->chunks_size; idx++) { 192 2 194
101 192 ▶ if(mchar_async->chunks[pos_idx][idx].begin)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[pos_idx][idx].begin) 3 189 192
102 3 free(mchar_async->chunks[pos_idx][idx].begin);
103 }
104
105 2 free(mchar_async->chunks[pos_idx]);
106 }
107 }
108
109 2 free(mchar_async->chunks);
110 2 mchar_async->chunks = NULL;
111 }
112
113 2 mchar_async_cache_destroy(&mchar_async->chunk_cache, false);
114
115 2 mchar_async->mcsync = mcsync_destroy(mchar_async->mcsync, 1);
116
117 2 memset(mchar_async, 0, sizeof(mchar_async_t));
118
119 2 ▶ if(destroy_self)
Branch: 1 branch
BranchTrueFalseTotal
if(destroy_self) 2 0 2
120 2 free(mchar_async);
121 else
122 0 return mchar_async;
123
124 2 return NULL;
125 }
126  
127 3 void mchar_async_mem_malloc(mchar_async_t *mchar_async, mchar_async_node_t *node, mchar_async_chunk_t *chunk, size_t length)
128 {
129 3 ▶ if(chunk == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk == NULL) 0 3 3
130 0 return;
131
132 3 ▶ if(chunk->begin) {
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->begin) { 0 3 3
133 0 ▶ if(length > chunk->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > chunk->size) { 0 0 0
134 0 free(chunk->begin);
135
136 0 chunk->size = length + mchar_async->origin_size;
137 0 chunk->begin = (char*)mymalloc(chunk->size * sizeof(char));
138 }
139 }
140 else {
141 3 chunk->size = mchar_async->origin_size;
142
143 3 ▶ if(length > chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if(length > chunk->size) 0 3 3
144 0 chunk->size = length;
145
146 3 chunk->begin = (char*)mymalloc(chunk->size * sizeof(char));
147 }
148
149 3 chunk->length = 0;
150 }
151  
152 3 mchar_async_chunk_t * mchar_async_chunk_malloc_without_lock(mchar_async_t *mchar_async, mchar_async_node_t *node, size_t length)
153 {
154 3 ▶ if(mchar_async_cache_has_nodes(mchar_async->chunk_cache))
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async_cache_has_nodes(mchar_async->chunk_cache)) 0 3 3
155 {
156 0 size_t index = mchar_async_cache_delete(&mchar_async->chunk_cache, length);
157
158 0 ▶ if(index)
Branch: 1 branch
BranchTrueFalseTotal
if(index) 0 0 0
159 0 return (mchar_async_chunk_t*)mchar_async->chunk_cache.nodes[index].value;
160 }
161
162 3 ▶ if(mchar_async->chunks_length >= mchar_async->chunks_size)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks_length >= mchar_async->chunks_size) 0 3 3
163 {
164 0 size_t current_idx = mchar_async->chunks_pos_length;
165 0 mchar_async->chunks_pos_length++;
166
167 0 ▶ if(mchar_async->chunks_pos_length >= mchar_async->chunks_pos_size)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks_pos_length >= mchar_async->chunks_pos_size) 0 0 0
168 {
169 0 mchar_async->chunks_pos_size <<= 1;
170 0 mchar_async_chunk_t **tmp_pos = myrealloc(mchar_async->chunks,
171 0 sizeof(mchar_async_chunk_t*) * mchar_async->chunks_pos_size);
172
173 0 ▶ if(tmp_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp_pos) { 0 0 0
174 0 memset(&tmp_pos[mchar_async->chunks_pos_length], 0, (mchar_async->chunks_pos_size - mchar_async->chunks_pos_length)
175 * sizeof(mchar_async_chunk_t*));
176
177 0 mchar_async->chunks = tmp_pos;
178 }
179 }
180
181 0 ▶ if(mchar_async->chunks[current_idx] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[current_idx] == NULL) { 0 0 0
182 0 mchar_async_chunk_t *tmp = mycalloc(mchar_async->chunks_size, sizeof(mchar_async_chunk_t));
183
184 0 ▶ if(tmp)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) 0 0 0
185 0 mchar_async->chunks[current_idx] = tmp;
186 }
187
188 0 mchar_async->chunks_length = 0;
189 }
190
191 3 mchar_async_chunk_t *chunk = &mchar_async->chunks[mchar_async->chunks_pos_length - 1][mchar_async->chunks_length];
192 3 mchar_async->chunks_length++;
193
194 3 mchar_async_mem_malloc(mchar_async, node, chunk, length);
195
196 3 return chunk;
197 }
198  
199 0 mchar_async_chunk_t * mchar_async_chunk_malloc(mchar_async_t *mchar_async, mchar_async_node_t *node, size_t length)
200 {
201 0 mcsync_lock(mchar_async->mcsync);
202 0 mchar_async_chunk_t *chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, length);
203 0 mcsync_unlock(mchar_async->mcsync);
204
205 0 return chunk;
206 }
207  
208 3 size_t mchar_async_node_add(mchar_async_t *mchar_async)
209 {
210 3 mcsync_lock(mchar_async->mcsync);
211
212 size_t node_idx;
213
214 3 ▶ if(mchar_async->nodes_cache_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache_length) { 0 3 3
215 0 mchar_async->nodes_cache_length--;
216
217 0 node_idx = mchar_async->nodes_cache[ mchar_async->nodes_cache_length ];
218 }
219 else {
220 3 ▶ if(mchar_async->nodes_length >= mchar_async->nodes_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length >= mchar_async->nodes_size) { 0 3 3
221 0 mcsync_unlock(mchar_async->mcsync);
222 0 return 0;
223 }
224
225 3 node_idx = mchar_async->nodes_length;
226 3 mchar_async->nodes_length++;
227 }
228
229 3 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
230
231 3 mchar_async_cache_init(&node->cache);
232
233 3 node->chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, mchar_async->origin_size);
234
235 3 node->chunk->next = NULL;
236 3 node->chunk->prev = NULL;
237
238 3 mcsync_unlock(mchar_async->mcsync);
239
240 3 return node_idx;
241 }
242  
243 4 void mchar_async_node_clean(mchar_async_t *mchar_async, size_t node_idx)
244 {
245 4 ▶ if(mchar_async->nodes_length <= node_idx)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length <= node_idx) 0 4 4
246 0 return;
247
248 4 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
249
250 4 ▶ while (node->chunk->prev)
Branch: 1 branch
BranchTrueFalseTotal
while (node->chunk->prev) 0 4 4
251 0 node->chunk = node->chunk->prev;
252
253 4 node->chunk->length = 0;
254 4 mchar_async_cache_clean(&node->cache);
255 }
256  
257 1 void mchar_async_node_delete(mchar_async_t *mchar_async, size_t node_idx)
258 {
259 1 mcsync_lock(mchar_async->mcsync);
260
261 1 ▶ if(mchar_async->nodes_length <= node_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length <= node_idx) { 0 1 1
262 0 mcsync_unlock(mchar_async->mcsync);
263 0 return;
264 }
265
266 1 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
267 1 mchar_async_chunk_t *chunk = node->chunk;
268
269 1 ▶ while (chunk->next)
Branch: 1 branch
BranchTrueFalseTotal
while (chunk->next) 0 1 1
270 0 chunk = chunk->next;
271
272 2 ▶ while (chunk)
Branch: 1 branch
BranchTrueFalseTotal
while (chunk) 1 1 2
273 {
274 1 mchar_async_cache_add(&mchar_async->chunk_cache, (void*)chunk, chunk->size);
275 1 chunk = chunk->prev;
276 }
277
278 1 ▶ if(node->cache.nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(node->cache.nodes) 1 0 1
279 1 mchar_async_cache_destroy(&node->cache, false);
280
281 1 memset(node, 0, sizeof(mchar_async_node_t));
282
283 1 ▶ if(mchar_async->nodes_cache_length >= mchar_async->nodes_cache_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache_length >= mchar_async->nodes_cache_size) { 0 1 1
284 0 size_t new_size = mchar_async->nodes_cache_size << 1;
285
286 0 size_t *tmp = (size_t*)myrealloc(mchar_async->nodes_cache, sizeof(size_t) * mchar_async->nodes_cache_size);
287
288 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
289 0 mchar_async->nodes_cache = tmp;
290 0 mchar_async->nodes_cache_size = new_size;
291 }
292 }
293
294 1 mchar_async->nodes_cache[ mchar_async->nodes_cache_length ] = node_idx;
295 1 mchar_async->nodes_cache_length++;
296
297 1 mcsync_unlock(mchar_async->mcsync);
298 }
299  
300 0 mchar_async_chunk_t * mchar_sync_chunk_find_by_size(mchar_async_node_t *node, size_t size)
301 {
302 0 mchar_async_chunk_t *chunk = node->chunk->next;
303
304 0 ▶ while (chunk) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk) { 0 0 0
305 0 ▶ if(chunk->size >= size)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->size >= size) 0 0 0
306 0 return chunk;
307
308 0 chunk = chunk->next;
309 }
310
311 0 return NULL;
312 }
313  
314 0 void mchar_sync_chunk_insert_after(mchar_async_chunk_t *base, mchar_async_chunk_t *chunk)
315 {
316 0 ▶ if(base->next == chunk)
Branch: 1 branch
BranchTrueFalseTotal
if(base->next == chunk) 0 0 0
317 0 return;
318
319 0 ▶ if(chunk->prev)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->prev) 0 0 0
320 0 chunk->prev->next = chunk->next;
321
322 0 ▶ if(chunk->next)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->next) 0 0 0
323 0 chunk->next->prev = chunk->prev;
324
325 0 ▶ if(base->next)
Branch: 1 branch
BranchTrueFalseTotal
if(base->next) 0 0 0
326 0 base->next->prev = chunk;
327
328 0 chunk->next = base->next;
329 0 chunk->prev = base;
330
331 0 base->next = chunk;
332 }
333  
334 0 char * mchar_async_malloc(mchar_async_t *mchar_async, size_t node_idx, size_t size)
335 {
336 0 ▶ if(size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(size == 0) 0 0 0
337 0 return NULL;
338
339 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
340 0 mchar_async_chunk_t *chunk = node->chunk;
341
342 0 ▶ if(mchar_async_cache_has_nodes(node->cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async_cache_has_nodes(node->cache)) { 0 0 0
343 0 size_t index = mchar_async_cache_delete(&node->cache, size);
344
345 0 ▶ if(index) {
Branch: 1 branch
BranchTrueFalseTotal
if(index) { 0 0 0
346 0 return (char *)(node->cache.nodes[index].value);
347 }
348 }
349
350 0 size_t new_size = chunk->length + size + sizeof(size_t);
351
352 0 ▶ if(new_size > chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if(new_size > chunk->size) 0 0 0
353 {
354 0 ▶ if((chunk->length + sizeof(size_t)) < chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if((chunk->length + sizeof(size_t)) < chunk->size) 0 0 0
355 {
356 0 size_t size = (chunk->size - chunk->length) - sizeof(size_t);
357
358 0 ▶ if(size) {
Branch: 1 branch
BranchTrueFalseTotal
if(size) { 0 0 0
359 0 char *tmp = &chunk->begin[(chunk->length + sizeof(size_t))];
360 0 *(size_t*)(&chunk->begin[chunk->length]) = size;
361
362 0 chunk->length = chunk->size;
363
364 0 mchar_async_cache_add(&node->cache, tmp, size);
365 }
366 }
367
368 0 chunk = mchar_sync_chunk_find_by_size(node, size);
369
370 0 ▶ if(chunk)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk) 0 0 0
371 0 chunk->length = 0;
372 else {
373 0 ▶ if((size + sizeof(size_t)) > mchar_async->origin_size)
Branch: 1 branch
BranchTrueFalseTotal
if((size + sizeof(size_t)) > mchar_async->origin_size) 0 0 0
374 0 chunk = mchar_async_chunk_malloc(mchar_async, node, (size + mchar_async->origin_size + sizeof(size_t)));
375 else
376 0 chunk = mchar_async_chunk_malloc(mchar_async, node, mchar_async->origin_size);
377 }
378
379 0 mchar_sync_chunk_insert_after(node->chunk, chunk);
380 0 node->chunk = chunk;
381 }
382
383 0 char *tmp = &chunk->begin[(chunk->length + sizeof(size_t))];
384 0 *((size_t*)(&chunk->begin[chunk->length])) = size;
385
386 0 chunk->length = chunk->length + size + sizeof(size_t);
387
388 0 return tmp;
389 }
390  
391 0 char * mchar_async_realloc(mchar_async_t *mchar_async, size_t node_idx, char *data, size_t data_len, size_t new_size)
392 {
393 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
394 0 return NULL;
395
396 0 size_t curr_size = *((size_t*)(data - sizeof(size_t)));
397
398 0 ▶ if(curr_size >= new_size)
Branch: 1 branch
BranchTrueFalseTotal
if(curr_size >= new_size) 0 0 0
399 0 return data;
400
401 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
402
403 0 ▶ if(node->chunk->length >= curr_size &&
Branch: 1 branch
BranchTrueFalseTotal
if(node->chunk->length >= curr_size && 0 0 0
404 0 ▶ &node->chunk->begin[ (node->chunk->length - curr_size) ] == data)
Branch: 1 branch
BranchTrueFalseTotal
&node->chunk->begin[ (node->chunk->length - curr_size) ] == data) 0 0 0
405 {
406 0 size_t next_size = (node->chunk->length - curr_size) + new_size;
407
408 0 ▶ if(next_size <= node->chunk->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(next_size <= node->chunk->size) { 0 0 0
409 /* it`s Magic */
410 0 *((size_t*)(&node->chunk->begin[ ((node->chunk->length - curr_size) - sizeof(size_t)) ])) = new_size;
411
412 0 node->chunk->length = next_size;
413
414 0 return data;
415 }
416 // else {
417 // size_t re_size = next_size - node->chunk->length;
418 //
419 // /* a little Magic ;) */
420 // *((size_t*)(&node->chunk->begin[ ((node->chunk->length - curr_size) - sizeof(size_t)) ])) = re_size;
421 //
422 // curr_size = re_size;
423 // }
424 }
425
426 0 char *tmp = mchar_async_malloc(mchar_async, node_idx, new_size);
427
428 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
429 0 memcpy(tmp, data, sizeof(char) * data_len);
430
431 0 mchar_async_cache_add(&node->cache, data, curr_size);
432 }
433
434 0 return tmp;
435 }
436  
437 0 char * mchar_async_crop_first_chars(mchar_async_t *mchar_async, size_t node_idx, char *data, size_t crop_len)
438 {
439 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
440 0 return NULL;
441
442 0 size_t curr_size = *((size_t*)(data - sizeof(size_t)));
443
444 0 char *tmp_old = data;
445 0 data = &data[crop_len];
446
447 0 *((size_t*)(data - sizeof(size_t))) = curr_size - crop_len;
448
449 0 ▶ if((crop_len + 4) > sizeof(size_t)) {
Branch: 1 branch
BranchTrueFalseTotal
if((crop_len + 4) > sizeof(size_t)) { 0 0 0
450 0 crop_len = crop_len - sizeof(size_t);
451 0 *((size_t*)(tmp_old - sizeof(size_t))) = crop_len;
452
453 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
454 0 mchar_async_cache_add(&node->cache, tmp_old, crop_len);
455 }
456
457 0 return data;
458 }
459  
460 0 char * mchar_async_crop_first_chars_without_cache(char *data, size_t crop_len)
461 {
462 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
463 0 return NULL;
464
465 0 size_t curr_size = *((size_t*)(data - sizeof(size_t)));
466 0 data = &data[crop_len];
467
468 0 *((size_t*)(data - sizeof(size_t))) = curr_size - crop_len;
469
470 0 return data;
471 }
472  
473 0 void mchar_async_free(mchar_async_t *mchar_async, size_t node_idx, char *entry)
474 {
475 0 mchar_async_cache_add(&mchar_async->nodes[node_idx].cache, entry, *(size_t*)(entry - sizeof(size_t)));
476 0 }
477  
478 5 void mchar_async_cache_init(mchar_async_cache_t *cache)
479 {
480 5 cache->count = 0;
481 5 cache->nodes_root = 0;
482 5 cache->nodes_length = 1;
483 5 cache->nodes_size = 1024;
484 5 cache->nodes = (mchar_async_cache_node_t*)mymalloc(sizeof(mchar_async_cache_node_t) * cache->nodes_size);
485
486 5 cache->nodes[0].left = 0;
487 5 cache->nodes[0].right = 0;
488 5 cache->nodes[0].size = 0;
489 5 cache->nodes[0].value = NULL;
490
491 5 cache->index_length = 0;
492 5 cache->index_size = cache->nodes_size;
493 5 cache->index = (size_t*)mymalloc(sizeof(size_t) * cache->index_size);
494 5 }
495  
496 6 void mchar_async_cache_clean(mchar_async_cache_t *cache)
497 {
498 6 cache->count = 0;
499 6 cache->nodes_root = 0;
500 6 cache->nodes_length = 1;
501 6 cache->index_length = 0;
502
503 6 ▶ if(cache->nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes) { 6 0 6
504 6 cache->nodes[0].left = 0;
505 6 cache->nodes[0].right = 0;
506 6 cache->nodes[0].size = 0;
507 6 cache->nodes[0].value = NULL;
508 }
509 6 }
510  
511 6 mchar_async_cache_t * mchar_async_cache_destroy(mchar_async_cache_t *cache, bool self_destroy)
512 {
513 6 ▶ if(cache == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(cache == NULL) 0 6 6
514 0 return NULL;
515
516 6 ▶ if(cache->nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes) 5 1 6
517 5 free(cache->nodes);
518
519 6 ▶ if(cache->index)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index) 5 1 6
520 5 free(cache->index);
521
522 6 ▶ if(self_destroy) {
Branch: 1 branch
BranchTrueFalseTotal
if(self_destroy) { 0 6 6
523 0 free(cache);
524 0 return NULL;
525 }
526
527 6 return cache;
528 }
529  
530 1 size_t mchar_async_cache_malloc(mchar_async_cache_t *cache)
531 {
532 1 ▶ if(cache->index_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index_length) { 0 1 1
533 0 cache->index_length--;
534 0 return cache->index[cache->index_length];
535 }
536
537 1 cache->nodes_length++;
538
539 1 ▶ if(cache->nodes_length >= cache->nodes_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes_length >= cache->nodes_size) { 0 1 1
540 0 cache->nodes_size <<= 1;
541
542 0 mchar_async_cache_node_t *tmp = (mchar_async_cache_node_t*)myrealloc(cache->nodes, sizeof(mchar_async_cache_node_t) * cache->nodes_size);
543
544 0 ▶ if(tmp)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) 0 0 0
545 0 cache->nodes = tmp;
546 }
547
548 1 return cache->nodes_length - 1;
549 }
550  
551 0 size_t mchar_async_cache_delete(mchar_async_cache_t *cache, size_t size)
552 {
553 0 mchar_async_cache_node_t *list = cache->nodes;
554 0 size_t idx = cache->nodes_root;
555
556 0 ▶ while (idx)
Branch: 1 branch
BranchTrueFalseTotal
while (idx) 0 0 0
557 {
558 0 ▶ if(size <= list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
if(size <= list[idx].size) 0 0 0
559 {
560 // for a left
561 0 size_t left = list[idx].left;
562
563 0 list[left].parent = list[idx].parent;
564
565 0 ▶ if(list[idx].parent) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].parent) { 0 0 0
566 0 ▶ if(list[ list[idx].parent ].left == idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[ list[idx].parent ].left == idx) { 0 0 0
567 0 list[ list[idx].parent ].left = left;
568 }
569 else {
570 0 list[ list[idx].parent ].right = left;
571 }
572 }
573 else {
574 0 cache->nodes_root = left;
575 }
576
577 0 ▶ while(list[left].right)
Branch: 1 branch
BranchTrueFalseTotal
while(list[left].right) 0 0 0
578 0 left = list[left].right;
579
580 0 list[left].right = list[idx].right;
581
582 0 list[ list[idx].right ].parent = left;
583
584 0 cache->index[cache->index_length] = idx;
585
586 0 cache->index_length++;
587 0 ▶ if(cache->index_length >= cache->index_size)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index_length >= cache->index_size) 0 0 0
588 {
589 0 cache->index_size <<= 1;
590 0 size_t *tmp = (size_t*)myrealloc(cache->index, sizeof(size_t) * cache->index_size);
591
592 0 ▶ if(tmp)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) 0 0 0
593 0 cache->index = tmp;
594 }
595
596 0 cache->count--;
597
598 0 return idx;
599 }
600 else {
601 0 idx = list[idx].right;
602 }
603 }
604
605 0 return 0;
606 }
607  
608 1 void mchar_async_cache_add(mchar_async_cache_t *cache, void* value, size_t size)
609 {
610 1 cache->count++;
611
612 1 ▶ if(cache->nodes_root == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes_root == 0) { 1 0 1
613 1 mchar_async_cache_node_t *list = cache->nodes;
614
615 1 cache->nodes_root = mchar_async_cache_malloc(cache);
616
617 1 list[cache->nodes_root].parent = 0;
618 1 list[cache->nodes_root].left = 0;
619 1 list[cache->nodes_root].right = 0;
620 1 list[cache->nodes_root].size = size;
621 1 list[cache->nodes_root].value = value;
622
623 1 return;
624 }
625
626 0 size_t idx = cache->nodes_root;
627 0 size_t new_idx = mchar_async_cache_malloc(cache);
628
629 0 mchar_async_cache_node_t *list = cache->nodes;
630
631 0 ▶ while(idx)
Branch: 1 branch
BranchTrueFalseTotal
while(idx) 0 0 0
632 {
633 0 ▶ if(size == list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
if(size == list[idx].size) 0 0 0
634 {
635 0 ▶ if(list[idx].parent)
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].parent) 0 0 0
636 {
637 0 ▶ if(list[ list[idx].parent ].left == idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[ list[idx].parent ].left == idx) { 0 0 0
638 0 list[ list[idx].parent ].left = new_idx;
639 }
640 else {
641 0 list[ list[idx].parent ].right = new_idx;
642 }
643
644 0 list[new_idx].parent = list[idx].parent;
645 }
646 else {
647 0 list[new_idx].parent = 0;
648 0 cache->nodes_root = new_idx;
649 }
650
651 0 list[new_idx].right = idx;
652 0 list[new_idx].left = 0;
653 0 list[new_idx].size = size;
654 0 list[new_idx].value = value;
655
656 0 list[idx].parent = new_idx;
657
658 0 break;
659 }
660 0 ▶ else if(size < list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
else if(size < list[idx].size) 0 0 0
661 {
662 0 ▶ if(list[idx].parent)
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].parent) 0 0 0
663 {
664 0 ▶ if(list[ list[idx].parent ].left == idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[ list[idx].parent ].left == idx) { 0 0 0
665 0 list[ list[idx].parent ].left = new_idx;
666 }
667 else {
668 0 list[ list[idx].parent ].right = new_idx;
669 }
670
671 0 list[new_idx].parent = list[idx].parent;
672 }
673 else {
674 0 list[new_idx].parent = 0;
675 0 cache->nodes_root = new_idx;
676 }
677
678 0 list[new_idx].right = idx;
679 0 list[new_idx].left = 0;
680 0 list[new_idx].size = size;
681 0 list[new_idx].value = value;
682
683 0 list[idx].parent = new_idx;
684
685 0 break;
686 }
687 0 ▶ else if(size > list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
else if(size > list[idx].size) 0 0 0
688 {
689 0 ▶ if(list[idx].right == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right == 0) 0 0 0
690 {
691 0 list[idx].right = new_idx;
692
693 0 list[new_idx].right = 0;
694 0 list[new_idx].left = 0;
695 0 list[new_idx].parent = idx;
696 0 list[new_idx].size = size;
697 0 list[new_idx].value = value;
698
699 0 break;
700 }
701 else
702 0 idx = list[idx].right;
703 }
704 else {
705 0 ▶ if(list[idx].left == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].left == 0) 0 0 0
706 {
707 0 list[idx].right = new_idx;
708
709 0 list[new_idx].right = 0;
710 0 list[new_idx].left = 0;
711 0 list[new_idx].parent = idx;
712 0 list[new_idx].size = size;
713 0 list[new_idx].value = value;
714
715 0 break;
716 }
717 else
718 0 idx = list[idx].left;
719 }
720 }
721 }
722  
723  
« source/myhtml/myosi.c ↑ Summary source/myhtml/thread.c »