X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmemcached.c;h=30099c39ff7a2f643f2edd9caaaddb0ee5b0d979;hb=6f50a6acc95303396a8a073c3746cdff83df2301;hp=6a7715de06cfac8251c7d56a049a1bc5717cb525;hpb=87424051896224c7fbc3744a1dcf94dbd76ccfef;p=m6w6%2Flibmemcached diff --git a/lib/memcached.c b/lib/memcached.c index 6a7715de..30099c39 100644 --- a/lib/memcached.c +++ b/lib/memcached.c @@ -1,10 +1,11 @@ /* Memcached library */ -#include +#include "common.h" -memcached_st *memcached_init(memcached_st *ptr) +memcached_st *memcached_create(memcached_st *ptr) { + memcached_string_st *string_ptr; if (!ptr) { ptr= (memcached_st *)malloc(sizeof(memcached_st)); @@ -19,30 +20,68 @@ memcached_st *memcached_init(memcached_st *ptr) { memset(ptr, 0, sizeof(memcached_st)); } + string_ptr= memcached_string_create(ptr, &ptr->result_buffer, 0); + WATCHPOINT_ASSERT(string_ptr); + ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT; + ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA; return ptr; } -void memcached_deinit(memcached_st *ptr) +void memcached_free(memcached_st *ptr) { - unsigned int x; - memcached_host_st *host_ptr; + /* If we have anything open, lets close it now */ + memcached_quit(ptr); + memcached_server_list_free(ptr->hosts); + memcached_string_free(&ptr->result_buffer); - if (ptr->hosts) + if (ptr->is_allocated == MEMCACHED_ALLOCATED) + free(ptr); + else + ptr->is_allocated= MEMCACHED_USED; +} + +/* + clone is the destination, while ptr is the structure to clone. + If ptr is NULL the call is the same as if a memcached_create() was + called. +*/ +memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr) +{ + memcached_return rc= MEMCACHED_SUCCESS; + memcached_st *new_clone; + + if (ptr == NULL) + return memcached_create(clone); + + if (ptr->is_allocated == MEMCACHED_USED) { - for (x= 0; x < ptr->number_of_hosts; x++) - { - if (ptr->hosts[x].fd > 0) - close(ptr->hosts[x].fd); + WATCHPOINT_ASSERT(0); + return NULL; + } + + new_clone= memcached_create(clone); + + if (new_clone == NULL) + return NULL; - free(ptr->hosts[x].hostname); - } + if (ptr->hosts) + rc= memcached_server_push(new_clone, ptr->hosts); - free(ptr->hosts); + if (rc != MEMCACHED_SUCCESS) + { + memcached_free(new_clone); + + return NULL; } - if (ptr->is_allocated == MEMCACHED_ALLOCATED) - free(ptr); - else - memset(ptr, 0, sizeof(memcached_st)); + + new_clone->flags= ptr->flags; + new_clone->send_size= ptr->send_size; + new_clone->recv_size= ptr->recv_size; + new_clone->poll_timeout= ptr->poll_timeout; + new_clone->distribution= ptr->distribution; + new_clone->hash= ptr->hash; + + return new_clone; }