third_party/modest/source/mycore/utils/mchar_async.c

filter: Statementstmt 0.0% 0 / 450 Branchbran 0.0% 0 / 224 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 674 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« third_party/modest/source/mycore/utils/avl_tree.c ↑ Summary third_party/modest/source/mycore/utils/mcobject.c »
1 /*
2 Copyright (C) 2015-2017 Alexander Borisov
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 Author: lex.borisov@gmail.com (Alexander Borisov)
19 */
20  
21 #include "mycore/utils/mchar_async.h"
22  
23 0 mchar_async_t * mchar_async_create(void)
24 {
25 0 return (mchar_async_t*)mycore_calloc(1, sizeof(mchar_async_t));
26 }
27  
28 0 mystatus_t mchar_async_init(mchar_async_t *mchar_async, size_t chunk_len, size_t char_size)
29 {
30 0 ▶ if(char_size < 4096)
Branch: 1 branch
BranchTrueFalseTotal
if(char_size < 4096) 0 0 0
31 0 char_size = 4096;
32
33 0 mchar_async->origin_size = char_size;
34
35 0 mchar_async->chunks_size = chunk_len;
36 0 mchar_async->chunks_pos_size = 1024;
37
38 /* Chunck, list of mchar_async_chunk_t* */
39 0 mchar_async->chunks = (mchar_async_chunk_t**)mycore_calloc(mchar_async->chunks_pos_size, sizeof(mchar_async_chunk_t*));
40
41 0 ▶ if(mchar_async->chunks == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks == NULL) 0 0 0
42 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
43
44 /* Init first mchar_async_chunk_t* */
45 0 mchar_async->chunks[0] = (mchar_async_chunk_t*)mycore_calloc(mchar_async->chunks_size, sizeof(mchar_async_chunk_t));
46
47 0 ▶ if(mchar_async->chunks[0] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[0] == NULL) { 0 0 0
48 0 mchar_async->chunks = mycore_free(mchar_async->chunks);
49 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
50 }
51
52 /* Init cache */
53 0 mystatus_t status = mchar_async_cache_init(&mchar_async->chunk_cache);
54
55 0 ▶ if(status) {
Branch: 1 branch
BranchTrueFalseTotal
if(status) { 0 0 0
56 0 mycore_free(mchar_async->chunks[0]);
57 0 mchar_async->chunks = mycore_free(mchar_async->chunks);
58
59 0 return status;
60 }
61
62 0 mchar_async->nodes_length = 0;
63 0 mchar_async->nodes_size = 64;
64 0 mchar_async->nodes = (mchar_async_node_t*)mycore_calloc(mchar_async->nodes_size, sizeof(mchar_async_node_t));
65
66 0 ▶ if(mchar_async->nodes == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes == NULL) 0 0 0
67 0 return status;
68
69 0 mchar_async->nodes_cache_length = 0;
70 0 mchar_async->nodes_cache_size = mchar_async->nodes_size;
71 0 mchar_async->nodes_cache = (size_t*)mycore_malloc(mchar_async->nodes_cache_size * sizeof(size_t));
72
73 0 ▶ if(mchar_async->nodes_cache == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache == NULL) 0 0 0
74 0 return status;
75
76 0 mchar_async_clean(mchar_async);
77
78 0 mchar_async->mcsync = mcsync_create();
79 0 ▶ if(mchar_async->mcsync == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->mcsync == NULL) 0 0 0
80 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
81
82 0 ▶ if((status = mcsync_init(mchar_async->mcsync)))
Branch: 1 branch
BranchTrueFalseTotal
if((status = mcsync_init(mchar_async->mcsync))) 0 0 0
83 0 return status;
84
85 0 return MyCORE_STATUS_OK;
86 }
87  
88 0 mystatus_t mchar_async_clean(mchar_async_t *mchar_async)
89 {
90 0 mchar_async->chunks_length = 0;
91 0 mchar_async->chunks_pos_length = 1;
92
93 0 mchar_async_cache_clean(&mchar_async->chunk_cache);
94
95 0 ▶ 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 0 0
96 {
97 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
98 0 mchar_async_cache_clean(&node->cache);
99
100 0 node->chunk = mchar_async_chunk_malloc(mchar_async, node, mchar_async->origin_size);
101
102 0 ▶ if(node->chunk == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(node->chunk == NULL) 0 0 0
103 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
104
105 0 node->chunk->prev = 0;
106 }
107
108 0 return MyCORE_STATUS_OK;
109 }
110  
111 0 mchar_async_t * mchar_async_destroy(mchar_async_t *mchar_async, int destroy_self)
112 {
113 0 ▶ if(mchar_async == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async == NULL) 0 0 0
114 0 return NULL;
115
116 0 ▶ if(mchar_async->nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes) 0 0 0
117 {
118 0 ▶ 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 0 0
119 {
120 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
121 0 mchar_async_cache_destroy(&node->cache, false);
122 }
123
124 0 mycore_free(mchar_async->nodes);
125 0 mchar_async->nodes = NULL;
126 }
127
128 0 ▶ if(mchar_async->nodes_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache) { 0 0 0
129 0 mycore_free(mchar_async->nodes_cache);
130 }
131
132 0 ▶ if(mchar_async->chunks)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks) 0 0 0
133 {
134 0 ▶ 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++) { 0 0 0
135 0 ▶ if(mchar_async->chunks[pos_idx])
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[pos_idx]) 0 0 0
136 {
137 0 ▶ 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++) { 0 0 0
138 0 ▶ if(mchar_async->chunks[pos_idx][idx].begin)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[pos_idx][idx].begin) 0 0 0
139 0 mycore_free(mchar_async->chunks[pos_idx][idx].begin);
140 }
141
142 0 mycore_free(mchar_async->chunks[pos_idx]);
143 }
144 }
145
146 0 mycore_free(mchar_async->chunks);
147 0 mchar_async->chunks = NULL;
148 }
149
150 0 mchar_async_cache_destroy(&mchar_async->chunk_cache, false);
151
152 0 mchar_async->mcsync = mcsync_destroy(mchar_async->mcsync, 1);
153
154 0 memset(mchar_async, 0, sizeof(mchar_async_t));
155
156 0 ▶ if(destroy_self)
Branch: 1 branch
BranchTrueFalseTotal
if(destroy_self) 0 0 0
157 0 mycore_free(mchar_async);
158 else
159 0 return mchar_async;
160
161 0 return NULL;
162 }
163  
164 0 void mchar_async_mem_malloc(mchar_async_t *mchar_async, mchar_async_node_t *node, mchar_async_chunk_t *chunk, size_t length)
165 {
166 0 ▶ if(chunk == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk == NULL) 0 0 0
167 0 return;
168
169 0 ▶ if(chunk->begin) {
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->begin) { 0 0 0
170 0 ▶ if(length > chunk->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > chunk->size) { 0 0 0
171 0 mycore_free(chunk->begin);
172
173 0 chunk->size = length + mchar_async->origin_size;
174 0 chunk->begin = (char*)mycore_malloc(chunk->size * sizeof(char));
175 }
176 }
177 else {
178 0 chunk->size = mchar_async->origin_size;
179
180 0 ▶ if(length > chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if(length > chunk->size) 0 0 0
181 0 chunk->size = length;
182
183 0 chunk->begin = (char*)mycore_malloc(chunk->size * sizeof(char));
184 }
185
186 0 chunk->length = 0;
187 }
188  
189 0 mchar_async_chunk_t * mchar_async_chunk_malloc_without_lock(mchar_async_t *mchar_async, mchar_async_node_t *node, size_t length)
190 {
191 0 ▶ 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 0 0
192 {
193 0 size_t index = mchar_async_cache_delete(&mchar_async->chunk_cache, length);
194
195 0 ▶ if(index)
Branch: 1 branch
BranchTrueFalseTotal
if(index) 0 0 0
196 0 return (mchar_async_chunk_t*)mchar_async->chunk_cache.nodes[index].value;
197 else
198 0 return NULL;
199 }
200
201 0 ▶ if(mchar_async->chunks_length >= mchar_async->chunks_size)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks_length >= mchar_async->chunks_size) 0 0 0
202 {
203 0 size_t current_idx = mchar_async->chunks_pos_length;
204 0 mchar_async->chunks_pos_length++;
205
206 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
207 {
208 0 mchar_async->chunks_pos_size <<= 1;
209 0 mchar_async_chunk_t **tmp_pos = mycore_realloc(mchar_async->chunks,
210 0 sizeof(mchar_async_chunk_t*) * mchar_async->chunks_pos_size);
211
212 0 ▶ if(tmp_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp_pos) { 0 0 0
213 0 memset(&tmp_pos[mchar_async->chunks_pos_length], 0, (mchar_async->chunks_pos_size - mchar_async->chunks_pos_length)
214 * sizeof(mchar_async_chunk_t*));
215
216 0 mchar_async->chunks = tmp_pos;
217 }
218 else
219 0 return NULL;
220 }
221
222 0 ▶ if(mchar_async->chunks[current_idx] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->chunks[current_idx] == NULL) { 0 0 0
223 0 mchar_async_chunk_t *tmp = mycore_calloc(mchar_async->chunks_size, sizeof(mchar_async_chunk_t));
224
225 0 ▶ if(tmp)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) 0 0 0
226 0 mchar_async->chunks[current_idx] = tmp;
227 else
228 0 return NULL;
229 }
230
231 0 mchar_async->chunks_length = 0;
232 }
233
234 0 mchar_async_chunk_t *chunk = &mchar_async->chunks[mchar_async->chunks_pos_length - 1][mchar_async->chunks_length];
235 0 mchar_async->chunks_length++;
236
237 0 mchar_async_mem_malloc(mchar_async, node, chunk, length);
238
239 0 ▶ if(chunk->begin == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->begin == NULL) 0 0 0
240 0 return NULL;
241
242 0 return chunk;
243 }
244  
245 0 mchar_async_chunk_t * mchar_async_chunk_malloc(mchar_async_t *mchar_async, mchar_async_node_t *node, size_t length)
246 {
247 0 mcsync_lock(mchar_async->mcsync);
248 0 mchar_async_chunk_t *chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, length);
249 0 mcsync_unlock(mchar_async->mcsync);
250
251 0 return chunk;
252 }
253  
254 0 size_t mchar_async_node_add(mchar_async_t *mchar_async, mystatus_t* status)
255 {
256 0 ▶ if(mcsync_lock(mchar_async->mcsync)) {
Branch: 1 branch
BranchTrueFalseTotal
if(mcsync_lock(mchar_async->mcsync)) { 0 0 0
257 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
258 0 *status = MyCORE_STATUS_ASYNC_ERROR_LOCK;
259
260 0 return 0;
261 }
262
263 size_t node_idx;
264
265 0 ▶ if(mchar_async->nodes_cache_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_cache_length) { 0 0 0
266 0 mchar_async->nodes_cache_length--;
267
268 0 node_idx = mchar_async->nodes_cache[ mchar_async->nodes_cache_length ];
269 }
270 else {
271 0 ▶ if(mchar_async->nodes_length >= mchar_async->nodes_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length >= mchar_async->nodes_size) { 0 0 0
272 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
273 0 *status = MyCORE_STATUS_ERROR_NO_FREE_SLOT;
274
275 0 mcsync_unlock(mchar_async->mcsync);
276 0 return 0;
277 }
278
279 0 node_idx = mchar_async->nodes_length;
280 0 mchar_async->nodes_length++;
281 }
282
283 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
284
285 0 ▶ if(mchar_async_cache_init(&node->cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async_cache_init(&node->cache)) { 0 0 0
286 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
287 0 *status = MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
288
289 0 mcsync_unlock(mchar_async->mcsync);
290 0 return 0;
291 }
292
293 0 node->chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, mchar_async->origin_size);
294
295 0 ▶ if(node->chunk == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(node->chunk == NULL) { 0 0 0
296 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
297 0 *status = MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
298
299 0 mcsync_unlock(mchar_async->mcsync);
300 0 return 0;
301 }
302
303 0 node->chunk->next = NULL;
304 0 node->chunk->prev = NULL;
305
306 0 mcsync_unlock(mchar_async->mcsync);
307
308 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
309 0 *status = MyCORE_STATUS_OK;
310
311 0 return node_idx;
312 }
313  
314 0 void mchar_async_node_clean(mchar_async_t *mchar_async, size_t node_idx)
315 {
316 0 ▶ if(mchar_async->nodes_length <= node_idx)
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length <= node_idx) 0 0 0
317 0 return;
318
319 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
320
321 0 ▶ while (node->chunk->prev)
Branch: 1 branch
BranchTrueFalseTotal
while (node->chunk->prev) 0 0 0
322 0 node->chunk = node->chunk->prev;
323
324 0 node->chunk->length = 0;
325 0 mchar_async_cache_clean(&node->cache);
326 }
327  
328 0 void mchar_async_node_delete(mchar_async_t *mchar_async, size_t node_idx)
329 {
330 0 mcsync_lock(mchar_async->mcsync);
331
332 0 ▶ if(mchar_async->nodes_length <= node_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async->nodes_length <= node_idx) { 0 0 0
333 0 mcsync_unlock(mchar_async->mcsync);
334 0 return;
335 }
336
337 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
338 0 mchar_async_chunk_t *chunk = node->chunk;
339
340 0 ▶ while (chunk->next)
Branch: 1 branch
BranchTrueFalseTotal
while (chunk->next) 0 0 0
341 0 chunk = chunk->next;
342
343 0 ▶ while (chunk)
Branch: 1 branch
BranchTrueFalseTotal
while (chunk) 0 0 0
344 {
345 0 mchar_async_cache_add(&mchar_async->chunk_cache, (void*)chunk, chunk->size);
346 0 chunk = chunk->prev;
347 }
348
349 0 ▶ if(node->cache.nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(node->cache.nodes) 0 0 0
350 0 mchar_async_cache_destroy(&node->cache, false);
351
352 0 memset(node, 0, sizeof(mchar_async_node_t));
353
354 0 ▶ 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 0 0
355 0 size_t new_size = mchar_async->nodes_cache_size << 1;
356
357 0 size_t *tmp = (size_t*)mycore_realloc(mchar_async->nodes_cache, sizeof(size_t) * mchar_async->nodes_cache_size);
358
359 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
360 0 mchar_async->nodes_cache = tmp;
361 0 mchar_async->nodes_cache_size = new_size;
362 }
363 }
364
365 0 mchar_async->nodes_cache[ mchar_async->nodes_cache_length ] = node_idx;
366 0 mchar_async->nodes_cache_length++;
367
368 0 mcsync_unlock(mchar_async->mcsync);
369 }
370  
371 0 mchar_async_chunk_t * mchar_sync_chunk_find_by_size(mchar_async_node_t *node, size_t size)
372 {
373 0 mchar_async_chunk_t *chunk = node->chunk->next;
374
375 0 ▶ while (chunk) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk) { 0 0 0
376 0 ▶ if(chunk->size >= size)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->size >= size) 0 0 0
377 0 return chunk;
378
379 0 chunk = chunk->next;
380 }
381
382 0 return NULL;
383 }
384  
385 0 void mchar_sync_chunk_insert_after(mchar_async_chunk_t *base, mchar_async_chunk_t *chunk)
386 {
387 0 ▶ if(base->next == chunk)
Branch: 1 branch
BranchTrueFalseTotal
if(base->next == chunk) 0 0 0
388 0 return;
389
390 0 ▶ if(chunk->prev)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->prev) 0 0 0
391 0 chunk->prev->next = chunk->next;
392
393 0 ▶ if(chunk->next)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk->next) 0 0 0
394 0 chunk->next->prev = chunk->prev;
395
396 0 ▶ if(base->next)
Branch: 1 branch
BranchTrueFalseTotal
if(base->next) 0 0 0
397 0 base->next->prev = chunk;
398
399 0 chunk->next = base->next;
400 0 chunk->prev = base;
401
402 0 base->next = chunk;
403 }
404  
405 0 char * mchar_async_malloc(mchar_async_t *mchar_async, size_t node_idx, size_t size)
406 {
407 0 ▶ if(size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(size == 0) 0 0 0
408 0 return NULL;
409
410 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
411 0 mchar_async_chunk_t *chunk = node->chunk;
412
413 0 ▶ if(mchar_async_cache_has_nodes(node->cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if(mchar_async_cache_has_nodes(node->cache)) { 0 0 0
414 0 size_t index = mchar_async_cache_delete(&node->cache, size);
415
416 0 ▶ if(index) {
Branch: 1 branch
BranchTrueFalseTotal
if(index) { 0 0 0
417 0 return (char *)(node->cache.nodes[index].value);
418 }
419 }
420
421 0 size_t new_size = chunk->length + size + sizeof(size_t);
422
423 0 ▶ if(new_size > chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if(new_size > chunk->size) 0 0 0
424 {
425 0 ▶ if((chunk->length + sizeof(size_t)) < chunk->size)
Branch: 1 branch
BranchTrueFalseTotal
if((chunk->length + sizeof(size_t)) < chunk->size) 0 0 0
426 {
427 0 size_t calc_size = (chunk->size - chunk->length) - sizeof(size_t);
428
429 0 ▶ if(calc_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(calc_size) { 0 0 0
430 0 char *tmp = &chunk->begin[(chunk->length + sizeof(size_t))];
431 0 memcpy(&chunk->begin[chunk->length], &calc_size, sizeof(size_t));
432
433 0 chunk->length = chunk->size;
434
435 0 mchar_async_cache_add(&node->cache, tmp, calc_size);
436 }
437 }
438
439 0 chunk = mchar_sync_chunk_find_by_size(node, (size + sizeof(size_t)));
440
441 0 ▶ if(chunk)
Branch: 1 branch
BranchTrueFalseTotal
if(chunk) 0 0 0
442 0 chunk->length = 0;
443 else {
444 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
445 0 chunk = mchar_async_chunk_malloc(mchar_async, node, (size + sizeof(size_t) + mchar_async->origin_size));
446 else
447 0 chunk = mchar_async_chunk_malloc(mchar_async, node, mchar_async->origin_size);
448 }
449
450 0 mchar_sync_chunk_insert_after(node->chunk, chunk);
451 0 node->chunk = chunk;
452 }
453
454 0 char *tmp = &chunk->begin[(chunk->length + sizeof(size_t))];
455 0 memcpy(&chunk->begin[chunk->length], &size, sizeof(size_t));
456
457 0 chunk->length = chunk->length + size + sizeof(size_t);
458
459 0 return tmp;
460 }
461  
462 0 char * mchar_async_realloc(mchar_async_t *mchar_async, size_t node_idx, char *data, size_t data_len, size_t new_size)
463 {
464 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
465 0 return NULL;
466
467 size_t curr_size;
468 0 memcpy(&curr_size, (data - sizeof(size_t)), sizeof(size_t));
469
470 0 ▶ if(curr_size >= new_size)
Branch: 1 branch
BranchTrueFalseTotal
if(curr_size >= new_size) 0 0 0
471 0 return data;
472
473 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
474
475 0 ▶ if(node->chunk->length >= curr_size &&
Branch: 1 branch
BranchTrueFalseTotal
if(node->chunk->length >= curr_size && 0 0 0
476 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
477 {
478 0 size_t next_size = (node->chunk->length - curr_size) + new_size;
479
480 0 ▶ if(next_size <= node->chunk->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(next_size <= node->chunk->size) { 0 0 0
481 /* it`s Magic */
482 0 memcpy(&node->chunk->begin[ ((node->chunk->length - curr_size) - sizeof(size_t)) ], &new_size, sizeof(size_t));
483
484 0 node->chunk->length = next_size;
485
486 0 return data;
487 }
488 // else {
489 // size_t re_size = next_size - node->chunk->length;
490 //
491 // /* a little Magic ;) */
492 // *((size_t*)(&node->chunk->begin[ ((node->chunk->length - curr_size) - sizeof(size_t)) ])) = re_size;
493 //
494 // curr_size = re_size;
495 // }
496 }
497
498 0 char *tmp = mchar_async_malloc(mchar_async, node_idx, new_size);
499
500 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
501 0 memcpy(tmp, data, sizeof(char) * data_len);
502
503 0 mchar_async_cache_add(&node->cache, data, curr_size);
504 }
505
506 0 return tmp;
507 }
508  
509 0 char * mchar_async_crop_first_chars(mchar_async_t *mchar_async, size_t node_idx, char *data, size_t crop_len)
510 {
511 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
512 0 return NULL;
513
514 size_t curr_size;
515 0 memcpy(&curr_size, (data - sizeof(size_t)), sizeof(size_t));
516
517 0 char *tmp_old = data;
518 0 data = &data[crop_len];
519
520 0 curr_size -= crop_len;
521 0 memcpy((data - sizeof(size_t)), &curr_size, sizeof(size_t));
522
523 0 ▶ if((crop_len + 4) > sizeof(size_t)) {
Branch: 1 branch
BranchTrueFalseTotal
if((crop_len + 4) > sizeof(size_t)) { 0 0 0
524 0 crop_len = crop_len - sizeof(size_t);
525 0 memcpy((tmp_old - sizeof(size_t)), &crop_len, sizeof(size_t));
526
527 0 mchar_async_node_t *node = &mchar_async->nodes[node_idx];
528 0 mchar_async_cache_add(&node->cache, tmp_old, crop_len);
529 }
530
531 0 return data;
532 }
533  
534 0 char * mchar_async_crop_first_chars_without_cache(char *data, size_t crop_len)
535 {
536 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
537 0 return NULL;
538
539 size_t curr_size;
540 0 memcpy(&curr_size, (data - sizeof(size_t)), sizeof(size_t));
541
542 0 data = &data[crop_len];
543
544 0 curr_size -= crop_len;
545 0 memcpy((data - sizeof(size_t)), &curr_size, sizeof(size_t));
546
547 0 return data;
548 }
549  
550 0 size_t mchar_async_get_size_by_data(const char *data)
551 {
552 0 ▶ if(data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) 0 0 0
553 0 return 0;
554
555 0 return *((size_t*)(data - sizeof(size_t)));
556 }
557  
558 0 void mchar_async_free(mchar_async_t *mchar_async, size_t node_idx, char *entry)
559 {
560 0 ▶ if(entry)
Branch: 1 branch
BranchTrueFalseTotal
if(entry) 0 0 0
561 0 mchar_async_cache_add(&mchar_async->nodes[node_idx].cache, entry, *(size_t*)(entry - sizeof(size_t)));
562 0 }
563  
564 0 mystatus_t mchar_async_cache_init(mchar_async_cache_t *cache)
565 {
566 0 cache->count = 0;
567 0 cache->nodes_root = 0;
568 0 cache->nodes_length = 1;
569 0 cache->nodes_size = 1024;
570 0 cache->nodes = (mchar_async_cache_node_t*)mycore_malloc(sizeof(mchar_async_cache_node_t) * cache->nodes_size);
571
572 0 ▶ if(cache->nodes == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes == NULL) 0 0 0
573 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
574
575 0 cache->nodes[0].left = 0;
576 0 cache->nodes[0].right = 0;
577 0 cache->nodes[0].size = 0;
578 0 cache->nodes[0].value = NULL;
579
580 0 cache->index_length = 0;
581 0 cache->index_size = cache->nodes_size;
582 0 cache->index = (size_t*)mycore_malloc(sizeof(size_t) * cache->index_size);
583
584 0 ▶ if(cache->index == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index == NULL) { 0 0 0
585 0 cache->nodes = mycore_free(cache->nodes);
586 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
587 }
588
589 0 return MyCORE_STATUS_OK;
590 }
591  
592 0 void mchar_async_cache_clean(mchar_async_cache_t *cache)
593 {
594 0 cache->count = 0;
595 0 cache->nodes_root = 0;
596 0 cache->nodes_length = 1;
597 0 cache->index_length = 0;
598
599 0 ▶ if(cache->nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes) { 0 0 0
600 0 cache->nodes[0].left = 0;
601 0 cache->nodes[0].right = 0;
602 0 cache->nodes[0].size = 0;
603 0 cache->nodes[0].value = NULL;
604 }
605 0 }
606  
607 0 mchar_async_cache_t * mchar_async_cache_destroy(mchar_async_cache_t *cache, bool self_destroy)
608 {
609 0 ▶ if(cache == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(cache == NULL) 0 0 0
610 0 return NULL;
611
612 0 ▶ if(cache->nodes)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes) 0 0 0
613 0 mycore_free(cache->nodes);
614
615 0 ▶ if(cache->index)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index) 0 0 0
616 0 mycore_free(cache->index);
617
618 0 ▶ if(self_destroy) {
Branch: 1 branch
BranchTrueFalseTotal
if(self_destroy) { 0 0 0
619 0 mycore_free(cache);
620 0 return NULL;
621 }
622
623 0 return cache;
624 }
625  
626 0 size_t mchar_async_cache_malloc(mchar_async_cache_t *cache)
627 {
628 0 ▶ if(cache->index_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index_length) { 0 0 0
629 0 cache->index_length--;
630 0 return cache->index[cache->index_length];
631 }
632
633 0 cache->nodes_length++;
634
635 0 ▶ if(cache->nodes_length >= cache->nodes_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes_length >= cache->nodes_size) { 0 0 0
636 0 cache->nodes_size <<= 1;
637
638 0 mchar_async_cache_node_t *tmp = (mchar_async_cache_node_t*)mycore_realloc(cache->nodes, sizeof(mchar_async_cache_node_t) * cache->nodes_size);
639
640 0 ▶ if(tmp)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) 0 0 0
641 0 cache->nodes = tmp;
642 }
643
644 0 return cache->nodes_length - 1;
645 }
646  
647 0 size_t mchar_async_cache_delete(mchar_async_cache_t *cache, size_t size)
648 {
649 0 mchar_async_cache_node_t *list = cache->nodes;
650 0 size_t idx = cache->nodes_root;
651
652 0 ▶ while (idx)
Branch: 1 branch
BranchTrueFalseTotal
while (idx) 0 0 0
653 {
654 0 ▶ if(size <= list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
if(size <= list[idx].size) 0 0 0
655 {
656 0 ▶ while( list[ list[idx].right ].size == size )
Branch: 1 branch
BranchTrueFalseTotal
while( list[ list[idx].right ].size == size ) 0 0 0
657 0 idx = list[idx].right;
658
659 0 size_t parent = list[idx].parent;
660
661 0 ▶ if(parent) {
Branch: 1 branch
BranchTrueFalseTotal
if(parent) { 0 0 0
662 0 ▶ if(list[parent].left == idx)
Branch: 1 branch
BranchTrueFalseTotal
if(list[parent].left == idx) 0 0 0
663 {
664 0 ▶ if(list[idx].right) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right) { 0 0 0
665 0 ▶ if(list[idx].left) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].left) { 0 0 0
666 0 size_t last_left = list[ list[idx].right ].left;
667
668 0 ▶ while( list[last_left].left )
Branch: 1 branch
BranchTrueFalseTotal
while( list[last_left].left ) 0 0 0
669 0 last_left = list[last_left].left;
670
671 0 ▶ if(last_left) {
Branch: 1 branch
BranchTrueFalseTotal
if(last_left) { 0 0 0
672 0 list[last_left].left = list[idx].left;
673 0 list[ list[idx].left ].parent = last_left;
674 }
675 else {
676 0 list[ list[idx].right ].left = list[idx].left;
677 }
678 }
679
680 0 list[parent].left = list[idx].right;
681 0 list[ list[idx].right ].parent = parent;
682 }
683 else {
684 0 list[parent].left = list[idx].left;
685 0 list[ list[idx].left ].parent = parent;
686 }
687 }
688 else {
689 0 ▶ if(list[idx].left) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].left) { 0 0 0
690 0 ▶ if(list[idx].right) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right) { 0 0 0
691 0 size_t last_right = list[ list[idx].left ].right;
692
693 0 ▶ while( list[last_right].right )
Branch: 1 branch
BranchTrueFalseTotal
while( list[last_right].right ) 0 0 0
694 0 last_right = list[last_right].right;
695
696 0 ▶ if(last_right) {
Branch: 1 branch
BranchTrueFalseTotal
if(last_right) { 0 0 0
697 0 list[last_right].right = list[idx].right;
698 0 list[ list[idx].right ].parent = last_right;
699 }
700 else {
701 0 list[ list[idx].left ].right = list[idx].right;
702 }
703 }
704
705 0 list[parent].right = list[idx].left;
706 0 list[ list[idx].left ].parent = parent;
707 }
708 else {
709 0 list[parent].right = list[idx].right;
710 0 list[ list[idx].right ].parent = parent;
711 }
712 }
713 }
714 else {
715 0 ▶ if(list[idx].left) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].left) { 0 0 0
716 0 ▶ if(list[idx].right) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right) { 0 0 0
717 0 size_t last_right = list[ list[idx].left ].right;
718
719 0 ▶ while( list[last_right].right )
Branch: 1 branch
BranchTrueFalseTotal
while( list[last_right].right ) 0 0 0
720 0 last_right = list[last_right].right;
721
722 0 ▶ if(last_right) {
Branch: 1 branch
BranchTrueFalseTotal
if(last_right) { 0 0 0
723 0 list[last_right].right = list[idx].right;
724 0 list[ list[idx].right ].parent = last_right;
725 }
726 else {
727 0 list[ list[idx].left ].right = list[idx].right;
728 }
729 }
730
731 0 cache->nodes_root = list[idx].left;
732 0 list[ list[idx].left ].parent = 0;
733 }
734 else {
735 0 cache->nodes_root = list[idx].right;
736 0 list[ list[idx].right ].parent = 0;
737 }
738 }
739
740 0 cache->index[cache->index_length] = idx;
741
742 0 cache->index_length++;
743 0 ▶ if(cache->index_length >= cache->index_size)
Branch: 1 branch
BranchTrueFalseTotal
if(cache->index_length >= cache->index_size) 0 0 0
744 {
745 0 size_t new_size = cache->index_size << 1;
746 0 size_t *tmp = (size_t*)mycore_realloc(cache->index, sizeof(size_t) * new_size);
747
748 0 ▶ if(tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if(tmp) { 0 0 0
749 0 cache->index = tmp;
750 0 cache->index_size = new_size;
751 }
752 else
753 0 return 0;
754 }
755
756 0 cache->count--;
757
758 0 return idx;
759 }
760 else {
761 0 idx = list[idx].right;
762 }
763 }
764
765 0 return 0;
766 }
767  
768 0 void mchar_async_cache_add(mchar_async_cache_t *cache, void* value, size_t size)
769 {
770 0 cache->count++;
771
772 0 ▶ if(cache->nodes_root == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(cache->nodes_root == 0) { 0 0 0
773 0 mchar_async_cache_node_t *list = cache->nodes;
774
775 0 cache->nodes_root = mchar_async_cache_malloc(cache);
776
777 0 list[cache->nodes_root].parent = 0;
778 0 list[cache->nodes_root].left = 0;
779 0 list[cache->nodes_root].right = 0;
780 0 list[cache->nodes_root].size = size;
781 0 list[cache->nodes_root].value = value;
782
783 0 return;
784 }
785
786 0 size_t idx = cache->nodes_root;
787 0 size_t new_idx = mchar_async_cache_malloc(cache);
788
789 0 mchar_async_cache_node_t *list = cache->nodes;
790
791 0 ▶ while(idx)
Branch: 1 branch
BranchTrueFalseTotal
while(idx) 0 0 0
792 {
793 0 ▶ if(size == list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
if(size == list[idx].size) 0 0 0
794 {
795 0 ▶ if(list[idx].right) {
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right) { 0 0 0
796 0 list[new_idx].right = list[idx].right;
797 0 list[ list[idx].right ].parent = new_idx;
798 }
799 else {
800 0 list[new_idx].right = 0;
801 }
802
803 0 list[idx].right = new_idx;
804
805 0 list[new_idx].parent = idx;
806 0 list[new_idx].left = 0;
807 0 list[new_idx].size = size;
808 0 list[new_idx].value = value;
809
810 0 break;
811 }
812 0 ▶ else if(size < list[idx].size)
Branch: 1 branch
BranchTrueFalseTotal
else if(size < list[idx].size) 0 0 0
813 {
814 0 size_t parent = list[idx].parent;
815
816 0 ▶ if(parent) {
Branch: 1 branch
BranchTrueFalseTotal
if(parent) { 0 0 0
817 0 ▶ if(list[parent].left == idx)
Branch: 1 branch
BranchTrueFalseTotal
if(list[parent].left == idx) 0 0 0
818 0 list[parent].left = new_idx;
819 else
820 0 list[parent].right = new_idx;
821
822 0 list[new_idx].parent = parent;
823 }
824 else {
825 0 cache->nodes_root = new_idx;
826 0 list[new_idx].parent = 0;
827 }
828
829 0 list[idx].parent = new_idx;
830
831 0 list[new_idx].right = idx;
832 0 list[new_idx].left = 0;
833 0 list[new_idx].size = size;
834 0 list[new_idx].value = value;
835
836 0 break;
837 }
838 else // size > list[idx].size
839 {
840 0 ▶ if(list[idx].right)
Branch: 1 branch
BranchTrueFalseTotal
if(list[idx].right) 0 0 0
841 0 idx = list[idx].right;
842 else {
843 0 list[idx].right = new_idx;
844
845 0 list[new_idx].right = 0;
846 0 list[new_idx].left = 0;
847 0 list[new_idx].parent = idx;
848 0 list[new_idx].size = size;
849 0 list[new_idx].value = value;
850
851 0 break;
852 }
853 }
854 }
855 }
856  
857  
« third_party/modest/source/mycore/utils/avl_tree.c ↑ Summary third_party/modest/source/mycore/utils/mcobject.c »