| 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/mcobject_async.h" |
| 20 |
|
| |
| 21 |
5 |
| mcobject_async_t * mcobject_async_create(void) |
| 22 |
|
| { |
| 23 |
5 |
| return (mcobject_async_t*)mycalloc(1, sizeof(mcobject_async_t)); |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
5 |
| mcobject_async_status_t mcobject_async_chunk_up(mcobject_async_t *mcobj_async) |
| 27 |
|
| { |
| 28 |
5 |
| mcobj_async->chunks_length = 0; |
| 29 |
|
| |
| 30 |
5 |
▶ | if(mcobj_async->chunks[ mcobj_async->chunks_pos_length ] == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks[ mcobj_async->chunks_pos_length ] == NULL) { |
5 |
0 |
5 |
|
| 31 |
5 |
| mcobj_async->chunks[ mcobj_async->chunks_pos_length ] = (mcobject_async_chunk_t*)mycalloc(mcobj_async->chunks_size, sizeof(mcobject_async_chunk_t)); |
| 32 |
|
| |
| 33 |
5 |
▶ | if(mcobj_async->chunks[ mcobj_async->chunks_pos_length ] == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks[ mcobj_async->chunks_pos_length ] == NULL) |
0 |
5 |
5 |
|
| 34 |
0 |
| return MCOBJECT_ASYNC_STATUS_CHUNK_ERROR_MEMORY_ALLOCATION; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
5 |
| return MCOBJECT_ASYNC_STATUS_OK; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
5 |
| mcobject_async_status_t mcobject_async_init(mcobject_async_t *mcobj_async, size_t chunk_len, size_t obj_size_by_one_chunk, size_t struct_size) |
| 41 |
|
| { |
| 42 |
5 |
| mcobj_async->origin_size = obj_size_by_one_chunk; |
| 43 |
5 |
| mcobj_async->struct_size = struct_size; |
| 44 |
5 |
| mcobj_async->struct_size_sn = struct_size + sizeof(size_t); |
| 45 |
|
| |
| 46 |
5 |
| mcobj_async->chunks_pos_length = 0; |
| 47 |
5 |
| mcobj_async->chunks_pos_size = 128; |
| 48 |
5 |
| mcobj_async->chunks_size = chunk_len; |
| 49 |
5 |
| mcobj_async->chunks = (mcobject_async_chunk_t**)mycalloc(mcobj_async->chunks_pos_size, sizeof(mcobject_async_chunk_t*)); |
| 50 |
|
| |
| 51 |
5 |
▶ | if(mcobj_async->chunks == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks == NULL) |
0 |
5 |
5 |
|
| 52 |
0 |
| return MCOBJECT_ASYNC_STATUS_CHUNK_ERROR_MEMORY_ALLOCATION; |
| 53 |
|
| |
| 54 |
5 |
| mcobject_async_chunk_up(mcobj_async); |
| 55 |
|
| |
| 56 |
5 |
| mcobj_async->chunk_cache_size = mcobj_async->chunks_size; |
| 57 |
5 |
| mcobj_async->chunk_cache = (mcobject_async_chunk_t**)mycalloc(mcobj_async->chunk_cache_size, sizeof(mcobject_async_chunk_t*)); |
| 58 |
|
| |
| 59 |
5 |
▶ | if(mcobj_async->chunk_cache == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunk_cache == NULL) |
0 |
5 |
5 |
|
| 60 |
0 |
| return MCOBJECT_ASYNC_STATUS_CHUNK_CACHE_ERROR_MEMORY_ALLOCATION; |
| 61 |
|
| |
| 62 |
5 |
| mcobj_async->nodes_length = 0; |
| 63 |
5 |
| mcobj_async->nodes_size = 64; |
| 64 |
5 |
| mcobj_async->nodes = (mcobject_async_node_t*)mycalloc(mcobj_async->nodes_size, sizeof(mcobject_async_node_t)); |
| 65 |
|
| |
| 66 |
5 |
▶ | if(mcobj_async->nodes == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes == NULL) |
0 |
5 |
5 |
|
| 67 |
0 |
| return MCOBJECT_ASYNC_STATUS_NODES_ERROR_MEMORY_ALLOCATION; |
| 68 |
|
| |
| 69 |
5 |
| mcobj_async->nodes_cache_length = 0; |
| 70 |
5 |
| mcobj_async->nodes_cache_size = mcobj_async->nodes_size; |
| 71 |
5 |
| mcobj_async->nodes_cache = (size_t*)mymalloc(mcobj_async->nodes_cache_size * sizeof(size_t)); |
| 72 |
|
| |
| 73 |
5 |
▶ | if(mcobj_async->nodes_cache == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_cache == NULL) |
0 |
5 |
5 |
|
| 74 |
0 |
| return MCOBJECT_ASYNC_STATUS_NODES_ERROR_MEMORY_ALLOCATION; |
| 75 |
|
| |
| 76 |
5 |
| mcobject_async_clean(mcobj_async); |
| 77 |
|
| |
| 78 |
5 |
| mcobj_async->mcsync = mcsync_create(); |
| 79 |
|
| |
| 80 |
5 |
| return MCOBJECT_ASYNC_STATUS_OK; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
5 |
| void mcobject_async_clean(mcobject_async_t *mcobj_async) |
| 84 |
|
| { |
| 85 |
5 |
▶ | if(mcobj_async->chunks[0] != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks[0] != NULL) |
5 |
0 |
5 |
|
| 86 |
5 |
| mcobj_async->chunks_pos_length = 1; |
| 87 |
|
| else |
| 88 |
0 |
| mcobj_async->chunks_pos_length = 0; |
| 89 |
|
| |
| 90 |
5 |
| mcobj_async->chunks_length = 0; |
| 91 |
5 |
| mcobj_async->chunk_cache_length = 0; |
| 92 |
|
| |
| 93 |
|
| size_t node_idx; |
| 94 |
5 |
▶ | for (node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) |
0 |
5 |
5 |
|
| 95 |
|
| { |
| 96 |
0 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 97 |
0 |
| node->cache_length = 0; |
| 98 |
|
| |
| 99 |
0 |
▶ | if(node->chunk) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->chunk) { |
0 |
0 |
0 |
|
| 100 |
0 |
| node->chunk = mcobject_async_chunk_malloc(mcobj_async, mcobj_async->origin_size, NULL); |
| 101 |
|
| } |
| 102 |
|
| } |
| 103 |
5 |
| } |
| 104 |
|
| |
| 105 |
5 |
| mcobject_async_t * mcobject_async_destroy(mcobject_async_t *mcobj_async, int destroy_self) |
| 106 |
|
| { |
| 107 |
5 |
▶ | if(mcobj_async == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async == NULL) |
0 |
5 |
5 |
|
| 108 |
0 |
| return NULL; |
| 109 |
|
| |
| 110 |
5 |
▶ | if(mcobj_async->nodes) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes) |
5 |
0 |
5 |
|
| 111 |
|
| { |
| 112 |
12 |
▶ | for (size_t node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) |
7 |
5 |
12 |
|
| 113 |
|
| { |
| 114 |
7 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 115 |
|
| |
| 116 |
7 |
▶ | if(node->cache) |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->cache) |
5 |
2 |
7 |
|
| 117 |
5 |
| free(node->cache); |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
5 |
| free(mcobj_async->nodes); |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
5 |
▶ | if(mcobj_async->nodes_cache) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_cache) { |
5 |
0 |
5 |
|
| 124 |
5 |
| free(mcobj_async->nodes_cache); |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
5 |
▶ | if(mcobj_async->chunks) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks) { |
5 |
0 |
5 |
|
| 128 |
10 |
▶ | for (size_t pos_idx = 0; pos_idx < mcobj_async->chunks_pos_length; pos_idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t pos_idx = 0; pos_idx < mcobj_async->chunks_pos_length; pos_idx++) { |
5 |
5 |
10 |
|
| 129 |
5 |
▶ | if(mcobj_async->chunks[pos_idx]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks[pos_idx]) |
5 |
0 |
5 |
|
| 130 |
|
| { |
| 131 |
549 |
▶ | for (size_t idx = 0; idx < mcobj_async->chunks_size; idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t idx = 0; idx < mcobj_async->chunks_size; idx++) { |
544 |
5 |
549 |
|
| 132 |
544 |
▶ | if(mcobj_async->chunks[pos_idx][idx].begin) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks[pos_idx][idx].begin) |
7 |
537 |
544 |
|
| 133 |
7 |
| free(mcobj_async->chunks[pos_idx][idx].begin); |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
5 |
| free(mcobj_async->chunks[pos_idx]); |
| 137 |
|
| } |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
5 |
| free(mcobj_async->chunks); |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
5 |
▶ | if(mcobj_async->chunk_cache) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunk_cache) { |
5 |
0 |
5 |
|
| 144 |
5 |
| free(mcobj_async->chunk_cache); |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
5 |
| mcobj_async->mcsync = mcsync_destroy(mcobj_async->mcsync, 1); |
| 148 |
|
| |
| 149 |
5 |
| memset(mcobj_async, 0, sizeof(mcobject_async_t)); |
| 150 |
|
| |
| 151 |
5 |
▶ | if(destroy_self) |
Branch: 1 branch
| Branch | True | False | Total |
| if(destroy_self) |
5 |
0 |
5 |
|
| 152 |
5 |
| free(mcobj_async); |
| 153 |
|
| else |
| 154 |
0 |
| return mcobj_async; |
| 155 |
|
| |
| 156 |
5 |
| return NULL; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
7 |
| mcobject_async_status_t mcobject_async_mem_malloc(mcobject_async_t *mcobj_async, mcobject_async_chunk_t *chunk, size_t length) |
| 160 |
|
| { |
| 161 |
7 |
▶ | if(chunk->begin) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(chunk->begin) { |
0 |
7 |
7 |
|
| 162 |
0 |
▶ | if(length > chunk->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(length > chunk->size) { |
0 |
0 |
0 |
|
| 163 |
0 |
| free(chunk->begin); |
| 164 |
|
| |
| 165 |
0 |
| chunk->size = length + mcobj_async->origin_size; |
| 166 |
0 |
| chunk->begin = (char*)mymalloc(chunk->size * mcobj_async->struct_size_sn); |
| 167 |
|
| } |
| 168 |
|
| } |
| 169 |
|
| else { |
| 170 |
7 |
| chunk->size = mcobj_async->origin_size; |
| 171 |
|
| |
| 172 |
7 |
▶ | if(length > chunk->size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(length > chunk->size) |
0 |
7 |
7 |
|
| 173 |
0 |
| chunk->size += length; |
| 174 |
|
| |
| 175 |
7 |
| chunk->begin = (char*)mymalloc(chunk->size * mcobj_async->struct_size_sn); |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
7 |
| chunk->length = 0; |
| 179 |
|
| |
| 180 |
7 |
▶ | if(chunk->begin == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(chunk->begin == NULL) |
0 |
7 |
7 |
|
| 181 |
0 |
| return MCOBJECT_ASYNC_STATUS_CHUNK_ERROR_MEMORY_ALLOCATION; |
| 182 |
|
| |
| 183 |
7 |
| return MCOBJECT_ASYNC_STATUS_OK; |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
7 |
| mcobject_async_chunk_t * mcobject_async_chunk_malloc_without_lock(mcobject_async_t *mcobj_async, size_t length, mcobject_async_status_t *status) |
| 187 |
|
| { |
| 188 |
7 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
5 |
2 |
7 |
|
| 189 |
5 |
| *status = MCOBJECT_ASYNC_STATUS_OK; |
| 190 |
|
| |
| 191 |
7 |
▶ | if(mcobj_async->chunk_cache_length) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunk_cache_length) |
0 |
7 |
7 |
|
| 192 |
|
| { |
| 193 |
0 |
| mcobj_async->chunk_cache_length--; |
| 194 |
|
| |
| 195 |
0 |
| mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ]->length = 0; |
| 196 |
0 |
| mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ]->next = NULL; |
| 197 |
0 |
| mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ]->prev = NULL; |
| 198 |
|
| |
| 199 |
0 |
| return mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ]; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
7 |
▶ | if(mcobj_async->chunks_length >= mcobj_async->chunks_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks_length >= mcobj_async->chunks_size) |
0 |
7 |
7 |
|
| 203 |
|
| { |
| 204 |
0 |
▶ | if(mcobj_async->chunks_pos_length >= mcobj_async->chunks_pos_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunks_pos_length >= mcobj_async->chunks_pos_size) |
0 |
0 |
0 |
|
| 205 |
|
| { |
| 206 |
0 |
| size_t tmp_pos_size = mcobj_async->chunks_pos_size << 1; |
| 207 |
0 |
| mcobject_async_chunk_t **tmp_pos = myrealloc(mcobj_async->chunks, |
| 208 |
|
| sizeof(mcobject_async_chunk_t*) * tmp_pos_size); |
| 209 |
|
| |
| 210 |
0 |
▶ | if(tmp_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmp_pos) |
0 |
0 |
0 |
|
| 211 |
|
| { |
| 212 |
0 |
| memset(&tmp_pos[mcobj_async->chunks_pos_length], 0, (tmp_pos_size - mcobj_async->chunks_pos_length) |
| 213 |
|
| * sizeof(mcobject_async_chunk_t*)); |
| 214 |
|
| |
| 215 |
0 |
| mcobj_async->chunks_pos_size = tmp_pos_size; |
| 216 |
0 |
| mcobj_async->chunks = tmp_pos; |
| 217 |
|
| } |
| 218 |
|
| else { |
| 219 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 220 |
0 |
| *status = MCOBJECT_ASYNC_STATUS_CHUNK_ERROR_MEMORY_ALLOCATION; |
| 221 |
|
| |
| 222 |
0 |
| return NULL; |
| 223 |
|
| } |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
0 |
▶ | if(mcobject_async_chunk_up(mcobj_async)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobject_async_chunk_up(mcobj_async)) { |
0 |
0 |
0 |
|
| 227 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 228 |
0 |
| *status = MCOBJECT_ASYNC_STATUS_CHUNK_ERROR_MEMORY_ALLOCATION; |
| 229 |
|
| |
| 230 |
0 |
| return NULL; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
0 |
| mcobj_async->chunks_pos_length++; |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
7 |
| mcobject_async_chunk_t* chunk = &mcobj_async->chunks[mcobj_async->chunks_pos_length - 1][mcobj_async->chunks_length]; |
| 237 |
7 |
| mcobj_async->chunks_length++; |
| 238 |
|
| |
| 239 |
7 |
| chunk->next = NULL; |
| 240 |
7 |
| chunk->prev = NULL; |
| 241 |
|
| |
| 242 |
7 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
5 |
2 |
7 |
|
| 243 |
5 |
| *status = mcobject_async_mem_malloc(mcobj_async, chunk, length); |
| 244 |
|
| else |
| 245 |
2 |
| mcobject_async_mem_malloc(mcobj_async, chunk, length); |
| 246 |
|
| |
| 247 |
7 |
| return chunk; |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
0 |
| mcobject_async_chunk_t * mcobject_async_chunk_malloc(mcobject_async_t *mcobj_async, size_t length, mcobject_async_status_t *status) |
| 251 |
|
| { |
| 252 |
0 |
| mcsync_lock(mcobj_async->mcsync); |
| 253 |
0 |
| mcobject_async_chunk_t* chunk = mcobject_async_chunk_malloc_without_lock(mcobj_async, length, status); |
| 254 |
0 |
| mcsync_unlock(mcobj_async->mcsync); |
| 255 |
|
| |
| 256 |
0 |
| return chunk; |
| 257 |
|
| } |
| 258 |
|
| |
| 259 |
7 |
| size_t mcobject_async_node_add(mcobject_async_t *mcobj_async, mcobject_async_status_t *status) |
| 260 |
|
| { |
| 261 |
7 |
| mcsync_lock(mcobj_async->mcsync); |
| 262 |
|
| |
| 263 |
|
| size_t node_idx; |
| 264 |
|
| |
| 265 |
7 |
▶ | if(mcobj_async->nodes_cache_length) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_cache_length) { |
0 |
7 |
7 |
|
| 266 |
0 |
| mcobj_async->nodes_cache_length--; |
| 267 |
|
| |
| 268 |
0 |
| node_idx = mcobj_async->nodes_cache[ mcobj_async->nodes_cache_length ]; |
| 269 |
|
| } |
| 270 |
|
| else { |
| 271 |
7 |
▶ | if(mcobj_async->nodes_length >= mcobj_async->nodes_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_length >= mcobj_async->nodes_size) { |
0 |
7 |
7 |
|
| 272 |
0 |
| mcsync_unlock(mcobj_async->mcsync); |
| 273 |
0 |
| return 0; |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
7 |
| node_idx = mcobj_async->nodes_length; |
| 277 |
7 |
| mcobj_async->nodes_length++; |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
7 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 281 |
|
| |
| 282 |
7 |
| node->chunk = mcobject_async_chunk_malloc_without_lock(mcobj_async, mcobj_async->origin_size, status); |
| 283 |
|
| |
| 284 |
7 |
▶ | if(status && *status) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(status && *status) { |
5 |
2 |
7 |
| if(status && *status) { |
0 |
5 |
5 |
|
| 285 |
0 |
| mcsync_unlock(mcobj_async->mcsync); |
| 286 |
0 |
| return 0; |
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
7 |
| node->chunk->next = NULL; |
| 290 |
7 |
| node->chunk->prev = NULL; |
| 291 |
|
| |
| 292 |
7 |
| node->cache_length = 0; |
| 293 |
7 |
| node->cache_size = mcobj_async->origin_size; |
| 294 |
7 |
| node->cache = (char**)mymalloc(sizeof(char*) * node->cache_size); |
| 295 |
|
| |
| 296 |
7 |
▶ | if(node->cache == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->cache == NULL) { |
0 |
7 |
7 |
|
| 297 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 298 |
0 |
| *status = MCOBJECT_ASYNC_STATUS_CHUNK_CACHE_ERROR_MEMORY_ALLOCATION; |
| 299 |
|
| |
| 300 |
0 |
| mcsync_unlock(mcobj_async->mcsync); |
| 301 |
0 |
| return 0; |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
7 |
| mcsync_unlock(mcobj_async->mcsync); |
| 305 |
|
| |
| 306 |
7 |
| return node_idx; |
| 307 |
|
| } |
| 308 |
|
| |
| 309 |
7 |
| void mcobject_async_node_clean(mcobject_async_t *mcobj_async, size_t node_idx) |
| 310 |
|
| { |
| 311 |
7 |
▶ | if(mcobj_async->nodes_length <= node_idx) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_length <= node_idx) |
0 |
7 |
7 |
|
| 312 |
0 |
| return; |
| 313 |
|
| |
| 314 |
7 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 315 |
7 |
| node->cache_length = 0; |
| 316 |
|
| |
| 317 |
7 |
▶ | if(node->chunk == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->chunk == NULL) |
0 |
7 |
7 |
|
| 318 |
0 |
| return; |
| 319 |
|
| |
| 320 |
7 |
▶ | while (node->chunk->prev) |
Branch: 1 branch
| Branch | True | False | Total |
| while (node->chunk->prev) |
0 |
7 |
7 |
|
| 321 |
0 |
| node->chunk = node->chunk->prev; |
| 322 |
|
| |
| 323 |
7 |
| node->chunk->length = 0; |
| 324 |
7 |
| node->cache_length = 0; |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
1 |
| void mcobject_async_node_all_clean(mcobject_async_t *mcobj_async) |
| 328 |
|
| { |
| 329 |
1 |
▶ | for (size_t node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t node_idx = 0; node_idx < mcobj_async->nodes_length; node_idx++) { |
0 |
1 |
1 |
|
| 330 |
0 |
| mcobject_async_node_clean(mcobj_async, node_idx); |
| 331 |
|
| } |
| 332 |
1 |
| } |
| 333 |
|
| |
| 334 |
2 |
| void mcobject_async_node_delete(mcobject_async_t *mcobj_async, size_t node_idx) |
| 335 |
|
| { |
| 336 |
2 |
| mcsync_lock(mcobj_async->mcsync); |
| 337 |
|
| |
| 338 |
2 |
▶ | if(mcobj_async->nodes_length <= node_idx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_length <= node_idx) { |
0 |
2 |
2 |
|
| 339 |
0 |
| mcsync_unlock(mcobj_async->mcsync); |
| 340 |
0 |
| return; |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
2 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 344 |
2 |
| mcobject_async_chunk_t *chunk = node->chunk; |
| 345 |
|
| |
| 346 |
2 |
▶ | while (chunk->next) |
Branch: 1 branch
| Branch | True | False | Total |
| while (chunk->next) |
0 |
2 |
2 |
|
| 347 |
0 |
| chunk = chunk->next; |
| 348 |
|
| |
| 349 |
4 |
▶ | while (chunk) |
Branch: 1 branch
| Branch | True | False | Total |
| while (chunk) |
2 |
2 |
4 |
|
| 350 |
|
| { |
| 351 |
2 |
▶ | if(mcobj_async->chunk_cache_length >= mcobj_async->chunk_cache_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->chunk_cache_length >= mcobj_async->chunk_cache_size) { |
0 |
2 |
2 |
|
| 352 |
0 |
| mcobj_async->chunk_cache_size <<= 1; |
| 353 |
|
| |
| 354 |
0 |
| mcobject_async_chunk_t **tmp = (mcobject_async_chunk_t**)myrealloc(mcobj_async->chunk_cache, |
| 355 |
0 |
| sizeof(mcobject_async_chunk_t*) * mcobj_async->chunk_cache_size); |
| 356 |
|
| |
| 357 |
0 |
▶ | if(tmp) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmp) |
0 |
0 |
0 |
|
| 358 |
0 |
| mcobj_async->chunk_cache = tmp; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
2 |
| mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ] = chunk; |
| 362 |
2 |
| mcobj_async->chunk_cache_length++; |
| 363 |
|
| |
| 364 |
2 |
| chunk = chunk->prev; |
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
2 |
▶ | if(node->cache) |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->cache) |
2 |
0 |
2 |
|
| 368 |
2 |
| free(node->cache); |
| 369 |
|
| |
| 370 |
2 |
| memset(node, 0, sizeof(mcobject_async_node_t)); |
| 371 |
|
| |
| 372 |
2 |
▶ | if(mcobj_async->nodes_cache_length >= mcobj_async->nodes_cache_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mcobj_async->nodes_cache_length >= mcobj_async->nodes_cache_size) { |
0 |
2 |
2 |
|
| 373 |
0 |
| size_t new_size = mcobj_async->nodes_cache_size << 1; |
| 374 |
|
| |
| 375 |
0 |
| size_t *tmp = (size_t*)myrealloc(mcobj_async->nodes_cache, sizeof(size_t) * mcobj_async->nodes_cache_size); |
| 376 |
|
| |
| 377 |
0 |
▶ | if(tmp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmp) { |
0 |
0 |
0 |
|
| 378 |
0 |
| mcobj_async->nodes_cache = tmp; |
| 379 |
0 |
| mcobj_async->nodes_cache_size = new_size; |
| 380 |
|
| } |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
2 |
| mcobj_async->nodes_cache[ mcobj_async->nodes_cache_length ] = node_idx; |
| 384 |
2 |
| mcobj_async->nodes_cache_length++; |
| 385 |
|
| |
| 386 |
2 |
| mcsync_unlock(mcobj_async->mcsync); |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
3 |
| void * mcobject_async_malloc(mcobject_async_t *mcobj_async, size_t node_idx, mcobject_async_status_t *status) |
| 390 |
|
| { |
| 391 |
3 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 392 |
|
| |
| 393 |
3 |
▶ | if(node->cache_length) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->cache_length) { |
0 |
3 |
3 |
|
| 394 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 395 |
0 |
| *status = MCOBJECT_ASYNC_STATUS_OK; |
| 396 |
|
| |
| 397 |
0 |
| node->cache_length--; |
| 398 |
0 |
| return node->cache[ node->cache_length ]; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
3 |
▶ | if(node->chunk->length >= node->chunk->size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->chunk->length >= node->chunk->size) |
0 |
3 |
3 |
|
| 402 |
|
| { |
| 403 |
0 |
▶ | if(node->chunk->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->chunk->next) { |
0 |
0 |
0 |
|
| 404 |
0 |
| node->chunk = node->chunk->next; |
| 405 |
0 |
| node->chunk->length = 0; |
| 406 |
|
| } |
| 407 |
|
| else { |
| 408 |
|
| mcobject_async_status_t mystatus; |
| 409 |
0 |
| mcobject_async_chunk_t *chunk = mcobject_async_chunk_malloc(mcobj_async, mcobj_async->origin_size, &mystatus); |
| 410 |
|
| |
| 411 |
0 |
▶ | if(mystatus) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mystatus) { |
0 |
0 |
0 |
|
| 412 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 413 |
0 |
| *status = mystatus; |
| 414 |
|
| |
| 415 |
0 |
| return NULL; |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
0 |
| chunk->prev = node->chunk; |
| 419 |
0 |
| node->chunk->next = chunk; |
| 420 |
|
| |
| 421 |
0 |
| node->chunk = chunk; |
| 422 |
|
| } |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
3 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
3 |
3 |
|
| 426 |
0 |
| *status = MCOBJECT_ASYNC_STATUS_OK; |
| 427 |
|
| |
| 428 |
3 |
| size_t offset = node->chunk->length * mcobj_async->struct_size_sn; |
| 429 |
3 |
| *((size_t*)(&node->chunk->begin[offset])) = node_idx; |
| 430 |
|
| |
| 431 |
3 |
| node->chunk->length++; |
| 432 |
3 |
| return &node->chunk->begin[(offset + sizeof(size_t))]; |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
0 |
| void mcobject_async_free(mcobject_async_t *mcobj_async, void *entry) |
| 436 |
|
| { |
| 437 |
0 |
| size_t node_idx = *((size_t*)((char*)entry - sizeof(size_t))); |
| 438 |
|
| |
| 439 |
0 |
▶ | if(node_idx >= mcobj_async->nodes_length) |
Branch: 1 branch
| Branch | True | False | Total |
| if(node_idx >= mcobj_async->nodes_length) |
0 |
0 |
0 |
|
| 440 |
0 |
| return; |
| 441 |
|
| |
| 442 |
0 |
| mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; |
| 443 |
|
| |
| 444 |
0 |
▶ | if(node->cache_length > node->cache_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->cache_length > node->cache_size) { |
0 |
0 |
0 |
|
| 445 |
0 |
| node->cache_size <<= 1; |
| 446 |
|
| |
| 447 |
0 |
| char **tmp = (char**)myrealloc(node->cache, sizeof(char*) * node->cache_size); |
| 448 |
|
| |
| 449 |
0 |
▶ | if(tmp) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmp) |
0 |
0 |
0 |
|
| 450 |
0 |
| node->cache = tmp; |
| 451 |
|
| } |
| 452 |
|
| |
| 453 |
0 |
| node->cache[ node->cache_length ] = entry; |
| 454 |
0 |
| node->cache_length++; |
| 455 |
|
| } |
| 456 |
|
| |