From: Brian Aker Date: Mon, 1 Oct 2007 13:25:02 +0000 (-0700) Subject: Missing hosts file. X-Git-Tag: 0.3~2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8996c21d87b51d6bcc72efd93cbf83cc559498a5;p=m6w6%2Flibmemcached Missing hosts file. free'ing memory from execute cycle --- diff --git a/lib/memcached_hosts.c b/lib/memcached_hosts.c new file mode 100644 index 00000000..ad7ed06b --- /dev/null +++ b/lib/memcached_hosts.c @@ -0,0 +1,158 @@ +#include +#include "common.h" + +memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list) +{ + unsigned int x; + unsigned int count; + memcached_server_st *new_host_list; + + if (!list) + return MEMCACHED_SUCCESS; + + for (count= 0; list[count].hostname; count++); + + new_host_list= + (memcached_server_st *)realloc(ptr->hosts, + sizeof(memcached_server_st) * (count + ptr->number_of_hosts + 1)); + + if (!new_host_list) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + ptr->hosts= new_host_list; + + for (x= 0; list[x].hostname; x++) + { + ptr->hosts[ptr->number_of_hosts].hostname= strdup(list[x].hostname); + ptr->hosts[ptr->number_of_hosts].port= list[x].port; + ptr->hosts[ptr->number_of_hosts].fd= list[x].fd; + ptr->number_of_hosts++; + } + memset(&ptr->hosts[ptr->number_of_hosts], 0, sizeof(memcached_server_st)); + + return MEMCACHED_SUCCESS; +} + +memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port) +{ + memcached_server_st *new_host_list; + char *new_hostname; + LIBMEMCACHED_MEMCACHED_SERVER_ADD_START(); + + if (!port) + port= MEMCACHED_DEFAULT_PORT; + + if (!hostname) + hostname= "localhost"; + + + if (ptr->number_of_hosts) + { + new_host_list= (memcached_server_st *)realloc(ptr->hosts, + sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); + if (!new_host_list) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + memset(&new_host_list[ptr->number_of_hosts], 0, sizeof(memcached_server_st)); + } + else + { + new_host_list= + (memcached_server_st *)malloc(sizeof(memcached_server_st) * 2); + if (!new_host_list) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + memset(new_host_list, 0, sizeof(memcached_server_st) * 2); + } + + ptr->hosts= new_host_list; + + new_hostname= + (char *)malloc(sizeof(char) * (strlen(hostname)+1)); + + if (!new_hostname) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + memset(new_hostname, 0, strlen(hostname)+1); + memcpy(new_hostname, hostname, strlen(hostname)); + ptr->hosts[ptr->number_of_hosts].hostname= new_hostname; + ptr->hosts[ptr->number_of_hosts].port= port; + ptr->hosts[ptr->number_of_hosts].fd= -1; + ptr->number_of_hosts++; + + LIBMEMCACHED_MEMCACHED_SERVER_ADD_END(); + + return MEMCACHED_SUCCESS; +} + +memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, + char *hostname, unsigned int port, + memcached_return *error) +{ + unsigned int count; + memcached_server_st *new_host_list; + char *new_hostname; + + if (!hostname) + return ptr; + + if (!port) + port= MEMCACHED_DEFAULT_PORT; + + /* Always count so that we keep a free host at the end */ + if (ptr) + { + for (count= 0; ptr[count].hostname; count++); + count+= 2; + new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count); + if (!new_host_list) + goto error; + memset(&new_host_list[count-1], 0, sizeof(memcached_server_st)); + } + else + { + count= 2; + new_host_list= (memcached_server_st *)malloc(sizeof(memcached_server_st) * count); + if (!new_host_list) + goto error; + memset(new_host_list, 0, sizeof(memcached_server_st) * 2); + } + + new_hostname= strdup(hostname); + + if (!new_hostname) + goto error; + + new_host_list[count-2].hostname= new_hostname; + new_host_list[count-2].port= port; + new_host_list[count-2].fd= -1; + + *error= MEMCACHED_SUCCESS; + return new_host_list; +error: + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + return NULL; +} + +unsigned int memcached_server_list_count(memcached_server_st *ptr) +{ + unsigned int x; + + for (x= 0; ptr[x].hostname; x++); + + return x; +} + +void memcached_server_list_free(memcached_server_st *ptr) +{ + unsigned int x; + + for (x= 0; ptr[x].hostname; x++) + { + if (ptr[x].fd > 0) + close(ptr[x].fd); + + free(ptr[x].hostname); + } + + free(ptr); +} diff --git a/src/memslap.c b/src/memslap.c index 5fe6d72e..c9c65771 100644 --- a/src/memslap.c +++ b/src/memslap.c @@ -322,6 +322,8 @@ void *run_task(void *p) pthread_mutex_unlock(&counter_mutex); memcached_free(memc); + if (context->execute_pairs) + pairs_free(context->execute_pairs); free(context); return NULL;