size_t mchar_async_node_add(mchar_async_t *mchar_async) { mcsync_lock(mchar_async->mcsync); size_t node_idx; if(mchar_async->nodes_cache_length) { mchar_async->nodes_cache_length--; node_idx = mchar_async->nodes_cache[ mchar_async->nodes_cache_length ]; } else { if(mchar_async->nodes_length >= mchar_async->nodes_size) { mcsync_unlock(mchar_async->mcsync); return 0; } node_idx = mchar_async->nodes_length; mchar_async->nodes_length++; } mchar_async_node_t *node = &mchar_async->nodes[node_idx]; mchar_async_cache_init(&node->cache); node->chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, mchar_async->origin_size); node->chunk->next = NULL; node->chunk->prev = NULL; mcsync_unlock(mchar_async->mcsync); return node_idx; }
size_t mcobject_async_node_add(mcobject_async_t *mcobj_async, mcobject_async_status_t *status) { mcsync_lock(mcobj_async->mcsync); size_t node_idx; if(mcobj_async->nodes_cache_length) { mcobj_async->nodes_cache_length--; node_idx = mcobj_async->nodes_cache[ mcobj_async->nodes_cache_length ]; } else { if(mcobj_async->nodes_length >= mcobj_async->nodes_size) { mcsync_unlock(mcobj_async->mcsync); return 0; } node_idx = mcobj_async->nodes_length; mcobj_async->nodes_length++; } mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; if(status) { node->chunk = mcobject_async_chunk_malloc_without_lock(mcobj_async, mcobj_async->origin_size, status); if(*status) { mcsync_unlock(mcobj_async->mcsync); return 0; } } else { mcobject_async_status_t mystatus; node->chunk = mcobject_async_chunk_malloc_without_lock(mcobj_async, mcobj_async->origin_size, &mystatus); if(mystatus) { mcsync_unlock(mcobj_async->mcsync); return 0; } } node->chunk->next = NULL; node->chunk->prev = NULL; node->cache_length = 0; node->cache_size = mcobj_async->origin_size; node->cache = (char**)mymalloc(sizeof(char*) * node->cache_size); if(node->cache == NULL) { if(status) *status = MCOBJECT_ASYNC_STATUS_CHUNK_CACHE_ERROR_MEMORY_ALLOCATION; mcsync_unlock(mcobj_async->mcsync); return 0; } mcsync_unlock(mcobj_async->mcsync); return node_idx; }
mchar_async_chunk_t * mchar_async_chunk_malloc(mchar_async_t *mchar_async, mchar_async_node_t *node, size_t length) { mcsync_lock(mchar_async->mcsync); mchar_async_chunk_t *chunk = mchar_async_chunk_malloc_without_lock(mchar_async, node, length); mcsync_unlock(mchar_async->mcsync); return chunk; }
mcobject_async_chunk_t * mcobject_async_chunk_malloc(mcobject_async_t *mcobj_async, size_t length, mcobject_async_status_t *status) { mcsync_lock(mcobj_async->mcsync); mcobject_async_chunk_t* chunk = mcobject_async_chunk_malloc_without_lock(mcobj_async, length, status); mcsync_unlock(mcobj_async->mcsync); return chunk; }
void mcobject_async_node_delete(mcobject_async_t *mcobj_async, size_t node_idx) { mcsync_lock(mcobj_async->mcsync); if(mcobj_async->nodes_length <= node_idx) { mcsync_unlock(mcobj_async->mcsync); return; } mcobject_async_node_t *node = &mcobj_async->nodes[node_idx]; mcobject_async_chunk_t *chunk = node->chunk; while (chunk->next) chunk = chunk->next; while (chunk) { if(mcobj_async->chunk_cache_length >= mcobj_async->chunk_cache_size) { mcobj_async->chunk_cache_size <<= 1; mcobject_async_chunk_t **tmp = (mcobject_async_chunk_t**)myrealloc(mcobj_async->chunk_cache, sizeof(mcobject_async_chunk_t*) * mcobj_async->chunk_cache_size); if(tmp) mcobj_async->chunk_cache = tmp; } mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ] = chunk; mcobj_async->chunk_cache_length++; chunk = chunk->prev; } if(node->cache) free(node->cache); memset(node, 0, sizeof(mcobject_async_node_t)); if(mcobj_async->nodes_cache_length >= mcobj_async->nodes_cache_size) { size_t new_size = mcobj_async->nodes_cache_size << 1; size_t *tmp = (size_t*)myrealloc(mcobj_async->nodes_cache, sizeof(size_t) * mcobj_async->nodes_cache_size); if(tmp) { mcobj_async->nodes_cache = tmp; mcobj_async->nodes_cache_size = new_size; } } mcobj_async->nodes_cache[ mcobj_async->nodes_cache_length ] = node_idx; mcobj_async->nodes_cache_length++; mcsync_unlock(mcobj_async->mcsync); }
void mchar_async_node_delete(mchar_async_t *mchar_async, size_t node_idx) { mcsync_lock(mchar_async->mcsync); if(mchar_async->nodes_length <= node_idx) { mcsync_unlock(mchar_async->mcsync); return; } mchar_async_node_t *node = &mchar_async->nodes[node_idx]; mchar_async_chunk_t *chunk = node->chunk; while (chunk->next) chunk = chunk->next; while (chunk) { mchar_async_cache_add(&mchar_async->chunk_cache, (void*)chunk, chunk->size); chunk = chunk->prev; } if(node->cache.nodes) mchar_async_cache_destroy(&node->cache, false); memset(node, 0, sizeof(mchar_async_node_t)); if(mchar_async->nodes_cache_length >= mchar_async->nodes_cache_size) { size_t new_size = mchar_async->nodes_cache_size << 1; size_t *tmp = (size_t*)myhtml_realloc(mchar_async->nodes_cache, sizeof(size_t) * mchar_async->nodes_cache_size); if(tmp) { mchar_async->nodes_cache = tmp; mchar_async->nodes_cache_size = new_size; } } mchar_async->nodes_cache[ mchar_async->nodes_cache_length ] = node_idx; mchar_async->nodes_cache_length++; mcsync_unlock(mchar_async->mcsync); }