From: Date: Tue, 18 Sep 2007 15:11:51 +0000 (+0200) Subject: Change hosts over to realloc array X-Git-Tag: 0.2~26 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=1fc4b1ae18810551553837b4c67c6075f1ad5941;p=awesomized%2Flibmemcached Change hosts over to realloc array --- diff --git a/include/memcached.h b/include/memcached.h index 33abcc81..b57b3941 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -1,3 +1,4 @@ +#include /* * Summary: interface for memcached server * Description: main include file for libmemcached @@ -66,7 +67,6 @@ struct memcached_host_st { char *hostname; unsigned int port; int fd; - memcached_host_st *next; }; struct memcached_stat_st { @@ -100,9 +100,9 @@ struct memcached_stat_st { struct memcached_st { memcached_allocated is_allocated; - int fd; - char connected; memcached_host_st *hosts; + unsigned int number_of_hosts; + char connected; }; /* Public API */ diff --git a/lib/memcached.c b/lib/memcached.c index fb046827..66a96f04 100644 --- a/lib/memcached.c +++ b/lib/memcached.c @@ -19,7 +19,6 @@ memcached_st *memcached_init(memcached_st *ptr) { memset(ptr, 0, sizeof(memcached_st)); } - ptr->fd= -1; return ptr; } @@ -50,7 +49,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) else send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "flush_all\r\n"); - if ((send(ptr->fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed flush_all TCP\n"); @@ -71,7 +70,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "verbosity %u\r\n", verbosity); - if ((send(ptr->fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed verbosity\n"); @@ -92,20 +91,21 @@ memcached_return memcached_quit(memcached_st *ptr, char *hostname, unsigned port void memcached_deinit(memcached_st *ptr) { + unsigned int x; memcached_host_st *host_ptr; - if (ptr->fd > 0) - close(ptr->fd); - - for (host_ptr= ptr->hosts; host_ptr;) + if (ptr->hosts) { - memcached_host_st *temp; + for (x= 0; x < ptr->number_of_hosts; x++) + { + if (ptr->hosts[x].fd > 0) + close(ptr->hosts[x].fd); + + if (ptr->hosts[x].hostname) + free(ptr->hosts[x].hostname); + } - temp= host_ptr; - host_ptr= host_ptr->next; - if (temp->hostname) - free(temp->hostname); - free(temp); + free(ptr->hosts); } if (ptr->is_allocated == MEMCACHED_ALLOCATED) diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index 5b3644a1..1be786e1 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -2,47 +2,27 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port) { - memcached_host_st *host_ptr, *prev_ptr; - if (!port) port= MEMCACHED_DEFAULT_PORT; if (!hostname) hostname= "localhost"; - if (ptr->hosts) - { - for (host_ptr= ptr->hosts; host_ptr; host_ptr= host_ptr->next) - prev_ptr= host_ptr; - host_ptr= (memcached_host_st *)malloc(sizeof(memcached_host_st)); - if (!host_ptr) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - prev_ptr->next= host_ptr; - } - else - { - ptr->hosts= - host_ptr= (memcached_host_st *)malloc(sizeof(memcached_host_st)); - if (!host_ptr) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - - memset(host_ptr, 0, sizeof(memcached_host_st)); - host_ptr->hostname= (char *)malloc(sizeof(char) * strlen(hostname)); - if (!host_ptr->hostname) - { - free(host_ptr); - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - memcpy(host_ptr->hostname, hostname, strlen(hostname)); - host_ptr->port= port; + ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts); + ptr->hosts[ptr->number_of_hosts].hostname= + (char *)malloc(sizeof(char) * strlen(hostname)); + memcpy(ptr->hosts[ptr->number_of_hosts].hostname, hostname, strlen(hostname)); + ptr->hosts[ptr->number_of_hosts].port= port; + ptr->hosts[ptr->number_of_hosts].fd= -1; + ptr->number_of_hosts++; return MEMCACHED_SUCCESS; } memcached_return memcached_connect(memcached_st *ptr) { + unsigned int x; struct sockaddr_in localAddr, servAddr; struct hostent *h; memcached_host_st *host_ptr; @@ -60,20 +40,20 @@ memcached_return memcached_connect(memcached_st *ptr) } - for (host_ptr= ptr->hosts; host_ptr; host_ptr= host_ptr->next) + for (x= 0; x < ptr->number_of_hosts; x++) { - if ((h= gethostbyname(host_ptr->hostname)) == NULL) + if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL) { - fprintf(stderr, "unknown host '%s'\n", host_ptr->hostname); + fprintf(stderr, "unknown host '%s'\n", ptr->hosts[x].hostname); return MEMCACHED_HOST_LOCKUP_FAILURE; } servAddr.sin_family= h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); - servAddr.sin_port = htons(host_ptr->port); + servAddr.sin_port = htons(ptr->hosts[x].port); /* Create the socket */ - if ((ptr->fd= socket(AF_INET, SOCK_STREAM, 0)) < 0) + if ((ptr->hosts[0].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "cannot open socket"); return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE; @@ -85,17 +65,17 @@ memcached_return memcached_connect(memcached_st *ptr) localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0); - if (bind(ptr->fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0) + if (bind(ptr->hosts[0].fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0) { - fprintf(stderr, "cannot bind port TCP %u\n", host_ptr->port); + fprintf(stderr, "cannot bind port TCP %u\n", ptr->hosts[x].port); return(MEMCACHED_CONNECTION_BIND_FAILURE); } /* connect to server */ - if (connect(ptr->fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) + if (connect(ptr->hosts[0].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { - fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", host_ptr->hostname, - host_ptr->port, + fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", ptr->hosts[x].hostname, + ptr->hosts[x].port, strerror(errno)); return MEMCACHED_HOST_LOCKUP_FAILURE; } diff --git a/lib/memcached_delete.c b/lib/memcached_delete.c index 98aac143..df16d2f4 100644 --- a/lib/memcached_delete.c +++ b/lib/memcached_delete.c @@ -15,7 +15,7 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt else send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "delete %.*s\r\n", key_length, key); - if ((write(ptr->fd, buffer, send_length) == -1)) + if ((write(ptr->hosts[0].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 1b53b119..c535a7ab 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -19,7 +19,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, if (*error != MEMCACHED_SUCCESS) return NULL; - if ((send(ptr->fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key); *error= MEMCACHED_WRITE_FAILURE; @@ -79,7 +79,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, need_to_read= *value_length - need_to_copy; - read_length= read(ptr->fd, pos_ptr, need_to_read); + read_length= read(ptr->hosts[0].fd, pos_ptr, need_to_read); if (read_length != need_to_read) { free(value); diff --git a/lib/memcached_response.c b/lib/memcached_response.c index 09d1fa4d..99020559 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -13,7 +13,7 @@ memcached_return memcached_response(memcached_st *ptr, size_t send_length; memset(buffer, 0, buffer_length); - send_length= read(ptr->fd, buffer, buffer_length); + send_length= read(ptr->hosts[0].fd, buffer, buffer_length); if (send_length) switch(buffer[0]) diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index 8fe5c822..c3371d1d 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -142,7 +142,7 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, send_length= snprintf(buffer, HUGE_STRING_LEN, "stats\r\n"); - if ((send(memc.fd, buffer, send_length, 0) == -1)) + if ((send(memc.hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed on stats\n"); diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 5ed2fd55..40d36394 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -28,7 +28,7 @@ static memcached_return memcached_send(memcached_st *ptr, send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %.*s %u %u %u\r\n", verb, key_length, key, flags, expiration, value_length); - if ((send(ptr->fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); @@ -38,14 +38,14 @@ static memcached_return memcached_send(memcached_st *ptr, send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%.*s\r\n", value_length, value); - if ((send(ptr->fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); return MEMCACHED_WRITE_FAILURE; } - send_length= read(ptr->fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + send_length= read(ptr->hosts[0].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); if (send_length && buffer[0] == 'S') /* STORED */ return MEMCACHED_SUCCESS;