void acl_xml_attr_free(ACL_XML_ATTR *attr) { acl_vstring_free(attr->name); acl_vstring_free(attr->value); if (attr->node->xml->slice) acl_slice_pool_free(__FILE__, __LINE__, attr); else acl_myfree(attr); }
static void acl_xml_node_free(ACL_XML_NODE *node) { acl_vstring_free(node->ltag); acl_vstring_free(node->rtag); acl_vstring_free(node->text); acl_ring_detach(&node->node); acl_array_free(node->attr_list, (void (*)(void*)) acl_xml_attr_free); if (node->xml->slice) acl_slice_pool_free(__FILE__, __LINE__, node); else acl_myfree(node); }
ACL_ARGV *acl_argv_splitn_append(ACL_ARGV *argvp, const char *str, const char *delim, size_t n) { char *saved_string = argvp->slice ? acl_slice_pool_strdup(__FILE__, __LINE__, argvp->slice, str) : acl_mystrdup(str); char *bp = saved_string; char *arg; while (n-- > 0 && (arg = acl_mystrtok(&bp, delim)) != 0) acl_argv_add(argvp, arg, (char *) 0); acl_argv_terminate(argvp); if (argvp->slice) acl_slice_pool_free(__FILE__, __LINE__, saved_string); else acl_myfree(saved_string); return (argvp); }
ACL_ARGV *acl_argv_splitn4(const char *str, const char *delim, size_t n, ACL_SLICE_POOL *slice) { ACL_ARGV *argvp = acl_argv_alloc2(n > 0 ? (int) n : 1, slice); char *saved_string = slice ? acl_slice_pool_strdup(__FILE__, __LINE__, slice, str) : acl_mystrdup(str); char *bp = saved_string; char *arg; while (n-- > 0 && (arg = acl_mystrtok(&bp, delim)) != 0) acl_argv_add(argvp, arg, (char *) 0); acl_argv_terminate(argvp); if (slice) acl_slice_pool_free(__FILE__, __LINE__, saved_string); else acl_myfree(saved_string); return (argvp); }
static void *tls_mem_realloc(const char *filename, int line, void *ptr, size_t size) { void *buf = tls_mem_alloc(filename, line, size); MBLOCK *old_real_ptr; size_t old_len; if (ptr == NULL) return buf; CHECK_IN_PTR2(ptr, old_real_ptr, old_len, filename, line); memcpy(buf, ptr, old_len > size ? size : old_len); if (old_real_ptr->mem_slice->tid != (unsigned long) acl_pthread_self()) { MUTEX_LOCK(old_real_ptr->mem_slice); PRIVATE_ARRAY_PUSH(old_real_ptr->mem_slice->list, old_real_ptr); MUTEX_UNLOCK(old_real_ptr->mem_slice); } else acl_slice_pool_free(filename, line, old_real_ptr); return buf; }
static int mem_slice_gc(ACL_MEM_SLICE *mem_slice) { int n = 0; /* 释放由其它线程交还的内存片 */ MUTEX_LOCK(mem_slice); while (1) { void *ptr; PRIVATE_ARRAY_POP(mem_slice->list, ptr); if (ptr == NULL) break; acl_slice_pool_free(__FILE__, __LINE__, ptr); n++; } MUTEX_UNLOCK(mem_slice); /* 实时进行垃圾回收? */ if ((mem_slice->slice_flag & ACL_SLICE_FLAG_RTGC_OFF) == 0) acl_slice_pool_gc(mem_slice->slice_pool); return n; }
int acl_xml_free(ACL_XML *xml) { ACL_RING *next; ACL_XML_NODE *node; int n = 1; while ((next = acl_ring_pop_head(&xml->root->children)) != NULL) { node = acl_ring_to_appl(next, ACL_XML_NODE, node); n += acl_xml_node_delete(node); } acl_xml_node_free(xml->root); xml->node_cnt--; acl_assert(xml->node_cnt == 0); acl_htable_free(xml->id_table, NULL); if (xml->node_cache != NULL) acl_array_free(xml->node_cache, (void (*)(void*)) acl_xml_node_free); if (xml->slice) acl_slice_pool_free(__FILE__, __LINE__, xml); else acl_myfree(xml); return (n); }
static void tls_mem_free(const char *filename, int line, void *ptr) { MBLOCK *real_ptr; size_t len; CHECK_IN_PTR2(ptr, real_ptr, len, filename, line); #if 1 if (real_ptr->mem_slice->tid != (unsigned long) acl_pthread_self()) { #else if (real_ptr->mem_slice->tid != mem_slice->tid) { #endif MUTEX_LOCK(real_ptr->mem_slice); PRIVATE_ARRAY_PUSH(real_ptr->mem_slice->list, real_ptr); MUTEX_UNLOCK(real_ptr->mem_slice); } else acl_slice_pool_free(filename, line, real_ptr); } static void *tls_mem_alloc(const char *filename, int line, size_t len) { const char *myname = "tls_mem_alloc"; ACL_MEM_SLICE *mem_slice = acl_pthread_getspecific(__mem_slice_key); char *ptr; MBLOCK *real_ptr; if (mem_slice == NULL) { /* 每个子线程获得自己的线程局部存储内存池 */ mem_slice = mem_slice_create(); mem_slice->slice_list = __mem_slice_list; /* 将子线程的线程局部存储内存池置入全局内存池句柄集合中 */ if (__mem_slice_list_lock) thread_mutex_lock(__mem_slice_list_lock); private_array_push(__mem_slice_list, mem_slice); if (__mem_slice_list_lock) thread_mutex_unlock(__mem_slice_list_lock); } real_ptr = (MBLOCK *) acl_slice_pool_alloc(filename, line, mem_slice->slice_pool, SPACE_FOR(len)); if (real_ptr == 0) { acl_msg_error("%s(%d): malloc: insufficient memory", myname, __LINE__); return 0; } mem_slice->nalloc++; if (mem_slice->nalloc == mem_slice->nalloc_gc) { mem_slice->nalloc = 0; mem_slice_gc(mem_slice); } CHECK_OUT_PTR(ptr, real_ptr, mem_slice, len); return ptr; } static void *tls_mem_calloc(const char *filename, int line, size_t nmemb, size_t size) { void *ptr = tls_mem_alloc(filename, line, nmemb * size); memset(ptr, 0, nmemb * size); return ptr; }