void acl_mem_slice_delay_destroy(void) { const char *myname = "acl_mem_slice_delay_destroy"; int i, n; if (__mem_slice_list_lock == NULL) return; thread_mutex_lock(__mem_slice_list_lock); n = private_array_size(__mem_slice_list); for (i = 0; i < n; i++) { ACL_MEM_SLICE *mem_slice = (ACL_MEM_SLICE*) private_array_index(__mem_slice_list, i); if (mem_slice == NULL) break; if (mem_slice->delay_free == 0) continue; if (acl_slice_pool_used(mem_slice->slice_pool) <= 0) { acl_msg_info("%s(%d): thread(%ld) free mem slice now", myname, __LINE__, mem_slice->tid); acl_slice_pool_destroy(mem_slice->slice_pool); private_array_destroy(mem_slice->list, NULL); mem_slice->list = NULL; /* 将子线程的线程局部存储内存池从全局内存池句柄集合中删除 */ private_array_delete_obj(__mem_slice_list, mem_slice, NULL); acl_default_free(__FILE__, __LINE__, mem_slice); } else mem_slice_gc(mem_slice); } thread_mutex_unlock(__mem_slice_list_lock); }
static void mem_slice_free(ACL_MEM_SLICE *mem_slice) { const char *myname = "mem_slice_free"; int n; if (mem_slice == NULL) { acl_msg_info("%s(%d): mem_slice null", myname, __LINE__); return; } /* 先回收本身线程的垃圾内存片 */ mem_slice_gc(mem_slice); if ((n = acl_slice_pool_used(mem_slice->slice_pool)) > 0) { acl_msg_info("%s(%d): thread(%ld) mem slice busy slices: %d, delay free it", myname, __LINE__, mem_slice->tid, n); if (__mem_slice_list_lock) thread_mutex_lock(__mem_slice_list_lock); mem_slice->delay_free = 1; if (__mem_slice_list_lock) thread_mutex_unlock(__mem_slice_list_lock); /* 尽量回收一些已经完全释放的内存 */ acl_slice_pool_gc(mem_slice->slice_pool); } else { acl_msg_info("%s(%d): thread(%ld) free mem slice now", myname, __LINE__, mem_slice->tid); acl_slice_pool_destroy(mem_slice->slice_pool); private_array_destroy(mem_slice->list, NULL); mem_slice->list = NULL; /* 将子线程的线程局部存储内存池从全局内存池句柄集合中删除 */ if (__mem_slice_list_lock) thread_mutex_lock(__mem_slice_list_lock); private_array_delete_obj(__mem_slice_list, mem_slice, NULL); if (__mem_slice_list_lock) thread_mutex_unlock(__mem_slice_list_lock); acl_default_free(__FILE__, __LINE__, mem_slice); } }
static void fifo_test(bool use_slice) { ACL_FIFO *fifo; int i; char *ptr; ACL_SLICE_POOL *slice; if (use_slice) slice = acl_slice_pool_create(10, 100, ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF); else slice = NULL; fifo = acl_fifo_new1(slice); for (i = 0; i < 20; i++) { if (slice) ptr = (char*) acl_slice_pool_alloc(__FILE__, __LINE__, slice, 100); else ptr = (char*) acl_mymalloc(100); snprintf(ptr, 100, "test:%d", i); (void) acl_fifo_push(fifo, ptr); printf(">>>ptr: %s\n", ptr); } while (1) { ptr = (char*) acl_fifo_pop(fifo); if (ptr == NULL) break; printf("fifo pop: %s\n", ptr); } if (slice == NULL) acl_fifo_free(fifo, acl_myfree_fn); else acl_slice_pool_destroy(slice); }
bool CMsnContactManager::GetAddresses(const CMsnTicket& ticket, acl::http_client& client) { acl::string request_body; if (BuildGetAddressRequest(ticket, request_body) == false) return (false); /////////////////////////////////////////////////////////////////////// // 向 MSN CONTACT 服务器发送请求数据 // 创建 HTTP 请求头 acl::http_header header; header.set_method(acl::HTTP_METHOD_POST); header.set_url(MSN_ADDRESS_BOOK_POST_URL); header.set_content_type("text/xml; charset=utf-8"); header.set_host(MSN_CONTACT_SERVER); header.set_content_length(request_body.length()); header.set_keep_alive(false); header.add_entry("SOAPAction", MSN_GET_ADDRESS_SOAP_ACTION); header.add_entry("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); header.add_entry("Accept", "*/*"); header.add_entry("Cache-Control", "no-cache"); header.accept_gzip(true); // 发送 HTTP 请求头 acl::string header_buf; header.build_request(header_buf); // 记录发送头 logger_format("send header: %s\r\n", header_buf.c_str()); if (client.get_ostream().write(header_buf) == -1) { logger_error("write http header error"); return (false); } // 发送 HTTP 请求体 // 记录发送体 logger_format("send body: %s\r\n", request_body.c_str()); if (client.get_ostream().write(request_body) == -1) { logger_error("write http body error"); return (false); } /////////////////////////////////////////////////////////////////////// // 从 MSN CONTACT 服务器读取响应数据 if (client.read_head() == false) { logger_error("read http respond head error"); return (false); } const HTTP_HDR_RES* hdr_res = client.get_respond_head(NULL); if (debug_fpout_) http_hdr_fprint(debug_fpout_, &hdr_res->hdr, "GetAddresses read header: "); if (hdr_res->hdr.content_length == 0 || (hdr_res->hdr.content_length == -1 && !hdr_res->hdr.chunked && hdr_res->reply_status > 300 && hdr_res->reply_status < 400)) { logger_error("http respond no body"); http_hdr_print(&hdr_res->hdr, "error"); return (false); } /* 读书 HTTP 响应体 */ acl::string buf; ACL_SLICE_POOL* slice = acl_slice_pool_create(16, 50, ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF | ACL_SLICE_FLAG_LP64_ALIGN); ACL_XML* body = acl_xml_alloc1(slice); int ret; while(true) { ret = client.read_body(buf); if (ret < 0) break; else if (client.body_finish()) break; acl_xml_update(body, buf.c_str()); logger_format("read body: %s\r\n", buf.c_str()); } //acl_xml_dump(body, ACL_VSTREAM_OUT); ParseAddresses(body); /////////////////////////////////////////////////////////////////////// double j = ACL_METER_TIME("---begin---"); //acl_xml_free(body); acl_slice_pool_destroy(slice); double k = ACL_METER_TIME("---end---"); return (true); }