From: Brian Aker Date: Sat, 16 Jan 2010 01:50:37 +0000 (-0800) Subject: Partial patch on pass through to fix issues with trigger writers not X-Git-Tag: 0.40~93 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=addf09c6f6cf93f980f3de9cf649258c965b2934;p=m6w6%2Flibmemcached Partial patch on pass through to fix issues with trigger writers not following docs on use of memcached_St --- diff --git a/ChangeLog b/ChangeLog index c324e485..50f7cd57 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,6 @@ -0.38 +0.38 + + * Functions used for callbacks are now given const memcached_st. * Added MEMCACHED_BEHAVIOR_CORK. diff --git a/clients/execute.c b/clients/execute.c index 130fef0b..daede9ec 100644 --- a/clients/execute.c +++ b/clients/execute.c @@ -77,9 +77,9 @@ unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int numbe /** * Callback function to count the number of results */ -static memcached_return_t callback_counter(memcached_st *ptr, - memcached_result_st *result, - void *context) +static memcached_return_t callback_counter(const memcached_st *ptr, + memcached_result_st *result, + void *context) { (void)ptr; (void)result; diff --git a/clients/memdump.c b/clients/memdump.c index 4329c1bd..e1854538 100644 --- a/clients/memdump.c +++ b/clients/memdump.c @@ -40,7 +40,7 @@ static char *opt_servers= NULL; static char *opt_hash= NULL; /* Print the keys and counter how many were found */ -static memcached_return_t key_printer(memcached_st *ptr __attribute__((unused)), +static memcached_return_t key_printer(const memcached_st *ptr __attribute__((unused)), const char *key, size_t key_length, void *context __attribute__((unused))) { diff --git a/libmemcached/allocators.c b/libmemcached/allocators.c index 767371cb..14502867 100644 --- a/libmemcached/allocators.c +++ b/libmemcached/allocators.c @@ -1,24 +1,24 @@ #include "common.h" -void libmemcached_free(memcached_st *ptr, void *mem) +void libmemcached_free(const memcached_st *ptr, void *mem) { (void) ptr; free(mem); } -void *libmemcached_malloc(memcached_st *ptr, size_t size) +void *libmemcached_malloc(const memcached_st *ptr, size_t size) { (void) ptr; return malloc(size); } -void *libmemcached_realloc(memcached_st *ptr, void *mem, size_t size) +void *libmemcached_realloc(const memcached_st *ptr, void *mem, size_t size) { (void) ptr; return realloc(mem, size); } -void *libmemcached_calloc(memcached_st *ptr, size_t nelem, size_t size) +void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size) { if (ptr->call_malloc != libmemcached_malloc) { @@ -47,7 +47,9 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, ptr->call_calloc= libmemcached_calloc; } else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL) + { return MEMCACHED_FAILURE; + } else { ptr->call_malloc= mem_malloc; @@ -59,7 +61,7 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, return MEMCACHED_SUCCESS; } -void memcached_get_memory_allocators(memcached_st *ptr, +void memcached_get_memory_allocators(const memcached_st *ptr, memcached_malloc_fn *mem_malloc, memcached_free_fn *mem_free, memcached_realloc_fn *mem_realloc, diff --git a/libmemcached/allocators.h b/libmemcached/allocators.h index d22bc652..30ed3821 100644 --- a/libmemcached/allocators.h +++ b/libmemcached/allocators.h @@ -24,12 +24,24 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, memcached_calloc_fn mem_calloc); LIBMEMCACHED_API -void memcached_get_memory_allocators(memcached_st *ptr, +void memcached_get_memory_allocators(const memcached_st *ptr, memcached_malloc_fn *mem_malloc, memcached_free_fn *mem_free, memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc); +LIBMEMCACHED_LOCAL +void libmemcached_free(const memcached_st *ptr, void *mem); + +LIBMEMCACHED_LOCAL +void *libmemcached_malloc(const memcached_st *ptr, const size_t size); + +LIBMEMCACHED_LOCAL +void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size); + +LIBMEMCACHED_LOCAL +void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size); + #ifdef __cplusplus } diff --git a/libmemcached/connect.c b/libmemcached/connect.c index 6d08eb70..b599977f 100644 --- a/libmemcached/connect.c +++ b/libmemcached/connect.c @@ -335,12 +335,16 @@ memcached_return_t memcached_connect(memcached_server_st *ptr) if (curr_time.tv_sec < ptr->next_retry) { - if (memcached_behavior_get(ptr->root, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS)) + memcached_st *root= (memcached_st *)ptr->root; + // @todo fix this by fixing behavior to no longer make use of + // memcached_st + if (memcached_behavior_get(root, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS)) { - run_distribution(ptr->root); + run_distribution(root); } - ptr->root->last_disconnected_server = ptr; + root->last_disconnected_server = ptr; + return MEMCACHED_SERVER_MARKED_DEAD; } } @@ -364,7 +368,12 @@ memcached_return_t memcached_connect(memcached_server_st *ptr) WATCHPOINT_ASSERT(0); } - unlikely ( rc != MEMCACHED_SUCCESS) ptr->root->last_disconnected_server = ptr; + unlikely ( rc != MEMCACHED_SUCCESS) + { + //@todo create interface around last_discontected_server + memcached_st *root= (memcached_st *)ptr->root; + root->last_disconnected_server = ptr; + } LIBMEMCACHED_MEMCACHED_CONNECT_END(); diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index 1cbc8030..7778735c 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -326,7 +326,6 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); - memcached_server_create(ptr, instance); /* TODO check return type */ (void)memcached_server_create_with(ptr, instance, list[x].hostname, list[x].port, list[x].weight, list[x].type); @@ -432,13 +431,21 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, return run_distribution(ptr); } +/** + @todo allow lists to query themselves even if they lack a root +*/ memcached_return_t memcached_server_remove(memcached_server_st *st_ptr) { uint32_t x, host_index; - memcached_st *ptr= st_ptr->root; - memcached_server_st *list= memcached_server_list(ptr); + memcached_st *root= (memcached_st *)st_ptr->root; + memcached_server_st *list; - for (x= 0, host_index= 0; x < memcached_server_count(ptr); x++) + if (root == NULL) + return MEMCACHED_FAILURE; + + list= memcached_server_list(root); + + for (x= 0, host_index= 0; x < memcached_server_count(root); x++) { if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port) { @@ -447,14 +454,14 @@ memcached_return_t memcached_server_remove(memcached_server_st *st_ptr) host_index++; } } - ptr->number_of_hosts= host_index; + root->number_of_hosts= host_index; if (st_ptr->address_info) { freeaddrinfo(st_ptr->address_info); st_ptr->address_info= NULL; } - run_distribution(ptr); + run_distribution(root); return MEMCACHED_SUCCESS; } diff --git a/libmemcached/internal.h b/libmemcached/internal.h index 2a151514..a02a30de 100644 --- a/libmemcached/internal.h +++ b/libmemcached/internal.h @@ -18,15 +18,6 @@ extern "C" { #endif -LIBMEMCACHED_LOCAL -void libmemcached_free(memcached_st *ptr, void *mem); -LIBMEMCACHED_LOCAL -void *libmemcached_malloc(memcached_st *ptr, const size_t size); -LIBMEMCACHED_LOCAL -void *libmemcached_realloc(memcached_st *ptr, void *mem, const size_t size); -LIBMEMCACHED_LOCAL -void *libmemcached_calloc(memcached_st *ptr, size_t nelem, size_t size); - #ifdef __cplusplus } #endif diff --git a/libmemcached/io.c b/libmemcached/io.c index 6aad4805..bca5ea4b 100644 --- a/libmemcached/io.c +++ b/libmemcached/io.c @@ -127,20 +127,21 @@ static bool process_input_buffer(memcached_server_instance_st *ptr) */ memcached_callback_st cb= *ptr->root->callbacks; - memcached_set_processing_input(ptr->root, true); + memcached_set_processing_input((memcached_st *)ptr->root, true); char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; memcached_return_t error; + memcached_st *root= (memcached_st *)ptr->root; error= memcached_response(ptr, buffer, sizeof(buffer), - &ptr->root->result); + &root->result); - memcached_set_processing_input(ptr->root, false); + memcached_set_processing_input(root, false); if (error == MEMCACHED_SUCCESS) { for (unsigned int x= 0; x < cb.number_of_callback; x++) { - error= (*cb.callback[x])(ptr->root, &ptr->root->result, cb.context); + error= (*cb.callback[x])(ptr->root, &root->result, cb.context); if (error != MEMCACHED_SUCCESS) break; } diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index a8463e1b..17c306e9 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -221,7 +221,9 @@ public: strstm << port; tmp_str.append(strstm.str()); memcached_server_st *server= memcached_servers_parse(tmp_str.c_str()); + memcached_return_t rc= memcached_server_remove(server); + return (rc == MEMCACHED_SUCCESS); } diff --git a/libmemcached/purge.c b/libmemcached/purge.c index 17c5b9d2..0225e42b 100644 --- a/libmemcached/purge.c +++ b/libmemcached/purge.c @@ -4,6 +4,7 @@ memcached_return_t memcached_purge(memcached_server_instance_st *ptr) { uint32_t x; memcached_return_t ret= MEMCACHED_SUCCESS; + memcached_st *root= (memcached_st *)ptr->root; if (memcached_is_purging(ptr->root) || /* already purging */ (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark && @@ -16,14 +17,14 @@ memcached_return_t memcached_purge(memcached_server_instance_st *ptr) /* memcached_io_write and memcached_response may call memcached_purge so we need to be able stop any recursion.. */ - memcached_set_purging(ptr->root, true); + memcached_set_purging(root, true); WATCHPOINT_ASSERT(ptr->fd != -1); /* Force a flush of the buffer to ensure that we don't have the n-1 pending requests buffered up.. */ if (memcached_io_write(ptr, NULL, 0, 1) == -1) { - memcached_set_purging(ptr->root, true); + memcached_set_purging(root, true); return MEMCACHED_WRITE_FAILURE; } @@ -41,10 +42,10 @@ memcached_return_t memcached_purge(memcached_server_instance_st *ptr) * data to be sent from the server (the commands was in the output buffer * and just flushed */ - int32_t timeo= ptr->root->poll_timeout; - ptr->root->poll_timeout= 2000; + const int32_t timeo= ptr->root->poll_timeout; + root->poll_timeout= 2000; - result_ptr= memcached_result_create(ptr->root, &result); + result_ptr= memcached_result_create(root, &result); WATCHPOINT_ASSERT(result_ptr); for (x= 0; x < no_msg; x++) @@ -81,9 +82,9 @@ memcached_return_t memcached_purge(memcached_server_instance_st *ptr) } memcached_result_free(result_ptr); - ptr->root->poll_timeout= timeo; + root->poll_timeout= timeo; } - memcached_set_purging(ptr->root, false); + memcached_set_purging(root, false); return ret; } diff --git a/libmemcached/response.c b/libmemcached/response.c index dafdbbc4..26418ebd 100644 --- a/libmemcached/response.c +++ b/libmemcached/response.c @@ -25,7 +25,10 @@ memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr memcached_server_response_decrement(ptr); if (result == NULL) - result = &ptr->root->result; + { + memcached_st *root= (memcached_st *)ptr->root; + result = &root->result; + } memcached_return_t rc; if (ptr->root->flags.binary_protocol) diff --git a/libmemcached/result.c b/libmemcached/result.c index 6de2cf63..bca92740 100644 --- a/libmemcached/result.c +++ b/libmemcached/result.c @@ -20,7 +20,8 @@ memcached_result_st *memcached_result_create(memcached_st *memc, memcached_result_st *ptr) { - WATCHPOINT_ASSERT(memc && memc->options.is_initialized); + WATCHPOINT_ASSERT(memc); + WATCHPOINT_ASSERT(memc->options.is_initialized); /* Saving malloc calls :) */ if (ptr) diff --git a/libmemcached/server.c b/libmemcached/server.c index f61f572e..a3426769 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -14,7 +14,7 @@ */ #include "common.h" -memcached_server_st *memcached_server_create(memcached_st *memc, memcached_server_st *ptr) +static memcached_server_st *_server_create(const memcached_st *memc, memcached_server_st *ptr) { if (ptr == NULL) { @@ -35,11 +35,11 @@ memcached_server_st *memcached_server_create(memcached_st *memc, memcached_serve return ptr; } -memcached_server_st *memcached_server_create_with(memcached_st *memc, memcached_server_st *host, +memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *host, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type) { - host= memcached_server_create(memc, host); + host= _server_create(memc, host); if (host == NULL) return NULL; diff --git a/libmemcached/server.h b/libmemcached/server.h index 1a92eea7..10cf50fd 100644 --- a/libmemcached/server.h +++ b/libmemcached/server.h @@ -43,7 +43,7 @@ struct memcached_server_st { size_t write_buffer_offset; struct addrinfo *address_info; time_t next_retry; - memcached_st *root; + const memcached_st *root; uint64_t limit_maxbytes; char read_buffer[MEMCACHED_MAX_BUFFER]; char write_buffer[MEMCACHED_MAX_BUFFER]; @@ -92,11 +92,9 @@ void memcached_server_error_reset(memcached_server_st *ptr); /* These should not currently be used by end users */ /* TODO: Is the above comment valid? If so, how can we unit test these if they * aren't exported. If not, we should remove the comment */ -LIBMEMCACHED_LOCAL -memcached_server_st *memcached_server_create(memcached_st *memc, memcached_server_st *ptr); LIBMEMCACHED_LOCAL -memcached_server_st *memcached_server_create_with(memcached_st *memc, +memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *host, const char *hostname, in_port_t port, diff --git a/libmemcached/types.h b/libmemcached/types.h index e01a47eb..710fd28b 100644 --- a/libmemcached/types.h +++ b/libmemcached/types.h @@ -23,21 +23,31 @@ typedef struct memcached_result_st memcached_result_st; typedef struct memcached_string_st memcached_string_st; typedef struct memcached_server_st memcached_server_st; typedef struct memcached_continuum_item_st memcached_continuum_item_st; + typedef memcached_return_t (*memcached_clone_fn)(memcached_st *parent, memcached_st *clone); -typedef memcached_return_t (*memcached_cleanup_fn)(memcached_st *ptr); -typedef void (*memcached_free_fn)(memcached_st *ptr, void *mem); -typedef void *(*memcached_malloc_fn)(memcached_st *ptr, const size_t size); -typedef void *(*memcached_realloc_fn)(memcached_st *ptr, void *mem, const size_t size); -typedef void *(*memcached_calloc_fn)(memcached_st *ptr, size_t nelem, const size_t elsize); -typedef memcached_return_t (*memcached_execute_fn)(memcached_st *ptr, memcached_result_st *result, void *context); -typedef memcached_return_t (*memcached_server_fn)(memcached_st *ptr, memcached_server_st *server, void *context); -typedef memcached_return_t (*memcached_trigger_key_fn)(memcached_st *ptr, +typedef memcached_return_t (*memcached_cleanup_fn)(const memcached_st *ptr); + +/** + Memory allocation functions. +*/ +typedef void (*memcached_free_fn)(const memcached_st *ptr, void *mem); +typedef void *(*memcached_malloc_fn)(const memcached_st *ptr, const size_t size); +typedef void *(*memcached_realloc_fn)(const memcached_st *ptr, void *mem, const size_t size); +typedef void *(*memcached_calloc_fn)(const memcached_st *ptr, size_t nelem, const size_t elsize); + +typedef memcached_return_t (*memcached_execute_fn)(const memcached_st *ptr, memcached_result_st *result, void *context); +typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, memcached_server_st *server, void *context); + +/** + Trigger functions. +*/ +typedef memcached_return_t (*memcached_trigger_key_fn)(const memcached_st *ptr, const char *key, size_t key_length, memcached_result_st *result); -typedef memcached_return_t (*memcached_trigger_delete_key_fn)(memcached_st *ptr, +typedef memcached_return_t (*memcached_trigger_delete_key_fn)(const memcached_st *ptr, const char *key, size_t key_length); -typedef memcached_return_t (*memcached_dump_fn)(memcached_st *ptr, +typedef memcached_return_t (*memcached_dump_fn)(const memcached_st *ptr, const char *key, size_t key_length, void *context); diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 6fa1995a..dbf50797 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -82,7 +82,8 @@ static test_return_t server_list_null_test(memcached_st *ptr __attribute__((unus #define TEST_PORT_COUNT 7 in_port_t test_ports[TEST_PORT_COUNT]; -static memcached_return_t server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context) +static memcached_return_t server_display_function(const memcached_st *ptr __attribute__((unused)), + memcached_server_st *server, void *context) { /* Do Nothing */ size_t bigger= *((size_t *)(context)); @@ -158,7 +159,7 @@ static test_return_t server_sort2_test(memcached_st *ptr __attribute__((unused)) return TEST_SUCCESS; } -static memcached_return_t server_display_unsort_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context) +static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context) { /* Do Nothing */ uint32_t x= *((uint32_t *)(context)); @@ -685,7 +686,7 @@ static test_return_t flush_test(memcached_st *memc) return TEST_SUCCESS; } -static memcached_return_t server_function(memcached_st *ptr __attribute__((unused)), +static memcached_return_t server_function(const memcached_st *ptr __attribute__((unused)), memcached_server_st *server __attribute__((unused)), void *context __attribute__((unused))) { @@ -1497,7 +1498,7 @@ static test_return_t mget_result_alloc_test(memcached_st *memc) } /* Count the results */ -static memcached_return_t callback_counter(memcached_st *ptr __attribute__((unused)), +static memcached_return_t callback_counter(const memcached_st *ptr __attribute__((unused)), memcached_result_st *result __attribute__((unused)), void *context) { @@ -3615,7 +3616,7 @@ static test_return_t pre_replication_noblock(memcached_st *memc) } -static void my_free(memcached_st *ptr __attribute__((unused)), void *mem) +static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem) { #ifdef HARD_MALLOC_TESTS void *real_ptr= (mem == NULL) ? mem : (void*)((caddr_t)mem - 8); @@ -3626,7 +3627,7 @@ static void my_free(memcached_st *ptr __attribute__((unused)), void *mem) } -static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size) +static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const size_t size) { #ifdef HARD_MALLOC_TESTS void *ret= malloc(size + 8); @@ -3647,7 +3648,7 @@ static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t s } -static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size) +static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *mem, const size_t size) { #ifdef HARD_MALLOC_TESTS void *real_ptr= (mem == NULL) ? NULL : (void*)((caddr_t)mem - 8); @@ -3666,7 +3667,7 @@ static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, co } -static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size) +static void *my_calloc(const memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size) { #ifdef HARD_MALLOC_TESTS void *mem= my_malloc(ptr, nelem * size); @@ -4098,10 +4099,10 @@ static test_return_t analyzer_test(memcached_st *memc) } /* Count the objects */ -static memcached_return_t callback_dump_counter(memcached_st *ptr __attribute__((unused)), - const char *key __attribute__((unused)), - size_t key_length __attribute__((unused)), - void *context) +static memcached_return_t callback_dump_counter(const memcached_st *ptr __attribute__((unused)), + const char *key __attribute__((unused)), + size_t key_length __attribute__((unused)), + void *context) { size_t *counter= (size_t *)context; diff --git a/tests/plus.cpp b/tests/plus.cpp index f9cbdd5e..0d588749 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -29,7 +29,7 @@ extern "C" { test_return_t mget_result_function(memcached_st *memc); test_return_t basic_behavior(memcached_st *memc); test_return_t mget_test(memcached_st *memc); - memcached_return_t callback_counter(memcached_st *, + memcached_return_t callback_counter(const memcached_st *, memcached_result_st *, void *context); } @@ -150,9 +150,9 @@ test_return_t basic_master_key_test(memcached_st *memc) } /* Count the results */ -memcached_return_t callback_counter(memcached_st *, - memcached_result_st *, - void *context) +memcached_return_t callback_counter(const memcached_st *, + memcached_result_st *, + void *context) { unsigned int *counter= static_cast(context);