int main(int argc, char**argv) { char*sites[] = { "www.alongsoft.com","www.baidu.com", "www.renren.com:80", "www.google.com", "www.weibo.com", "www.126.com", "www.163.com/index.html" }; int test_count = sizeof(sites) / sizeof(char*); int* sockfds = (int*) memory_malloc(sizeof(int) * test_count); int i = 0; for (; i < test_count; ++i) { if ((sockfds[i] = request(sites[i])) < 0) { ERROR("request %s error!\n",sites[i]); free(sockfds); return -1; } } if (do_select(sockfds, test_count) != 0) { ERROR("select deal error\n"); return -1; } //关闭 for (i = 0; i < test_count; ++i) { close(sockfds[i]); } return 0; }
/** *功能:与指定URL站点建立连接,并发出请求 *参数:@weburl 站点地址 *返回:如果建立连接成功,返回连接描述符,否则返回-1 */ int request(char* weburl) { int sockfd; int port = 80; int web_host_name_length; char*web_host, *request_document; web_host = request_document = NULL; //定位request_document if ((request_document = strstr(weburl, "/")) != NULL) { web_host_name_length = request_document - weburl; //定位端口号 char*port_end; if ((port_end = strstr(weburl, ":")) != NULL && port_end < request_document) { port = atoi(port_end + 1); } } else { web_host_name_length = strlen(weburl); //定位端口号 char*port_end; if ((port_end = strstr(weburl, ":")) != NULL) { port = atoi(port_end + 1); } } web_host = (char*) memory_malloc(sizeof(char) * web_host_name_length + 1); strncpy(web_host, weburl, web_host_name_length); web_host[web_host_name_length] = '\0'; if ((sockfd = request_inner(web_host, port)) < 0) { goto dealfail; } //请求数据 if (send_get_request(sockfd, web_host, request_document) < 0) { goto dealfail; } free(web_host); return sockfd; dealfail: free(web_host); return -1; }
static int trace_malloc (void *buf) { size_t size = buffer_get(size_t); uintptr_t caller = buffer_get(uintptr_t); uintptr_t result = buffer_get(uintptr_t); unsigned long long time = buffer_get(unsigned long long); int res = memory_malloc(size, caller, result, time); assert_inner(!res, "memory_malloc"); return 0; }
// This duplicates memory for a variable and makes a new dictionary entry for the new variable void *MallocPlus::memory_duplicate(void *malloc_mem_ptr, const char *addname){ map <void *, malloc_plus_memory_entry*>::iterator it = memory_ptr_dict.find(malloc_mem_ptr); void *mem_ptr_dup; if (it != memory_ptr_dict.end()){ malloc_plus_memory_entry *memory_item = it->second; // The memory_malloc will add the database entry mem_ptr_dup = memory_malloc(memory_item->mem_nelem[0], memory_item->mem_elsize, addname, memory_item->mem_flags); return(mem_ptr_dup); } else { if (DEBUG) printf("Warning -- memory not found\n"); } return(NULL); }
int send_get_request(int sockfd, char*web_host, char*request_document) { //GET $REQUEST_DOCUMENT HTTP/1.1\r\n //Host: $web_host //Connect: close //Content-Encoding: gzip, compress CHECK_ARG_NULL(web_host,-1,"argment host is null!\n"); int request_document_length = 1; if (request_document == NULL) { request_document = "/"; } else { request_document_length = strlen(request_document); } const char * const str_format = "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nContent-Encoding: gzip, compress\r\n\r\n\r\n"; int total_length = strlen(web_host) + request_document_length + strlen( str_format) + 1; char*buffer = memory_malloc(total_length * sizeof(char)); int length = snprintf(buffer, total_length, str_format, request_document, web_host); buffer[length] = '\0'; if (write(sockfd, buffer, length) != length) { ERROR("send :%s to %s error!\n",buffer,web_host); free(buffer); return -1; } //ERROR("send :%s to %s\n",buffer,web_host); free(buffer); return 0; }
void* EPLIB_malloc(size_t bytes) { return memory_malloc(bytes); }