From: Date: Sun, 28 Oct 2007 00:47:43 +0000 (-0700) Subject: Adding new memcached_clone() function X-Git-Tag: 0.7~2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=77d70ea2571e43495bb66efb9072f6a8b5009b64;p=awesomized%2Flibmemcached Adding new memcached_clone() function --- diff --git a/docs/Makefile.am b/docs/Makefile.am index 01d028f9..9a7e9075 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -33,6 +33,7 @@ man_MANS = libmemcached.3\ memcached_add.3\ memcached_behavior_get.3\ memcached_behavior_set.3\ + memcached_clone.3\ memcached_create.3\ memcached_decrement.3\ memcached_delete.3\ @@ -71,6 +72,9 @@ memcached_create.3: memcached_create.pod memcached_free.3: memcached_create.pod pod2man -c "libmemcached" -r "" -s 3 memcached_create.pod > memcached_free.3 +memcached_clone.3: memcached_create.pod + pod2man -c "libmemcached" -r "" -s 3 memcached_create.pod > memcached_clone.3 + memcached_set.3: memcached_set.pod pod2man -c "libmemcached" -r "" -s 3 memcached_set.pod > memcached_set.3 diff --git a/docs/libmemcached.pod b/docs/libmemcached.pod index 5f2b1e30..cd6ad95e 100755 --- a/docs/libmemcached.pod +++ b/docs/libmemcached.pod @@ -54,7 +54,7 @@ Brian Aker, Ebrian@tangent.orgE =head1 SEE ALSO -memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) +memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) =cut diff --git a/docs/memcached_create.pod b/docs/memcached_create.pod index cbe6fc1e..f76e9c7e 100755 --- a/docs/memcached_create.pod +++ b/docs/memcached_create.pod @@ -14,6 +14,8 @@ C Client Library for memcached (libmemcached, -lmemcached) void memcached_free (memcached_st *ptr); + memcached_st *memcached_clone(memcached_st *clone, memcached_st *source); + =head1 DESCRIPTION memcached_create() is used to create a C structure that will then @@ -21,6 +23,11 @@ be used by other libmemcached(3) functions to communicate with the server. You should either pass a statically declared C to memcached_create() or a NULL. If a NULL passed in then a structure is allocated for you. +memcached_clone() is similar to memcached_create(3) but it copies the +defaults and list of servers from the source C. If you pass a null as +the argument for the source to clone, it is the same as a call to memcached_create(). +If the clone argument is NULL a C will be allocated for you. + To clean up memory associated with a C structure you should pass it to memcached_free() when you are finished using it. memcached_free() is the only way to make sure all memory is deallocated when you finish using @@ -31,6 +38,9 @@ the structure. memcached_create() returns a pointer to the memcached_st that was created (or initialized). On an allocation failure, it returns NULL. +memcached_clone() returns a pointer to the memcached_st that was created +(or initialized). On an allocation failure, it returns NULL. + =head1 HOME To find out more information please check: diff --git a/include/memcached.h b/include/memcached.h index 1131de2b..2cdc34d0 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -140,6 +140,7 @@ struct memcached_st { /* Public API */ memcached_st *memcached_create(memcached_st *ptr); void memcached_free(memcached_st *ptr); +memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr); memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length, time_t expiration); diff --git a/lib/memcached.c b/lib/memcached.c index 0bab7958..157dc89f 100644 --- a/lib/memcached.c +++ b/lib/memcached.c @@ -37,3 +37,32 @@ void memcached_free(memcached_st *ptr) else memset(ptr, 0, sizeof(memcached_st)); } + +memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr) +{ + memcached_return rc; + memcached_st *new_clone; + + if (ptr == NULL) + return memcached_create(clone); + + new_clone= memcached_create(clone); + + + rc= memcached_server_push(new_clone, ptr->hosts); + + if (rc != MEMCACHED_SUCCESS) + { + memcached_free(new_clone); + + return NULL; + } + + + new_clone->flags= ptr->flags; + new_clone->number_of_hosts= ptr->number_of_hosts; + new_clone->send_size= ptr->send_size; + new_clone->recv_size= ptr->recv_size; + + return new_clone; +} diff --git a/support/libmemcached.spec b/support/libmemcached.spec index f5880308..46cf9b5a 100644 --- a/support/libmemcached.spec +++ b/support/libmemcached.spec @@ -63,6 +63,7 @@ memslap - Generate testing loads on a memcached cluster %{_mandir}/man3/memcached_add.3.gz %{_mandir}/man3/memcached_behavior_get.3.gz %{_mandir}/man3/memcached_behavior_set.3.gz +%{_mandir}/man3/memcached_clone.3.gz %{_mandir}/man3/memcached_create.3.gz %{_mandir}/man3/memcached_decrement.3.gz %{_mandir}/man3/memcached_delete.3.gz diff --git a/tests/test.c b/tests/test.c index 85adaf1c..ac319bc0 100644 --- a/tests/test.c +++ b/tests/test.c @@ -37,6 +37,43 @@ void allocation_test(memcached_st *not_used) memcached_free(memc); } +void clone_test(memcached_st *memc) +{ + /* All null? */ + { + memcached_st *clone; + clone= memcached_clone(NULL, NULL); + assert(clone); + memcached_free(clone); + } + + /* Can we init from null? */ + { + memcached_st *clone; + clone= memcached_clone(NULL, memc); + assert(clone); + memcached_free(clone); + } + + /* Can we init from struct? */ + { + memcached_st declared_clone; + memcached_st *clone; + clone= memcached_clone(&declared_clone, NULL); + assert(clone); + memcached_free(clone); + } + + /* Can we init from struct? */ + { + memcached_st declared_clone; + memcached_st *clone; + clone= memcached_clone(&declared_clone, memc); + assert(clone); + memcached_free(clone); + } +} + void connection_test(memcached_st *memc) { memcached_return rc; @@ -832,6 +869,7 @@ int main(int argc, char *argv[]) {"flush", 0, flush_test }, {"init", 0, init_test }, {"allocation", 0, allocation_test }, + {"clone_test", 0, clone_test }, {"error", 0, error_test }, {"set", 0, set_test }, {"set2", 0, set_test2 },