From: Michael Wallner Date: Fri, 23 Oct 2020 11:39:02 +0000 (+0200) Subject: src/libmemcached: apply clang-format X-Git-Tag: 1.1.0-beta1~176 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=c8a5aecba8e189092266f304ca81ab42625edf60;p=awesomized%2Flibmemcached src/libmemcached: apply clang-format --- diff --git a/src/libmemcached/allocators.cc b/src/libmemcached/allocators.cc index 2bf5f6d1..38fb4441 100644 --- a/src/libmemcached/allocators.cc +++ b/src/libmemcached/allocators.cc @@ -1,79 +1,50 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -void _libmemcached_free(const memcached_st*, void *mem, void*) -{ - if (mem) - { +void _libmemcached_free(const memcached_st *, void *mem, void *) { + if (mem) { std::free(mem); } } -void *_libmemcached_malloc(const memcached_st *, size_t size, void *) -{ +void *_libmemcached_malloc(const memcached_st *, size_t size, void *) { return std::malloc(size); } -void *_libmemcached_realloc(const memcached_st*, void *mem, size_t size, void *) -{ +void *_libmemcached_realloc(const memcached_st *, void *mem, size_t size, void *) { return std::realloc(mem, size); } -void *_libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size, void *context) -{ - if (self->allocators.malloc != _libmemcached_malloc) - { - void *ret= _libmemcached_malloc(self, nelem * size, context); - if (ret) - { - memset(ret, 0, nelem * size); - } - - return ret; +void *_libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size, void *context) { + if (self->allocators.malloc != _libmemcached_malloc) { + void *ret = _libmemcached_malloc(self, nelem * size, context); + if (ret) { + memset(ret, 0, nelem * size); + } + + return ret; } return std::calloc(nelem, size); } -struct memcached_allocator_t memcached_allocators_return_default(void) -{ - static struct memcached_allocator_t global_default_allocator= { _libmemcached_calloc, _libmemcached_free, _libmemcached_malloc, _libmemcached_realloc, 0 }; +struct memcached_allocator_t memcached_allocators_return_default(void) { + static struct memcached_allocator_t global_default_allocator = { + _libmemcached_calloc, _libmemcached_free, _libmemcached_malloc, _libmemcached_realloc, 0}; return global_default_allocator; } @@ -81,74 +52,58 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *shell, memcached_malloc_fn mem_malloc, memcached_free_fn mem_free, memcached_realloc_fn mem_realloc, - memcached_calloc_fn mem_calloc, - void *context) -{ - Memcached* self= memcached2Memcached(shell); - if (self == NULL) - { + memcached_calloc_fn mem_calloc, void *context) { + Memcached *self = memcached2Memcached(shell); + if (self == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } /* All should be set, or none should be set */ - if (mem_malloc == NULL and mem_free == NULL and mem_realloc == NULL and mem_calloc == NULL) - { - self->allocators= memcached_allocators_return_default(); - } - else if (mem_malloc == NULL or mem_free == NULL or mem_realloc == NULL or mem_calloc == NULL) - { - return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("NULL parameter provided for one or more allocators")); - } - else - { - self->allocators.malloc= mem_malloc; - self->allocators.free= mem_free; - self->allocators.realloc= mem_realloc; - self->allocators.calloc= mem_calloc; - self->allocators.context= context; + if (mem_malloc == NULL and mem_free == NULL and mem_realloc == NULL and mem_calloc == NULL) { + self->allocators = memcached_allocators_return_default(); + } else if (mem_malloc == NULL or mem_free == NULL or mem_realloc == NULL or mem_calloc == NULL) { + return memcached_set_error( + *self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("NULL parameter provided for one or more allocators")); + } else { + self->allocators.malloc = mem_malloc; + self->allocators.free = mem_free; + self->allocators.realloc = mem_realloc; + self->allocators.calloc = mem_calloc; + self->allocators.context = context; } return MEMCACHED_SUCCESS; } -void *memcached_get_memory_allocators_context(const memcached_st *shell) -{ - const Memcached* self= memcached2Memcached(shell); - if (self) - { +void *memcached_get_memory_allocators_context(const memcached_st *shell) { + const Memcached *self = memcached2Memcached(shell); + if (self) { return self->allocators.context; } return NULL; } -void memcached_get_memory_allocators(const memcached_st *shell, - memcached_malloc_fn *mem_malloc, - memcached_free_fn *mem_free, - memcached_realloc_fn *mem_realloc, - memcached_calloc_fn *mem_calloc) -{ - const Memcached* self= memcached2Memcached(shell); - if (self) - { - if (mem_malloc) - { - *mem_malloc= self->allocators.malloc; +void memcached_get_memory_allocators(const memcached_st *shell, memcached_malloc_fn *mem_malloc, + memcached_free_fn *mem_free, memcached_realloc_fn *mem_realloc, + memcached_calloc_fn *mem_calloc) { + const Memcached *self = memcached2Memcached(shell); + if (self) { + if (mem_malloc) { + *mem_malloc = self->allocators.malloc; } - if (mem_free) - { - *mem_free= self->allocators.free; + if (mem_free) { + *mem_free = self->allocators.free; } - if (mem_realloc) - { - *mem_realloc= self->allocators.realloc; + if (mem_realloc) { + *mem_realloc = self->allocators.realloc; } - if (mem_calloc) - { - *mem_calloc= self->allocators.calloc; + if (mem_calloc) { + *mem_calloc = self->allocators.calloc; } } } diff --git a/src/libmemcached/allocators.hpp b/src/libmemcached/allocators.hpp index 8f196247..0f438947 100644 --- a/src/libmemcached/allocators.hpp +++ b/src/libmemcached/allocators.hpp @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/analyze.cc b/src/libmemcached/analyze.cc index c7a74757..121639e5 100644 --- a/src/libmemcached/analyze.cc +++ b/src/libmemcached/analyze.cc @@ -1,111 +1,100 @@ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "libmemcached/common.h" -static void calc_largest_consumption(memcached_analysis_st *result, - const uint32_t server_num, - const uint64_t nbytes) -{ - if (result->most_used_bytes < nbytes) - { - result->most_used_bytes= nbytes; - result->most_consumed_server= server_num; +static void calc_largest_consumption(memcached_analysis_st *result, const uint32_t server_num, + const uint64_t nbytes) { + if (result->most_used_bytes < nbytes) { + result->most_used_bytes = nbytes; + result->most_consumed_server = server_num; } } -static void calc_oldest_node(memcached_analysis_st *result, - const uint32_t server_num, - const uint32_t uptime) -{ - if (result->longest_uptime < uptime) - { - result->longest_uptime= uptime; - result->oldest_server= server_num; +static void calc_oldest_node(memcached_analysis_st *result, const uint32_t server_num, + const uint32_t uptime) { + if (result->longest_uptime < uptime) { + result->longest_uptime = uptime; + result->oldest_server = server_num; } } -static void calc_least_free_node(memcached_analysis_st *result, - const uint32_t server_num, - const uint64_t max_allowed_bytes, - const uint64_t used_bytes) -{ - uint64_t remaining_bytes= max_allowed_bytes - used_bytes; - - if (result->least_remaining_bytes == 0 || - remaining_bytes < result->least_remaining_bytes) - { - result->least_remaining_bytes= remaining_bytes; - result->least_free_server= server_num; +static void calc_least_free_node(memcached_analysis_st *result, const uint32_t server_num, + const uint64_t max_allowed_bytes, const uint64_t used_bytes) { + uint64_t remaining_bytes = max_allowed_bytes - used_bytes; + + if (result->least_remaining_bytes == 0 || remaining_bytes < result->least_remaining_bytes) { + result->least_remaining_bytes = remaining_bytes; + result->least_free_server = server_num; } } -static void calc_average_item_size(memcached_analysis_st *result, - const uint64_t total_items, - const uint64_t total_bytes) -{ - if (total_items > 0 && total_bytes > 0) - { - result->average_item_size= (uint32_t) (total_bytes / total_items); +static void calc_average_item_size(memcached_analysis_st *result, const uint64_t total_items, + const uint64_t total_bytes) { + if (total_items > 0 && total_bytes > 0) { + result->average_item_size = (uint32_t)(total_bytes / total_items); } } -static void calc_hit_ratio(memcached_analysis_st *result, - const uint64_t total_get_hits, - const uint64_t total_get_cmds) -{ - if (total_get_hits == 0 || total_get_cmds == 0) - { - result->pool_hit_ratio= 0; +static void calc_hit_ratio(memcached_analysis_st *result, const uint64_t total_get_hits, + const uint64_t total_get_cmds) { + if (total_get_hits == 0 || total_get_cmds == 0) { + result->pool_hit_ratio = 0; return; } - double temp= double(total_get_hits) / total_get_cmds; - result->pool_hit_ratio= temp * 100; + double temp = double(total_get_hits) / total_get_cmds; + result->pool_hit_ratio = temp * 100; } -memcached_analysis_st *memcached_analyze(memcached_st *shell, - memcached_stat_st *memc_stat, - memcached_return_t *error) -{ - Memcached* memc= memcached2Memcached(shell); - uint64_t total_items= 0, total_bytes= 0; - uint64_t total_get_cmds= 0, total_get_hits= 0; +memcached_analysis_st *memcached_analyze(memcached_st *shell, memcached_stat_st *memc_stat, + memcached_return_t *error) { + Memcached *memc = memcached2Memcached(shell); + uint64_t total_items = 0, total_bytes = 0; + uint64_t total_get_cmds = 0, total_get_hits = 0; - if (memc == NULL or memc_stat == NULL) - { + if (memc == NULL or memc_stat == NULL) { return NULL; } memcached_return_t not_used; - if (error == NULL) - { - error= ¬_used; + if (error == NULL) { + error = ¬_used; } - *error= MEMCACHED_SUCCESS; - uint32_t server_count= memcached_server_count(memc); - memcached_analysis_st *result= (memcached_analysis_st*)libmemcached_xcalloc(memc, - memcached_server_count(memc), - memcached_analysis_st); + *error = MEMCACHED_SUCCESS; + uint32_t server_count = memcached_server_count(memc); + memcached_analysis_st *result = (memcached_analysis_st *) libmemcached_xcalloc( + memc, memcached_server_count(memc), memcached_analysis_st); - if (result == NULL) - { - *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + if (result == NULL) { + *error = MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } - result->root= memc; + result->root = memc; - for (uint32_t x= 0; x < server_count; x++) - { + for (uint32_t x = 0; x < server_count; x++) { calc_largest_consumption(result, x, memc_stat[x].bytes); calc_oldest_node(result, x, uint32_t(memc_stat[x].uptime)); - calc_least_free_node(result, x, - memc_stat[x].limit_maxbytes, - memc_stat[x].bytes); - - total_get_hits+= memc_stat[x].get_hits; - total_get_cmds+= memc_stat[x].cmd_get; - total_items+= memc_stat[x].curr_items; - total_bytes+= memc_stat[x].bytes; + calc_least_free_node(result, x, memc_stat[x].limit_maxbytes, memc_stat[x].bytes); + + total_get_hits += memc_stat[x].get_hits; + total_get_cmds += memc_stat[x].cmd_get; + total_items += memc_stat[x].curr_items; + total_bytes += memc_stat[x].bytes; } calc_average_item_size(result, total_items, total_bytes); @@ -114,7 +103,6 @@ memcached_analysis_st *memcached_analyze(memcached_st *shell, return result; } -void memcached_analyze_free(memcached_analysis_st *ptr) -{ +void memcached_analyze_free(memcached_analysis_st *ptr) { libmemcached_free(ptr->root, ptr); } diff --git a/src/libmemcached/array.c b/src/libmemcached/array.c index 62c85ece..a93b3071 100644 --- a/src/libmemcached/array.c +++ b/src/libmemcached/array.c @@ -1,126 +1,90 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include #include -struct memcached_array_st -{ +struct memcached_array_st { Memcached *root; size_t size; char c_str[]; }; - -memcached_array_st *memcached_array_clone(Memcached *memc, const memcached_array_st *original) -{ - if (original) - { +memcached_array_st *memcached_array_clone(Memcached *memc, const memcached_array_st *original) { + if (original) { return memcached_strcpy(memc, original->c_str, original->size); } return NULL; } -memcached_array_st *memcached_strcpy(Memcached *memc, const char *str, size_t str_length) -{ +memcached_array_st *memcached_strcpy(Memcached *memc, const char *str, size_t str_length) { assert(memc); assert(str); assert(str_length); - memcached_array_st *array= (struct memcached_array_st *)libmemcached_malloc(memc, sizeof(struct memcached_array_st) +str_length +1); + memcached_array_st *array = (struct memcached_array_st *) libmemcached_malloc( + memc, sizeof(struct memcached_array_st) + str_length + 1); - if (array) - { - array->root= memc; - array->size= str_length; // We don't count the NULL ending + if (array) { + array->root = memc; + array->size = str_length; // We don't count the NULL ending memcpy(array->c_str, str, str_length); - array->c_str[str_length]= 0; + array->c_str[str_length] = 0; } return array; } -bool memcached_array_is_null(memcached_array_st *array) -{ - if (array) - { +bool memcached_array_is_null(memcached_array_st *array) { + if (array) { return false; } return true; } -memcached_string_t memcached_array_to_string(memcached_array_st *array) -{ +memcached_string_t memcached_array_to_string(memcached_array_st *array) { assert(array); assert(array->c_str); assert(array->size); memcached_string_t tmp; - tmp.c_str= array->c_str; - tmp.size= array->size; + tmp.c_str = array->c_str; + tmp.size = array->size; return tmp; } -void memcached_array_free(memcached_array_st *array) -{ - if (array) - { +void memcached_array_free(memcached_array_st *array) { + if (array) { WATCHPOINT_ASSERT(array->root); libmemcached_free(array->root, array); } } -size_t memcached_array_size(memcached_array_st *array) -{ - if (array) - { +size_t memcached_array_size(memcached_array_st *array) { + if (array) { return array->size; } return 0; } -const char *memcached_array_string(memcached_array_st *array) -{ - if (array) - { +const char *memcached_array_string(memcached_array_st *array) { + if (array) { return array->c_str; } diff --git a/src/libmemcached/array.h b/src/libmemcached/array.h index afb3f916..2b4eed93 100644 --- a/src/libmemcached/array.h +++ b/src/libmemcached/array.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once @@ -41,9 +19,9 @@ extern "C" { #endif -memcached_array_st *memcached_array_clone(Memcached* memc, const memcached_array_st *original); +memcached_array_st *memcached_array_clone(Memcached *memc, const memcached_array_st *original); -memcached_array_st *memcached_strcpy(Memcached* memc, const char *str, size_t str_length); +memcached_array_st *memcached_strcpy(Memcached *memc, const char *str, size_t str_length); void memcached_array_free(memcached_array_st *array); @@ -60,9 +38,10 @@ bool memcached_array_is_null(memcached_array_st *array); #endif #ifdef __cplusplus -#define memcached_print_array(X) static_cast(memcached_array_size(X)), memcached_array_string(X) -#define memcached_param_array(X) memcached_array_string(X), memcached_array_size(X) +# define memcached_print_array(X) \ + static_cast(memcached_array_size(X)), memcached_array_string(X) +# define memcached_param_array(X) memcached_array_string(X), memcached_array_size(X) #else -#define memcached_print_array(X) (int)memcached_array_size((X)), memcached_array_string((X)) -#define memcached_param_array(X) memcached_array_string(X), memcached_array_size(X) +# define memcached_print_array(X) (int) memcached_array_size((X)), memcached_array_string((X)) +# define memcached_param_array(X) memcached_array_string(X), memcached_array_size(X) #endif diff --git a/src/libmemcached/assert.hpp b/src/libmemcached/assert.hpp index de2283a7..062ebd9c 100644 --- a/src/libmemcached/assert.hpp +++ b/src/libmemcached/assert.hpp @@ -1,93 +1,73 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * libmcachedd client library. - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifdef __cplusplus -# include +# include #else -# include +# include #endif // __cplusplus #ifdef NDEBUG -# define assert_msg(__expr, __mesg) (void)(__expr); (void)(__mesg); -# define assert_vmsg(__expr, __mesg, ...) (void)(__expr); (void)(__mesg); +# define assert_msg(__expr, __mesg) \ + (void) (__expr); \ + (void) (__mesg); +# define assert_vmsg(__expr, __mesg, ...) \ + (void) (__expr); \ + (void) (__mesg); #else -# ifdef _WIN32 -# include -# elif HAVE_ALLOCA_H -# include -# endif +# ifdef _WIN32 +# include +# elif HAVE_ALLOCA_H +# include +# endif -#ifdef __cplusplus -# include -# include -#else -# include -# include -#endif +# ifdef __cplusplus +# include +# include +# else +# include +# include +# endif -# include "libmemcached/backtrace.hpp" +# include "libmemcached/backtrace.hpp" -# define assert_msg(__expr, __mesg) \ -do \ -{ \ - if (not (__expr)) \ - { \ - fprintf(stderr, "\n%s:%d Assertion \"%s\" failed for function \"%s\" likely for %s\n", __FILE__, __LINE__, #__expr, __func__, (#__mesg));\ - custom_backtrace(); \ - abort(); \ - } \ -} while (0) +# define assert_msg(__expr, __mesg) \ + do { \ + if (not(__expr)) { \ + fprintf(stderr, "\n%s:%d Assertion \"%s\" failed for function \"%s\" likely for %s\n", \ + __FILE__, __LINE__, #__expr, __func__, (#__mesg)); \ + custom_backtrace(); \ + abort(); \ + } \ + } while (0) -# define assert_vmsg(__expr, __mesg, ...) \ -do \ -{ \ - if (not (__expr)) \ - { \ - size_t ask= snprintf(0, 0, (__mesg), __VA_ARGS__); \ - ask++; \ - char *_error_message= (char*)alloca(sizeof(char) * ask); \ - size_t _error_message_size= snprintf(_error_message, ask, (__mesg), __VA_ARGS__); \ - fprintf(stderr, "\n%s:%d Assertion '%s' failed for function '%s' [ %.*s ]\n", __FILE__, __LINE__, #__expr, __func__, int(_error_message_size), _error_message);\ - custom_backtrace(); \ - abort(); \ - } \ -} while (0) +# define assert_vmsg(__expr, __mesg, ...) \ + do { \ + if (not(__expr)) { \ + size_t ask = snprintf(0, 0, (__mesg), __VA_ARGS__); \ + ask++; \ + char *_error_message = (char *) alloca(sizeof(char) * ask); \ + size_t _error_message_size = snprintf(_error_message, ask, (__mesg), __VA_ARGS__); \ + fprintf(stderr, "\n%s:%d Assertion '%s' failed for function '%s' [ %.*s ]\n", __FILE__, \ + __LINE__, #__expr, __func__, int(_error_message_size), _error_message); \ + custom_backtrace(); \ + abort(); \ + } \ + } while (0) #endif // NDEBUG diff --git a/src/libmemcached/auto.cc b/src/libmemcached/auto.cc index 1d829f68..3ae163f6 100644 --- a/src/libmemcached/auto.cc +++ b/src/libmemcached/auto.cc @@ -1,207 +1,153 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static void auto_response(memcached_instance_st* instance, const bool reply, memcached_return_t& rc, uint64_t* value) -{ +static void auto_response(memcached_instance_st *instance, const bool reply, memcached_return_t &rc, + uint64_t *value) { // If the message was successfully sent, then get the response, otherwise // fail. - if (memcached_success(rc)) - { - if (reply == false) - { - *value= UINT64_MAX; + if (memcached_success(rc)) { + if (reply == false) { + *value = UINT64_MAX; return; } - rc= memcached_response(instance, &instance->root->result); + rc = memcached_response(instance, &instance->root->result); } - if (memcached_fatal(rc)) - { + if (memcached_fatal(rc)) { assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS); - *value= UINT64_MAX; - } - else if (memcached_failed(rc)) - { - *value= UINT64_MAX; - } - else - { + *value = UINT64_MAX; + } else if (memcached_failed(rc)) { + *value = UINT64_MAX; + } else { assert(memcached_last_error(instance->root) != MEMCACHED_NOTFOUND); - *value= instance->root->result.numeric_value; + *value = instance->root->result.numeric_value; } } -static memcached_return_t text_incr_decr(memcached_instance_st* instance, - const bool is_incr, - const char *key, size_t key_length, - const uint64_t offset, - const bool reply) -{ +static memcached_return_t text_incr_decr(memcached_instance_st *instance, const bool is_incr, + const char *key, size_t key_length, const uint64_t offset, + const bool reply) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - int send_length= snprintf(buffer, sizeof(buffer), " %" PRIu64, offset); - if (size_t(send_length) >= sizeof(buffer) or send_length < 0) - { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + int send_length = snprintf(buffer, sizeof(buffer), " %" PRIu64, offset); + if (size_t(send_length) >= sizeof(buffer) or send_length < 0) { + return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { memcached_literal_param("incr ") }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length }, - { buffer, size_t(send_length) }, - { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") }, - { memcached_literal_param("\r\n") } - }; - - if (is_incr == false) - { - vector[1].buffer= "decr "; + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {memcached_literal_param("incr ")}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}, + {buffer, size_t(send_length)}, + {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")}, + {memcached_literal_param("\r\n")}}; + + if (is_incr == false) { + vector[1].buffer = "decr "; } return memcached_vdo(instance, vector, 7, true); } -static memcached_return_t binary_incr_decr(memcached_instance_st* instance, - protocol_binary_command cmd, - const char *key, const size_t key_length, - const uint64_t offset, - const uint64_t initial, - const uint32_t expiration, - const bool reply) -{ - if (reply == false) - { - if(cmd == PROTOCOL_BINARY_CMD_DECREMENT) - { - cmd= PROTOCOL_BINARY_CMD_DECREMENTQ; +static memcached_return_t binary_incr_decr(memcached_instance_st *instance, + protocol_binary_command cmd, const char *key, + const size_t key_length, const uint64_t offset, + const uint64_t initial, const uint32_t expiration, + const bool reply) { + if (reply == false) { + if (cmd == PROTOCOL_BINARY_CMD_DECREMENT) { + cmd = PROTOCOL_BINARY_CMD_DECREMENTQ; } - if(cmd == PROTOCOL_BINARY_CMD_INCREMENT) - { - cmd= PROTOCOL_BINARY_CMD_INCREMENTQ; + if (cmd == PROTOCOL_BINARY_CMD_INCREMENT) { + cmd = PROTOCOL_BINARY_CMD_INCREMENTQ; } } - protocol_binary_request_incr request= {}; // = {.bytes= {0}}; + protocol_binary_request_incr request = {}; // = {.bytes= {0}}; initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode= cmd; - request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace))); - request.message.header.request.extlen= 20; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace) +request.message.header.request.extlen)); - request.message.body.delta= memcached_htonll(offset); - request.message.body.initial= memcached_htonll(initial); - request.message.body.expiration= htonl((uint32_t) expiration); - - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, sizeof(request.bytes) }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length } - }; + request.message.header.request.opcode = cmd; + request.message.header.request.keylen = + htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace))); + request.message.header.request.extlen = 20; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = + htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace) + + request.message.header.request.extlen)); + request.message.body.delta = memcached_htonll(offset); + request.message.body.initial = memcached_htonll(initial); + request.message.body.expiration = htonl((uint32_t) expiration); + + libmemcached_io_vector_st vector[] = {{NULL, 0}, + {request.bytes, sizeof(request.bytes)}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}}; return memcached_vdo(instance, vector, 4, true); } -memcached_return_t memcached_increment(memcached_st *memc, - const char *key, size_t key_length, - uint32_t offset, - uint64_t *value) -{ +memcached_return_t memcached_increment(memcached_st *memc, const char *key, size_t key_length, + uint32_t offset, uint64_t *value) { return memcached_increment_by_key(memc, key, key_length, key, key_length, offset, value); } static memcached_return_t increment_decrement_by_key(const protocol_binary_command command, - Memcached *memc, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - uint64_t offset, - uint64_t *value) -{ + Memcached *memc, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, uint64_t offset, + uint64_t *value) { uint64_t local_value; - if (value == NULL) - { - value= &local_value; + if (value == NULL) { + value = &local_value; } memcached_return_t rc; - if (memcached_failed(rc= initialize_query(memc, true))) - { + if (memcached_failed(rc = initialize_query(memc, true))) { return rc; } - if (memcached_is_encrypted(memc)) - { - return memcached_set_error(*memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, - memcached_literal_param("Operation not allowed while encyrption is enabled")); + if (memcached_is_encrypted(memc)) { + return memcached_set_error( + *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param("Operation not allowed while encyrption is enabled")); } - if (memcached_failed(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1))) - { + if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) { return memcached_last_error(memc); } - uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); + uint32_t server_key = + memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(memc, server_key); - bool reply= memcached_is_replying(instance->root); + bool reply = memcached_is_replying(instance->root); - if (memcached_is_binary(memc)) - { - rc= binary_incr_decr(instance, command, - key, key_length, - uint64_t(offset), 0, MEMCACHED_EXPIRATION_NOT_ADD, - reply); - } - else - { - rc= text_incr_decr(instance, - command == PROTOCOL_BINARY_CMD_INCREMENT ? true : false, - key, key_length, - offset, reply); + if (memcached_is_binary(memc)) { + rc = binary_incr_decr(instance, command, key, key_length, uint64_t(offset), 0, + MEMCACHED_EXPIRATION_NOT_ADD, reply); + } else { + rc = text_incr_decr(instance, command == PROTOCOL_BINARY_CMD_INCREMENT ? true : false, key, + key_length, offset, reply); } auto_response(instance, reply, rc, value); @@ -209,57 +155,46 @@ static memcached_return_t increment_decrement_by_key(const protocol_binary_comma return rc; } -static memcached_return_t increment_decrement_with_initial_by_key(const protocol_binary_command command, - Memcached *memc, - const char *group_key, - size_t group_key_length, - const char *key, - size_t key_length, - uint64_t offset, - uint64_t initial, - time_t expiration, - uint64_t *value) -{ +static memcached_return_t +increment_decrement_with_initial_by_key(const protocol_binary_command command, Memcached *memc, + const char *group_key, size_t group_key_length, + const char *key, size_t key_length, uint64_t offset, + uint64_t initial, time_t expiration, uint64_t *value) { uint64_t local_value; - if (value == NULL) - { - value= &local_value; + if (value == NULL) { + value = &local_value; } memcached_return_t rc; - if (memcached_failed(rc= initialize_query(memc, true))) - { + if (memcached_failed(rc = initialize_query(memc, true))) { return rc; } - if (memcached_is_encrypted(memc)) - { - return memcached_set_error(*memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, - memcached_literal_param("Operation not allowed while encryption is enabled")); + if (memcached_is_encrypted(memc)) { + return memcached_set_error( + *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param("Operation not allowed while encryption is enabled")); } - if (memcached_failed(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1))) - { + if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) { return memcached_last_error(memc); } - uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); + uint32_t server_key = + memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(memc, server_key); - bool reply= memcached_is_replying(instance->root); + bool reply = memcached_is_replying(instance->root); - if (memcached_is_binary(memc)) - { - rc= binary_incr_decr(instance, command, - key, key_length, - offset, initial, uint32_t(expiration), - reply); - - } - else - { - rc= memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("memcached_increment_with_initial_by_key() is not supported via the ASCII protocol")); + if (memcached_is_binary(memc)) { + rc = binary_incr_decr(instance, command, key, key_length, offset, initial, uint32_t(expiration), + reply); + + } else { + rc = memcached_set_error( + *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "memcached_increment_with_initial_by_key() is not supported via the ASCII protocol")); } auto_response(instance, reply, rc, value); @@ -267,117 +202,75 @@ static memcached_return_t increment_decrement_with_initial_by_key(const protocol return rc; } -memcached_return_t memcached_decrement(memcached_st *memc, - const char *key, size_t key_length, - uint32_t offset, - uint64_t *value) -{ +memcached_return_t memcached_decrement(memcached_st *memc, const char *key, size_t key_length, + uint32_t offset, uint64_t *value) { return memcached_decrement_by_key(memc, key, key_length, key, key_length, offset, value); } - -memcached_return_t memcached_increment_by_key(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - uint64_t offset, - uint64_t *value) -{ - Memcached* memc= memcached2Memcached(shell); +memcached_return_t memcached_increment_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, uint64_t offset, uint64_t *value) { + Memcached *memc = memcached2Memcached(shell); LIBMEMCACHED_MEMCACHED_INCREMENT_START(); - memcached_return_t rc= increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT, - memc, - group_key, group_key_length, - key, key_length, - offset, value); + memcached_return_t rc = + increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length, + key, key_length, offset, value); LIBMEMCACHED_MEMCACHED_INCREMENT_END(); return rc; } -memcached_return_t memcached_decrement_by_key(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - uint64_t offset, - uint64_t *value) -{ - Memcached* memc= memcached2Memcached(shell); +memcached_return_t memcached_decrement_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, uint64_t offset, uint64_t *value) { + Memcached *memc = memcached2Memcached(shell); LIBMEMCACHED_MEMCACHED_DECREMENT_START(); - memcached_return_t rc= increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT, - memc, - group_key, group_key_length, - key, key_length, - offset, value); + memcached_return_t rc = + increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length, + key, key_length, offset, value); LIBMEMCACHED_MEMCACHED_DECREMENT_END(); return rc; } -memcached_return_t memcached_increment_with_initial(memcached_st *memc, - const char *key, - size_t key_length, - uint64_t offset, - uint64_t initial, - time_t expiration, - uint64_t *value) -{ - return memcached_increment_with_initial_by_key(memc, key, key_length, - key, key_length, - offset, initial, expiration, value); +memcached_return_t memcached_increment_with_initial(memcached_st *memc, const char *key, + size_t key_length, uint64_t offset, + uint64_t initial, time_t expiration, + uint64_t *value) { + return memcached_increment_with_initial_by_key(memc, key, key_length, key, key_length, offset, + initial, expiration, value); } -memcached_return_t memcached_increment_with_initial_by_key(memcached_st *shell, - const char *group_key, - size_t group_key_length, - const char *key, - size_t key_length, - uint64_t offset, - uint64_t initial, - time_t expiration, - uint64_t *value) -{ +memcached_return_t memcached_increment_with_initial_by_key( + memcached_st *shell, const char *group_key, size_t group_key_length, const char *key, + size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) { LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START(); - Memcached* memc= memcached2Memcached(shell); - memcached_return_t rc= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_INCREMENT, - memc, - group_key, group_key_length, - key, key_length, - offset, initial, expiration, value); + Memcached *memc = memcached2Memcached(shell); + memcached_return_t rc = increment_decrement_with_initial_by_key( + PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length, key, key_length, offset, + initial, expiration, value); LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END(); return rc; } -memcached_return_t memcached_decrement_with_initial(memcached_st *memc, - const char *key, - size_t key_length, - uint64_t offset, - uint64_t initial, - time_t expiration, - uint64_t *value) -{ - return memcached_decrement_with_initial_by_key(memc, key, key_length, - key, key_length, - offset, initial, expiration, value); +memcached_return_t memcached_decrement_with_initial(memcached_st *memc, const char *key, + size_t key_length, uint64_t offset, + uint64_t initial, time_t expiration, + uint64_t *value) { + return memcached_decrement_with_initial_by_key(memc, key, key_length, key, key_length, offset, + initial, expiration, value); } -memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *shell, - const char *group_key, - size_t group_key_length, - const char *key, - size_t key_length, - uint64_t offset, - uint64_t initial, - time_t expiration, - uint64_t *value) -{ +memcached_return_t memcached_decrement_with_initial_by_key( + memcached_st *shell, const char *group_key, size_t group_key_length, const char *key, + size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) { LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START(); - Memcached* memc= memcached2Memcached(shell); - memcached_return_t rc= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_DECREMENT, - memc, - group_key, group_key_length, - key, key_length, - offset, initial, expiration, value); + Memcached *memc = memcached2Memcached(shell); + memcached_return_t rc = increment_decrement_with_initial_by_key( + PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length, key, key_length, offset, + initial, expiration, value); LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END(); diff --git a/src/libmemcached/backtrace.cc b/src/libmemcached/backtrace.cc index 8017f7a2..3687d62c 100644 --- a/src/libmemcached/backtrace.cc +++ b/src/libmemcached/backtrace.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached client library. - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -45,70 +23,58 @@ #if HAVE_BACKTRACE -#include BACKTRACE_HEADER - -#if HAVE_ABI____CXA_DEMANGLE -# include -# define USE_DEMANGLE 1 -#else -# define USE_DEMANGLE 0 -#endif - -#ifdef HAVE_DLFCN_H -# include -#endif - -const int MAX_DEPTH= 50; - -void custom_backtrace(void) -{ - void *backtrace_buffer[MAX_DEPTH +1]; - - int stack_frames= backtrace(backtrace_buffer, MAX_DEPTH); - if (stack_frames) - { - char **symbollist= backtrace_symbols(backtrace_buffer, stack_frames); - if (symbollist) - { - for (int x= 0; x < stack_frames; x++) - { - bool was_demangled= false; - - if (USE_DEMANGLE) - { -#ifdef HAVE_DLFCN_H +# include BACKTRACE_HEADER + +# if HAVE_ABI____CXA_DEMANGLE +# include +# define USE_DEMANGLE 1 +# else +# define USE_DEMANGLE 0 +# endif + +# ifdef HAVE_DLFCN_H +# include +# endif + +const int MAX_DEPTH = 50; + +void custom_backtrace(void) { + void *backtrace_buffer[MAX_DEPTH + 1]; + + int stack_frames = backtrace(backtrace_buffer, MAX_DEPTH); + if (stack_frames) { + char **symbollist = backtrace_symbols(backtrace_buffer, stack_frames); + if (symbollist) { + for (int x = 0; x < stack_frames; x++) { + bool was_demangled = false; + + if (USE_DEMANGLE) { +# ifdef HAVE_DLFCN_H Dl_info dlinfo; - if (dladdr(backtrace_buffer[x], &dlinfo)) - { + if (dladdr(backtrace_buffer[x], &dlinfo)) { char demangled_buffer[1024]; - const char *called_in= ""; - if (dlinfo.dli_sname) - { - size_t demangled_size= sizeof(demangled_buffer); + const char *called_in = ""; + if (dlinfo.dli_sname) { + size_t demangled_size = sizeof(demangled_buffer); int status; - char* demangled; - if ((demangled= abi::__cxa_demangle(dlinfo.dli_sname, demangled_buffer, &demangled_size, &status))) - { - called_in= demangled; + char *demangled; + if ((demangled = abi::__cxa_demangle(dlinfo.dli_sname, demangled_buffer, + &demangled_size, &status))) { + called_in = demangled; fprintf(stderr, "---> demangled: %s -> %s\n", demangled_buffer, demangled); - } - else - { - called_in= dlinfo.dli_sname; + } else { + called_in = dlinfo.dli_sname; } - was_demangled= true; - fprintf(stderr, "#%d %p in %s at %s\n", - x, backtrace_buffer[x], - called_in, + was_demangled = true; + fprintf(stderr, "#%d %p in %s at %s\n", x, backtrace_buffer[x], called_in, dlinfo.dli_fname); } } -#endif +# endif } - if (was_demangled == false) - { + if (was_demangled == false) { fprintf(stderr, "?%d %p in %s\n", x, backtrace_buffer[x], symbollist[x]); } } @@ -118,10 +84,9 @@ void custom_backtrace(void) } } -#else // HAVE_BACKTRACE +#else // HAVE_BACKTRACE -void custom_backtrace(void) -{ +void custom_backtrace(void) { fprintf(stderr, "Backtrace null function called\n"); } #endif // HAVE_BACKTRACE diff --git a/src/libmemcached/backtrace.hpp b/src/libmemcached/backtrace.hpp index a83d3ea1..9b94dcaa 100644 --- a/src/libmemcached/backtrace.hpp +++ b/src/libmemcached/backtrace.hpp @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * libmcachedd client library. - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/behavior.cc b/src/libmemcached/behavior.cc index 36201737..08f86f94 100644 --- a/src/libmemcached/behavior.cc +++ b/src/libmemcached/behavior.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/options.hpp" @@ -42,21 +20,17 @@ #include #include -bool memcached_is_consistent_distribution(const Memcached* memc) -{ - switch (memc->distribution) - { +bool memcached_is_consistent_distribution(const Memcached *memc) { + switch (memc->distribution) { case MEMCACHED_DISTRIBUTION_CONSISTENT: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: - case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: - return true; + case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: return true; case MEMCACHED_DISTRIBUTION_MODULA: case MEMCACHED_DISTRIBUTION_RANDOM: case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: - case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: - break; + case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: break; } return false; @@ -68,139 +42,114 @@ bool memcached_is_consistent_distribution(const Memcached* memc) We quit all connections so we can reset the sockets. */ -memcached_return_t memcached_behavior_set(memcached_st *shell, - const memcached_behavior_t flag, - uint64_t data) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr == NULL) - { +memcached_return_t memcached_behavior_set(memcached_st *shell, const memcached_behavior_t flag, + uint64_t data) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - switch (flag) - { - case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: - ptr->number_of_replicas= (uint32_t)data; - break; + switch (flag) { + case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: ptr->number_of_replicas = (uint32_t) data; break; - case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK: - ptr->io_msg_watermark= (uint32_t) data; - break; + case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK: ptr->io_msg_watermark = (uint32_t) data; break; - case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK: - ptr->io_bytes_watermark= (uint32_t)data; - break; + case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK: ptr->io_bytes_watermark = (uint32_t) data; break; - case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: - ptr->io_key_prefetch = (uint32_t)data; - break; + case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: ptr->io_key_prefetch = (uint32_t) data; break; - case MEMCACHED_BEHAVIOR_SND_TIMEOUT: - ptr->snd_timeout= (int32_t)data; - break; + case MEMCACHED_BEHAVIOR_SND_TIMEOUT: ptr->snd_timeout = (int32_t) data; break; - case MEMCACHED_BEHAVIOR_RCV_TIMEOUT: - ptr->rcv_timeout= (int32_t)data; - break; + case MEMCACHED_BEHAVIOR_RCV_TIMEOUT: ptr->rcv_timeout = (int32_t) data; break; - case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: - ptr->flags.auto_eject_hosts= bool(data); - break; + case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: ptr->flags.auto_eject_hosts = bool(data); break; case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: - if (data == 0) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT requires a value greater then zero.")); + if (data == 0) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT requires a value greater then zero.")); } - ptr->server_failure_limit= uint32_t(data); + ptr->server_failure_limit = uint32_t(data); break; - case MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT: - ptr->server_timeout_limit= uint32_t(data); - break; + case MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT: ptr->server_timeout_limit = uint32_t(data); break; case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: - send_quit(ptr); // We need t shutdown all of the connections to make sure we do the correct protocol - if (data) - { - ptr->flags.verify_key= false; + send_quit( + ptr); // We need t shutdown all of the connections to make sure we do the correct protocol + if (data) { + ptr->flags.verify_key = false; } - ptr->flags.binary_protocol= bool(data); + ptr->flags.binary_protocol = bool(data); break; - case MEMCACHED_BEHAVIOR_SUPPORT_CAS: - ptr->flags.support_cas= bool(data); - break; + case MEMCACHED_BEHAVIOR_SUPPORT_CAS: ptr->flags.support_cas = bool(data); break; case MEMCACHED_BEHAVIOR_NO_BLOCK: - ptr->flags.no_block= bool(data); + ptr->flags.no_block = bool(data); send_quit(ptr); break; case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: - if (memcached_is_udp(ptr)) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_BUFFER_REQUESTS cannot be set while MEMCACHED_BEHAVIOR_USE_UDP is enabled.")); + if (memcached_is_udp(ptr)) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("MEMCACHED_BEHAVIOR_BUFFER_REQUESTS cannot be set while " + "MEMCACHED_BEHAVIOR_USE_UDP is enabled.")); } - ptr->flags.buffer_requests= bool(data); + ptr->flags.buffer_requests = bool(data); send_quit(ptr); break; case MEMCACHED_BEHAVIOR_USE_UDP: - send_quit(ptr); // We need t shutdown all of the connections to make sure we do the correct protocol - ptr->flags.use_udp= bool(data); - if (bool(data)) - { - ptr->flags.reply= false; - ptr->flags.buffer_requests= false; - } - else - { - ptr->flags.reply= true; + send_quit( + ptr); // We need t shutdown all of the connections to make sure we do the correct protocol + ptr->flags.use_udp = bool(data); + if (bool(data)) { + ptr->flags.reply = false; + ptr->flags.buffer_requests = false; + } else { + ptr->flags.reply = true; } break; case MEMCACHED_BEHAVIOR_TCP_NODELAY: - ptr->flags.tcp_nodelay= bool(data); + ptr->flags.tcp_nodelay = bool(data); send_quit(ptr); break; case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: - ptr->flags.tcp_keepalive= bool(data); + ptr->flags.tcp_keepalive = bool(data); send_quit(ptr); break; case MEMCACHED_BEHAVIOR_DISTRIBUTION: - return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data); + return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t) data); - case MEMCACHED_BEHAVIOR_KETAMA: + case MEMCACHED_BEHAVIOR_KETAMA: { + if (data) // Turn on { - if (data) // Turn on - { - return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); - } - - return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA); + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); } - case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: - { - if (bool(data) == false) - { - return memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_KETAMA, true); - } + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA); + } - (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5); - (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5); - /** - @note We try to keep the same distribution going. This should be deprecated and rewritten. - */ - return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED); + case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: { + if (bool(data) == false) { + return memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_KETAMA, true); } + (void) memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5); + (void) memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5); + /** + @note We try to keep the same distribution going. This should be deprecated and rewritten. + */ + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED); + } + case MEMCACHED_BEHAVIOR_HASH: return memcached_behavior_set_key_hash(ptr, (memcached_hash_t)(data)); @@ -208,52 +157,45 @@ memcached_return_t memcached_behavior_set(memcached_st *shell, return memcached_behavior_set_distribution_hash(ptr, (memcached_hash_t)(data)); case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: - return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_CACHE_LOOKUPS has been deprecated.")); + return memcached_set_error( + *ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, + memcached_literal_param("MEMCACHED_BEHAVIOR_CACHE_LOOKUPS has been deprecated.")); case MEMCACHED_BEHAVIOR_VERIFY_KEY: - if (ptr->flags.binary_protocol) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_VERIFY_KEY if the binary protocol has been enabled.")); + if (ptr->flags.binary_protocol) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "MEMCACHED_BEHAVIOR_VERIFY_KEY if the binary protocol has been enabled.")); } - ptr->flags.verify_key= bool(data); + ptr->flags.verify_key = bool(data); break; - case MEMCACHED_BEHAVIOR_SORT_HOSTS: - { - ptr->flags.use_sort_hosts= bool(data); - return run_distribution(ptr); - } + case MEMCACHED_BEHAVIOR_SORT_HOSTS: { + ptr->flags.use_sort_hosts = bool(data); + return run_distribution(ptr); + } - case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: - ptr->poll_timeout= (int32_t)data; - break; + case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: ptr->poll_timeout = (int32_t) data; break; - case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: - ptr->connect_timeout= (int32_t)data; - break; + case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: ptr->connect_timeout = (int32_t) data; break; - case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: - ptr->retry_timeout= int32_t(data); - break; + case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: ptr->retry_timeout = int32_t(data); break; - case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: - ptr->dead_timeout= int32_t(data); - break; + case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: ptr->dead_timeout = int32_t(data); break; case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: - ptr->send_size= (int32_t)data; + ptr->send_size = (int32_t) data; send_quit(ptr); break; case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: - ptr->recv_size= (int32_t)data; + ptr->recv_size = (int32_t) data; send_quit(ptr); break; case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: - ptr->tcp_keepidle= (uint32_t)data; + ptr->tcp_keepidle = (uint32_t) data; send_quit(ptr); break; @@ -261,241 +203,197 @@ memcached_return_t memcached_behavior_set(memcached_st *shell, return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated.")); - case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY: - ptr->flags.hash_with_namespace= bool(data); - break; + case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY: ptr->flags.hash_with_namespace = bool(data); break; case MEMCACHED_BEHAVIOR_NOREPLY: - if (memcached_is_udp(ptr) and bool(data) == false) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_NOREPLY cannot be disabled while MEMCACHED_BEHAVIOR_USE_UDP is enabled.")); + if (memcached_is_udp(ptr) and bool(data) == false) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("MEMCACHED_BEHAVIOR_NOREPLY cannot be disabled while " + "MEMCACHED_BEHAVIOR_USE_UDP is enabled.")); } // We reverse the logic here to make it easier to understand throughout the // code. - ptr->flags.reply= bool(data) ? false : true; + ptr->flags.reply = bool(data) ? false : true; break; - case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: - ptr->flags.auto_eject_hosts= bool(data); - break; + case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: ptr->flags.auto_eject_hosts = bool(data); break; case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: - srandom((uint32_t) time(NULL)); - ptr->flags.randomize_replica_read= bool(data); - break; + srandom((uint32_t) time(NULL)); + ptr->flags.randomize_replica_read = bool(data); + break; case MEMCACHED_BEHAVIOR_CORK: - return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default.")); + return memcached_set_error( + *ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, + memcached_literal_param( + "MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default.")); case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE: - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("MEMCACHED_BEHAVIOR_LOAD_FROM_FILE can not be set with memcached_behavior_set()")); + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "MEMCACHED_BEHAVIOR_LOAD_FROM_FILE can not be set with memcached_behavior_set()")); case MEMCACHED_BEHAVIOR_MAX: default: - /* Shouldn't get here */ - assert_msg(0, "Invalid behavior passed to memcached_behavior_set()"); - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Invalid behavior passed to memcached_behavior_set()")); + /* Shouldn't get here */ + assert_msg(0, "Invalid behavior passed to memcached_behavior_set()"); + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid behavior passed to memcached_behavior_set()")); } return MEMCACHED_SUCCESS; } -bool _is_auto_eject_host(const memcached_st *ptr) -{ +bool _is_auto_eject_host(const memcached_st *ptr) { return ptr->flags.auto_eject_hosts; } -uint64_t memcached_behavior_get(memcached_st *shell, - const memcached_behavior_t flag) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr == NULL) - { +uint64_t memcached_behavior_get(memcached_st *shell, const memcached_behavior_t flag) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - switch (flag) - { - case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: - return ptr->number_of_replicas; + switch (flag) { + case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: return ptr->number_of_replicas; - case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK: - return ptr->io_msg_watermark; + case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK: return ptr->io_msg_watermark; - case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK: - return ptr->io_bytes_watermark; + case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK: return ptr->io_bytes_watermark; - case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: - return ptr->io_key_prefetch; + case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: return ptr->io_key_prefetch; - case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: - return ptr->flags.binary_protocol; + case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: return ptr->flags.binary_protocol; - case MEMCACHED_BEHAVIOR_SUPPORT_CAS: - return ptr->flags.support_cas; + case MEMCACHED_BEHAVIOR_SUPPORT_CAS: return ptr->flags.support_cas; - case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: - return true; + case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: return true; - case MEMCACHED_BEHAVIOR_NO_BLOCK: - return ptr->flags.no_block; + case MEMCACHED_BEHAVIOR_NO_BLOCK: return ptr->flags.no_block; - case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: - return ptr->flags.buffer_requests; + case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: return ptr->flags.buffer_requests; - case MEMCACHED_BEHAVIOR_USE_UDP: - return memcached_is_udp(ptr); + case MEMCACHED_BEHAVIOR_USE_UDP: return memcached_is_udp(ptr); - case MEMCACHED_BEHAVIOR_TCP_NODELAY: - return ptr->flags.tcp_nodelay; + case MEMCACHED_BEHAVIOR_TCP_NODELAY: return ptr->flags.tcp_nodelay; - case MEMCACHED_BEHAVIOR_VERIFY_KEY: - return ptr->flags.verify_key; + case MEMCACHED_BEHAVIOR_VERIFY_KEY: return ptr->flags.verify_key; case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: - if (memcached_is_consistent_distribution(ptr)) - { + if (memcached_is_consistent_distribution(ptr)) { return memcached_is_weighted_ketama(ptr); } return false; - case MEMCACHED_BEHAVIOR_DISTRIBUTION: - return ptr->distribution; + case MEMCACHED_BEHAVIOR_DISTRIBUTION: return ptr->distribution; - case MEMCACHED_BEHAVIOR_KETAMA: - return memcached_is_consistent_distribution(ptr); + case MEMCACHED_BEHAVIOR_KETAMA: return memcached_is_consistent_distribution(ptr); - case MEMCACHED_BEHAVIOR_HASH: - return hashkit_get_function(&ptr->hashkit); + case MEMCACHED_BEHAVIOR_HASH: return hashkit_get_function(&ptr->hashkit); - case MEMCACHED_BEHAVIOR_KETAMA_HASH: - return hashkit_get_distribution_function(&ptr->hashkit); + case MEMCACHED_BEHAVIOR_KETAMA_HASH: return hashkit_get_distribution_function(&ptr->hashkit); - case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: - return ptr->flags.auto_eject_hosts; + case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: return ptr->flags.auto_eject_hosts; - case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: - return ptr->server_failure_limit; + case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: return ptr->server_failure_limit; - case MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT: - return ptr->server_timeout_limit; + case MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT: return ptr->server_timeout_limit; - case MEMCACHED_BEHAVIOR_SORT_HOSTS: - return ptr->flags.use_sort_hosts; + case MEMCACHED_BEHAVIOR_SORT_HOSTS: return ptr->flags.use_sort_hosts; - case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: - return (uint64_t)ptr->poll_timeout; + case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: return (uint64_t) ptr->poll_timeout; - case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: - return (uint64_t)ptr->connect_timeout; + case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: return (uint64_t) ptr->connect_timeout; - case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: - return (uint64_t)ptr->retry_timeout; + case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: return (uint64_t) ptr->retry_timeout; - case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: - return uint64_t(ptr->dead_timeout); + case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: return uint64_t(ptr->dead_timeout); - case MEMCACHED_BEHAVIOR_SND_TIMEOUT: - return (uint64_t)ptr->snd_timeout; + case MEMCACHED_BEHAVIOR_SND_TIMEOUT: return (uint64_t) ptr->snd_timeout; - case MEMCACHED_BEHAVIOR_RCV_TIMEOUT: - return (uint64_t)ptr->rcv_timeout; + case MEMCACHED_BEHAVIOR_RCV_TIMEOUT: return (uint64_t) ptr->rcv_timeout; - case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: - return (uint64_t)ptr->tcp_keepidle; + case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: return (uint64_t) ptr->tcp_keepidle; - case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: { + int sock_size = 0; + socklen_t sock_length = sizeof(int); + + if (ptr->send_size != -1) // If value is -1 then we are using the default { - int sock_size= 0; - socklen_t sock_length= sizeof(int); + return (uint64_t) ptr->send_size; + } - if (ptr->send_size != -1) // If value is -1 then we are using the default - { - return (uint64_t) ptr->send_size; + memcached_instance_st *instance = memcached_instance_fetch(ptr, 0); + + if (instance) // If we have an instance we test, otherwise we just set and pray + { + /* REFACTOR */ + /* We just try the first host, and if it is down we return zero */ + if (memcached_failed(memcached_connect(instance))) { + return 0; } - memcached_instance_st* instance= memcached_instance_fetch(ptr, 0); - - if (instance) // If we have an instance we test, otherwise we just set and pray - { - /* REFACTOR */ - /* We just try the first host, and if it is down we return zero */ - if (memcached_failed(memcached_connect(instance))) - { - return 0; - } - - if (memcached_failed(memcached_io_wait_for_write(instance))) - { - return 0; - } - - if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, (char*)&sock_size, &sock_length) < 0) - { - memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); - return 0; /* Zero means error */ - } + if (memcached_failed(memcached_io_wait_for_write(instance))) { + return 0; } - return (uint64_t) sock_size; + if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, (char *) &sock_size, &sock_length) < 0) { + memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); + return 0; /* Zero means error */ + } } - case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: - { - int sock_size= 0; - socklen_t sock_length= sizeof(int); - - if (ptr->recv_size != -1) // If value is -1 then we are using the default - return (uint64_t) ptr->recv_size; - - memcached_instance_st* instance= memcached_instance_fetch(ptr, 0); - - /** - @note REFACTOR - */ - if (instance) - { - /* We just try the first host, and if it is down we return zero */ - if (memcached_failed(memcached_connect(instance))) - { - return 0; - } - - if (memcached_failed(memcached_io_wait_for_write(instance))) - { - return 0; - } - - if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, (char*)&sock_size, &sock_length) < 0) - { - memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); - return 0; /* Zero means error */ - } + return (uint64_t) sock_size; + } + + case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: { + int sock_size = 0; + socklen_t sock_length = sizeof(int); + + if (ptr->recv_size != -1) // If value is -1 then we are using the default + return (uint64_t) ptr->recv_size; + + memcached_instance_st *instance = memcached_instance_fetch(ptr, 0); + + /** + @note REFACTOR + */ + if (instance) { + /* We just try the first host, and if it is down we return zero */ + if (memcached_failed(memcached_connect(instance))) { + return 0; } - return (uint64_t) sock_size; + if (memcached_failed(memcached_io_wait_for_write(instance))) { + return 0; + } + + if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, (char *) &sock_size, &sock_length) < 0) { + memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); + return 0; /* Zero means error */ + } } + return (uint64_t) sock_size; + } + case MEMCACHED_BEHAVIOR_USER_DATA: memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT, memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated.")); return 0; - case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY: - return ptr->flags.hash_with_namespace; + case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY: return ptr->flags.hash_with_namespace; - case MEMCACHED_BEHAVIOR_NOREPLY: - return ptr->flags.reply ? false : true; + case MEMCACHED_BEHAVIOR_NOREPLY: return ptr->flags.reply ? false : true; - case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: - return ptr->flags.auto_eject_hosts; + case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: return ptr->flags.auto_eject_hosts; - case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: - return ptr->flags.randomize_replica_read; + case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: return ptr->flags.randomize_replica_read; case MEMCACHED_BEHAVIOR_CORK: #ifdef HAVE_MSG_MORE @@ -504,56 +402,44 @@ uint64_t memcached_behavior_get(memcached_st *shell, return false; #endif - case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: - return ptr->flags.tcp_keepalive; + case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: return ptr->flags.tcp_keepalive; - case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE: - return bool(memcached_parse_filename(ptr)); + case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE: return bool(memcached_parse_filename(ptr)); case MEMCACHED_BEHAVIOR_MAX: - default: - assert_msg(0, "Invalid behavior passed to memcached_behavior_get()"); - return 0; + default: assert_msg(0, "Invalid behavior passed to memcached_behavior_get()"); return 0; } /* NOTREACHED */ } - -memcached_return_t memcached_behavior_set_distribution(memcached_st *shell, memcached_server_distribution_t type) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - switch (type) - { - case MEMCACHED_DISTRIBUTION_MODULA: - break; +memcached_return_t memcached_behavior_set_distribution(memcached_st *shell, + memcached_server_distribution_t type) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + switch (type) { + case MEMCACHED_DISTRIBUTION_MODULA: break; case MEMCACHED_DISTRIBUTION_CONSISTENT: - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: - memcached_set_weighted_ketama(ptr, false); - break; + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: memcached_set_weighted_ketama(ptr, false); break; - case MEMCACHED_DISTRIBUTION_RANDOM: - break; + case MEMCACHED_DISTRIBUTION_RANDOM: break; - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: - break; + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: break; case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: memcached_set_weighted_ketama(ptr, true); break; - case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: - break; + case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: break; default: case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Invalid memcached_server_distribution_t")); + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid memcached_server_distribution_t")); } - ptr->distribution= type; + ptr->distribution = type; return run_distribution(ptr); } @@ -561,25 +447,19 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *shell, memc return MEMCACHED_INVALID_ARGUMENTS; } - -memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *shell) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { +memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *shell) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { return ptr->distribution; } return MEMCACHED_DISTRIBUTION_CONSISTENT_MAX; } -memcached_return_t memcached_behavior_set_key_hash(memcached_st *shell, memcached_hash_t type) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - if (hashkit_success(hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type))) - { +memcached_return_t memcached_behavior_set_key_hash(memcached_st *shell, memcached_hash_t type) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + if (hashkit_success(hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t) type))) { return MEMCACHED_SUCCESS; } @@ -590,23 +470,21 @@ memcached_return_t memcached_behavior_set_key_hash(memcached_st *shell, memcache return MEMCACHED_INVALID_ARGUMENTS; } -memcached_hash_t memcached_behavior_get_key_hash(memcached_st *shell) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - return (memcached_hash_t)hashkit_get_function(&ptr->hashkit); +memcached_hash_t memcached_behavior_get_key_hash(memcached_st *shell) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + return (memcached_hash_t) hashkit_get_function(&ptr->hashkit); } return MEMCACHED_HASH_MAX; } -memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *shell, memcached_hash_t type) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - if (hashkit_success(hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type))) +memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *shell, + memcached_hash_t type) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + if (hashkit_success( + hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t) type))) { return MEMCACHED_SUCCESS; } @@ -618,21 +496,17 @@ memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *shell, return MEMCACHED_INVALID_ARGUMENTS; } -memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *shell) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - return (memcached_hash_t)hashkit_get_function(&ptr->hashkit); +memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *shell) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + return (memcached_hash_t) hashkit_get_function(&ptr->hashkit); } return MEMCACHED_HASH_MAX; } -const char *libmemcached_string_behavior(const memcached_behavior_t flag) -{ - switch (flag) - { +const char *libmemcached_string_behavior(const memcached_behavior_t flag) { + switch (flag) { case MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT: return "MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT"; case MEMCACHED_BEHAVIOR_NO_BLOCK: return "MEMCACHED_BEHAVIOR_NO_BLOCK"; case MEMCACHED_BEHAVIOR_TCP_NODELAY: return "MEMCACHED_BEHAVIOR_TCP_NODELAY"; @@ -666,7 +540,8 @@ const char *libmemcached_string_behavior(const memcached_behavior_t flag) case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: return "MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS"; case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: return "MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS"; case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: return "MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS"; - case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: return "MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ"; + case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: + return "MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ"; case MEMCACHED_BEHAVIOR_CORK: return "MEMCACHED_BEHAVIOR_CORK"; case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: return "MEMCACHED_BEHAVIOR_TCP_KEEPALIVE"; case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: return "MEMCACHED_BEHAVIOR_TCP_KEEPIDLE"; @@ -676,49 +551,46 @@ const char *libmemcached_string_behavior(const memcached_behavior_t flag) } } -const char *libmemcached_string_distribution(const memcached_server_distribution_t flag) -{ - switch (flag) - { +const char *libmemcached_string_distribution(const memcached_server_distribution_t flag) { + switch (flag) { case MEMCACHED_DISTRIBUTION_MODULA: return "MEMCACHED_DISTRIBUTION_MODULA"; case MEMCACHED_DISTRIBUTION_CONSISTENT: return "MEMCACHED_DISTRIBUTION_CONSISTENT"; case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA"; case MEMCACHED_DISTRIBUTION_RANDOM: return "MEMCACHED_DISTRIBUTION_RANDOM"; - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY"; - case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: return "MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED"; + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: + return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY"; + case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: + return "MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED"; case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: return "MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET"; default: case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: return "INVALID memcached_server_distribution_t"; } } -memcached_return_t memcached_bucket_set(memcached_st *shell, - const uint32_t *host_map, - const uint32_t *forward_map, - const uint32_t buckets, - const uint32_t replicas) -{ - Memcached* self= memcached2Memcached(shell); +memcached_return_t memcached_bucket_set(memcached_st *shell, const uint32_t *host_map, + const uint32_t *forward_map, const uint32_t buckets, + const uint32_t replicas) { + Memcached *self = memcached2Memcached(shell); memcached_return_t rc; - if (self == NULL) - { + if (self == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (host_map == NULL) - { + if (host_map == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - memcached_server_distribution_t old= memcached_behavior_get_distribution(self); + memcached_server_distribution_t old = memcached_behavior_get_distribution(self); - if (memcached_failed(rc =memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET))) + if (memcached_failed( + rc = memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET))) { return rc; } - if (memcached_failed(rc= memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas))) + if (memcached_failed( + rc = memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas))) { memcached_behavior_set_distribution(self, old); } diff --git a/src/libmemcached/behavior.hpp b/src/libmemcached/behavior.hpp index 0b06c0ca..8d1de793 100644 --- a/src/libmemcached/behavior.hpp +++ b/src/libmemcached/behavior.hpp @@ -1,40 +1,19 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -bool memcached_is_consistent_distribution(const memcached_st*); +bool memcached_is_consistent_distribution(const memcached_st *); bool _is_auto_eject_host(const memcached_st *ptr); diff --git a/src/libmemcached/byteorder.cc b/src/libmemcached/byteorder.cc index c3867a5a..1bfe12b9 100644 --- a/src/libmemcached/byteorder.cc +++ b/src/libmemcached/byteorder.cc @@ -1,72 +1,47 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" #include "libmemcached/byteorder.h" /* Byte swap a 64-bit number. */ #ifndef swap64 -# ifndef HAVE_HTONLL -static inline uint64_t swap64(uint64_t in) -{ -#if !WORDS_BIGENDIAN +# ifndef HAVE_HTONLL +static inline uint64_t swap64(uint64_t in) { +# if !WORDS_BIGENDIAN /* Little endian, flip the bytes around until someone makes a faster/better * way to do this. */ - uint64_t rv= 0; - for (uint8_t x= 0; x < 8; ++x) - { - rv= (rv << 8) | (in & 0xff); + uint64_t rv = 0; + for (uint8_t x = 0; x < 8; ++x) { + rv = (rv << 8) | (in & 0xff); in >>= 8; } return rv; -#else +# else /* big-endian machines don't need byte swapping */ return in; -#endif // WORDS_BIGENDIAN +# endif // WORDS_BIGENDIAN } -# endif +# endif #endif #ifdef HAVE_SYS_TYPES_H -# include +# include #endif -uint64_t memcached_ntohll(uint64_t value) -{ +uint64_t memcached_ntohll(uint64_t value) { #ifdef HAVE_HTONLL return ntohll(value); #else @@ -74,8 +49,7 @@ uint64_t memcached_ntohll(uint64_t value) #endif } -uint64_t memcached_htonll(uint64_t value) -{ +uint64_t memcached_htonll(uint64_t value) { #ifdef HAVE_HTONLL return htonll(value); #else diff --git a/src/libmemcached/byteorder.h b/src/libmemcached/byteorder.h index a7d108d3..30c393e9 100644 --- a/src/libmemcached/byteorder.h +++ b/src/libmemcached/byteorder.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/callback.cc b/src/libmemcached/callback.cc index b7cc127b..eaa54568 100644 --- a/src/libmemcached/callback.cc +++ b/src/libmemcached/callback.cc @@ -1,88 +1,86 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: Change any of the possible callbacks. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include #ifndef __INTEL_COMPILER -#pragma GCC diagnostic ignored "-Wstrict-aliasing" +# pragma GCC diagnostic ignored "-Wstrict-aliasing" #endif /* These functions provide data and function callback support */ -memcached_return_t memcached_callback_set(memcached_st *shell, - const memcached_callback_t flag, - const void *data) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - switch (flag) - { - case MEMCACHED_CALLBACK_PREFIX_KEY: - { - return memcached_set_namespace(*ptr, (char*)data, data ? strlen((char*)data) : 0); - } +memcached_return_t memcached_callback_set(memcached_st *shell, const memcached_callback_t flag, + const void *data) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + switch (flag) { + case MEMCACHED_CALLBACK_PREFIX_KEY: { + return memcached_set_namespace(*ptr, (char *) data, data ? strlen((char *) data) : 0); + } - case MEMCACHED_CALLBACK_USER_DATA: - { - ptr->user_data= const_cast(data); - break; - } + case MEMCACHED_CALLBACK_USER_DATA: { + ptr->user_data = const_cast(data); + break; + } - case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: - { - memcached_cleanup_fn func= *(memcached_cleanup_fn *)&data; - ptr->on_cleanup= func; - break; - } + case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: { + memcached_cleanup_fn func = *(memcached_cleanup_fn *) &data; + ptr->on_cleanup = func; + break; + } - case MEMCACHED_CALLBACK_CLONE_FUNCTION: - { - memcached_clone_fn func= *(memcached_clone_fn *)&data; - ptr->on_clone= func; - break; - } + case MEMCACHED_CALLBACK_CLONE_FUNCTION: { + memcached_clone_fn func = *(memcached_clone_fn *) &data; + ptr->on_clone = func; + break; + } - case MEMCACHED_CALLBACK_GET_FAILURE: - { - memcached_trigger_key_fn func= *(memcached_trigger_key_fn *)&data; - ptr->get_key_failure= func; - break; - } + case MEMCACHED_CALLBACK_GET_FAILURE: { + memcached_trigger_key_fn func = *(memcached_trigger_key_fn *) &data; + ptr->get_key_failure = func; + break; + } - case MEMCACHED_CALLBACK_DELETE_TRIGGER: + case MEMCACHED_CALLBACK_DELETE_TRIGGER: { + if (data) // NULL would mean we are disabling. { - if (data) // NULL would mean we are disabling. - { - if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS)) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if buffering is enabled")); - } - - if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_NOREPLY)) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set")); - } + if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS)) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Delete triggers cannot be used if buffering is enabled")); } - memcached_trigger_delete_key_fn func= *(memcached_trigger_delete_key_fn *)&data; - ptr->delete_trigger= func; - break; + if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_NOREPLY)) { + return memcached_set_error( + *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set")); + } } + memcached_trigger_delete_key_fn func = *(memcached_trigger_delete_key_fn *) &data; + ptr->delete_trigger = func; + break; + } + case MEMCACHED_CALLBACK_MAX: - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid callback supplied")); + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid callback supplied")); } return MEMCACHED_SUCCESS; @@ -91,70 +89,57 @@ memcached_return_t memcached_callback_set(memcached_st *shell, return MEMCACHED_INVALID_ARGUMENTS; } -void *memcached_callback_get(memcached_st *shell, - const memcached_callback_t flag, - memcached_return_t *error) -{ - Memcached* ptr= memcached2Memcached(shell); +void *memcached_callback_get(memcached_st *shell, const memcached_callback_t flag, + memcached_return_t *error) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t local_error; - if (error == NULL) - { + if (error == NULL) { error = &local_error; } - if (ptr == NULL) - { - *error= MEMCACHED_INVALID_ARGUMENTS; + if (ptr == NULL) { + *error = MEMCACHED_INVALID_ARGUMENTS; return NULL; } - switch (flag) - { - case MEMCACHED_CALLBACK_PREFIX_KEY: - { - *error= MEMCACHED_SUCCESS; - if (ptr->_namespace) - { - return (void *)memcached_array_string(ptr->_namespace); - } - return NULL; + switch (flag) { + case MEMCACHED_CALLBACK_PREFIX_KEY: { + *error = MEMCACHED_SUCCESS; + if (ptr->_namespace) { + return (void *) memcached_array_string(ptr->_namespace); } + return NULL; + } - case MEMCACHED_CALLBACK_USER_DATA: - { - *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; - return (void *)ptr->user_data; - } + case MEMCACHED_CALLBACK_USER_DATA: { + *error = ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return (void *) ptr->user_data; + } - case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: - { - *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; - return *(void **)&ptr->on_cleanup; - } + case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: { + *error = ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return *(void **) &ptr->on_cleanup; + } - case MEMCACHED_CALLBACK_CLONE_FUNCTION: - { - *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; - return *(void **)&ptr->on_clone; - } + case MEMCACHED_CALLBACK_CLONE_FUNCTION: { + *error = ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return *(void **) &ptr->on_clone; + } - case MEMCACHED_CALLBACK_GET_FAILURE: - { - *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; - return *(void **)&ptr->get_key_failure; - } + case MEMCACHED_CALLBACK_GET_FAILURE: { + *error = ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return *(void **) &ptr->get_key_failure; + } - case MEMCACHED_CALLBACK_DELETE_TRIGGER: - { - *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; - return *(void **)&ptr->delete_trigger; - } + case MEMCACHED_CALLBACK_DELETE_TRIGGER: { + *error = ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return *(void **) &ptr->delete_trigger; + } - case MEMCACHED_CALLBACK_MAX: - break; + case MEMCACHED_CALLBACK_MAX: break; } assert_msg(0, "Invalid callback passed to memcached_callback_get()"); - *error= MEMCACHED_FAILURE; + *error = MEMCACHED_FAILURE; return NULL; } diff --git a/src/libmemcached/callback.h b/src/libmemcached/callback.h index 3949ea3d..c3f848ee 100644 --- a/src/libmemcached/callback.h +++ b/src/libmemcached/callback.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached Client and Server - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/common.h b/src/libmemcached/common.h index df3fd180..4b025fb3 100644 --- a/src/libmemcached/common.h +++ b/src/libmemcached/common.h @@ -1,43 +1,16 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - /* - Common include file for libmemached + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #pragma once @@ -45,70 +18,70 @@ #include "mem_config.h" #ifdef __cplusplus -# include -# include -# include -# include -# include -# include -# include -# include +# include +# include +# include +# include +# include +# include +# include +# include #else -# ifdef HAVE_STDDEF_H -# include -# endif -# ifdef HAVE_STDLIB_H -# include -# endif -# ifdef HAVE_STDLIB_H -# include -# endif -# include -# ifdef HAVE_TIME_H -# include -# endif -# ifdef HAVE_ERRNO_H -# include -# endif -# ifdef HAVE_LIMITS_H -# include -# endif +# ifdef HAVE_STDDEF_H +# include +# endif +# ifdef HAVE_STDLIB_H +# include +# endif +# ifdef HAVE_STDLIB_H +# include +# endif +# include +# ifdef HAVE_TIME_H +# include +# endif +# ifdef HAVE_ERRNO_H +# include +# endif +# ifdef HAVE_LIMITS_H +# include +# endif #endif #ifdef HAVE_SYS_UN_H -# include +# include #endif #ifdef HAVE_SYS_TIME_H -# include +# include #endif #ifdef HAVE_FCNTL_H -# include +# include #endif #ifdef HAVE_SYS_TYPES_H -# include +# include #endif #ifdef HAVE_UNISTD_H -# include +# include #endif #ifdef HAVE_SYS_SOCKET_H -# include +# include #endif #ifdef HAVE_STRINGS_H -# include +# include #endif #ifdef HAVE_DLFCN_H -# include +# include #endif #if defined(_WIN32) -# include "libmemcached/windows.hpp" +# include "libmemcached/windows.hpp" #endif #include "libmemcached-1.0/memcached.h" @@ -117,13 +90,13 @@ typedef struct memcached_st Memcached; #ifdef HAVE_POLL_H -# include +# include #else -# include "libmemcached/poll.h" +# include "libmemcached/poll.h" #endif #ifdef __cplusplus -memcached_instance_st* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key); +memcached_instance_st *memcached_instance_fetch(memcached_st *ptr, uint32_t server_key); #endif /* These are private not to be installed headers */ @@ -131,23 +104,23 @@ memcached_instance_st* memcached_instance_fetch(memcached_st *ptr, uint32_t serv #include "libmemcached/memory.h" #include "libmemcached/io.h" #ifdef __cplusplus -# include "libmemcached/string.hpp" -# include "libmemcachedprotocol-0.0/binary.h" -# include "libmemcached/io.hpp" -# include "libmemcached/udp.hpp" -# include "libmemcached/do.hpp" -# include "libmemcached/socket.hpp" -# include "libmemcached/connect.hpp" -# include "libmemcached/allocators.hpp" -# include "libmemcached/hash.hpp" -# include "libmemcached/quit.hpp" -# include "libmemcached/instance.hpp" -# include "libmemcached/server_instance.h" -# include "libmemcached/server.hpp" -# include "libmemcached/flag.hpp" -# include "libmemcached/behavior.hpp" -# include "libmemcached/sasl.hpp" -# include "libmemcached/server_list.hpp" +# include "libmemcached/string.hpp" +# include "libmemcachedprotocol-0.0/binary.h" +# include "libmemcached/io.hpp" +# include "libmemcached/udp.hpp" +# include "libmemcached/do.hpp" +# include "libmemcached/socket.hpp" +# include "libmemcached/connect.hpp" +# include "libmemcached/allocators.hpp" +# include "libmemcached/hash.hpp" +# include "libmemcached/quit.hpp" +# include "libmemcached/instance.hpp" +# include "libmemcached/server_instance.h" +# include "libmemcached/server.hpp" +# include "libmemcached/flag.hpp" +# include "libmemcached/behavior.hpp" +# include "libmemcached/sasl.hpp" +# include "libmemcached/server_list.hpp" #endif #include "libmemcached/internal.h" @@ -157,38 +130,38 @@ memcached_instance_st* memcached_instance_fetch(memcached_st *ptr, uint32_t serv #include "libmemcached/initialize_query.h" #ifdef __cplusplus -# include "libmemcached/response.h" -# include "libmemcached/namespace.h" +# include "libmemcached/response.h" +# include "libmemcached/namespace.h" #else -# include "libmemcached/virtual_bucket.h" +# include "libmemcached/virtual_bucket.h" #endif #ifdef __cplusplus -# include "libmemcached/backtrace.hpp" -# include "libmemcached/assert.hpp" -# include "libmemcached/server.hpp" -# include "libmemcached/key.hpp" -# include "libmemcached/result.h" -# include "libmemcached/version.hpp" +# include "libmemcached/backtrace.hpp" +# include "libmemcached/assert.hpp" +# include "libmemcached/server.hpp" +# include "libmemcached/key.hpp" +# include "libmemcached/result.h" +# include "libmemcached/version.hpp" #endif #include "libmemcached/continuum.hpp" #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) -#define likely(x) if((x)) -#define unlikely(x) if((x)) +# define likely(x) if ((x)) +# define unlikely(x) if ((x)) #else -#define likely(x) if(__builtin_expect((x) != 0, 1)) -#define unlikely(x) if(__builtin_expect((x) != 0, 0)) +# define likely(x) if (__builtin_expect((x) != 0, 1)) +# define unlikely(x) if (__builtin_expect((x) != 0, 0)) #endif -#define MEMCACHED_BLOCK_SIZE 1024 +#define MEMCACHED_BLOCK_SIZE 1024 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350 -#define SMALL_STRING_LEN 1024 -#define HUGE_STRING_LEN 8196 +#define SMALL_STRING_LEN 1024 +#define HUGE_STRING_LEN 8196 #ifdef __cplusplus extern "C" { @@ -197,25 +170,24 @@ extern "C" { memcached_return_t run_distribution(memcached_st *ptr); #ifdef __cplusplus -static inline void memcached_server_response_increment(memcached_instance_st* instance) -{ +static inline void memcached_server_response_increment(memcached_instance_st *instance) { instance->events(POLLIN); instance->cursor_active_++; } #endif #define memcached_server_response_decrement(A) (A)->cursor_active_-- -#define memcached_server_response_reset(A) (A)->cursor_active_=0 +#define memcached_server_response_reset(A) (A)->cursor_active_ = 0 #define memcached_instance_response_increment(A) (A)->cursor_active_++ #define memcached_instance_response_decrement(A) (A)->cursor_active_-- -#define memcached_instance_response_reset(A) (A)->cursor_active_=0 +#define memcached_instance_response_reset(A) (A)->cursor_active_ = 0 #ifdef __cplusplus } #endif #ifdef __cplusplus -bool memcached_purge(memcached_instance_st*); -memcached_instance_st* memcached_instance_by_position(const memcached_st *ptr, uint32_t server_key); +bool memcached_purge(memcached_instance_st *); +memcached_instance_st *memcached_instance_by_position(const memcached_st *ptr, uint32_t server_key); #endif diff --git a/src/libmemcached/connect.cc b/src/libmemcached/connect.cc index 7579de5c..04b86f62 100644 --- a/src/libmemcached/connect.cc +++ b/src/libmemcached/connect.cc @@ -1,40 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -45,61 +22,60 @@ #endif #ifndef SOCK_NONBLOCK -# define SOCK_NONBLOCK 0 +# define SOCK_NONBLOCK 0 #endif #ifndef FD_CLOEXEC -# define FD_CLOEXEC 0 +# define FD_CLOEXEC 0 #endif #ifndef SO_NOSIGPIPE -# define SO_NOSIGPIPE 0 +# define SO_NOSIGPIPE 0 #endif #ifndef TCP_NODELAY -# define TCP_NODELAY 0 +# define TCP_NODELAY 0 #endif #ifndef TCP_KEEPIDLE -# define TCP_KEEPIDLE 0 +# define TCP_KEEPIDLE 0 #endif -static memcached_return_t connect_poll(memcached_instance_st* server, const int connection_error) -{ +static memcached_return_t connect_poll(memcached_instance_st *server, const int connection_error) { struct pollfd fds[1]; - fds[0].fd= server->fd; - fds[0].events= server->events(); - fds[0].revents= 0; + fds[0].fd = server->fd; + fds[0].events = server->events(); + fds[0].revents = 0; - size_t loop_max= 5; + size_t loop_max = 5; - if (server->root->poll_timeout == 0) - { - return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, - memcached_literal_param("The time to wait for a connection to be established was set to zero which produces a timeout to every call to poll().")); + if (server->root->poll_timeout == 0) { + return memcached_set_error( + *server, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_literal_param("The time to wait for a connection to be established was set to " + "zero which produces a timeout to every call to poll().")); } while (--loop_max) // Should only loop on cases of ERESTART or EINTR { int number_of; - if ((number_of= poll(fds, 1, server->root->connect_timeout)) == -1) - { - int local_errno= get_socket_errno(); // We cache in case closesocket() modifies errno - switch (local_errno) - { + if ((number_of = poll(fds, 1, server->root->connect_timeout)) == -1) { + int local_errno = get_socket_errno(); // We cache in case closesocket() modifies errno + switch (local_errno) { #ifdef __linux__ case ERESTART: #endif - case EINTR: - continue; + case EINTR: continue; case EFAULT: case ENOMEM: return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); case EINVAL: - return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid")); + return memcached_set_error( + *server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param( + "RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid")); default: // This should not happen break; @@ -107,43 +83,45 @@ static memcached_return_t connect_poll(memcached_instance_st* server, const int assert_msg(server->fd != INVALID_SOCKET, "poll() was passed an invalid file descriptor"); server->reset_socket(); - server->state= MEMCACHED_SERVER_STATE_NEW; + server->state = MEMCACHED_SERVER_STATE_NEW; return memcached_set_errno(*server, local_errno, MEMCACHED_AT); } - if (number_of == 0) - { - if (connection_error == EINPROGRESS) - { + if (number_of == 0) { + if (connection_error == EINPROGRESS) { int err; - socklen_t len= sizeof(err); - if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == -1) - { - return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getsockopt() error'ed while looking for error connect_poll(EINPROGRESS)")); + socklen_t len = sizeof(err); + if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) { + return memcached_set_errno( + *server, errno, MEMCACHED_AT, + memcached_literal_param( + "getsockopt() error'ed while looking for error connect_poll(EINPROGRESS)")); } // If Zero, my hero, we just fail to a generic MEMCACHED_TIMEOUT error - if (err != 0) - { - return memcached_set_errno(*server, err, MEMCACHED_AT, memcached_literal_param("getsockopt() found the error from poll() after connect() returned EINPROGRESS.")); + if (err != 0) { + return memcached_set_errno( + *server, err, MEMCACHED_AT, + memcached_literal_param("getsockopt() found the error from poll() after connect() " + "returned EINPROGRESS.")); } } - return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("(number_of == 0)")); + return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_literal_param("(number_of == 0)")); } - assert (number_of == 1); + assert(number_of == 1); - if (fds[0].revents & POLLERR or - fds[0].revents & POLLHUP or - fds[0].revents & POLLNVAL) - { + if (fds[0].revents & POLLERR or fds[0].revents & POLLHUP or fds[0].revents & POLLNVAL) { int err; - socklen_t len= sizeof (err); - if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == -1) - { - return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getsockopt() errored while looking up error state from poll()")); + socklen_t len = sizeof(err); + if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) { + return memcached_set_errno( + *server, errno, MEMCACHED_AT, + memcached_literal_param( + "getsockopt() errored while looking up error state from poll()")); } // We check the value to see what happened with the socket. @@ -151,46 +129,47 @@ static memcached_return_t connect_poll(memcached_instance_st* server, const int { return MEMCACHED_SUCCESS; } - errno= err; + errno = err; - return memcached_set_errno(*server, err, MEMCACHED_AT, memcached_literal_param("getsockopt() found the error from poll() during connect.")); + return memcached_set_errno( + *server, err, MEMCACHED_AT, + memcached_literal_param("getsockopt() found the error from poll() during connect.")); } assert(fds[0].revents & POLLOUT); - if (fds[0].revents & POLLOUT and connection_error == EINPROGRESS) - { + if (fds[0].revents & POLLOUT and connection_error == EINPROGRESS) { int err; - socklen_t len= sizeof(err); - if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == -1) - { + socklen_t len = sizeof(err); + if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) { return memcached_set_errno(*server, errno, MEMCACHED_AT); } - if (err == 0) - { + if (err == 0) { return MEMCACHED_SUCCESS; } - return memcached_set_errno(*server, err, MEMCACHED_AT, memcached_literal_param("getsockopt() found the error from poll() after connect() returned EINPROGRESS.")); + return memcached_set_errno( + *server, err, MEMCACHED_AT, + memcached_literal_param( + "getsockopt() found the error from poll() after connect() returned EINPROGRESS.")); } break; // We only have the loop setup for errno types that require restart } // This should only be possible from ERESTART or EINTR; - return memcached_set_errno(*server, connection_error, MEMCACHED_AT, memcached_literal_param("connect_poll() was exhausted")); + return memcached_set_errno(*server, connection_error, MEMCACHED_AT, + memcached_literal_param("connect_poll() was exhausted")); } -static memcached_return_t set_hostinfo(memcached_instance_st* server) -{ +static memcached_return_t set_hostinfo(memcached_instance_st *server) { assert(server->type != MEMCACHED_CONNECTION_UNIX_SOCKET); server->clear_addrinfo(); - char str_port[MEMCACHED_NI_MAXSERV]= { 0 }; - errno= 0; - int length= snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port())); - if (length >= MEMCACHED_NI_MAXSERV or length <= 0 or errno != 0) - { + char str_port[MEMCACHED_NI_MAXSERV] = {0}; + errno = 0; + int length = snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port())); + if (length >= MEMCACHED_NI_MAXSERV or length <= 0 or errno != 0) { return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(NI_MAXSERV)")); } @@ -198,87 +177,78 @@ static memcached_return_t set_hostinfo(memcached_instance_st* server) struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family= AF_UNSPEC; - if (memcached_is_udp(server->root)) - { - hints.ai_protocol= IPPROTO_UDP; - hints.ai_socktype= SOCK_DGRAM; - } - else - { - hints.ai_socktype= SOCK_STREAM; - hints.ai_protocol= IPPROTO_TCP; + hints.ai_family = AF_UNSPEC; + if (memcached_is_udp(server->root)) { + hints.ai_protocol = IPPROTO_UDP; + hints.ai_socktype = SOCK_DGRAM; + } else { + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; } assert(server->address_info == NULL); assert(server->address_info_next == NULL); int errcode; assert(server->hostname()); - switch(errcode= getaddrinfo(server->hostname(), str_port, &hints, &server->address_info)) - { + switch (errcode = getaddrinfo(server->hostname(), str_port, &hints, &server->address_info)) { case 0: - server->address_info_next= server->address_info; - server->state= MEMCACHED_SERVER_STATE_ADDRINFO; + server->address_info_next = server->address_info; + server->state = MEMCACHED_SERVER_STATE_ADDRINFO; break; case EAI_AGAIN: - return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); + return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_string_make_from_cstr(gai_strerror(errcode))); case EAI_SYSTEM: server->clear_addrinfo(); - return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_SYSTEM)")); + return memcached_set_errno(*server, errno, MEMCACHED_AT, + memcached_literal_param("getaddrinfo(EAI_SYSTEM)")); case EAI_BADFLAGS: server->clear_addrinfo(); - return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_BADFLAGS)")); + return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("getaddrinfo(EAI_BADFLAGS)")); case EAI_MEMORY: server->clear_addrinfo(); - return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_MEMORY)")); + return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("getaddrinfo(EAI_MEMORY)")); - default: - { - server->clear_addrinfo(); - return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); - } + default: { + server->clear_addrinfo(); + return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, + memcached_string_make_from_cstr(gai_strerror(errcode))); + } } return MEMCACHED_SUCCESS; } -static inline void set_socket_nonblocking(memcached_instance_st* server) -{ +static inline void set_socket_nonblocking(memcached_instance_st *server) { #if defined(_WIN32) - u_long arg= 1; - if (ioctlsocket(server->fd, FIONBIO, &arg) == SOCKET_ERROR) - { + u_long arg = 1; + if (ioctlsocket(server->fd, FIONBIO, &arg) == SOCKET_ERROR) { memcached_set_errno(*server, get_socket_errno(), NULL); } #else int flags; - if (SOCK_NONBLOCK == 0) - { - do - { - flags= fcntl(server->fd, F_GETFL, 0); + if (SOCK_NONBLOCK == 0) { + do { + flags = fcntl(server->fd, F_GETFL, 0); } while (flags == -1 && (errno == EINTR || errno == EAGAIN)); - if (flags == -1) - { + if (flags == -1) { memcached_set_errno(*server, errno, NULL); - } - else if ((flags & O_NONBLOCK) == 0) - { + } else if ((flags & O_NONBLOCK) == 0) { int rval; - do - { - rval= fcntl(server->fd, F_SETFL, flags | O_NONBLOCK); + do { + rval = fcntl(server->fd, F_SETFL, flags | O_NONBLOCK); } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); - if (rval == -1) - { + if (rval == -1) { memcached_set_errno(*server, errno, NULL); } } @@ -286,28 +256,22 @@ static inline void set_socket_nonblocking(memcached_instance_st* server) #endif } -static bool set_socket_options(memcached_instance_st* server) -{ +static bool set_socket_options(memcached_instance_st *server) { assert_msg(server->fd != INVALID_SOCKET, "invalid socket was passed to set_socket_options()"); #ifdef HAVE_FCNTL // If SOCK_CLOEXEC exists then we don't need to call the following - if (SOCK_CLOEXEC == 0) - { - if (FD_CLOEXEC != 0) - { + if (SOCK_CLOEXEC == 0) { + if (FD_CLOEXEC != 0) { int flags; - do - { - flags= fcntl(server->fd, F_GETFD, 0); + do { + flags = fcntl(server->fd, F_GETFD, 0); } while (flags == -1 and (errno == EINTR or errno == EAGAIN)); - if (flags != -1) - { + if (flags != -1) { int rval; - do - { - rval= fcntl (server->fd, F_SETFD, flags | FD_CLOEXEC); + do { + rval = fcntl(server->fd, F_SETFD, flags | FD_CLOEXEC); } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); // we currently ignore the case where rval is -1 } @@ -315,122 +279,108 @@ static bool set_socket_options(memcached_instance_st* server) } #endif - if (memcached_is_udp(server->root)) - { + if (memcached_is_udp(server->root)) { return true; } #ifdef HAVE_SO_SNDTIMEO - if (server->root->snd_timeout > 0) - { + if (server->root->snd_timeout > 0) { struct timeval waittime; - waittime.tv_sec= server->root->snd_timeout / 1000000; - waittime.tv_usec= server->root->snd_timeout % 1000000; + waittime.tv_sec = server->root->snd_timeout / 1000000; + waittime.tv_usec = server->root->snd_timeout % 1000000; - int error= setsockopt(server->fd, SOL_SOCKET, SO_SNDTIMEO, - (char*)&waittime, (socklen_t)sizeof(struct timeval)); - (void)error; + int error = setsockopt(server->fd, SOL_SOCKET, SO_SNDTIMEO, (char *) &waittime, + (socklen_t) sizeof(struct timeval)); + (void) error; assert(error == 0); } #endif #ifdef HAVE_SO_RCVTIMEO - if (server->root->rcv_timeout > 0) - { + if (server->root->rcv_timeout > 0) { struct timeval waittime; - waittime.tv_sec= server->root->rcv_timeout / 1000000; - waittime.tv_usec= server->root->rcv_timeout % 1000000; + waittime.tv_sec = server->root->rcv_timeout / 1000000; + waittime.tv_usec = server->root->rcv_timeout % 1000000; - int error= setsockopt(server->fd, SOL_SOCKET, SO_RCVTIMEO, - (char*)&waittime, (socklen_t)sizeof(struct timeval)); - (void)(error); + int error = setsockopt(server->fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &waittime, + (socklen_t) sizeof(struct timeval)); + (void) (error); assert(error == 0); } #endif - #if defined(_WIN32) #else -# if defined(SO_NOSIGPIPE) - if (SO_NOSIGPIPE) - { - int set= 1; - int error= setsockopt(server->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int)); +# if defined(SO_NOSIGPIPE) + if (SO_NOSIGPIPE) { + int set = 1; + int error = setsockopt(server->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *) &set, sizeof(int)); assert(error == 0); // This is not considered a fatal error - if (error == -1) - { -#if 0 + if (error == -1) { +# if 0 perror("setsockopt(SO_NOSIGPIPE)"); -#endif +# endif } } -# endif // SO_NOSIGPIPE -#endif // _WIN32 +# endif // SO_NOSIGPIPE +#endif // _WIN32 - if (server->root->flags.no_block) - { + if (server->root->flags.no_block) { struct linger linger; - linger.l_onoff= 1; - linger.l_linger= 0; /* By default on close() just drop the socket */ - int error= setsockopt(server->fd, SOL_SOCKET, SO_LINGER, - (char*)&linger, (socklen_t)sizeof(struct linger)); - (void)(error); + linger.l_onoff = 1; + linger.l_linger = 0; /* By default on close() just drop the socket */ + int error = setsockopt(server->fd, SOL_SOCKET, SO_LINGER, (char *) &linger, + (socklen_t) sizeof(struct linger)); + (void) (error); assert(error == 0); } - if (TCP_NODELAY) - { - if (server->root->flags.tcp_nodelay) - { - int flag= 1; + if (TCP_NODELAY) { + if (server->root->flags.tcp_nodelay) { + int flag = 1; - int error= setsockopt(server->fd, IPPROTO_TCP, TCP_NODELAY, - (char*)&flag, (socklen_t)sizeof(int)); - (void)(error); + int error = + setsockopt(server->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, (socklen_t) sizeof(int)); + (void) (error); assert(error == 0); } } - if (server->root->flags.tcp_keepalive) - { - int flag= 1; + if (server->root->flags.tcp_keepalive) { + int flag = 1; - int error= setsockopt(server->fd, SOL_SOCKET, SO_KEEPALIVE, - (char*)&flag, (socklen_t)sizeof(int)); - (void)(error); + int error = + setsockopt(server->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, (socklen_t) sizeof(int)); + (void) (error); assert(error == 0); } - if (TCP_KEEPIDLE) - { - if (server->root->tcp_keepidle > 0) - { - int error= setsockopt(server->fd, IPPROTO_TCP, TCP_KEEPIDLE, - (char*)&server->root->tcp_keepidle, (socklen_t)sizeof(int)); - (void)(error); + if (TCP_KEEPIDLE) { + if (server->root->tcp_keepidle > 0) { + int error = setsockopt(server->fd, IPPROTO_TCP, TCP_KEEPIDLE, + (char *) &server->root->tcp_keepidle, (socklen_t) sizeof(int)); + (void) (error); assert(error == 0); } } - if (server->root->send_size > 0) - { - int error= setsockopt(server->fd, SOL_SOCKET, SO_SNDBUF, - (char*)&server->root->send_size, (socklen_t)sizeof(int)); - (void)(error); + if (server->root->send_size > 0) { + int error = setsockopt(server->fd, SOL_SOCKET, SO_SNDBUF, (char *) &server->root->send_size, + (socklen_t) sizeof(int)); + (void) (error); assert(error == 0); } - if (server->root->recv_size > 0) - { - int error= setsockopt(server->fd, SOL_SOCKET, SO_RCVBUF, - (char*)&server->root->recv_size, (socklen_t)sizeof(int)); - (void)(error); + if (server->root->recv_size > 0) { + int error = setsockopt(server->fd, SOL_SOCKET, SO_RCVBUF, (char *) &server->root->recv_size, + (socklen_t) sizeof(int)); + (void) (error); assert(error == 0); } @@ -440,57 +390,48 @@ static bool set_socket_options(memcached_instance_st* server) return true; } -static memcached_return_t unix_socket_connect(memcached_instance_st* server) -{ +static memcached_return_t unix_socket_connect(memcached_instance_st *server) { #ifndef _WIN32 WATCHPOINT_ASSERT(server->fd == INVALID_SOCKET); do { - int type= SOCK_STREAM; - if (SOCK_CLOEXEC != 0) - { - type|= SOCK_CLOEXEC; + int type = SOCK_STREAM; + if (SOCK_CLOEXEC != 0) { + type |= SOCK_CLOEXEC; } - if (SOCK_NONBLOCK != 0) - { - type|= SOCK_NONBLOCK; + if (SOCK_NONBLOCK != 0) { + type |= SOCK_NONBLOCK; } - if ((server->fd= socket(AF_UNIX, type, 0)) == -1) - { + if ((server->fd = socket(AF_UNIX, type, 0)) == -1) { return memcached_set_errno(*server, errno, NULL); } struct sockaddr_un servAddr; - memset(&servAddr, 0, sizeof (struct sockaddr_un)); - servAddr.sun_family= AF_UNIX; + memset(&servAddr, 0, sizeof(struct sockaddr_un)); + servAddr.sun_family = AF_UNIX; if (strlen(server->hostname()) >= sizeof(servAddr.sun_path)) { - return memcached_set_error(*server, MEMCACHED_FAIL_UNIX_SOCKET, MEMCACHED_AT); + return memcached_set_error(*server, MEMCACHED_FAIL_UNIX_SOCKET, MEMCACHED_AT); } - strncpy(servAddr.sun_path, server->hostname(), sizeof(servAddr.sun_path)-1); /* Copy filename */ + strncpy(servAddr.sun_path, server->hostname(), + sizeof(servAddr.sun_path) - 1); /* Copy filename */ - if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) - { - switch (errno) - { + if (connect(server->fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) == -1) { + switch (errno) { case EINPROGRESS: case EALREADY: - case EAGAIN: - server->events(POLLOUT); - break; + case EAGAIN: server->events(POLLOUT); break; - case EINTR: - server->reset_socket(); - continue; + case EINTR: server->reset_socket(); continue; case EISCONN: /* We were spinning waiting on connect */ - { - assert(0); // Programmer error - server->reset_socket(); - continue; - } + { + assert(0); // Programmer error + server->reset_socket(); + continue; + } default: WATCHPOINT_ERRNO(errno); @@ -499,35 +440,33 @@ static memcached_return_t unix_socket_connect(memcached_instance_st* server) } } } while (0); - server->state= MEMCACHED_SERVER_STATE_CONNECTED; + server->state = MEMCACHED_SERVER_STATE_CONNECTED; WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); return MEMCACHED_SUCCESS; #else - (void)server; + (void) server; return MEMCACHED_NOT_SUPPORTED; #endif } -static memcached_return_t network_connect(memcached_instance_st* server) -{ - bool timeout_error_occured= false; +static memcached_return_t network_connect(memcached_instance_st *server) { + bool timeout_error_occured = false; WATCHPOINT_ASSERT(server->fd == INVALID_SOCKET); WATCHPOINT_ASSERT(server->cursor_active_ == 0); /* - We want to check both of these because if address_info_next has been fully tried, we want to do a new lookup to make sure we have picked up on any new DNS information. + We want to check both of these because if address_info_next has been fully tried, we want to do + a new lookup to make sure we have picked up on any new DNS information. */ - if (server->address_info == NULL or server->address_info_next == NULL) - { + if (server->address_info == NULL or server->address_info_next == NULL) { WATCHPOINT_ASSERT(server->state == MEMCACHED_SERVER_STATE_NEW); - server->address_info_next= NULL; - memcached_return_t rc= set_hostinfo(server); + server->address_info_next = NULL; + memcached_return_t rc = set_hostinfo(server); - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { return rc; } } @@ -536,74 +475,64 @@ static memcached_return_t network_connect(memcached_instance_st* server) assert(server->address_info); /* Create the socket */ - while (server->address_info_next and server->fd == INVALID_SOCKET) - { - int type= server->address_info_next->ai_socktype; - if (SOCK_CLOEXEC != 0) - { - type|= SOCK_CLOEXEC; + while (server->address_info_next and server->fd == INVALID_SOCKET) { + int type = server->address_info_next->ai_socktype; + if (SOCK_CLOEXEC != 0) { + type |= SOCK_CLOEXEC; } - if (SOCK_NONBLOCK != 0) - { - type|= SOCK_NONBLOCK; + if (SOCK_NONBLOCK != 0) { + type |= SOCK_NONBLOCK; } - server->fd= socket(server->address_info_next->ai_family, - type, - server->address_info_next->ai_protocol); + server->fd = + socket(server->address_info_next->ai_family, type, server->address_info_next->ai_protocol); - if (int(server->fd) == SOCKET_ERROR) - { + if (int(server->fd) == SOCKET_ERROR) { return memcached_set_errno(*server, get_socket_errno(), NULL); } - if (set_socket_options(server) == false) - { + if (set_socket_options(server) == false) { server->reset_socket(); return MEMCACHED_CONNECTION_FAILURE; } /* connect to server */ - if ((connect(server->fd, server->address_info_next->ai_addr, server->address_info_next->ai_addrlen) != SOCKET_ERROR)) + if ((connect(server->fd, server->address_info_next->ai_addr, + server->address_info_next->ai_addrlen) + != SOCKET_ERROR)) { - server->state= MEMCACHED_SERVER_STATE_CONNECTED; + server->state = MEMCACHED_SERVER_STATE_CONNECTED; return MEMCACHED_SUCCESS; } /* An error occurred */ - int local_error= get_socket_errno(); - switch (local_error) - { - case ETIMEDOUT: - timeout_error_occured= true; - break; + int local_error = get_socket_errno(); + switch (local_error) { + case ETIMEDOUT: timeout_error_occured = true; break; #if EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif case EINPROGRESS: // nonblocking mode - first return - case EALREADY: // nonblocking mode - subsequent returns - { - server->events(POLLOUT); - server->state= MEMCACHED_SERVER_STATE_IN_PROGRESS; - memcached_return_t rc= connect_poll(server, local_error); - - if (memcached_success(rc)) - { - server->state= MEMCACHED_SERVER_STATE_CONNECTED; - return MEMCACHED_SUCCESS; - } + case EALREADY: // nonblocking mode - subsequent returns + { + server->events(POLLOUT); + server->state = MEMCACHED_SERVER_STATE_IN_PROGRESS; + memcached_return_t rc = connect_poll(server, local_error); - // A timeout here is treated as an error, we will not retry - if (rc == MEMCACHED_TIMEOUT) - { - timeout_error_occured= true; - } + if (memcached_success(rc)) { + server->state = MEMCACHED_SERVER_STATE_CONNECTED; + return MEMCACHED_SUCCESS; } - break; - case EISCONN: // we are connected :-) + // A timeout here is treated as an error, we will not retry + if (rc == MEMCACHED_TIMEOUT) { + timeout_error_occured = true; + } + } break; + + case EISCONN: // we are connected :-) WATCHPOINT_ASSERT(0); // This is a programmer's error break; @@ -615,50 +544,46 @@ static memcached_return_t network_connect(memcached_instance_st* server) case ECONNREFUSED: // Probably not running service - default: - memcached_set_errno(*server, local_error, MEMCACHED_AT); - break; + default: memcached_set_errno(*server, local_error, MEMCACHED_AT); break; } WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); server->reset_socket(); - server->address_info_next= server->address_info_next->ai_next; + server->address_info_next = server->address_info_next->ai_next; } WATCHPOINT_ASSERT(server->fd == INVALID_SOCKET); - if (timeout_error_occured) - { + if (timeout_error_occured) { server->reset_socket(); } WATCHPOINT_STRING("Never got a good file descriptor"); - if (memcached_has_current_error(*server)) - { + if (memcached_has_current_error(*server)) { return memcached_instance_error_return(server); } - if (timeout_error_occured and server->state < MEMCACHED_SERVER_STATE_IN_PROGRESS) - { - return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, - memcached_literal_param("if (timeout_error_occured and server->state < MEMCACHED_SERVER_STATE_IN_PROGRESS)")); + if (timeout_error_occured and server->state < MEMCACHED_SERVER_STATE_IN_PROGRESS) { + return memcached_set_error( + *server, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_literal_param( + "if (timeout_error_occured and server->state < MEMCACHED_SERVER_STATE_IN_PROGRESS)")); } - return memcached_set_error(*server, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT); /* The last error should be from connect() */ + return memcached_set_error(*server, MEMCACHED_CONNECTION_FAILURE, + MEMCACHED_AT); /* The last error should be from connect() */ } - /* backoff_handling() - Based on time/failure count fail the connect without trying. This prevents waiting in a state where - we get caught spending cycles just waiting. + Based on time/failure count fail the connect without trying. This prevents waiting in a state + where we get caught spending cycles just waiting. */ -static memcached_return_t backoff_handling(memcached_instance_st* server, bool& in_timeout) -{ +static memcached_return_t backoff_handling(memcached_instance_st *server, bool &in_timeout) { struct timeval curr_time; - bool _gettime_success= (gettimeofday(&curr_time, NULL) == 0); + bool _gettime_success = (gettimeofday(&curr_time, NULL) == 0); /* If we hit server_failure_limit then something is completely wrong about the server. @@ -666,106 +591,94 @@ static memcached_return_t backoff_handling(memcached_instance_st* server, bool& 1) If autoeject is enabled we do that. 2) If not? We go into timeout again, there is much else to do :( */ - if (server->server_failure_counter >= server->root->server_failure_limit) - { + if (server->server_failure_counter >= server->root->server_failure_limit) { /* We just auto_eject if we hit this point */ - if (_is_auto_eject_host(server->root)) - { + if (_is_auto_eject_host(server->root)) { set_last_disconnected_host(server); // Retry dead servers if requested - if (_gettime_success and server->root->dead_timeout > 0) - { - server->next_retry= curr_time.tv_sec +server->root->dead_timeout; + if (_gettime_success and server->root->dead_timeout > 0) { + server->next_retry = curr_time.tv_sec + server->root->dead_timeout; // We only retry dead servers once before assuming failure again - server->server_failure_counter= server->root->server_failure_limit -1; + server->server_failure_counter = server->root->server_failure_limit - 1; } memcached_return_t rc; - if (memcached_failed(rc= run_distribution((memcached_st *)server->root))) - { - return memcached_set_error(*server, rc, MEMCACHED_AT, memcached_literal_param("Backoff handling failed during run_distribution")); + if (memcached_failed(rc = run_distribution((memcached_st *) server->root))) { + return memcached_set_error( + *server, rc, MEMCACHED_AT, + memcached_literal_param("Backoff handling failed during run_distribution")); } return memcached_set_error(*server, MEMCACHED_SERVER_MARKED_DEAD, MEMCACHED_AT); } - server->state= MEMCACHED_SERVER_STATE_IN_TIMEOUT; + server->state = MEMCACHED_SERVER_STATE_IN_TIMEOUT; // Sanity check/setting - if (server->next_retry == 0) - { - server->next_retry= 1; + if (server->next_retry == 0) { + server->next_retry = 1; } } - if (server->state == MEMCACHED_SERVER_STATE_IN_TIMEOUT) - { + if (server->state == MEMCACHED_SERVER_STATE_IN_TIMEOUT) { /* If next_retry is less then our current time, then we reset and try everything again. */ - if (_gettime_success and server->next_retry < curr_time.tv_sec) - { - server->state= MEMCACHED_SERVER_STATE_NEW; - server->server_timeout_counter= 0; - } - else - { + if (_gettime_success and server->next_retry < curr_time.tv_sec) { + server->state = MEMCACHED_SERVER_STATE_NEW; + server->server_timeout_counter = 0; + } else { return memcached_set_error(*server, MEMCACHED_SERVER_TEMPORARILY_DISABLED, MEMCACHED_AT); } - in_timeout= true; + in_timeout = true; } return MEMCACHED_SUCCESS; } -static memcached_return_t _memcached_connect(memcached_instance_st* server, const bool set_last_disconnected) -{ +static memcached_return_t _memcached_connect(memcached_instance_st *server, + const bool set_last_disconnected) { assert(server); - if (server->fd != INVALID_SOCKET) - { + if (server->fd != INVALID_SOCKET) { return MEMCACHED_SUCCESS; } LIBMEMCACHED_MEMCACHED_CONNECT_START(); - bool in_timeout= false; + bool in_timeout = false; memcached_return_t rc; - if (memcached_failed(rc= backoff_handling(server, in_timeout))) - { + if (memcached_failed(rc = backoff_handling(server, in_timeout))) { set_last_disconnected_host(server); return rc; } - if (LIBMEMCACHED_WITH_SASL_SUPPORT and server->root->sasl.callbacks and memcached_is_udp(server->root)) + if (LIBMEMCACHED_WITH_SASL_SUPPORT and server->root->sasl.callbacks + and memcached_is_udp(server->root)) { - return memcached_set_error(*server, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT, memcached_literal_param("SASL is not supported for UDP connections")); + return memcached_set_error( + *server, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT, + memcached_literal_param("SASL is not supported for UDP connections")); } - if (server->hostname()[0] == '/') - { - server->type= MEMCACHED_CONNECTION_UNIX_SOCKET; + if (server->hostname()[0] == '/') { + server->type = MEMCACHED_CONNECTION_UNIX_SOCKET; } /* We need to clean up the multi startup piece */ - switch (server->type) - { + switch (server->type) { case MEMCACHED_CONNECTION_UDP: - case MEMCACHED_CONNECTION_TCP: - rc= network_connect(server); + case MEMCACHED_CONNECTION_TCP: rc = network_connect(server); #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) - if (LIBMEMCACHED_WITH_SASL_SUPPORT) - { - if (server->fd != INVALID_SOCKET and server->root->sasl.callbacks) - { - rc= memcached_sasl_authenticate_connection(server); - if (memcached_failed(rc) and server->fd != INVALID_SOCKET) - { + if (LIBMEMCACHED_WITH_SASL_SUPPORT) { + if (server->fd != INVALID_SOCKET and server->root->sasl.callbacks) { + rc = memcached_sasl_authenticate_connection(server); + if (memcached_failed(rc) and server->fd != INVALID_SOCKET) { WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); server->reset_socket(); } @@ -774,45 +687,37 @@ static memcached_return_t _memcached_connect(memcached_instance_st* server, cons #endif break; - case MEMCACHED_CONNECTION_UNIX_SOCKET: - rc= unix_socket_connect(server); - break; + case MEMCACHED_CONNECTION_UNIX_SOCKET: rc = unix_socket_connect(server); break; } - if (memcached_success(rc)) - { + if (memcached_success(rc)) { server->mark_server_as_clean(); memcached_version_instance(server); return rc; - } - else if (set_last_disconnected) - { + } else if (set_last_disconnected) { set_last_disconnected_host(server); - if (memcached_has_current_error(*server)) - { + if (memcached_has_current_error(*server)) { memcached_mark_server_for_timeout(server); assert(memcached_failed(memcached_instance_error_return(server))); - } - else - { + } else { memcached_set_error(*server, rc, MEMCACHED_AT); memcached_mark_server_for_timeout(server); } LIBMEMCACHED_MEMCACHED_CONNECT_END(); - if (in_timeout) - { + if (in_timeout) { char buffer[1024]; - int snprintf_length= snprintf(buffer, sizeof(buffer), "%s:%d", server->hostname(), int(server->port())); - return memcached_set_error(*server, MEMCACHED_SERVER_TEMPORARILY_DISABLED, MEMCACHED_AT, buffer, snprintf_length); + int snprintf_length = + snprintf(buffer, sizeof(buffer), "%s:%d", server->hostname(), int(server->port())); + return memcached_set_error(*server, MEMCACHED_SERVER_TEMPORARILY_DISABLED, MEMCACHED_AT, + buffer, snprintf_length); } } return rc; } -memcached_return_t memcached_connect(memcached_instance_st* server) -{ +memcached_return_t memcached_connect(memcached_instance_st *server) { return _memcached_connect(server, true); } diff --git a/src/libmemcached/connect.hpp b/src/libmemcached/connect.hpp index d1dafe5e..bfdb3bd2 100644 --- a/src/libmemcached/connect.hpp +++ b/src/libmemcached/connect.hpp @@ -1,40 +1,18 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -memcached_return_t memcached_connect(memcached_instance_st*); +memcached_return_t memcached_connect(memcached_instance_st *); diff --git a/src/libmemcached/continuum.hpp b/src/libmemcached/continuum.hpp index e39fcdc3..a960e5bd 100644 --- a/src/libmemcached/continuum.hpp +++ b/src/libmemcached/continuum.hpp @@ -1,46 +1,22 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once /* string value */ -struct memcached_continuum_item_st -{ +struct memcached_continuum_item_st { uint32_t index; uint32_t value; }; diff --git a/src/libmemcached/csl/common.h b/src/libmemcached/csl/common.h index 067c2428..818e25ef 100644 --- a/src/libmemcached/csl/common.h +++ b/src/libmemcached/csl/common.h @@ -1,59 +1,36 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Configure Scripting Language - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #define YYDEBUG 1 #ifndef YY_EXTRA_TYPE -# define YY_EXTRA_TYPE Context* +# define YY_EXTRA_TYPE Context * #endif #ifndef YY_TYPEDEF_YY_SCANNER_T -# define YY_TYPEDEF_YY_SCANNER_T -typedef void* yyscan_t; +# define YY_TYPEDEF_YY_SCANNER_T +typedef void *yyscan_t; #endif #ifndef YYSTYPE -# define YYSTYPE CONFIG_STYPE +# define YYSTYPE CONFIG_STYPE #endif #define config_tokentype int #include "libmemcached/common.h" #include "libmemcached/csl/server.h" - diff --git a/src/libmemcached/csl/context.cc b/src/libmemcached/csl/context.cc index 713912c6..ca32d75f 100644 --- a/src/libmemcached/csl/context.cc +++ b/src/libmemcached/csl/context.cc @@ -1,56 +1,32 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Configure Scripting Language - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/csl/common.h" #include "libmemcached/csl/context.h" -void Context::abort(const char *error_arg, config_tokentype last_token, const char *last_token_str) -{ - rc= MEMCACHED_PARSE_ERROR; - (void)last_token; +void Context::abort(const char *error_arg, config_tokentype last_token, + const char *last_token_str) { + rc = MEMCACHED_PARSE_ERROR; + (void) last_token; - if (error_arg) - { + if (error_arg) { memcached_set_parser_error(*memc, MEMCACHED_AT, "%s", error_arg); return; } - if (last_token_str) - { + if (last_token_str) { memcached_set_parser_error(*memc, MEMCACHED_AT, "%s", last_token_str); return; } @@ -58,61 +34,56 @@ void Context::abort(const char *error_arg, config_tokentype last_token, const ch memcached_set_parser_error(*memc, MEMCACHED_AT, "unknown parsing error"); } -void Context::error(const char *error_arg, config_tokentype last_token, const char *last_token_str) -{ - rc= MEMCACHED_PARSE_ERROR; - if (not error_arg) - { - memcached_set_parser_error(*memc, MEMCACHED_AT, "Unknown error occured during parsing (%s)", last_token_str ? last_token_str : " "); +void Context::error(const char *error_arg, config_tokentype last_token, + const char *last_token_str) { + rc = MEMCACHED_PARSE_ERROR; + if (not error_arg) { + memcached_set_parser_error(*memc, MEMCACHED_AT, "Unknown error occured during parsing (%s)", + last_token_str ? last_token_str : " "); return; } - if (strcmp(error_arg, "memory exhausted") == 0) - { - (void)memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(error_arg)); + if (strcmp(error_arg, "memory exhausted") == 0) { + (void) memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_string_make_from_cstr(error_arg)); return; } // We now test if it is something other then a syntax error, if it we - // return a generic message - if (strcmp(error_arg, "syntax error") != 0) - { - memcached_set_parser_error(*memc, MEMCACHED_AT, "Error occured during parsing (%s): last_token=%s(%d)", error_arg, last_token_str, last_token); + // return a generic message + if (strcmp(error_arg, "syntax error") != 0) { + memcached_set_parser_error(*memc, MEMCACHED_AT, + "Error occured during parsing (%s): last_token=%s(%d)", error_arg, + last_token_str, last_token); return; } - if (last_token == UNKNOWN_OPTION and begin) - { + if (last_token == UNKNOWN_OPTION and begin) { memcached_set_parser_error(*memc, MEMCACHED_AT, "Unknown option: %s", begin); - } - else if (last_token == UNKNOWN) - { - memcached_set_parser_error(*memc, MEMCACHED_AT, "Error occured durring parsing, an unknown token was found."); - } - else - { - memcached_set_parser_error(*memc, MEMCACHED_AT, "Error occured while parsing (%s)", last_token_str ? last_token_str : " "); + } else if (last_token == UNKNOWN) { + memcached_set_parser_error(*memc, MEMCACHED_AT, + "Error occured durring parsing, an unknown token was found."); + } else { + memcached_set_parser_error(*memc, MEMCACHED_AT, "Error occured while parsing (%s)", + last_token_str ? last_token_str : " "); } } -void Context::hostname(const char *str, size_t size, server_t& server_) -{ - size_t copy_length= size_t(NI_MAXHOST) > size ? size : size_t(NI_MAXHOST); +void Context::hostname(const char *str, size_t size, server_t &server_) { + size_t copy_length = size_t(NI_MAXHOST) > size ? size : size_t(NI_MAXHOST); memcpy(_hostname, str, copy_length); - _hostname[copy_length]= 0; + _hostname[copy_length] = 0; - server_.port= MEMCACHED_DEFAULT_PORT; - server_.weight= 1; - server_.c_str= _hostname; - server_.size= size; + server_.port = MEMCACHED_DEFAULT_PORT; + server_.weight = 1; + server_.c_str = _hostname; + server_.size = size; } -bool Context::string_buffer(const char *str, size_t size, memcached_string_t& string_) -{ - if (memcached_string_set(_string_buffer, str, size)) - { - string_.c_str= memcached_string_value(_string_buffer); - string_.size= memcached_string_length(_string_buffer); +bool Context::string_buffer(const char *str, size_t size, memcached_string_t &string_) { + if (memcached_string_set(_string_buffer, str, size)) { + string_.c_str = memcached_string_value(_string_buffer); + string_.size = memcached_string_length(_string_buffer); return true; } @@ -120,17 +91,14 @@ bool Context::string_buffer(const char *str, size_t size, memcached_string_t& st return false; } -bool Context::set_hash(memcached_hash_t hash) -{ - if (_has_hash) - { +bool Context::set_hash(memcached_hash_t hash) { + if (_has_hash) { return false; } - if ((memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, hash)) != MEMCACHED_SUCCESS) - { + if ((memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, hash)) != MEMCACHED_SUCCESS) { return false; } - return _has_hash= true; + return _has_hash = true; } diff --git a/src/libmemcached/csl/context.h b/src/libmemcached/csl/context.h index 64d1f2f3..2fa62407 100644 --- a/src/libmemcached/csl/context.h +++ b/src/libmemcached/csl/context.h @@ -1,119 +1,76 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Configure Scripting Language - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include "libmemcached/csl/common.h" #include "libmemcached/csl/parser.h" -class Context -{ +class Context { public: Context(const char *option_string, size_t option_string_length, memcached_st *memc_, - memcached_return_t &rc_arg) : - previous_token(END), - scanner(NULL), - buf(option_string), - begin(NULL), - pos(0), - length(option_string_length), - memc(memc_), - rc(rc_arg), - _is_server(false), - _end(false), - _has_hash(false) - { - _hostname[0]= 0; + memcached_return_t &rc_arg) + : previous_token(END) + , scanner(NULL) + , buf(option_string) + , begin(NULL) + , pos(0) + , length(option_string_length) + , memc(memc_) + , rc(rc_arg) + , _is_server(false) + , _end(false) + , _has_hash(false) { + _hostname[0] = 0; init_scanner(); - rc= MEMCACHED_SUCCESS; + rc = MEMCACHED_SUCCESS; - memc->state.is_parsing= true; - memcached_string_create(memc, - &_string_buffer, - 1024); + memc->state.is_parsing = true; + memcached_string_create(memc, &_string_buffer, 1024); } - bool end() - { - return _end; - } + bool end() { return _end; } void start(); - void set_end() - { - rc= MEMCACHED_SUCCESS; - _end= true; + void set_end() { + rc = MEMCACHED_SUCCESS; + _end = true; } bool set_hash(memcached_hash_t hash); - void set_server() - { - _is_server= true; - } + void set_server() { _is_server = true; } - void unset_server() - { - _is_server= false; - } + void unset_server() { _is_server = false; } - bool is_server() const - { - return _is_server; - } + bool is_server() const { return _is_server; } - void hostname(const char*, size_t, server_t&); + void hostname(const char *, size_t, server_t &); - bool string_buffer(const char*, size_t, memcached_string_t&); + bool string_buffer(const char *, size_t, memcached_string_t &); - const char *hostname() const - { - return _hostname; - } + const char *hostname() const { return _hostname; } void abort(const char *, config_tokentype, const char *); - void error(const char *, config_tokentype, const char* ); + void error(const char *, config_tokentype, const char *); - ~Context() - { + ~Context() { memcached_string_free(&_string_buffer); destroy_scanner(); - memc->state.is_parsing= false; + memc->state.is_parsing = false; } config_tokentype previous_token; @@ -126,7 +83,7 @@ public: memcached_return_t &rc; protected: - void init_scanner(); + void init_scanner(); void destroy_scanner(); private: @@ -135,4 +92,4 @@ private: char _hostname[NI_MAXHOST]; bool _has_hash; memcached_string_st _string_buffer; -}; +}; diff --git a/src/libmemcached/csl/server.h b/src/libmemcached/csl/server.h index 21915d64..20846303 100644 --- a/src/libmemcached/csl/server.h +++ b/src/libmemcached/csl/server.h @@ -1,50 +1,27 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Configure Scripting Language - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include #ifdef HAVE_ARPA_INET_H -# include +# include #endif -struct server_t -{ +struct server_t { const char *c_str; size_t size; in_port_t port; diff --git a/src/libmemcached/csl/symbol.h b/src/libmemcached/csl/symbol.h index c185afdd..22059a20 100644 --- a/src/libmemcached/csl/symbol.h +++ b/src/libmemcached/csl/symbol.h @@ -1,47 +1,24 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Configure Scripting Language - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include "libmemcached/csl/common.h" -union CONFIG_STYPE -{ - long long number; +union CONFIG_STYPE { + long long number; memcached_string_t string; memcached_string_t option; double double_number; diff --git a/src/libmemcached/delete.cc b/src/libmemcached/delete.cc index fc0f6ddc..52e14979 100644 --- a/src/libmemcached/delete.cc +++ b/src/libmemcached/delete.cc @@ -1,121 +1,82 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" memcached_return_t memcached_delete(memcached_st *shell, const char *key, size_t key_length, - time_t expiration) -{ + time_t expiration) { return memcached_delete_by_key(shell, key, key_length, key, key_length, expiration); } -static inline memcached_return_t ascii_delete(memcached_instance_st* instance, - uint32_t , - const char *key, - const size_t key_length, - const bool reply, - const bool is_buffering) -{ - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { memcached_literal_param("delete ") }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length }, - { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") }, - { memcached_literal_param("\r\n") } - }; +static inline memcached_return_t ascii_delete(memcached_instance_st *instance, uint32_t, + const char *key, const size_t key_length, + const bool reply, const bool is_buffering) { + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {memcached_literal_param("delete ")}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}, + {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")}, + {memcached_literal_param("\r\n")}}; /* Send command header, only flush if we are NOT buffering */ return memcached_vdo(instance, vector, 6, is_buffering ? false : true); } -static inline memcached_return_t binary_delete(memcached_instance_st* instance, - uint32_t server_key, - const char *key, - const size_t key_length, - const bool reply, - const bool is_buffering) -{ - protocol_binary_request_delete request= {}; +static inline memcached_return_t binary_delete(memcached_instance_st *instance, uint32_t server_key, + const char *key, const size_t key_length, + const bool reply, const bool is_buffering) { + protocol_binary_request_delete request = {}; - bool should_flush= is_buffering ? false : true; + bool should_flush = is_buffering ? false : true; initialize_binary_request(instance, request.message.header); - if (reply) - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE; + if (reply) { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_DELETE; + } else { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_DELETEQ; } - else - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ; - } - request.message.header.request.keylen= htons(uint16_t(key_length + memcached_array_size(instance->root->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl(uint32_t(key_length + memcached_array_size(instance->root->_namespace))); - - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, sizeof(request.bytes) }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length } - }; - - memcached_return_t rc= memcached_vdo(instance, vector, 4, should_flush); - - if (memcached_has_replicas(instance)) - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ; - - for (uint32_t x= 0; x < memcached_has_replicas(instance); ++x) - { + request.message.header.request.keylen = + htons(uint16_t(key_length + memcached_array_size(instance->root->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = + htonl(uint32_t(key_length + memcached_array_size(instance->root->_namespace))); + + libmemcached_io_vector_st vector[] = {{NULL, 0}, + {request.bytes, sizeof(request.bytes)}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}}; + + memcached_return_t rc = memcached_vdo(instance, vector, 4, should_flush); + + if (memcached_has_replicas(instance)) { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_DELETEQ; + + for (uint32_t x = 0; x < memcached_has_replicas(instance); ++x) { ++server_key; - if (server_key == memcached_server_count(instance->root)) - { - server_key= 0; + if (server_key == memcached_server_count(instance->root)) { + server_key = 0; } - memcached_instance_st* replica= memcached_instance_fetch(instance->root, server_key); + memcached_instance_st *replica = memcached_instance_fetch(instance->root, server_key); - if (memcached_success(memcached_vdo(replica, vector, 4, should_flush))) - { + if (memcached_success(memcached_vdo(replica, vector, 4, should_flush))) { memcached_server_response_decrement(replica); } } @@ -124,81 +85,68 @@ static inline memcached_return_t binary_delete(memcached_instance_st* instance, return rc; } -memcached_return_t memcached_delete_by_key(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - time_t expiration) -{ - Memcached* memc= memcached2Memcached(shell); +memcached_return_t memcached_delete_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, time_t expiration) { + Memcached *memc = memcached2Memcached(shell); LIBMEMCACHED_MEMCACHED_DELETE_START(); memcached_return_t rc; - if (memcached_fatal(rc= initialize_query(memc, true))) - { + if (memcached_fatal(rc = initialize_query(memc, true))) { return rc; } - if (memcached_fatal(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1))) - { + if (memcached_fatal(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) { return memcached_last_error(memc); } - if (expiration) - { - return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Memcached server version does not allow expiration of deleted items")); + if (expiration) { + return memcached_set_error( + *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "Memcached server version does not allow expiration of deleted items")); } - uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); - - bool is_buffering= memcached_is_buffering(instance->root); - bool is_replying= memcached_is_replying(instance->root); + uint32_t server_key = + memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(memc, server_key); + + bool is_buffering = memcached_is_buffering(instance->root); + bool is_replying = memcached_is_replying(instance->root); // If a delete trigger exists, we need a response, so no buffering/noreply - if (memc->delete_trigger) - { - if (is_buffering) - { - return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Delete triggers cannot be used if buffering is enabled")); + if (memc->delete_trigger) { + if (is_buffering) { + return memcached_set_error( + *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Delete triggers cannot be used if buffering is enabled")); } - if (is_replying == false) - { - return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set")); + if (is_replying == false) { + return memcached_set_error( + *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set")); } } - if (memcached_is_binary(memc)) - { - rc= binary_delete(instance, server_key, key, key_length, is_replying, is_buffering); - } - else - { - rc= ascii_delete(instance, server_key, key, key_length, is_replying, is_buffering); + if (memcached_is_binary(memc)) { + rc = binary_delete(instance, server_key, key, key_length, is_replying, is_buffering); + } else { + rc = ascii_delete(instance, server_key, key, key_length, is_replying, is_buffering); } - if (rc == MEMCACHED_SUCCESS) - { - if (is_buffering == true) - { - rc= MEMCACHED_BUFFERED; - } - else if (is_replying == false) - { - rc= MEMCACHED_SUCCESS; - } - else - { + if (rc == MEMCACHED_SUCCESS) { + if (is_buffering == true) { + rc = MEMCACHED_BUFFERED; + } else if (is_replying == false) { + rc = MEMCACHED_SUCCESS; + } else { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); - if (rc == MEMCACHED_DELETED) - { - rc= MEMCACHED_SUCCESS; - if (memc->delete_trigger) - { + rc = memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); + if (rc == MEMCACHED_DELETED) { + rc = MEMCACHED_SUCCESS; + if (memc->delete_trigger) { memc->delete_trigger(memc, key, key_length); } } diff --git a/src/libmemcached/do.cc b/src/libmemcached/do.cc index 19ccd1bb..eb592a6d 100644 --- a/src/libmemcached/do.cc +++ b/src/libmemcached/do.cc @@ -1,53 +1,50 @@ -/* LibMemcached - * Copyright (C) 2006-2010 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static memcached_return_t _vdo_udp(memcached_instance_st* instance, - libmemcached_io_vector_st vector[], - const size_t count) -{ +static memcached_return_t _vdo_udp(memcached_instance_st *instance, + libmemcached_io_vector_st vector[], const size_t count) { #ifndef __MINGW32__ - if (vector[0].buffer or vector[0].length) - { - return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, - memcached_literal_param("UDP messages was attempted, but vector was not setup for it")); + if (vector[0].buffer or vector[0].length) { + return memcached_set_error( + *instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param("UDP messages was attempted, but vector was not setup for it")); } struct msghdr msg; memset(&msg, 0, sizeof(msg)); increment_udp_message_id(instance); - vector[0].buffer= instance->write_buffer; - vector[0].length= UDP_DATAGRAM_HEADER_LENGTH; + vector[0].buffer = instance->write_buffer; + vector[0].length = UDP_DATAGRAM_HEADER_LENGTH; - msg.msg_iov= (struct iovec*)vector; -#ifdef __APPLE__ - msg.msg_iovlen= int(count); -#else - msg.msg_iovlen= count; -#endif + msg.msg_iov = (struct iovec *) vector; +# ifdef __APPLE__ + msg.msg_iovlen = int(count); +# else + msg.msg_iovlen = count; +# endif - uint32_t retry= 5; - while (--retry) - { - ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0); - if (sendmsg_length > 0) - { + uint32_t retry = 5; + while (--retry) { + ssize_t sendmsg_length = ::sendmsg(instance->fd, &msg, 0); + if (sendmsg_length > 0) { break; - } - else if (sendmsg_length < 0) - { - if (errno == EMSGSIZE) - { + } else if (sendmsg_length < 0) { + if (errno == EMSGSIZE) { return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); } @@ -57,26 +54,24 @@ static memcached_return_t _vdo_udp(memcached_instance_st* instance, return MEMCACHED_SUCCESS; #else - (void)instance; - (void)vector; - (void)count; + (void) instance; + (void) vector; + (void) count; return MEMCACHED_FAILURE; #endif } -memcached_return_t memcached_vdo(memcached_instance_st* instance, - libmemcached_io_vector_st vector[], - const size_t count, - const bool with_flush) -{ +memcached_return_t memcached_vdo(memcached_instance_st *instance, + libmemcached_io_vector_st vector[], const size_t count, + const bool with_flush) { memcached_return_t rc; assert_msg(vector, "Invalid vector passed"); - if (memcached_failed(rc= memcached_connect(instance))) - { + if (memcached_failed(rc = memcached_connect(instance))) { WATCHPOINT_ERROR(rc); - assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none."); + assert_msg(instance->error_messages, + "memcached_connect() returned an error but the Instance showed none."); return rc; } @@ -86,23 +81,18 @@ memcached_return_t memcached_vdo(memcached_instance_st* instance, ** otherwise we might get a partial write. **/ bool sent_success; - if (memcached_is_udp(instance->root)) - { - sent_success= memcached_success(rc= _vdo_udp(instance, vector, count)); + if (memcached_is_udp(instance->root)) { + sent_success = memcached_success(rc = _vdo_udp(instance, vector, count)); } else { - sent_success= memcached_io_writev(instance, vector, count, with_flush); + sent_success = memcached_io_writev(instance, vector, count, with_flush); } - if (sent_success == false) - { - rc= memcached_last_error(instance->root); - if (rc == MEMCACHED_SUCCESS) - { + if (sent_success == false) { + rc = memcached_last_error(instance->root); + if (rc == MEMCACHED_SUCCESS) { memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); } memcached_io_reset(instance); - } - else if (memcached_is_replying(instance->root) && !memcached_is_udp(instance->root)) - { + } else if (memcached_is_replying(instance->root) && !memcached_is_udp(instance->root)) { memcached_server_response_increment(instance); } diff --git a/src/libmemcached/do.hpp b/src/libmemcached/do.hpp index d2dc6671..fb1eefe5 100644 --- a/src/libmemcached/do.hpp +++ b/src/libmemcached/do.hpp @@ -1,43 +1,19 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -memcached_return_t memcached_vdo(memcached_instance_st*, - libmemcached_io_vector_st vector[], - const size_t count, - const bool with_flush); +memcached_return_t memcached_vdo(memcached_instance_st *, libmemcached_io_vector_st vector[], + const size_t count, const bool with_flush); diff --git a/src/libmemcached/dump.cc b/src/libmemcached/dump.cc index 2e6aa631..d74a74b8 100644 --- a/src/libmemcached/dump.cc +++ b/src/libmemcached/dump.cc @@ -1,145 +1,105 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - /* - We use this to dump all keys. - - At this point we only support a callback method. This could be optimized by first - calling items and finding active slabs. For the moment though we just loop through - all slabs on servers and "grab" the keys. + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #include "libmemcached/common.h" -static memcached_return_t ascii_dump(Memcached *memc, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) -{ +static memcached_return_t ascii_dump(Memcached *memc, memcached_dump_fn *callback, void *context, + uint32_t number_of_callbacks) { memcached_version(memc); /* MAX_NUMBER_OF_SLAB_CLASSES is defined to 200 in Memcached 1.4.10 */ - for (uint32_t x= 0; x < 200; x++) - { + for (uint32_t x = 0; x < 200; x++) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x); - if (size_t(buffer_length) >= sizeof(buffer) or buffer_length < 0) - { - return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + int buffer_length = snprintf(buffer, sizeof(buffer), "%u", x); + if (size_t(buffer_length) >= sizeof(buffer) or buffer_length < 0) { + return memcached_set_error( + *memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); } // @NOTE the hard coded zero means "no limit" - libmemcached_io_vector_st vector[]= - { - { memcached_literal_param("stats cachedump ") }, - { buffer, size_t(buffer_length) }, - { memcached_literal_param(" 0\r\n") } - }; + libmemcached_io_vector_st vector[] = {{memcached_literal_param("stats cachedump ")}, + {buffer, size_t(buffer_length)}, + {memcached_literal_param(" 0\r\n")}}; // Send message to all servers - for (uint32_t server_key= 0; server_key < memcached_server_count(memc); server_key++) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); + for (uint32_t server_key = 0; server_key < memcached_server_count(memc); server_key++) { + memcached_instance_st *instance = memcached_instance_fetch(memc, server_key); // skip slabs >63 for server versions >= 1.4.23 if (x < 64 || memcached_version_instance_cmp(instance, 1, 4, 23) < 0) { memcached_return_t vdo_rc; - if (memcached_failed((vdo_rc= memcached_vdo(instance, vector, 3, true)))) - { + if (memcached_failed((vdo_rc = memcached_vdo(instance, vector, 3, true)))) { return vdo_rc; } } } // Collect the returned items - memcached_instance_st* instance; - memcached_return_t read_ret= MEMCACHED_SUCCESS; - while ((instance= memcached_io_get_readable_server(memc, read_ret))) - { - memcached_return_t response_rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); - if (response_rc == MEMCACHED_ITEM) - { + memcached_instance_st *instance; + memcached_return_t read_ret = MEMCACHED_SUCCESS; + while ((instance = memcached_io_get_readable_server(memc, read_ret))) { + memcached_return_t response_rc = + memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); + if (response_rc == MEMCACHED_ITEM) { char *string_ptr, *end_ptr; - string_ptr= buffer; - string_ptr+= 5; /* Move past ITEM */ + string_ptr = buffer; + string_ptr += 5; /* Move past ITEM */ - for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ; + for (end_ptr = string_ptr; isgraph(*end_ptr); end_ptr++) { + }; - char *key= string_ptr; - key[(size_t)(end_ptr-string_ptr)]= 0; + char *key = string_ptr; + key[(size_t)(end_ptr - string_ptr)] = 0; - for (uint32_t callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++) - { - memcached_return_t callback_rc= (*callback[callback_counter])(memc, key, (size_t)(end_ptr-string_ptr), context); - if (callback_rc != MEMCACHED_SUCCESS) - { + for (uint32_t callback_counter = 0; callback_counter < number_of_callbacks; + callback_counter++) { + memcached_return_t callback_rc = + (*callback[callback_counter])(memc, key, (size_t)(end_ptr - string_ptr), context); + if (callback_rc != MEMCACHED_SUCCESS) { // @todo build up a message for the error from the value memcached_set_error(*instance, callback_rc, MEMCACHED_AT); break; } } - } - else if (response_rc == MEMCACHED_END) - { + } else if (response_rc == MEMCACHED_END) { // All items have been returned - } - else if (response_rc == MEMCACHED_SERVER_ERROR) - { + } else if (response_rc == MEMCACHED_SERVER_ERROR) { /* If we try to request stats cachedump for a slab class that is too big * the server will return an incorrect error message: * "MEMCACHED_SERVER_ERROR failed to allocate memory" * This isn't really a fatal error, so let's just skip it. I want to * fix the return value from the memcached server to a CLIENT_ERROR, * so let's add support for that as well right now. - */ + */ assert(response_rc == MEMCACHED_SUCCESS); // Just fail return response_rc; - } - else if (response_rc == MEMCACHED_CLIENT_ERROR) - { + } else if (response_rc == MEMCACHED_CLIENT_ERROR) { /* The maximum number of slabs has changed in the past (currently 1<<6-1), * so ignore any client errors complaining about an illegal slab id. */ - if (0 == strncmp(buffer, "CLIENT_ERROR Illegal slab id", sizeof("CLIENT_ERROR Illegal slab id") - 1)) { + if (0 + == strncmp(buffer, "CLIENT_ERROR Illegal slab id", + sizeof("CLIENT_ERROR Illegal slab id") - 1)) + { memcached_error_free(*instance); memcached_error_free(*memc); } else { return response_rc; } - } - else - { + } else { // IO error of some sort must have occurred return response_rc; } @@ -149,22 +109,22 @@ static memcached_return_t ascii_dump(Memcached *memc, memcached_dump_fn *callbac return memcached_has_current_error(*memc) ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS; } -memcached_return_t memcached_dump(memcached_st *shell, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_dump(memcached_st *shell, memcached_dump_fn *callback, void *context, + uint32_t number_of_callbacks) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, true))) - { + if (memcached_failed(rc = initialize_query(ptr, true))) { return rc; } - /* + /* No support for Binary protocol yet @todo Fix this so that we just flush, switch to ascii, and then go back to binary. */ - if (memcached_is_binary(ptr)) - { - return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, memcached_literal_param("Binary protocol is not supported for memcached_dump()")); + if (memcached_is_binary(ptr)) { + return memcached_set_error( + *ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param("Binary protocol is not supported for memcached_dump()")); } return ascii_dump(ptr, callback, context, number_of_callbacks); diff --git a/src/libmemcached/encoding_key.cc b/src/libmemcached/encoding_key.cc index 714dd1cc..1df0dcf1 100644 --- a/src/libmemcached/encoding_key.cc +++ b/src/libmemcached/encoding_key.cc @@ -1,44 +1,22 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -memcached_return_t memcached_set_encoding_key(memcached_st* shell, const char *key, size_t key_length) -{ +memcached_return_t memcached_set_encoding_key(memcached_st *shell, const char *key, + size_t key_length) { if (!shell) { return MEMCACHED_INVALID_ARGUMENTS; } diff --git a/src/libmemcached/error.cc b/src/libmemcached/error.cc index c0e82d90..d148aafe 100644 --- a/src/libmemcached/error.cc +++ b/src/libmemcached/error.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -44,8 +22,7 @@ #include #define MAX_ERROR_LENGTH 2048 -struct memcached_error_t -{ +struct memcached_error_t { Memcached *root; uint64_t query_id; struct memcached_error_t *next; @@ -55,26 +32,21 @@ struct memcached_error_t char message[MAX_ERROR_LENGTH]; }; -static void _set(memcached_instance_st& server, Memcached& memc) -{ - if (server.error_messages and server.error_messages->query_id != server.root->query_id) - { +static void _set(memcached_instance_st &server, Memcached &memc) { + if (server.error_messages and server.error_messages->query_id != server.root->query_id) { memcached_error_free(server); } - if (memc.error_messages) - { - if (memc.error_messages->rc == MEMCACHED_TIMEOUT) - { + if (memc.error_messages) { + if (memc.error_messages->rc == MEMCACHED_TIMEOUT) { server.io_wait_count.timeouts++; } - memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t); - if (error) - { + memcached_error_t *error = libmemcached_xmalloc(&memc, memcached_error_t); + if (error) { memcpy(error, memc.error_messages, sizeof(memcached_error_t)); - error->next= server.error_messages; - server.error_messages= error; + error->next = server.error_messages; + server.error_messages = error; } } } @@ -83,139 +55,107 @@ static void _set(memcached_instance_st& server, Memcached& memc) static int error_log_fd= -1; #endif -static void _set(Memcached& memc, memcached_string_t *str, memcached_return_t &rc, const char *at, int local_errno= 0) -{ - if (memc.error_messages && memc.error_messages->query_id != memc.query_id) - { +static void _set(Memcached &memc, memcached_string_t *str, memcached_return_t &rc, const char *at, + int local_errno = 0) { + if (memc.error_messages && memc.error_messages->query_id != memc.query_id) { memcached_error_free(memc); } - if (memcached_fatal(rc) or rc == MEMCACHED_CLIENT_ERROR) - { + if (memcached_fatal(rc) or rc == MEMCACHED_CLIENT_ERROR) { // For memory allocation we use our error since it is a bit more specific - if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO) - { - rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO) { + rc = MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) - { - local_errno= ENOMEM; + if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) { + local_errno = ENOMEM; } - if (rc == MEMCACHED_ERRNO and not local_errno) - { - local_errno= errno; - rc= MEMCACHED_ERRNO; + if (rc == MEMCACHED_ERRNO and not local_errno) { + local_errno = errno; + rc = MEMCACHED_ERRNO; } - if (rc == MEMCACHED_ERRNO and local_errno == ENOTCONN) - { - rc= MEMCACHED_CONNECTION_FAILURE; + if (rc == MEMCACHED_ERRNO and local_errno == ENOTCONN) { + rc = MEMCACHED_CONNECTION_FAILURE; } - if (rc == MEMCACHED_ERRNO and local_errno == ECONNRESET) - { - rc= MEMCACHED_CONNECTION_FAILURE; + if (rc == MEMCACHED_ERRNO and local_errno == ECONNRESET) { + rc = MEMCACHED_CONNECTION_FAILURE; } - if (local_errno == EINVAL) - { - rc= MEMCACHED_INVALID_ARGUMENTS; + if (local_errno == EINVAL) { + rc = MEMCACHED_INVALID_ARGUMENTS; } - if (local_errno == ECONNREFUSED) - { - rc= MEMCACHED_CONNECTION_FAILURE; + if (local_errno == ECONNREFUSED) { + rc = MEMCACHED_CONNECTION_FAILURE; } - if (rc == MEMCACHED_TIMEOUT) - { + if (rc == MEMCACHED_TIMEOUT) { } - memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t); + memcached_error_t *error = libmemcached_xmalloc(&memc, memcached_error_t); if (error == NULL) // Bad business if this happens { assert_msg(error, "libmemcached_xmalloc() failed to allocate a memcached_error_t"); return; } - error->root= &memc; - error->query_id= memc.query_id; - error->rc= rc; - error->local_errno= local_errno; + error->root = &memc; + error->query_id = memc.query_id; + error->rc = rc; + error->local_errno = local_errno; // MEMCACHED_CLIENT_ERROR is a special case because it is an error coming from the server - if (rc == MEMCACHED_CLIENT_ERROR) - { + if (rc == MEMCACHED_CLIENT_ERROR) { assert(str); assert(str->size); - if (str and str->size) - { + if (str and str->size) { assert(error->local_errno == 0); - error->local_errno= 0; + error->local_errno = 0; - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %.*s", - error->root, - int(str->size), str->c_str); + error->size = (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %.*s", error->root, + int(str->size), str->c_str); } - } - else if (local_errno) - { + } else if (local_errno) { const char *errmsg_ptr; char errmsg[MAX_ERROR_LENGTH]; - errmsg[0]= 0; - errmsg_ptr= errmsg; + errmsg[0] = 0; + errmsg_ptr = errmsg; #if defined(HAVE_STRERROR_R_CHAR_P) && HAVE_STRERROR_R_CHAR_P - errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg)); + errmsg_ptr = strerror_r(local_errno, errmsg, sizeof(errmsg)); #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R strerror_r(local_errno, errmsg, sizeof(errmsg)); - errmsg_ptr= errmsg; + errmsg_ptr = errmsg; #elif defined(HAVE_STRERROR) && HAVE_STRERROR snprintf(errmsg, sizeof(errmsg), "%s", strerror(local_errno)); - errmsg_ptr= errmsg; + errmsg_ptr = errmsg; #endif - if (str and str->size and local_errno) - { - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s(%s), %.*s -> %s", - error->root, - memcached_strerror(&memc, rc), - errmsg_ptr, - memcached_string_printf(*str), at); - } - else - { - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s(%s) -> %s", - error->root, - memcached_strerror(&memc, rc), - errmsg_ptr, - at); + if (str and str->size and local_errno) { + error->size = (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s(%s), %.*s -> %s", + error->root, memcached_strerror(&memc, rc), errmsg_ptr, + memcached_string_printf(*str), at); + } else { + error->size = (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s(%s) -> %s", + error->root, memcached_strerror(&memc, rc), errmsg_ptr, at); } - } - else if (rc == MEMCACHED_PARSE_ERROR and str and str->size) - { - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %.*s -> %s", - error->root, - int(str->size), str->c_str, at); - } - else if (str and str->size) - { - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s, %.*s -> %s", - error->root, - memcached_strerror(&memc, rc), - int(str->size), str->c_str, at); - } - else - { - error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s -> %s", - error->root, - memcached_strerror(&memc, rc), at); + } else if (rc == MEMCACHED_PARSE_ERROR and str and str->size) { + error->size = (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %.*s -> %s", error->root, + int(str->size), str->c_str, at); + } else if (str and str->size) { + error->size = + (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s, %.*s -> %s", error->root, + memcached_strerror(&memc, rc), int(str->size), str->c_str, at); + } else { + error->size = (int) snprintf(error->message, MAX_ERROR_LENGTH, "(%p) %s -> %s", error->root, + memcached_strerror(&memc, rc), at); } - error->next= memc.error_messages; - memc.error_messages= error; + error->next = memc.error_messages; + memc.error_messages = error; #if 0 if (error_log_fd == -1) { @@ -232,97 +172,96 @@ static void _set(Memcached& memc, memcached_string_t *str, memcached_return_t &r } } -memcached_return_t memcached_set_error(Memcached& memc, memcached_return_t rc, const char *at, const char *str, size_t length) -{ - assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); - memcached_string_t tmp= { str, length }; +memcached_return_t memcached_set_error(Memcached &memc, memcached_return_t rc, const char *at, + const char *str, size_t length) { + assert_msg(rc != MEMCACHED_ERRNO, + "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); + memcached_string_t tmp = {str, length}; return memcached_set_error(memc, rc, at, tmp); } -memcached_return_t memcached_set_error(memcached_instance_st& self, memcached_return_t rc, const char *at, const char *str, size_t length) -{ - assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); - assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance"); +memcached_return_t memcached_set_error(memcached_instance_st &self, memcached_return_t rc, + const char *at, const char *str, size_t length) { + assert_msg(rc != MEMCACHED_ERRNO, + "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); + assert_msg(rc != MEMCACHED_SOME_ERRORS, + "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance"); - memcached_string_t tmp= { str, length }; + memcached_string_t tmp = {str, length}; return memcached_set_error(self, rc, at, tmp); } #ifndef __INTEL_COMPILER -#pragma GCC diagnostic ignored "-Wformat-nonliteral" +# pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif -memcached_return_t memcached_set_error(Memcached& memc, memcached_return_t rc, const char *at, memcached_string_t& str) -{ - assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); - if (memcached_fatal(rc)) - { +memcached_return_t memcached_set_error(Memcached &memc, memcached_return_t rc, const char *at, + memcached_string_t &str) { + assert_msg(rc != MEMCACHED_ERRNO, + "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); + if (memcached_fatal(rc)) { _set(memc, &str, rc, at); } return rc; } -memcached_return_t memcached_set_parser_error(Memcached& memc, - const char *at, - const char *format, ...) -{ +memcached_return_t memcached_set_parser_error(Memcached &memc, const char *at, const char *format, + ...) { va_list args; char buffer[BUFSIZ]; va_start(args, format); - int length= vsnprintf(buffer, sizeof(buffer), format, args); + int length = vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); return memcached_set_error(memc, MEMCACHED_PARSE_ERROR, at, buffer, length); } -static inline size_t append_host_to_string(memcached_instance_st& self, char* buffer, const size_t buffer_length) -{ - size_t size= 0; - switch (self.type) - { +static inline size_t append_host_to_string(memcached_instance_st &self, char *buffer, + const size_t buffer_length) { + size_t size = 0; + switch (self.type) { case MEMCACHED_CONNECTION_TCP: case MEMCACHED_CONNECTION_UDP: - size+= snprintf(buffer, buffer_length, " host: %s:%d", - self.hostname(), int(self.port())); + size += snprintf(buffer, buffer_length, " host: %s:%d", self.hostname(), int(self.port())); break; case MEMCACHED_CONNECTION_UNIX_SOCKET: - size+= snprintf(buffer, buffer_length, " socket: %s", - self.hostname()); + size += snprintf(buffer, buffer_length, " socket: %s", self.hostname()); break; } return size; } -memcached_return_t memcached_set_error(memcached_instance_st& self, memcached_return_t rc, const char *at, memcached_string_t& str) -{ - assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); - assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st"); - if (memcached_fatal(rc) == false and rc != MEMCACHED_CLIENT_ERROR) - { +memcached_return_t memcached_set_error(memcached_instance_st &self, memcached_return_t rc, + const char *at, memcached_string_t &str) { + assert_msg(rc != MEMCACHED_ERRNO, + "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); + assert_msg( + rc != MEMCACHED_SOME_ERRORS, + "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st"); + if (memcached_fatal(rc) == false and rc != MEMCACHED_CLIENT_ERROR) { return rc; } char hostname_port_message[MAX_ERROR_LENGTH]; - char* hostname_port_message_ptr= hostname_port_message; - int size= 0; - if (str.size) - { - size= snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ", - memcached_string_printf(str)); - hostname_port_message_ptr+= size; + char *hostname_port_message_ptr = hostname_port_message; + int size = 0; + if (str.size) { + size = snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ", + memcached_string_printf(str)); + hostname_port_message_ptr += size; } - size+= append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) -size); + size += + append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) - size); - memcached_string_t error_host= { hostname_port_message, size_t(size) }; + memcached_string_t error_host = {hostname_port_message, size_t(size)}; assert_msg(self.root, "Programmer error, root was not set on instance"); - if (self.root) - { + if (self.root) { _set(*self.root, &error_host, rc, at); _set(self, (*self.root)); assert(self.error_messages); @@ -333,21 +272,21 @@ memcached_return_t memcached_set_error(memcached_instance_st& self, memcached_re return rc; } -memcached_return_t memcached_set_error(memcached_instance_st& self, memcached_return_t rc, const char *at) -{ - assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st"); - if (memcached_fatal(rc) == false) - { +memcached_return_t memcached_set_error(memcached_instance_st &self, memcached_return_t rc, + const char *at) { + assert_msg( + rc != MEMCACHED_SOME_ERRORS, + "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st"); + if (memcached_fatal(rc) == false) { return rc; } - char hostname_port[MEMCACHED_NI_MAXHOST +MEMCACHED_NI_MAXSERV + sizeof("host : ")]; - size_t size= append_host_to_string(self, hostname_port, sizeof(hostname_port)); + char hostname_port[MEMCACHED_NI_MAXHOST + MEMCACHED_NI_MAXSERV + sizeof("host : ")]; + size_t size = append_host_to_string(self, hostname_port, sizeof(hostname_port)); - memcached_string_t error_host= { hostname_port, size}; + memcached_string_t error_host = {hostname_port, size}; - if (self.root) - { + if (self.root) { _set(*self.root, &error_host, rc, at); _set(self, *self.root); } @@ -355,11 +294,10 @@ memcached_return_t memcached_set_error(memcached_instance_st& self, memcached_re return rc; } -memcached_return_t memcached_set_error(Memcached& self, memcached_return_t rc, const char *at) -{ - assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); - if (memcached_fatal(rc) == false) - { +memcached_return_t memcached_set_error(Memcached &self, memcached_return_t rc, const char *at) { + assert_msg(rc != MEMCACHED_ERRNO, + "Programmer error, MEMCACHED_ERRNO was set to be returned to client"); + if (memcached_fatal(rc) == false) { return rc; } @@ -368,65 +306,61 @@ memcached_return_t memcached_set_error(Memcached& self, memcached_return_t rc, c return rc; } -memcached_return_t memcached_set_errno(Memcached& self, int local_errno, const char *at, const char *str, size_t length) -{ - memcached_string_t tmp= { str, length }; +memcached_return_t memcached_set_errno(Memcached &self, int local_errno, const char *at, + const char *str, size_t length) { + memcached_string_t tmp = {str, length}; return memcached_set_errno(self, local_errno, at, tmp); } -memcached_return_t memcached_set_errno(memcached_instance_st& self, int local_errno, const char *at, const char *str, size_t length) -{ - memcached_string_t tmp= { str, length }; +memcached_return_t memcached_set_errno(memcached_instance_st &self, int local_errno, const char *at, + const char *str, size_t length) { + memcached_string_t tmp = {str, length}; return memcached_set_errno(self, local_errno, at, tmp); } -memcached_return_t memcached_set_errno(Memcached& self, int local_errno, const char *at) -{ - if (local_errno == 0) - { +memcached_return_t memcached_set_errno(Memcached &self, int local_errno, const char *at) { + if (local_errno == 0) { return MEMCACHED_SUCCESS; } - memcached_return_t rc= MEMCACHED_ERRNO; + memcached_return_t rc = MEMCACHED_ERRNO; _set(self, NULL, rc, at, local_errno); return rc; } -memcached_return_t memcached_set_errno(Memcached& memc, int local_errno, const char *at, memcached_string_t& str) -{ - if (local_errno == 0) - { +memcached_return_t memcached_set_errno(Memcached &memc, int local_errno, const char *at, + memcached_string_t &str) { + if (local_errno == 0) { return MEMCACHED_SUCCESS; } - memcached_return_t rc= MEMCACHED_ERRNO; + memcached_return_t rc = MEMCACHED_ERRNO; _set(memc, &str, rc, at, local_errno); return rc; } -memcached_return_t memcached_set_errno(memcached_instance_st& self, int local_errno, const char *at, memcached_string_t& str) -{ - if (local_errno == 0) - { +memcached_return_t memcached_set_errno(memcached_instance_st &self, int local_errno, const char *at, + memcached_string_t &str) { + if (local_errno == 0) { return MEMCACHED_SUCCESS; } char hostname_port_message[MAX_ERROR_LENGTH]; - char* hostname_port_message_ptr= hostname_port_message; - size_t size= 0; - if (str.size) - { - size= snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ", memcached_string_printf(str)); + char *hostname_port_message_ptr = hostname_port_message; + size_t size = 0; + if (str.size) { + size = snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ", + memcached_string_printf(str)); } - size+= append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) -size); + size += + append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) - size); - memcached_string_t error_host= { hostname_port_message, size }; + memcached_string_t error_host = {hostname_port_message, size}; - memcached_return_t rc= MEMCACHED_ERRNO; - if (self.root == NULL) - { + memcached_return_t rc = MEMCACHED_ERRNO; + if (self.root == NULL) { return rc; } @@ -445,21 +379,19 @@ memcached_return_t memcached_set_errno(memcached_instance_st& self, int local_er return rc; } -memcached_return_t memcached_set_errno(memcached_instance_st& self, int local_errno, const char *at) -{ - if (local_errno == 0) - { +memcached_return_t memcached_set_errno(memcached_instance_st &self, int local_errno, + const char *at) { + if (local_errno == 0) { return MEMCACHED_SUCCESS; } char hostname_port_message[MAX_ERROR_LENGTH]; - size_t size= append_host_to_string(self, hostname_port_message, sizeof(hostname_port_message)); + size_t size = append_host_to_string(self, hostname_port_message, sizeof(hostname_port_message)); - memcached_string_t error_host= { hostname_port_message, size }; + memcached_string_t error_host = {hostname_port_message, size}; - memcached_return_t rc= MEMCACHED_ERRNO; - if (self.root == NULL) - { + memcached_return_t rc = MEMCACHED_ERRNO; + if (self.root == NULL) { return rc; } @@ -469,85 +401,67 @@ memcached_return_t memcached_set_errno(memcached_instance_st& self, int local_er return rc; } -static void _error_print(const memcached_error_t *error) -{ - if (error == NULL) - { +static void _error_print(const memcached_error_t *error) { + if (error == NULL) { return; } - if (error->size == 0) - { - fprintf(stderr, "\t%s\n", memcached_strerror(NULL, error->rc) ); - } - else - { + if (error->size == 0) { + fprintf(stderr, "\t%s\n", memcached_strerror(NULL, error->rc)); + } else { fprintf(stderr, "\t%s %s\n", memcached_strerror(NULL, error->rc), error->message); } _error_print(error->next); } -void memcached_error_print(const Memcached *shell) -{ - const Memcached* self= memcached2Memcached(shell); - if (self == NULL) - { +void memcached_error_print(const Memcached *shell) { + const Memcached *self = memcached2Memcached(shell); + if (self == NULL) { return; } _error_print(self->error_messages); - for (uint32_t x= 0; x < memcached_server_count(self); x++) - { - memcached_instance_st* instance= memcached_instance_by_position(self, x); + for (uint32_t x = 0; x < memcached_server_count(self); x++) { + memcached_instance_st *instance = memcached_instance_by_position(self, x); _error_print(instance->error_messages); } } -static void _error_free(memcached_error_t *error) -{ - if (error) - { +static void _error_free(memcached_error_t *error) { + if (error) { _error_free(error->next); libmemcached_free(error->root, error); } } -void memcached_error_free(Memcached& self) -{ +void memcached_error_free(Memcached &self) { _error_free(self.error_messages); - self.error_messages= NULL; + self.error_messages = NULL; } -void memcached_error_free(memcached_instance_st& self) -{ +void memcached_error_free(memcached_instance_st &self) { _error_free(self.error_messages); - self.error_messages= NULL; + self.error_messages = NULL; } -void memcached_error_free(memcached_server_st& self) -{ +void memcached_error_free(memcached_server_st &self) { _error_free(self.error_messages); - self.error_messages= NULL; + self.error_messages = NULL; } -const char *memcached_error(const memcached_st *memc) -{ +const char *memcached_error(const memcached_st *memc) { return memcached_last_error_message(memc); } -const char *memcached_last_error_message(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { - if (memc->error_messages) - { - if (memc->error_messages->size and memc->error_messages->message[0]) - { +const char *memcached_last_error_message(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { + if (memc->error_messages) { + if (memc->error_messages->size and memc->error_messages->message[0]) { return memc->error_messages->message; } @@ -560,10 +474,8 @@ const char *memcached_last_error_message(const memcached_st *shell) return memcached_strerror(memc, MEMCACHED_INVALID_ARGUMENTS); } -bool memcached_has_current_error(Memcached &memc) -{ - if (memc.error_messages - and memc.error_messages->query_id == memc.query_id +bool memcached_has_current_error(Memcached &memc) { + if (memc.error_messages and memc.error_messages->query_id == memc.query_id and memcached_failed(memc.error_messages->rc)) { return true; @@ -572,18 +484,14 @@ bool memcached_has_current_error(Memcached &memc) return false; } -bool memcached_has_current_error(memcached_instance_st& server) -{ +bool memcached_has_current_error(memcached_instance_st &server) { return memcached_has_current_error(*(server.root)); } -memcached_return_t memcached_last_error(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { - if (memc->error_messages) - { +memcached_return_t memcached_last_error(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { + if (memc->error_messages) { return memc->error_messages->rc; } @@ -593,81 +501,65 @@ memcached_return_t memcached_last_error(const memcached_st *shell) return MEMCACHED_INVALID_ARGUMENTS; } -int memcached_last_error_errno(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc == NULL) - { +int memcached_last_error_errno(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc == NULL) { return 0; } - if (memc->error_messages == NULL) - { + if (memc->error_messages == NULL) { return 0; } return memc->error_messages->local_errno; } -const char *memcached_server_error(const memcached_instance_st * server) -{ - if (server == NULL) - { +const char *memcached_server_error(const memcached_instance_st *server) { + if (server == NULL) { return NULL; } - if (server->error_messages == NULL) - { + if (server->error_messages == NULL) { return memcached_strerror(server->root, MEMCACHED_SUCCESS); } - if (server->error_messages->size == 0) - { + if (server->error_messages->size == 0) { return memcached_strerror(server->root, server->error_messages->rc); } return server->error_messages->message; } - -memcached_error_t *memcached_error_copy(const memcached_instance_st& server) -{ - if (server.error_messages == NULL) - { +memcached_error_t *memcached_error_copy(const memcached_instance_st &server) { + if (server.error_messages == NULL) { return NULL; } - memcached_error_t *error= libmemcached_xmalloc(server.root, memcached_error_t); + memcached_error_t *error = libmemcached_xmalloc(server.root, memcached_error_t); memcpy(error, server.error_messages, sizeof(memcached_error_t)); - error->next= NULL; + error->next = NULL; return error; } -memcached_return_t memcached_server_error_return(const memcached_instance_st * ptr) -{ - if (ptr == NULL) - { +memcached_return_t memcached_server_error_return(const memcached_instance_st *ptr) { + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (ptr->error_messages) - { + if (ptr->error_messages) { return ptr->error_messages->rc; } return MEMCACHED_SUCCESS; } -memcached_return_t memcached_instance_error_return(memcached_instance_st* instance) -{ - if (instance == NULL) - { +memcached_return_t memcached_instance_error_return(memcached_instance_st *instance) { + if (instance == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (instance->error_messages) - { + if (instance->error_messages) { return instance->error_messages->rc; } diff --git a/src/libmemcached/error.hpp b/src/libmemcached/error.hpp index 847dbc24..544854df 100644 --- a/src/libmemcached/error.hpp +++ b/src/libmemcached/error.hpp @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -41,50 +19,58 @@ #ifdef __cplusplus -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) -#define MEMCACHED_AT __FILE__ ":" TOSTRING(__LINE__) +# define STRINGIFY(x) # x +# define TOSTRING(x) STRINGIFY(x) +# define MEMCACHED_AT __FILE__ ":" TOSTRING(__LINE__) -memcached_return_t memcached_set_parser_error(Memcached& memc, - const char *at, - const char *format, ...); +memcached_return_t memcached_set_parser_error(Memcached &memc, const char *at, const char *format, + ...); -memcached_return_t memcached_set_error(Memcached&, memcached_return_t rc, const char *at); +memcached_return_t memcached_set_error(Memcached &, memcached_return_t rc, const char *at); -memcached_return_t memcached_set_error(memcached_instance_st&, memcached_return_t rc, const char *at); +memcached_return_t memcached_set_error(memcached_instance_st &, memcached_return_t rc, + const char *at); -memcached_return_t memcached_set_error(Memcached&, memcached_return_t rc, const char *at, const char *str, size_t length); +memcached_return_t memcached_set_error(Memcached &, memcached_return_t rc, const char *at, + const char *str, size_t length); -memcached_return_t memcached_set_error(memcached_instance_st&, memcached_return_t rc, const char *at, const char *str, size_t length); +memcached_return_t memcached_set_error(memcached_instance_st &, memcached_return_t rc, + const char *at, const char *str, size_t length); -memcached_return_t memcached_set_error(Memcached& memc, memcached_return_t rc, const char *at, memcached_string_t& str); +memcached_return_t memcached_set_error(Memcached &memc, memcached_return_t rc, const char *at, + memcached_string_t &str); -memcached_return_t memcached_set_error(memcached_instance_st&, memcached_return_t rc, const char *at, memcached_string_t& str); +memcached_return_t memcached_set_error(memcached_instance_st &, memcached_return_t rc, + const char *at, memcached_string_t &str); -memcached_return_t memcached_set_errno(Memcached& memc, int local_errno, const char *at, memcached_string_t& str); +memcached_return_t memcached_set_errno(Memcached &memc, int local_errno, const char *at, + memcached_string_t &str); -memcached_return_t memcached_set_errno(memcached_instance_st&, int local_errno, const char *at, memcached_string_t& str); +memcached_return_t memcached_set_errno(memcached_instance_st &, int local_errno, const char *at, + memcached_string_t &str); -memcached_return_t memcached_set_errno(Memcached& memc, int local_errno, const char *at, const char *str, size_t length); +memcached_return_t memcached_set_errno(Memcached &memc, int local_errno, const char *at, + const char *str, size_t length); -memcached_return_t memcached_set_errno(memcached_instance_st&, int local_errno, const char *at, const char *str, size_t length); +memcached_return_t memcached_set_errno(memcached_instance_st &, int local_errno, const char *at, + const char *str, size_t length); -memcached_return_t memcached_set_errno(Memcached& memc, int local_errno, const char *at); +memcached_return_t memcached_set_errno(Memcached &memc, int local_errno, const char *at); -memcached_return_t memcached_set_errno(memcached_instance_st&, int local_errno, const char *at); +memcached_return_t memcached_set_errno(memcached_instance_st &, int local_errno, const char *at); -bool memcached_has_current_error(Memcached&); +bool memcached_has_current_error(Memcached &); -bool memcached_has_current_error(memcached_instance_st&); +bool memcached_has_current_error(memcached_instance_st &); -void memcached_error_free(Memcached&); +void memcached_error_free(Memcached &); -void memcached_error_free(memcached_server_st&); +void memcached_error_free(memcached_server_st &); -void memcached_error_free(memcached_instance_st& self); +void memcached_error_free(memcached_instance_st &self); -memcached_error_t *memcached_error_copy(const memcached_instance_st&); +memcached_error_t *memcached_error_copy(const memcached_instance_st &); -memcached_return_t memcached_instance_error_return(memcached_instance_st*); +memcached_return_t memcached_instance_error_return(memcached_instance_st *); #endif diff --git a/src/libmemcached/exist.cc b/src/libmemcached/exist.cc index c5aef71c..43f2ac0f 100644 --- a/src/libmemcached/exist.cc +++ b/src/libmemcached/exist.cc @@ -1,158 +1,121 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static memcached_return_t ascii_exist(Memcached *memc, memcached_instance_st* instance, const char *key, size_t key_length) -{ - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { memcached_literal_param("add ") }, - { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) }, - { key, key_length }, - { memcached_literal_param(" 0") }, - { memcached_literal_param(" 2678400") }, - { memcached_literal_param(" 0") }, - { memcached_literal_param("\r\n") }, - { memcached_literal_param("\r\n") } - }; +static memcached_return_t ascii_exist(Memcached *memc, memcached_instance_st *instance, + const char *key, size_t key_length) { + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {memcached_literal_param("add ")}, + {memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace)}, + {key, key_length}, + {memcached_literal_param(" 0")}, + {memcached_literal_param(" 2678400")}, + {memcached_literal_param(" 0")}, + {memcached_literal_param("\r\n")}, + {memcached_literal_param("\r\n")}}; /* Send command header */ memcached_return_t rc; - if (memcached_fatal(rc= memcached_vdo(instance, vector, 9, true))) - { + if (memcached_fatal(rc = memcached_vdo(instance, vector, 9, true))) { return rc; } char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); + rc = memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); - if (rc == MEMCACHED_NOTSTORED) - { - rc= MEMCACHED_SUCCESS; + if (rc == MEMCACHED_NOTSTORED) { + rc = MEMCACHED_SUCCESS; } - if (rc == MEMCACHED_STORED) - { - rc= MEMCACHED_NOTFOUND; + if (rc == MEMCACHED_STORED) { + rc = MEMCACHED_NOTFOUND; } return rc; } -static memcached_return_t binary_exist(Memcached *memc, memcached_instance_st* instance, const char *key, size_t key_length) -{ - protocol_binary_request_set request= {}; - size_t send_length= sizeof(request.bytes); +static memcached_return_t binary_exist(Memcached *memc, memcached_instance_st *instance, + const char *key, size_t key_length) { + protocol_binary_request_set request = {}; + size_t send_length = sizeof(request.bytes); initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD; - request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(memc->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.extlen= 8; - request.message.body.flags= 0; - request.message.body.expiration= htonl(2678400); - - request.message.header.request.bodylen= htonl((uint32_t) (key_length - +memcached_array_size(memc->_namespace) - +request.message.header.request.extlen)); - - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, send_length }, - { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) }, - { key, key_length } - }; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_ADD; + request.message.header.request.keylen = + htons((uint16_t)(key_length + memcached_array_size(memc->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.extlen = 8; + request.message.body.flags = 0; + request.message.body.expiration = htonl(2678400); + + request.message.header.request.bodylen = htonl((uint32_t)( + key_length + memcached_array_size(memc->_namespace) + request.message.header.request.extlen)); + + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {request.bytes, send_length}, + {memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace)}, + {key, key_length}}; /* write the header */ memcached_return_t rc; - if (memcached_fatal(rc= memcached_vdo(instance, vector, 4, true))) - { + if (memcached_fatal(rc = memcached_vdo(instance, vector, 4, true))) { return rc; } - rc= memcached_response(instance, NULL, 0, NULL); + rc = memcached_response(instance, NULL, 0, NULL); - if (rc == MEMCACHED_SUCCESS) - { - rc= MEMCACHED_NOTFOUND; + if (rc == MEMCACHED_SUCCESS) { + rc = MEMCACHED_NOTFOUND; } - if (rc == MEMCACHED_DATA_EXISTS) - { - rc= MEMCACHED_SUCCESS; + if (rc == MEMCACHED_DATA_EXISTS) { + rc = MEMCACHED_SUCCESS; } return rc; } -memcached_return_t memcached_exist(memcached_st *memc, const char *key, size_t key_length) -{ +memcached_return_t memcached_exist(memcached_st *memc, const char *key, size_t key_length) { return memcached_exist_by_key(memc, key, key_length, key, key_length); } -memcached_return_t memcached_exist_by_key(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length) -{ - Memcached* memc= memcached2Memcached(shell); +memcached_return_t memcached_exist_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length) { + Memcached *memc = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(memc, true))) - { + if (memcached_failed(rc = initialize_query(memc, true))) { return rc; } - if (memcached_is_udp(memc)) - { + if (memcached_is_udp(memc)) { return memcached_set_error(*memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } - uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); + uint32_t server_key = + memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(memc, server_key); - if (memcached_is_binary(memc)) - { - rc= binary_exist(memc, instance, key, key_length); - } - else - { - rc= ascii_exist(memc, instance, key, key_length); + if (memcached_is_binary(memc)) { + rc = binary_exist(memc, instance, key, key_length); + } else { + rc = ascii_exist(memc, instance, key, key_length); } return rc; diff --git a/src/libmemcached/fetch.cc b/src/libmemcached/fetch.cc index 2303688c..eef8f401 100644 --- a/src/libmemcached/fetch.cc +++ b/src/libmemcached/fetch.cc @@ -1,318 +1,230 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -char *memcached_fetch(memcached_st *shell, char *key, size_t *key_length, - size_t *value_length, - uint32_t *flags, - memcached_return_t *error) -{ - Memcached* ptr= memcached2Memcached(shell); +char *memcached_fetch(memcached_st *shell, char *key, size_t *key_length, size_t *value_length, + uint32_t *flags, memcached_return_t *error) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - if (memcached_is_udp(ptr)) - { - if (value_length) - { - *value_length= 0; + if (memcached_is_udp(ptr)) { + if (value_length) { + *value_length = 0; } - if (key_length) - { - *key_length= 0; + if (key_length) { + *key_length = 0; } - if (flags) - { - *flags= 0; + if (flags) { + *flags = 0; } - if (key) - { - *key= 0; + if (key) { + *key = 0; } - *error= MEMCACHED_NOT_SUPPORTED; + *error = MEMCACHED_NOT_SUPPORTED; return NULL; } - memcached_result_st *result_buffer= &ptr->result; - result_buffer= memcached_fetch_result(ptr, result_buffer, error); - if (result_buffer == NULL or memcached_failed(*error)) - { + memcached_result_st *result_buffer = &ptr->result; + result_buffer = memcached_fetch_result(ptr, result_buffer, error); + if (result_buffer == NULL or memcached_failed(*error)) { WATCHPOINT_ASSERT(result_buffer == NULL); - if (value_length) - { - *value_length= 0; + if (value_length) { + *value_length = 0; } - if (key_length) - { - *key_length= 0; + if (key_length) { + *key_length = 0; } - if (flags) - { - *flags= 0; + if (flags) { + *flags = 0; } - if (key) - { - *key= 0; + if (key) { + *key = 0; } return NULL; } - if (value_length) - { - *value_length= memcached_string_length(&result_buffer->value); + if (value_length) { + *value_length = memcached_string_length(&result_buffer->value); } - if (key) - { - if (result_buffer->key_length > MEMCACHED_MAX_KEY) - { - *error= MEMCACHED_KEY_TOO_BIG; - if (value_length) - { - *value_length= 0; + if (key) { + if (result_buffer->key_length > MEMCACHED_MAX_KEY) { + *error = MEMCACHED_KEY_TOO_BIG; + if (value_length) { + *value_length = 0; } - if (key_length) - { - *key_length= 0; + if (key_length) { + *key_length = 0; } - if (flags) - { - *flags= 0; + if (flags) { + *flags = 0; } - if (key) - { - *key= 0; + if (key) { + *key = 0; } return NULL; } - strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :( - if (key_length) - { - *key_length= result_buffer->key_length; + strncpy(key, result_buffer->item_key, + result_buffer->key_length); // For the binary protocol we will cut off the key :( + if (key_length) { + *key_length = result_buffer->key_length; } } - if (flags) - { - *flags= result_buffer->item_flags; + if (flags) { + *flags = result_buffer->item_flags; } return memcached_string_take_value(&result_buffer->value); } -memcached_result_st *memcached_fetch_result(memcached_st *ptr, - memcached_result_st *result, - memcached_return_t *error) -{ +memcached_result_st *memcached_fetch_result(memcached_st *ptr, memcached_result_st *result, + memcached_return_t *error) { memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - if (ptr == NULL) - { - *error= MEMCACHED_INVALID_ARGUMENTS; + if (ptr == NULL) { + *error = MEMCACHED_INVALID_ARGUMENTS; return NULL; } - if (memcached_is_udp(ptr)) - { - *error= MEMCACHED_NOT_SUPPORTED; + if (memcached_is_udp(ptr)) { + *error = MEMCACHED_NOT_SUPPORTED; return NULL; } - if (result == NULL) - { + if (result == NULL) { // If we have already initialized (ie it is in use) our internal, we // create one. - if (memcached_is_initialized(&ptr->result)) - { - if ((result= memcached_result_create(ptr, NULL)) == NULL) - { - *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + if (memcached_is_initialized(&ptr->result)) { + if ((result = memcached_result_create(ptr, NULL)) == NULL) { + *error = MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } - } - else - { - result= memcached_result_create(ptr, &ptr->result); + } else { + result = memcached_result_create(ptr, &ptr->result); } } - *error= MEMCACHED_MAXIMUM_RETURN; // We use this to see if we ever go into the loop + *error = MEMCACHED_MAXIMUM_RETURN; // We use this to see if we ever go into the loop memcached_instance_st *server; - memcached_return_t read_ret= MEMCACHED_SUCCESS; - bool connection_failures= false; - while ((server= memcached_io_get_readable_server(ptr, read_ret))) - { + memcached_return_t read_ret = MEMCACHED_SUCCESS; + bool connection_failures = false; + while ((server = memcached_io_get_readable_server(ptr, read_ret))) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - *error= memcached_response(server, buffer, sizeof(buffer), result); + *error = memcached_response(server, buffer, sizeof(buffer), result); - if (*error == MEMCACHED_IN_PROGRESS) - { + if (*error == MEMCACHED_IN_PROGRESS) { continue; - } - else if (*error == MEMCACHED_CONNECTION_FAILURE) - { - connection_failures= true; + } else if (*error == MEMCACHED_CONNECTION_FAILURE) { + connection_failures = true; continue; - } - else if (*error == MEMCACHED_SUCCESS) - { + } else if (*error == MEMCACHED_SUCCESS) { result->count++; return result; - } - else if (*error == MEMCACHED_END) - { + } else if (*error == MEMCACHED_END) { memcached_server_response_reset(server); - } - else if (*error != MEMCACHED_NOTFOUND) - { + } else if (*error != MEMCACHED_NOTFOUND) { break; } } - if (*error == MEMCACHED_NOTFOUND and result->count) + if (*error == MEMCACHED_NOTFOUND and result->count) { + *error = MEMCACHED_END; + } else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count) { + *error = MEMCACHED_END; + } else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered { - *error= MEMCACHED_END; - } - else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count) - { - *error= MEMCACHED_END; - } - else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered - { - *error= MEMCACHED_NOTFOUND; - } - else if (connection_failures) - { - /* + *error = MEMCACHED_NOTFOUND; + } else if (connection_failures) { + /* If we have a connection failure to some servers, the caller may wish to treat that differently to getting a definitive NOT_FOUND from all servers, so return MEMCACHED_CONNECTION_FAILURE to allow - that. + that. */ - *error= MEMCACHED_CONNECTION_FAILURE; - } - else if (*error == MEMCACHED_SUCCESS) - { - *error= MEMCACHED_END; - } - else if (result->count == 0) - { - *error= MEMCACHED_NOTFOUND; + *error = MEMCACHED_CONNECTION_FAILURE; + } else if (*error == MEMCACHED_SUCCESS) { + *error = MEMCACHED_END; + } else if (result->count == 0) { + *error = MEMCACHED_NOTFOUND; } /* We have completed reading data */ - if (memcached_is_allocated(result)) - { + if (memcached_is_allocated(result)) { memcached_result_free(result); - } - else - { - result->count= 0; + } else { + result->count = 0; memcached_string_reset(&result->value); } return NULL; } -memcached_return_t memcached_fetch_execute(memcached_st *shell, - memcached_execute_fn *callback, - void *context, - uint32_t number_of_callbacks) -{ - Memcached* ptr= memcached2Memcached(shell); - memcached_result_st *result= &ptr->result; +memcached_return_t memcached_fetch_execute(memcached_st *shell, memcached_execute_fn *callback, + void *context, uint32_t number_of_callbacks) { + Memcached *ptr = memcached2Memcached(shell); + memcached_result_st *result = &ptr->result; memcached_return_t rc; - bool some_errors= false; + bool some_errors = false; - while ((result= memcached_fetch_result(ptr, result, &rc))) - { - if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND) - { + while ((result = memcached_fetch_result(ptr, result, &rc))) { + if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND) { continue; - } - else if (memcached_failed(rc)) - { + } else if (memcached_failed(rc)) { memcached_set_error(*ptr, rc, MEMCACHED_AT); - some_errors= true; + some_errors = true; continue; } - for (uint32_t x= 0; x < number_of_callbacks; x++) - { - memcached_return_t ret= (*callback[x])(ptr, result, context); - if (memcached_failed(ret)) - { - some_errors= true; + for (uint32_t x = 0; x < number_of_callbacks; x++) { + memcached_return_t ret = (*callback[x])(ptr, result, context); + if (memcached_failed(ret)) { + some_errors = true; memcached_set_error(*ptr, ret, MEMCACHED_AT); break; } } } - if (some_errors) - { + if (some_errors) { return MEMCACHED_SOME_ERRORS; } // If we were able to run all keys without issue we return // MEMCACHED_SUCCESS - if (memcached_success(rc)) - { + if (memcached_success(rc)) { return MEMCACHED_SUCCESS; } diff --git a/src/libmemcached/flag.cc b/src/libmemcached/flag.cc index 99327541..c17ea4e0 100644 --- a/src/libmemcached/flag.cc +++ b/src/libmemcached/flag.cc @@ -1,156 +1,88 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -bool memcached_flag(const memcached_st& memc, const memcached_flag_t flag) -{ - switch (flag) - { - case MEMCACHED_FLAG_AUTO_EJECT_HOSTS: - return memcached_is_auto_eject_hosts(&memc); +bool memcached_flag(const memcached_st &memc, const memcached_flag_t flag) { + switch (flag) { + case MEMCACHED_FLAG_AUTO_EJECT_HOSTS: return memcached_is_auto_eject_hosts(&memc); - case MEMCACHED_FLAG_BINARY_PROTOCOL: - return memcached_is_binary(&memc); + case MEMCACHED_FLAG_BINARY_PROTOCOL: return memcached_is_binary(&memc); - case MEMCACHED_FLAG_BUFFER_REQUESTS: - return memcached_is_buffering(&memc); + case MEMCACHED_FLAG_BUFFER_REQUESTS: return memcached_is_buffering(&memc); - case MEMCACHED_FLAG_HASH_WITH_NAMESPACE: - return memcached_is_hash_with_namespace(&memc); + case MEMCACHED_FLAG_HASH_WITH_NAMESPACE: return memcached_is_hash_with_namespace(&memc); - case MEMCACHED_FLAG_NO_BLOCK: - return memcached_is_no_block(&memc); + case MEMCACHED_FLAG_NO_BLOCK: return memcached_is_no_block(&memc); - case MEMCACHED_FLAG_REPLY: - return memcached_is_replying(&memc); + case MEMCACHED_FLAG_REPLY: return memcached_is_replying(&memc); - case MEMCACHED_FLAG_RANDOMIZE_REPLICA_READ: - return memcached_is_randomize_replica_read(&memc); + case MEMCACHED_FLAG_RANDOMIZE_REPLICA_READ: return memcached_is_randomize_replica_read(&memc); - case MEMCACHED_FLAG_SUPPORT_CAS: - return memcached_is_cas(&memc); + case MEMCACHED_FLAG_SUPPORT_CAS: return memcached_is_cas(&memc); - case MEMCACHED_FLAG_TCP_NODELAY: - return memcached_is_tcp_nodelay(&memc); + case MEMCACHED_FLAG_TCP_NODELAY: return memcached_is_tcp_nodelay(&memc); - case MEMCACHED_FLAG_USE_SORT_HOSTS: - return memcached_is_use_sort_hosts(&memc); + case MEMCACHED_FLAG_USE_SORT_HOSTS: return memcached_is_use_sort_hosts(&memc); - case MEMCACHED_FLAG_USE_UDP: - return memcached_is_udp(&memc); + case MEMCACHED_FLAG_USE_UDP: return memcached_is_udp(&memc); - case MEMCACHED_FLAG_VERIFY_KEY: - return memcached_is_verify_key(&memc); + case MEMCACHED_FLAG_VERIFY_KEY: return memcached_is_verify_key(&memc); - case MEMCACHED_FLAG_TCP_KEEPALIVE: - return memcached_is_use_sort_hosts(&memc); + case MEMCACHED_FLAG_TCP_KEEPALIVE: return memcached_is_use_sort_hosts(&memc); - case MEMCACHED_FLAG_IS_AES: - return memcached_is_aes(&memc); + case MEMCACHED_FLAG_IS_AES: return memcached_is_aes(&memc); - case MEMCACHED_FLAG_IS_FETCHING_VERSION: - return memcached_is_fetching_version(&memc); + case MEMCACHED_FLAG_IS_FETCHING_VERSION: return memcached_is_fetching_version(&memc); } abort(); } -void memcached_flag(memcached_st& memc, const memcached_flag_t flag, const bool arg) -{ - switch (flag) - { - case MEMCACHED_FLAG_AUTO_EJECT_HOSTS: - memcached_set_auto_eject_hosts(memc, arg); - break; +void memcached_flag(memcached_st &memc, const memcached_flag_t flag, const bool arg) { + switch (flag) { + case MEMCACHED_FLAG_AUTO_EJECT_HOSTS: memcached_set_auto_eject_hosts(memc, arg); break; - case MEMCACHED_FLAG_BINARY_PROTOCOL: - memcached_set_binary(memc, arg); - break; + case MEMCACHED_FLAG_BINARY_PROTOCOL: memcached_set_binary(memc, arg); break; - case MEMCACHED_FLAG_BUFFER_REQUESTS: - memcached_set_buffering(memc, arg); - break; + case MEMCACHED_FLAG_BUFFER_REQUESTS: memcached_set_buffering(memc, arg); break; - case MEMCACHED_FLAG_HASH_WITH_NAMESPACE: - memcached_set_hash_with_namespace(memc, arg); - break; + case MEMCACHED_FLAG_HASH_WITH_NAMESPACE: memcached_set_hash_with_namespace(memc, arg); break; - case MEMCACHED_FLAG_NO_BLOCK: - memcached_set_no_block(memc, arg); - break; + case MEMCACHED_FLAG_NO_BLOCK: memcached_set_no_block(memc, arg); break; - case MEMCACHED_FLAG_REPLY: - memcached_set_replying(memc, arg); - break; + case MEMCACHED_FLAG_REPLY: memcached_set_replying(memc, arg); break; case MEMCACHED_FLAG_RANDOMIZE_REPLICA_READ: memcached_set_randomize_replica_read(memc, arg); break; - case MEMCACHED_FLAG_SUPPORT_CAS: - memcached_set_cas(memc, arg); - break; + case MEMCACHED_FLAG_SUPPORT_CAS: memcached_set_cas(memc, arg); break; - case MEMCACHED_FLAG_TCP_NODELAY: - memcached_set_tcp_nodelay(memc, arg); - break; + case MEMCACHED_FLAG_TCP_NODELAY: memcached_set_tcp_nodelay(memc, arg); break; - case MEMCACHED_FLAG_USE_SORT_HOSTS: - memcached_set_use_sort_hosts(memc, arg); - break; + case MEMCACHED_FLAG_USE_SORT_HOSTS: memcached_set_use_sort_hosts(memc, arg); break; - case MEMCACHED_FLAG_USE_UDP: - memcached_set_udp(memc, arg); - break; + case MEMCACHED_FLAG_USE_UDP: memcached_set_udp(memc, arg); break; - case MEMCACHED_FLAG_VERIFY_KEY: - memcached_set_verify_key(memc, arg); - break; + case MEMCACHED_FLAG_VERIFY_KEY: memcached_set_verify_key(memc, arg); break; - case MEMCACHED_FLAG_TCP_KEEPALIVE: - memcached_set_use_sort_hosts(memc, arg); - break; + case MEMCACHED_FLAG_TCP_KEEPALIVE: memcached_set_use_sort_hosts(memc, arg); break; - case MEMCACHED_FLAG_IS_AES: - memcached_set_aes(memc, arg); - break; + case MEMCACHED_FLAG_IS_AES: memcached_set_aes(memc, arg); break; - case MEMCACHED_FLAG_IS_FETCHING_VERSION: - memcached_set_fetching_version(memc, arg); - break; + case MEMCACHED_FLAG_IS_FETCHING_VERSION: memcached_set_fetching_version(memc, arg); break; } } diff --git a/src/libmemcached/flag.hpp b/src/libmemcached/flag.hpp index 6efe2ef1..383f98b3 100644 --- a/src/libmemcached/flag.hpp +++ b/src/libmemcached/flag.hpp @@ -1,43 +1,21 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -enum memcached_flag_t -{ +enum memcached_flag_t { MEMCACHED_FLAG_AUTO_EJECT_HOSTS, MEMCACHED_FLAG_BINARY_PROTOCOL, MEMCACHED_FLAG_BUFFER_REQUESTS, @@ -55,5 +33,5 @@ enum memcached_flag_t MEMCACHED_FLAG_IS_FETCHING_VERSION }; -bool memcached_flag(const memcached_st&, const memcached_flag_t); -void memcached_flag(memcached_st&, const memcached_flag_t, const bool); +bool memcached_flag(const memcached_st &, const memcached_flag_t); +void memcached_flag(memcached_st &, const memcached_flag_t, const bool); diff --git a/src/libmemcached/flush.cc b/src/libmemcached/flush.cc index 82b8b8f6..bc663984 100644 --- a/src/libmemcached/flush.cc +++ b/src/libmemcached/flush.cc @@ -1,170 +1,120 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static memcached_return_t memcached_flush_binary(Memcached *ptr, - time_t expiration, - const bool reply) -{ - protocol_binary_request_flush request= {}; +static memcached_return_t memcached_flush_binary(Memcached *ptr, time_t expiration, + const bool reply) { + protocol_binary_request_flush request = {}; - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH; - request.message.header.request.extlen= 4; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl(request.message.header.request.extlen); - request.message.body.expiration= htonl((uint32_t) expiration); + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSH; + request.message.header.request.extlen = 4; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = htonl(request.message.header.request.extlen); + request.message.body.expiration = htonl((uint32_t) expiration); - memcached_return_t rc= MEMCACHED_SUCCESS; + memcached_return_t rc = MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); + for (uint32_t x = 0; x < memcached_server_count(ptr); x++) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); initialize_binary_request(instance, request.message.header); - if (reply) - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH; - } - else - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ; + if (reply) { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSH; + } else { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSHQ; } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, sizeof(request.bytes) } - }; + libmemcached_io_vector_st vector[] = {{NULL, 0}, {request.bytes, sizeof(request.bytes)}}; memcached_return_t rrc; - if (memcached_failed(rrc= memcached_vdo(instance, vector, 2, true))) - { - if (instance->error_messages == NULL or instance->root->error_messages == NULL) - { + if (memcached_failed(rrc = memcached_vdo(instance, vector, 2, true))) { + if (instance->error_messages == NULL or instance->root->error_messages == NULL) { memcached_set_error(*instance, rrc, MEMCACHED_AT); } - rc= MEMCACHED_SOME_ERRORS; - } + rc = MEMCACHED_SOME_ERRORS; + } } - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); + for (uint32_t x = 0; x < memcached_server_count(ptr); x++) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); - if (instance->response_count() > 0) - { - (void)memcached_response(instance, NULL, 0, NULL); + if (instance->response_count() > 0) { + (void) memcached_response(instance, NULL, 0, NULL); } } return rc; } -static memcached_return_t memcached_flush_textual(Memcached *ptr, - time_t expiration, - const bool reply) -{ - char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int send_length= 0; - if (expiration) - { - send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration); +static memcached_return_t memcached_flush_textual(Memcached *ptr, time_t expiration, + const bool reply) { + char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int send_length = 0; + if (expiration) { + send_length = snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long) expiration); } - if (size_t(send_length) >= sizeof(buffer) or send_length < 0) - { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + if (size_t(send_length) >= sizeof(buffer) or send_length < 0) { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); } - memcached_return_t rc= MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); - - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { memcached_literal_param("flush_all ") }, - { buffer, size_t(send_length) }, - { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") }, - { memcached_literal_param("\r\n") } - }; - - memcached_return_t rrc= memcached_vdo(instance, vector, 5, true); - if (memcached_success(rrc) and reply == true) - { + memcached_return_t rc = MEMCACHED_SUCCESS; + for (uint32_t x = 0; x < memcached_server_count(ptr); x++) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); + + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {memcached_literal_param("flush_all ")}, + {buffer, size_t(send_length)}, + {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")}, + {memcached_literal_param("\r\n")}}; + + memcached_return_t rrc = memcached_vdo(instance, vector, 5, true); + if (memcached_success(rrc) and reply == true) { char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL); + rrc = memcached_response(instance, response_buffer, sizeof(response_buffer), NULL); } - if (memcached_failed(rrc)) - { + if (memcached_failed(rrc)) { // If an error has already been reported, then don't add to it - if (instance->error_messages == NULL or instance->root->error_messages == NULL) - { + if (instance->error_messages == NULL or instance->root->error_messages == NULL) { memcached_set_error(*instance, rrc, MEMCACHED_AT); } - rc= MEMCACHED_SOME_ERRORS; + rc = MEMCACHED_SOME_ERRORS; } } return rc; } -memcached_return_t memcached_flush(memcached_st *shell, time_t expiration) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_flush(memcached_st *shell, time_t expiration) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, true))) - { + if (memcached_failed(rc = initialize_query(ptr, true))) { return rc; } - bool reply= memcached_is_replying(ptr); + bool reply = memcached_is_replying(ptr); LIBMEMCACHED_MEMCACHED_FLUSH_START(); - if (memcached_is_binary(ptr)) - { - rc= memcached_flush_binary(ptr, expiration, reply); - } - else - { - rc= memcached_flush_textual(ptr, expiration, reply); + if (memcached_is_binary(ptr)) { + rc = memcached_flush_binary(ptr, expiration, reply); + } else { + rc = memcached_flush_textual(ptr, expiration, reply); } LIBMEMCACHED_MEMCACHED_FLUSH_END(); diff --git a/src/libmemcached/flush_buffers.cc b/src/libmemcached/flush_buffers.cc index d40bd327..6226775e 100644 --- a/src/libmemcached/flush_buffers.cc +++ b/src/libmemcached/flush_buffers.cc @@ -1,65 +1,37 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -memcached_return_t memcached_flush_buffers(memcached_st *shell) -{ - Memcached* memc= memcached2Memcached(shell); - if (memc) - { - memcached_return_t ret= MEMCACHED_SUCCESS; +memcached_return_t memcached_flush_buffers(memcached_st *shell) { + Memcached *memc = memcached2Memcached(shell); + if (memc) { + memcached_return_t ret = MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < memcached_server_count(memc); ++x) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + for (uint32_t x = 0; x < memcached_server_count(memc); ++x) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); - if (instance->write_buffer_offset != 0) - { - if (instance->fd == INVALID_SOCKET and - (ret= memcached_connect(instance)) != MEMCACHED_SUCCESS) - { + if (instance->write_buffer_offset != 0) { + if (instance->fd == INVALID_SOCKET + and (ret = memcached_connect(instance)) != MEMCACHED_SUCCESS) { WATCHPOINT_ERROR(ret); return ret; } - if (memcached_io_write(instance) == false) - { - ret= MEMCACHED_SOME_ERRORS; + if (memcached_io_write(instance) == false) { + ret = MEMCACHED_SOME_ERRORS; } } } diff --git a/src/libmemcached/get.cc b/src/libmemcached/get.cc index d0d3f687..93330f45 100644 --- a/src/libmemcached/get.cc +++ b/src/libmemcached/get.cc @@ -1,162 +1,106 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -char *memcached_get(memcached_st *ptr, const char *key, - size_t key_length, - size_t *value_length, - uint32_t *flags, - memcached_return_t *error) -{ - return memcached_get_by_key(ptr, NULL, 0, key, key_length, value_length, - flags, error); +char *memcached_get(memcached_st *ptr, const char *key, size_t key_length, size_t *value_length, + uint32_t *flags, memcached_return_t *error) { + return memcached_get_by_key(ptr, NULL, 0, key, key_length, value_length, flags, error); } -static memcached_return_t __mget_by_key_real(memcached_st *ptr, - const char *group_key, - size_t group_key_length, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys, +static memcached_return_t __mget_by_key_real(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *const *keys, + const size_t *key_length, size_t number_of_keys, const bool mget_mode); -char *memcached_get_by_key(memcached_st *shell, - const char *group_key, - size_t group_key_length, - const char *key, size_t key_length, - size_t *value_length, - uint32_t *flags, - memcached_return_t *error) -{ - Memcached* ptr= memcached2Memcached(shell); +char *memcached_get_by_key(memcached_st *shell, const char *group_key, size_t group_key_length, + const char *key, size_t key_length, size_t *value_length, + uint32_t *flags, memcached_return_t *error) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - uint64_t query_id= 0; - if (ptr) - { - query_id= ptr->query_id; + uint64_t query_id = 0; + if (ptr) { + query_id = ptr->query_id; } /* Request the key */ - *error= __mget_by_key_real(ptr, group_key, group_key_length, - (const char * const *)&key, &key_length, - 1, false); - if (ptr) - { - assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented."); + *error = __mget_by_key_real(ptr, group_key, group_key_length, (const char *const *) &key, + &key_length, 1, false); + if (ptr) { + assert_msg(ptr->query_id == query_id + 1, + "Programmer error, the query_id was not incremented."); } - if (memcached_failed(*error)) - { - if (ptr) - { + if (memcached_failed(*error)) { + if (ptr) { if (memcached_has_current_error(*ptr)) // Find the most accurate error { - *error= memcached_last_error(ptr); + *error = memcached_last_error(ptr); } } - if (value_length) - { - *value_length= 0; + if (value_length) { + *value_length = 0; } return NULL; } - char *value= memcached_fetch(ptr, NULL, NULL, - value_length, flags, error); - assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented."); + char *value = memcached_fetch(ptr, NULL, NULL, value_length, flags, error); + assert_msg(ptr->query_id == query_id + 1, "Programmer error, the query_id was not incremented."); /* This is for historical reasons */ - if (*error == MEMCACHED_END) - { - *error= MEMCACHED_NOTFOUND; + if (*error == MEMCACHED_END) { + *error = MEMCACHED_NOTFOUND; } - if (value == NULL) - { - if (ptr->get_key_failure and *error == MEMCACHED_NOTFOUND) - { + if (value == NULL) { + if (ptr->get_key_failure and *error == MEMCACHED_NOTFOUND) { memcached_result_st key_failure_result; - memcached_result_st* result_ptr= memcached_result_create(ptr, &key_failure_result); - memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, result_ptr); + memcached_result_st *result_ptr = memcached_result_create(ptr, &key_failure_result); + memcached_return_t rc = ptr->get_key_failure(ptr, key, key_length, result_ptr); /* On all failure drop to returning NULL */ - if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED) - { - if (rc == MEMCACHED_BUFFERED) - { + if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED) { + if (rc == MEMCACHED_BUFFERED) { uint64_t latch; /* We use latch to track the state of the original socket */ - latch= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS); - if (latch == 0) - { + latch = memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS); + if (latch == 0) { memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1); } - rc= memcached_set(ptr, key, key_length, - (memcached_result_value(result_ptr)), - (memcached_result_length(result_ptr)), - 0, - (memcached_result_flags(result_ptr))); + rc = memcached_set(ptr, key, key_length, (memcached_result_value(result_ptr)), + (memcached_result_length(result_ptr)), 0, + (memcached_result_flags(result_ptr))); - if (rc == MEMCACHED_BUFFERED and latch == 0) - { + if (rc == MEMCACHED_BUFFERED and latch == 0) { memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0); } - } - else - { - rc= memcached_set(ptr, key, key_length, - (memcached_result_value(result_ptr)), - (memcached_result_length(result_ptr)), - 0, - (memcached_result_flags(result_ptr))); + } else { + rc = memcached_set(ptr, key, key_length, (memcached_result_value(result_ptr)), + (memcached_result_length(result_ptr)), 0, + (memcached_result_flags(result_ptr))); } - if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED) - { - *error= rc; - *value_length= memcached_result_length(result_ptr); - *flags= memcached_result_flags(result_ptr); - char *result_value= memcached_string_take_value(&result_ptr->value); + if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED) { + *error = rc; + *value_length = memcached_result_length(result_ptr); + *flags = memcached_result_flags(result_ptr); + char *result_value = memcached_string_take_value(&result_ptr->value); memcached_result_free(result_ptr); return result_value; @@ -165,7 +109,8 @@ char *memcached_get_by_key(memcached_st *shell, memcached_result_free(result_ptr); } - assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented."); + assert_msg(ptr->query_id == query_id + 1, + "Programmer error, the query_id was not incremented."); return NULL; } @@ -173,65 +118,52 @@ char *memcached_get_by_key(memcached_st *shell, return value; } -memcached_return_t memcached_mget(memcached_st *ptr, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys) -{ +memcached_return_t memcached_mget(memcached_st *ptr, const char *const *keys, + const size_t *key_length, size_t number_of_keys) { return memcached_mget_by_key(ptr, NULL, 0, keys, key_length, number_of_keys); } -static memcached_return_t binary_mget_by_key(memcached_st *ptr, - const uint32_t master_server_key, - const bool is_group_key_set, - const char * const *keys, - const size_t *key_length, - const size_t number_of_keys, +static memcached_return_t binary_mget_by_key(memcached_st *ptr, const uint32_t master_server_key, + const bool is_group_key_set, const char *const *keys, + const size_t *key_length, const size_t number_of_keys, const bool mget_mode); -static memcached_return_t __mget_by_key_real(memcached_st *ptr, - const char *group_key, - const size_t group_key_length, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys, - const bool mget_mode) -{ - bool failures_occured_in_sending= false; - const char *get_command= "get"; - uint8_t get_command_length= 3; - unsigned int master_server_key= (unsigned int)-1; /* 0 is a valid server id! */ +static memcached_return_t __mget_by_key_real(memcached_st *ptr, const char *group_key, + const size_t group_key_length, const char *const *keys, + const size_t *key_length, size_t number_of_keys, + const bool mget_mode) { + bool failures_occured_in_sending = false; + const char *get_command = "get"; + uint8_t get_command_length = 3; + unsigned int master_server_key = (unsigned int) -1; /* 0 is a valid server id! */ memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, true))) - { + if (memcached_failed(rc = initialize_query(ptr, true))) { return rc; } - if (memcached_is_udp(ptr)) - { + if (memcached_is_udp(ptr)) { return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } LIBMEMCACHED_MEMCACHED_MGET_START(); - if (number_of_keys == 0) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Numbers of keys provided was zero")); + if (number_of_keys == 0) { + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Numbers of keys provided was zero")); } - if (memcached_failed((rc= memcached_key_test(*ptr, keys, key_length, number_of_keys)))) - { + if (memcached_failed((rc = memcached_key_test(*ptr, keys, key_length, number_of_keys)))) { assert(memcached_last_error(ptr) == rc); return rc; } - bool is_group_key_set= false; - if (group_key and group_key_length) - { - master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); - is_group_key_set= true; + bool is_group_key_set = false; + if (group_key and group_key_length) { + master_server_key = + memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); + is_group_key_set = true; } /* @@ -240,36 +172,30 @@ static memcached_return_t __mget_by_key_real(memcached_st *ptr, It might be optimum to bounce the connection if count > some number. */ - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); + for (uint32_t x = 0; x < memcached_server_count(ptr); x++) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); - if (instance->response_count()) - { + if (instance->response_count()) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - if (ptr->flags.no_block) - { + if (ptr->flags.no_block) { memcached_io_write(instance); } - while(instance->response_count()) - { - (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result); + while (instance->response_count()) { + (void) memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result); } } } - if (memcached_is_binary(ptr)) - { - return binary_mget_by_key(ptr, master_server_key, is_group_key_set, keys, - key_length, number_of_keys, mget_mode); + if (memcached_is_binary(ptr)) { + return binary_mget_by_key(ptr, master_server_key, is_group_key_set, keys, key_length, + number_of_keys, mget_mode); } - if (ptr->flags.support_cas) - { - get_command= "gets"; - get_command_length= 4; + if (ptr->flags.support_cas) { + get_command = "gets"; + get_command_length = 4; } /* @@ -277,45 +203,35 @@ static memcached_return_t __mget_by_key_real(memcached_st *ptr, to the server. */ WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS); - size_t hosts_connected= 0; - for (uint32_t x= 0; x < number_of_keys; x++) - { + size_t hosts_connected = 0; + for (uint32_t x = 0; x < number_of_keys; x++) { uint32_t server_key; - if (is_group_key_set) - { - server_key= master_server_key; - } - else - { - server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); + if (is_group_key_set) { + server_key = master_server_key; + } else { + server_key = memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } - memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key); - - libmemcached_io_vector_st vector[]= - { - { get_command, get_command_length }, - { memcached_literal_param(" ") }, - { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) }, - { keys[x], key_length[x] } - }; + memcached_instance_st *instance = memcached_instance_fetch(ptr, server_key); + libmemcached_io_vector_st vector[] = { + {get_command, get_command_length}, + {memcached_literal_param(" ")}, + {memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)}, + {keys[x], key_length[x]}}; - if (instance->response_count() == 0) - { - rc= memcached_connect(instance); + if (instance->response_count() == 0) { + rc = memcached_connect(instance); - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { memcached_set_error(*instance, rc, MEMCACHED_AT); continue; } hosts_connected++; - if ((memcached_io_writev(instance, vector, 1, false)) == false) - { - failures_occured_in_sending= true; + if ((memcached_io_writev(instance, vector, 1, false)) == false) { + failures_occured_in_sending = true; continue; } WATCHPOINT_ASSERT(instance->cursor_active_ == 0); @@ -324,145 +240,111 @@ static memcached_return_t __mget_by_key_real(memcached_st *ptr, } { - if ((memcached_io_writev(instance, (vector + 1), 3, false)) == false) - { + if ((memcached_io_writev(instance, (vector + 1), 3, false)) == false) { memcached_instance_response_reset(instance); - failures_occured_in_sending= true; + failures_occured_in_sending = true; continue; } } } - if (hosts_connected == 0) - { + if (hosts_connected == 0) { LIBMEMCACHED_MEMCACHED_MGET_END(); - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { return rc; } return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT); } - /* Should we muddle on if some servers are dead? */ - bool success_happened= false; - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); + bool success_happened = false; + for (uint32_t x = 0; x < memcached_server_count(ptr); x++) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); - if (instance->response_count()) - { + if (instance->response_count()) { /* We need to do something about non-connnected hosts in the future */ - if ((memcached_io_write(instance, "\r\n", 2, true)) == -1) - { - failures_occured_in_sending= true; - } - else - { - success_happened= true; + if ((memcached_io_write(instance, "\r\n", 2, true)) == -1) { + failures_occured_in_sending = true; + } else { + success_happened = true; } } } LIBMEMCACHED_MEMCACHED_MGET_END(); - if (failures_occured_in_sending and success_happened) - { + if (failures_occured_in_sending and success_happened) { return MEMCACHED_SOME_ERRORS; } - if (success_happened) - { + if (success_happened) { return MEMCACHED_SUCCESS; } return MEMCACHED_FAILURE; // Complete failure occurred } -memcached_return_t memcached_mget_by_key(memcached_st *shell, - const char *group_key, - size_t group_key_length, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys) -{ - Memcached* ptr= memcached2Memcached(shell); - return __mget_by_key_real(ptr, group_key, group_key_length, keys, key_length, number_of_keys, true); +memcached_return_t memcached_mget_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *const *keys, + const size_t *key_length, size_t number_of_keys) { + Memcached *ptr = memcached2Memcached(shell); + return __mget_by_key_real(ptr, group_key, group_key_length, keys, key_length, number_of_keys, + true); } -memcached_return_t memcached_mget_execute(memcached_st *ptr, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys, - memcached_execute_fn *callback, - void *context, - unsigned int number_of_callbacks) -{ - return memcached_mget_execute_by_key(ptr, NULL, 0, keys, key_length, - number_of_keys, callback, +memcached_return_t memcached_mget_execute(memcached_st *ptr, const char *const *keys, + const size_t *key_length, size_t number_of_keys, + memcached_execute_fn *callback, void *context, + unsigned int number_of_callbacks) { + return memcached_mget_execute_by_key(ptr, NULL, 0, keys, key_length, number_of_keys, callback, context, number_of_callbacks); } -memcached_return_t memcached_mget_execute_by_key(memcached_st *shell, - const char *group_key, - size_t group_key_length, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys, - memcached_execute_fn *callback, - void *context, - unsigned int number_of_callbacks) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_mget_execute_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *const *keys, + const size_t *key_length, size_t number_of_keys, + memcached_execute_fn *callback, void *context, + unsigned int number_of_callbacks) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, false))) - { + if (memcached_failed(rc = initialize_query(ptr, false))) { return rc; } - if (memcached_is_udp(ptr)) - { + if (memcached_is_udp(ptr)) { return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } - if (memcached_is_binary(ptr) == false) - { - return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, - memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()")); + if (memcached_is_binary(ptr) == false) { + return memcached_set_error( + *ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param( + "ASCII protocol is not supported for memcached_mget_execute_by_key()")); } - memcached_callback_st *original_callbacks= ptr->callbacks; - memcached_callback_st cb= { - callback, - context, - number_of_callbacks - }; + memcached_callback_st *original_callbacks = ptr->callbacks; + memcached_callback_st cb = {callback, context, number_of_callbacks}; - ptr->callbacks= &cb; - rc= memcached_mget_by_key(ptr, group_key, group_key_length, keys, - key_length, number_of_keys); - ptr->callbacks= original_callbacks; + ptr->callbacks = &cb; + rc = memcached_mget_by_key(ptr, group_key, group_key_length, keys, key_length, number_of_keys); + ptr->callbacks = original_callbacks; return rc; } -static memcached_return_t simple_binary_mget(memcached_st *ptr, - const uint32_t master_server_key, - bool is_group_key_set, - const char * const *keys, - const size_t *key_length, - const size_t number_of_keys, const bool mget_mode) -{ - memcached_return_t rc= MEMCACHED_NOTFOUND; +static memcached_return_t simple_binary_mget(memcached_st *ptr, const uint32_t master_server_key, + bool is_group_key_set, const char *const *keys, + const size_t *key_length, const size_t number_of_keys, + const bool mget_mode) { + memcached_return_t rc = MEMCACHED_NOTFOUND; - bool flush= (number_of_keys == 1); + bool flush = (number_of_keys == 1); - if (memcached_failed(rc= memcached_key_test(*ptr, keys, key_length, number_of_keys))) - { + if (memcached_failed(rc = memcached_key_test(*ptr, keys, key_length, number_of_keys))) { return rc; } @@ -470,39 +352,30 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, If a server fails we warn about errors and start all over with sending keys to the server. */ - for (uint32_t x= 0; x < number_of_keys; ++x) - { + for (uint32_t x = 0; x < number_of_keys; ++x) { uint32_t server_key; - if (is_group_key_set) - { - server_key= master_server_key; - } - else - { - server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); + if (is_group_key_set) { + server_key = master_server_key; + } else { + server_key = memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } - memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key); + memcached_instance_st *instance = memcached_instance_fetch(ptr, server_key); - if (instance->response_count() == 0) - { - rc= memcached_connect(instance); - if (memcached_failed(rc)) - { + if (instance->response_count() == 0) { + rc = memcached_connect(instance); + if (memcached_failed(rc)) { continue; } } - protocol_binary_request_getk request= { }; //= {.bytes= {0}}; + protocol_binary_request_getk request = {}; //= {.bytes= {0}}; initialize_binary_request(instance, request.message.header); - if (mget_mode) - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETKQ; - } - else - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK; + if (mget_mode) { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_GETKQ; + } else { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_GETK; } #if 0 @@ -522,21 +395,20 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, } #endif - request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->_namespace))); + request.message.header.request.keylen = + htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = + htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace))); - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) }, - { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) }, - { keys[x], key_length[x] } - }; + libmemcached_io_vector_st vector[] = { + {request.bytes, sizeof(request.bytes)}, + {memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)}, + {keys[x], key_length[x]}}; - if (memcached_io_writev(instance, vector, 3, flush) == false) - { + if (memcached_io_writev(instance, vector, 3, flush) == false) { memcached_server_response_reset(instance); - rc= MEMCACHED_SOME_ERRORS; + rc = MEMCACHED_SOME_ERRORS; continue; } @@ -545,32 +417,29 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, memcached_server_response_increment(instance); if ((x > 0 and x == ptr->io_key_prefetch) and memcached_flush_buffers(ptr) != MEMCACHED_SUCCESS) { - rc= MEMCACHED_SOME_ERRORS; + rc = MEMCACHED_SOME_ERRORS; } } - if (mget_mode) - { + if (mget_mode) { /* Send a noop command to flush the buffers */ - protocol_binary_request_noop request= {}; //= {.bytes= {0}}; - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_NOOP; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; + protocol_binary_request_noop request = {}; //= {.bytes= {0}}; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_NOOP; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; - for (uint32_t x= 0; x < memcached_server_count(ptr); ++x) - { - memcached_instance_st* instance= memcached_instance_fetch(ptr, x); + for (uint32_t x = 0; x < memcached_server_count(ptr); ++x) { + memcached_instance_st *instance = memcached_instance_fetch(ptr, x); - if (instance->response_count()) - { + if (instance->response_count()) { initialize_binary_request(instance, request.message.header); - if ((memcached_io_write(instance) == false) or - (memcached_io_write(instance, request.bytes, sizeof(request.bytes), true) == -1)) + if ((memcached_io_write(instance) == false) + or (memcached_io_write(instance, request.bytes, sizeof(request.bytes), true) == -1)) { memcached_instance_response_reset(instance); memcached_io_reset(instance); - rc= MEMCACHED_SOME_ERRORS; + rc = MEMCACHED_SOME_ERRORS; } } } @@ -579,73 +448,63 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, return rc; } -static memcached_return_t replication_binary_mget(memcached_st *ptr, - uint32_t* hash, - bool* dead_servers, - const char *const *keys, +static memcached_return_t replication_binary_mget(memcached_st *ptr, uint32_t *hash, + bool *dead_servers, const char *const *keys, const size_t *key_length, - const size_t number_of_keys) -{ - memcached_return_t rc= MEMCACHED_NOTFOUND; - uint32_t start= 0; - uint64_t randomize_read= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ); - - if (randomize_read) - { - start= (uint32_t)random() % (uint32_t)(ptr->number_of_replicas + 1); + const size_t number_of_keys) { + memcached_return_t rc = MEMCACHED_NOTFOUND; + uint32_t start = 0; + uint64_t randomize_read = memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ); + + if (randomize_read) { + start = (uint32_t) random() % (uint32_t)(ptr->number_of_replicas + 1); } /* Loop for each replica */ - for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica) - { - bool success= true; + for (uint32_t replica = 0; replica <= ptr->number_of_replicas; ++replica) { + bool success = true; - for (uint32_t x= 0; x < number_of_keys; ++x) - { - if (hash[x] == memcached_server_count(ptr)) - { + for (uint32_t x = 0; x < number_of_keys; ++x) { + if (hash[x] == memcached_server_count(ptr)) { continue; /* Already successfully sent */ } - uint32_t server= hash[x] +replica; + uint32_t server = hash[x] + replica; /* In case of randomized reads */ - if (randomize_read and ((server + start) <= (hash[x] + ptr->number_of_replicas))) - { - server+= start; + if (randomize_read and ((server + start) <= (hash[x] + ptr->number_of_replicas))) { + server += start; } - while (server >= memcached_server_count(ptr)) - { + while (server >= memcached_server_count(ptr)) { server -= memcached_server_count(ptr); } - if (dead_servers[server]) - { + if (dead_servers[server]) { continue; } - memcached_instance_st* instance= memcached_instance_fetch(ptr, server); + memcached_instance_st *instance = memcached_instance_fetch(ptr, server); - if (instance->response_count() == 0) - { - rc= memcached_connect(instance); + if (instance->response_count() == 0) { + rc = memcached_connect(instance); - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { memcached_io_reset(instance); - dead_servers[server]= true; - success= false; + dead_servers[server] = true; + success = false; continue; } } - protocol_binary_request_getk request= {}; + protocol_binary_request_getk request = {}; initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK; - request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace))); + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_GETK; + request.message.header.request.keylen = + htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = + htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace))); /* * We need to disable buffering to actually know that the request was @@ -656,28 +515,24 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, * and we used the callback interface from memcached_mget_execute so * that we might have processed some of the responses etc. For now, * just make sure we work _correctly_ - */ - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) }, - { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) }, - { keys[x], key_length[x] } - }; + */ + libmemcached_io_vector_st vector[] = { + {request.bytes, sizeof(request.bytes)}, + {memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)}, + {keys[x], key_length[x]}}; - if (memcached_io_writev(instance, vector, 3, true) == false) - { + if (memcached_io_writev(instance, vector, 3, true) == false) { memcached_io_reset(instance); - dead_servers[server]= true; - success= false; + dead_servers[server] = true; + success = false; continue; } memcached_server_response_increment(instance); - hash[x]= memcached_server_count(ptr); + hash[x] = memcached_server_count(ptr); } - if (success) - { + if (success) { break; } } @@ -685,47 +540,36 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, return rc; } -static memcached_return_t binary_mget_by_key(memcached_st *ptr, - const uint32_t master_server_key, - bool is_group_key_set, - const char * const *keys, - const size_t *key_length, - const size_t number_of_keys, - const bool mget_mode) -{ - if (ptr->number_of_replicas == 0) - { - return simple_binary_mget(ptr, master_server_key, is_group_key_set, - keys, key_length, number_of_keys, mget_mode); +static memcached_return_t binary_mget_by_key(memcached_st *ptr, const uint32_t master_server_key, + bool is_group_key_set, const char *const *keys, + const size_t *key_length, const size_t number_of_keys, + const bool mget_mode) { + if (ptr->number_of_replicas == 0) { + return simple_binary_mget(ptr, master_server_key, is_group_key_set, keys, key_length, + number_of_keys, mget_mode); } - uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t); - bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool); + uint32_t *hash = libmemcached_xvalloc(ptr, number_of_keys, uint32_t); + bool *dead_servers = libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool); - if (hash == NULL or dead_servers == NULL) - { + if (hash == NULL or dead_servers == NULL) { libmemcached_free(ptr, hash); libmemcached_free(ptr, dead_servers); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - if (is_group_key_set) - { - for (size_t x= 0; x < number_of_keys; x++) - { - hash[x]= master_server_key; + if (is_group_key_set) { + for (size_t x = 0; x < number_of_keys; x++) { + hash[x] = master_server_key; } - } - else - { - for (size_t x= 0; x < number_of_keys; x++) - { - hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); + } else { + for (size_t x = 0; x < number_of_keys; x++) { + hash[x] = memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } } - memcached_return_t rc= replication_binary_mget(ptr, hash, dead_servers, keys, - key_length, number_of_keys); + memcached_return_t rc = + replication_binary_mget(ptr, hash, dead_servers, keys, key_length, number_of_keys); WATCHPOINT_IFERROR(rc); libmemcached_free(ptr, hash); diff --git a/src/libmemcached/hash.cc b/src/libmemcached/hash.cc index aa5dbfaf..33766b5f 100644 --- a/src/libmemcached/hash.cc +++ b/src/libmemcached/hash.cc @@ -1,40 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -42,52 +19,44 @@ #include "libmemcached/virtual_bucket.h" -uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm) -{ - return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm); +uint32_t memcached_generate_hash_value(const char *key, size_t key_length, + memcached_hash_t hash_algorithm) { + return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t) hash_algorithm); } -static inline uint32_t generate_hash(const Memcached *ptr, const char *key, size_t key_length) -{ +static inline uint32_t generate_hash(const Memcached *ptr, const char *key, size_t key_length) { return hashkit_digest(&ptr->hashkit, key, key_length); } -static uint32_t dispatch_host(const Memcached *ptr, uint32_t hash) -{ - switch (ptr->distribution) - { +static uint32_t dispatch_host(const Memcached *ptr, uint32_t hash) { + switch (ptr->distribution) { case MEMCACHED_DISTRIBUTION_CONSISTENT: case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: - { - uint32_t num= ptr->ketama.continuum_points_counter; - WATCHPOINT_ASSERT(ptr->ketama.continuum); - - memcached_continuum_item_st *begin, *end, *left, *right, *middle; - begin= left= ptr->ketama.continuum; - end= right= ptr->ketama.continuum + num; - - while (left < right) - { - middle= left + (right - left) / 2; - if (middle->value < hash) - left= middle + 1; - else - right= middle; - } - if (right == end) - right= begin; - return right->index; - } - case MEMCACHED_DISTRIBUTION_MODULA: - return hash % memcached_server_count(ptr); - case MEMCACHED_DISTRIBUTION_RANDOM: - return (uint32_t) random() % memcached_server_count(ptr); - case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: - { - return memcached_virtual_bucket_get(ptr, hash); + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: { + uint32_t num = ptr->ketama.continuum_points_counter; + WATCHPOINT_ASSERT(ptr->ketama.continuum); + + memcached_continuum_item_st *begin, *end, *left, *right, *middle; + begin = left = ptr->ketama.continuum; + end = right = ptr->ketama.continuum + num; + + while (left < right) { + middle = left + (right - left) / 2; + if (middle->value < hash) + left = middle + 1; + else + right = middle; } + if (right == end) + right = begin; + return right->index; + } + case MEMCACHED_DISTRIBUTION_MODULA: return hash % memcached_server_count(ptr); + case MEMCACHED_DISTRIBUTION_RANDOM: return (uint32_t) random() % memcached_server_count(ptr); + case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: { + return memcached_virtual_bucket_get(ptr, hash); + } default: case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */ @@ -99,87 +68,73 @@ static uint32_t dispatch_host(const Memcached *ptr, uint32_t hash) /* One version is public and will not modify the distribution hash, the other will. */ -static inline uint32_t _generate_hash_wrapper(const Memcached *ptr, const char *key, size_t key_length) -{ +static inline uint32_t _generate_hash_wrapper(const Memcached *ptr, const char *key, + size_t key_length) { WATCHPOINT_ASSERT(memcached_server_count(ptr)); if (memcached_server_count(ptr) == 1) return 0; - if (ptr->flags.hash_with_namespace) - { - size_t temp_length= memcached_array_size(ptr->_namespace) + key_length; + if (ptr->flags.hash_with_namespace) { + size_t temp_length = memcached_array_size(ptr->_namespace) + key_length; char temp[MEMCACHED_MAX_KEY]; - if (temp_length > MEMCACHED_MAX_KEY -1) + if (temp_length > MEMCACHED_MAX_KEY - 1) return 0; strncpy(temp, memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)); strncpy(temp + memcached_array_size(ptr->_namespace), key, key_length); return generate_hash(ptr, temp, temp_length); - } - else - { + } else { return generate_hash(ptr, key, key_length); } } -static inline void _regen_for_auto_eject(Memcached *ptr) -{ - if (_is_auto_eject_host(ptr) && ptr->ketama.next_distribution_rebuild) - { +static inline void _regen_for_auto_eject(Memcached *ptr) { + if (_is_auto_eject_host(ptr) && ptr->ketama.next_distribution_rebuild) { struct timeval now; - if (gettimeofday(&now, NULL) == 0 and - now.tv_sec > ptr->ketama.next_distribution_rebuild) - { + if (gettimeofday(&now, NULL) == 0 and now.tv_sec > ptr->ketama.next_distribution_rebuild) { run_distribution(ptr); } } } -void memcached_autoeject(memcached_st *ptr) -{ +void memcached_autoeject(memcached_st *ptr) { _regen_for_auto_eject(ptr); } -uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length) -{ - uint32_t hash= _generate_hash_wrapper(ptr, key, key_length); +uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, + size_t key_length) { + uint32_t hash = _generate_hash_wrapper(ptr, key, key_length); _regen_for_auto_eject(ptr); return dispatch_host(ptr, hash); } -uint32_t memcached_generate_hash(const memcached_st *shell, const char *key, size_t key_length) -{ - const Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { +uint32_t memcached_generate_hash(const memcached_st *shell, const char *key, size_t key_length) { + const Memcached *ptr = memcached2Memcached(shell); + if (ptr) { return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length)); } return UINT32_MAX; } -const hashkit_st *memcached_get_hashkit(const memcached_st *shell) -{ - const Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { +const hashkit_st *memcached_get_hashkit(const memcached_st *shell) { + const Memcached *ptr = memcached2Memcached(shell); + if (ptr) { return &ptr->hashkit; } return NULL; } -memcached_return_t memcached_set_hashkit(memcached_st *shell, hashkit_st *hashk) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { +memcached_return_t memcached_set_hashkit(memcached_st *shell, hashkit_st *hashk) { + Memcached *self = memcached2Memcached(shell); + if (self) { hashkit_free(&self->hashkit); hashkit_clone(&self->hashkit, hashk); @@ -189,7 +144,6 @@ memcached_return_t memcached_set_hashkit(memcached_st *shell, hashkit_st *hashk) return MEMCACHED_INVALID_ARGUMENTS; } -const char * libmemcached_string_hash(memcached_hash_t type) -{ - return libhashkit_string_hash((hashkit_hash_algorithm_t)type); +const char *libmemcached_string_hash(memcached_hash_t type) { + return libhashkit_string_hash((hashkit_hash_algorithm_t) type); } diff --git a/src/libmemcached/hash.hpp b/src/libmemcached/hash.hpp index bf450eed..1b40a9fa 100644 --- a/src/libmemcached/hash.hpp +++ b/src/libmemcached/hash.hpp @@ -1,40 +1,19 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length); +uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, + size_t key_length); diff --git a/src/libmemcached/hosts.cc b/src/libmemcached/hosts.cc index b8292b2b..8afbdb25 100644 --- a/src/libmemcached/hosts.cc +++ b/src/libmemcached/hosts.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/assert.hpp" @@ -44,344 +22,279 @@ /* Protoypes (static) */ static memcached_return_t update_continuum(Memcached *ptr); -static int compare_servers(const void *p1, const void *p2) -{ - const memcached_instance_st * a= (const memcached_instance_st *)p1; - const memcached_instance_st * b= (const memcached_instance_st *)p2; +static int compare_servers(const void *p1, const void *p2) { + const memcached_instance_st *a = (const memcached_instance_st *) p1; + const memcached_instance_st *b = (const memcached_instance_st *) p2; - int return_value= strcmp(a->_hostname, b->_hostname); + int return_value = strcmp(a->_hostname, b->_hostname); - if (return_value == 0) - { - return_value= int(a->port() - b->port()); + if (return_value == 0) { + return_value = int(a->port() - b->port()); } return return_value; } -static void sort_hosts(Memcached *ptr) -{ - if (memcached_server_count(ptr)) - { - qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(memcached_instance_st), compare_servers); +static void sort_hosts(Memcached *ptr) { + if (memcached_server_count(ptr)) { + qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(memcached_instance_st), + compare_servers); } } - -memcached_return_t run_distribution(Memcached *ptr) -{ - if (ptr->flags.use_sort_hosts) - { +memcached_return_t run_distribution(Memcached *ptr) { + if (ptr->flags.use_sort_hosts) { sort_hosts(ptr); } - switch (ptr->distribution) - { + switch (ptr->distribution) { case MEMCACHED_DISTRIBUTION_CONSISTENT: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: - case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: - return update_continuum(ptr); + case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: return update_continuum(ptr); case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: - case MEMCACHED_DISTRIBUTION_MODULA: - break; + case MEMCACHED_DISTRIBUTION_MODULA: break; - case MEMCACHED_DISTRIBUTION_RANDOM: - srandom((uint32_t) time(NULL)); - break; + case MEMCACHED_DISTRIBUTION_RANDOM: srandom((uint32_t) time(NULL)); break; case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: - default: - assert_msg(0, "Invalid distribution type passed to run_distribution()"); + default: assert_msg(0, "Invalid distribution type passed to run_distribution()"); } return MEMCACHED_SUCCESS; } -static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment) -{ +static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment) { unsigned char results[16]; - libhashkit_md5_signature((unsigned char*)key, key_length, results); + libhashkit_md5_signature((unsigned char *) key, key_length, results); - return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24) - | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16) - | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8) - | (results[0 + alignment * 4] & 0xFF); + return ((uint32_t)(results[3 + alignment * 4] & 0xFF) << 24) + | ((uint32_t)(results[2 + alignment * 4] & 0xFF) << 16) + | ((uint32_t)(results[1 + alignment * 4] & 0xFF) << 8) | (results[0 + alignment * 4] & 0xFF); } -static int continuum_item_cmp(const void *t1, const void *t2) -{ - memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1; - memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2; +static int continuum_item_cmp(const void *t1, const void *t2) { + memcached_continuum_item_st *ct1 = (memcached_continuum_item_st *) t1; + memcached_continuum_item_st *ct2 = (memcached_continuum_item_st *) t2; /* Why 153? Hmmm... */ WATCHPOINT_ASSERT(ct1->value != 153); - if (ct1->value == ct2->value) - { - if (ct1->index == ct2->index) - { + if (ct1->value == ct2->value) { + if (ct1->index == ct2->index) { return 0; - } - else if (ct1->index > ct2->index) - { + } else if (ct1->index > ct2->index) { return 1; - } - else - { + } else { return -1; } - } - else if (ct1->value > ct2->value) - { + } else if (ct1->value > ct2->value) { return 1; - } - else - { + } else { return -1; } } -static memcached_return_t update_continuum(Memcached *ptr) -{ - uint32_t continuum_index= 0; - uint32_t pointer_counter= 0; - uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER; - uint32_t pointer_per_hash= 1; - uint32_t live_servers= 0; +static memcached_return_t update_continuum(Memcached *ptr) { + uint32_t continuum_index = 0; + uint32_t pointer_counter = 0; + uint32_t pointer_per_server = MEMCACHED_POINTS_PER_SERVER; + uint32_t pointer_per_hash = 1; + uint32_t live_servers = 0; struct timeval now; - if (gettimeofday(&now, NULL)) - { + if (gettimeofday(&now, NULL)) { return memcached_set_errno(*ptr, errno, MEMCACHED_AT); } - memcached_instance_st* list= memcached_instance_list(ptr); + memcached_instance_st *list = memcached_instance_list(ptr); /* count live servers (those without a retry delay set) */ - bool is_auto_ejecting= _is_auto_eject_host(ptr); - if (is_auto_ejecting) - { - live_servers= 0; - ptr->ketama.next_distribution_rebuild= 0; - for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index) - { - if (list[host_index].next_retry <= now.tv_sec) - { + bool is_auto_ejecting = _is_auto_eject_host(ptr); + if (is_auto_ejecting) { + live_servers = 0; + ptr->ketama.next_distribution_rebuild = 0; + for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { + if (list[host_index].next_retry <= now.tv_sec) { live_servers++; - } - else - { - if (ptr->ketama.next_distribution_rebuild == 0 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild) + } else { + if (ptr->ketama.next_distribution_rebuild == 0 + or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild) { - ptr->ketama.next_distribution_rebuild= list[host_index].next_retry; + ptr->ketama.next_distribution_rebuild = list[host_index].next_retry; } } } - } - else - { - live_servers= memcached_server_count(ptr); + } else { + live_servers = memcached_server_count(ptr); } - if (live_servers == 0) - { + if (live_servers == 0) { return MEMCACHED_SUCCESS; } - uint32_t points_per_server = (uint32_t) (memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER); + uint32_t points_per_server = + (uint32_t)(memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA + : MEMCACHED_POINTS_PER_SERVER); uint32_t continuum_limit = live_servers * points_per_server; uint32_t continuum_extra = MEMCACHED_CONTINUUM_ADDITION * points_per_server; - if (continuum_limit > ptr->ketama.continuum_count) - { + if (continuum_limit > ptr->ketama.continuum_count) { memcached_continuum_item_st *new_ptr; - new_ptr= libmemcached_xrealloc(ptr, ptr->ketama.continuum, continuum_limit + continuum_extra, memcached_continuum_item_st); + new_ptr = libmemcached_xrealloc(ptr, ptr->ketama.continuum, continuum_limit + continuum_extra, + memcached_continuum_item_st); - if (new_ptr == 0) - { + if (new_ptr == 0) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - ptr->ketama.continuum= new_ptr; - ptr->ketama.continuum_count= continuum_limit + continuum_extra; + ptr->ketama.continuum = new_ptr; + ptr->ketama.continuum_count = continuum_limit + continuum_extra; } assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum"); - uint64_t total_weight= 0; - if (memcached_is_weighted_ketama(ptr)) - { - for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) - { - if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec) - { + uint64_t total_weight = 0; + if (memcached_is_weighted_ketama(ptr)) { + for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { + if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec) { total_weight += list[host_index].weight; } } } - for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index) - { - if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec) - { + for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { + if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec) { continue; } - if (memcached_is_weighted_ketama(ptr)) - { - float pct= (float)list[host_index].weight / (float)total_weight; - pointer_per_server= (uint32_t) ((::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001F))) * 4); - pointer_per_hash= 4; - if (0 && DEBUG) - { - printf("ketama_weighted:%s|%d|%llu|%u\n", - list[host_index]._hostname, - list[host_index].port(), - (unsigned long long)list[host_index].weight, - pointer_per_server); - } + if (memcached_is_weighted_ketama(ptr)) { + float pct = (float) list[host_index].weight / (float) total_weight; + pointer_per_server = (uint32_t)( + (::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float) live_servers + + 0.0000000001F))) + * 4); + pointer_per_hash = 4; + if (0 && DEBUG) { + printf("ketama_weighted:%s|%d|%llu|%u\n", list[host_index]._hostname, + list[host_index].port(), (unsigned long long) list[host_index].weight, + pointer_per_server); + } } - - if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) - { - for (uint32_t pointer_index= 0; - pointer_index < pointer_per_server / pointer_per_hash; + if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) { + for (uint32_t pointer_index = 0; pointer_index < pointer_per_server / pointer_per_hash; pointer_index++) { - char sort_host[1 +MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 + MEMCACHED_NI_MAXSERV ]= ""; + char sort_host[1 + MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1 + + MEMCACHED_NI_MAXSERV] = ""; int sort_host_length; // Spymemcached ketema key format is: hostname/ip:port-index // If hostname is not available then: /ip:port-index - sort_host_length= snprintf(sort_host, sizeof(sort_host), - "/%s:%u-%u", - list[host_index]._hostname, - (uint32_t)list[host_index].port(), - pointer_index); + sort_host_length = + snprintf(sort_host, sizeof(sort_host), "/%s:%u-%u", list[host_index]._hostname, + (uint32_t) list[host_index].port(), pointer_index); - if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) - { + if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) { return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(sizeof(sort_host))")); } - if (0 && DEBUG) - { + if (0 && DEBUG) { fprintf(stdout, "update_continuum: key is %s\n", sort_host); } - if (memcached_is_weighted_ketama(ptr)) - { - for (uint32_t x= 0; x < pointer_per_hash; x++) - { - uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - ptr->ketama.continuum[continuum_index].index= host_index; - ptr->ketama.continuum[continuum_index++].value= value; + if (memcached_is_weighted_ketama(ptr)) { + for (uint32_t x = 0; x < pointer_per_hash; x++) { + uint32_t value = ketama_server_hash(sort_host, (size_t) sort_host_length, x); + ptr->ketama.continuum[continuum_index].index = host_index; + ptr->ketama.continuum[continuum_index++].value = value; } - } - else - { - uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length); - ptr->ketama.continuum[continuum_index].index= host_index; - ptr->ketama.continuum[continuum_index++].value= value; + } else { + uint32_t value = hashkit_digest(&ptr->hashkit, sort_host, (size_t) sort_host_length); + ptr->ketama.continuum[continuum_index].index = host_index; + ptr->ketama.continuum[continuum_index++].value = value; } } - } - else - { - for (uint32_t pointer_index= 1; - pointer_index <= pointer_per_server / pointer_per_hash; + } else { + for (uint32_t pointer_index = 1; pointer_index <= pointer_per_server / pointer_per_hash; pointer_index++) { - char sort_host[MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 +MEMCACHED_NI_MAXSERV]= ""; + char sort_host[MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1 + MEMCACHED_NI_MAXSERV] = + ""; int sort_host_length; - if (list[host_index].port() == MEMCACHED_DEFAULT_PORT) - { - sort_host_length= snprintf(sort_host, sizeof(sort_host), - "%s-%u", - list[host_index]._hostname, - pointer_index - 1); - } - else - { - sort_host_length= snprintf(sort_host, sizeof(sort_host), - "%s:%u-%u", - list[host_index]._hostname, - (uint32_t)list[host_index].port(), - pointer_index - 1); + if (list[host_index].port() == MEMCACHED_DEFAULT_PORT) { + sort_host_length = snprintf(sort_host, sizeof(sort_host), "%s-%u", + list[host_index]._hostname, pointer_index - 1); + } else { + sort_host_length = + snprintf(sort_host, sizeof(sort_host), "%s:%u-%u", list[host_index]._hostname, + (uint32_t) list[host_index].port(), pointer_index - 1); } - if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) - { + if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) { return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(sizeof(sort_host)))")); } - if (memcached_is_weighted_ketama(ptr)) - { - for (uint32_t x = 0; x < pointer_per_hash; x++) - { - uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - ptr->ketama.continuum[continuum_index].index= host_index; - ptr->ketama.continuum[continuum_index++].value= value; + if (memcached_is_weighted_ketama(ptr)) { + for (uint32_t x = 0; x < pointer_per_hash; x++) { + uint32_t value = ketama_server_hash(sort_host, (size_t) sort_host_length, x); + ptr->ketama.continuum[continuum_index].index = host_index; + ptr->ketama.continuum[continuum_index++].value = value; } - } - else - { - uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length); - ptr->ketama.continuum[continuum_index].index= host_index; - ptr->ketama.continuum[continuum_index++].value= value; + } else { + uint32_t value = hashkit_digest(&ptr->hashkit, sort_host, (size_t) sort_host_length); + ptr->ketama.continuum[continuum_index].index = host_index; + ptr->ketama.continuum[continuum_index++].value = value; } } } - pointer_counter+= pointer_per_server; + pointer_counter += pointer_per_server; } assert_msg(ptr, "Programmer Error, no valid ptr"); assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum"); - assert_msg(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE, "invalid size information being given to qsort()"); - ptr->ketama.continuum_points_counter= pointer_counter; - qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp); - - if (DEBUG) - { - for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++) + assert_msg(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE, + "invalid size information being given to qsort()"); + ptr->ketama.continuum_points_counter = pointer_counter; + qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, + sizeof(memcached_continuum_item_st), continuum_item_cmp); + + if (DEBUG) { + for (uint32_t pointer_index = 0; memcached_server_count(ptr) + && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); + pointer_index++) { - WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value); + WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value + <= ptr->ketama.continuum[pointer_index + 1].value); } } return MEMCACHED_SUCCESS; } -static memcached_return_t server_add(Memcached *memc, - const memcached_string_t& hostname, - in_port_t port, - uint32_t weight, - memcached_connection_t type) -{ +static memcached_return_t server_add(Memcached *memc, const memcached_string_t &hostname, + in_port_t port, uint32_t weight, memcached_connection_t type) { assert_msg(memc, "Programmer mistake, somehow server_add() was passed a NULL memcached_st"); - if (memc->number_of_hosts) - { + if (memc->number_of_hosts) { assert(memcached_instance_list(memc)); } - if (memcached_instance_list(memc)) - { + if (memcached_instance_list(memc)) { assert(memc->number_of_hosts); } - uint32_t host_list_size= memc->number_of_hosts +1; - memcached_instance_st* new_host_list= libmemcached_xrealloc(memc, memcached_instance_list(memc), host_list_size, memcached_instance_st); + uint32_t host_list_size = memc->number_of_hosts + 1; + memcached_instance_st *new_host_list = libmemcached_xrealloc( + memc, memcached_instance_list(memc), host_list_size, memcached_instance_st); - if (new_host_list == NULL) - { + if (new_host_list == NULL) { return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } @@ -389,17 +302,15 @@ static memcached_return_t server_add(Memcached *memc, assert(memc->number_of_hosts == host_list_size); /* TODO: Check return type */ - memcached_instance_st* instance= memcached_instance_fetch(memc, memcached_server_count(memc) -1); + memcached_instance_st *instance = + memcached_instance_fetch(memc, memcached_server_count(memc) - 1); - if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL) - { + if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL) { return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - if (weight > 1) - { - if (memcached_is_consistent_distribution(memc)) - { + if (weight > 1) { + if (memcached_is_consistent_distribution(memc)) { memcached_set_weighted_ketama(memc, true); } } @@ -407,54 +318,48 @@ static memcached_return_t server_add(Memcached *memc, return run_distribution(memc); } - -memcached_return_t memcached_server_push(memcached_st *shell, const memcached_server_list_st list) -{ - if (list == NULL) - { +memcached_return_t memcached_server_push(memcached_st *shell, const memcached_server_list_st list) { + if (list == NULL) { return MEMCACHED_SUCCESS; } - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - uint32_t original_host_size= memcached_server_count(ptr); - uint32_t count= memcached_server_list_count(list); - uint32_t host_list_size= count +original_host_size; + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + uint32_t original_host_size = memcached_server_count(ptr); + uint32_t count = memcached_server_list_count(list); + uint32_t host_list_size = count + original_host_size; - memcached_instance_st* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st); + memcached_instance_st *new_host_list = libmemcached_xrealloc( + ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st); - if (new_host_list == NULL) - { + if (new_host_list == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } memcached_instance_set(ptr, new_host_list, host_list_size); - ptr->state.is_parsing= true; - for (uint32_t x= 0; x < count; ++x, ++original_host_size) - { + ptr->state.is_parsing = true; + for (uint32_t x = 0; x < count; ++x, ++original_host_size) { WATCHPOINT_ASSERT(list[x].hostname[0] != 0); // We have extended the array, and now we will find it, and use it. - memcached_instance_st* instance= memcached_instance_fetch(ptr, original_host_size); + memcached_instance_st *instance = memcached_instance_fetch(ptr, original_host_size); WATCHPOINT_ASSERT(instance); - memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) }; - if (__instance_create_with(ptr, instance, - hostname, - list[x].port, list[x].weight, list[x].type) == NULL) + memcached_string_t hostname = {memcached_string_make_from_cstr(list[x].hostname)}; + if (__instance_create_with(ptr, instance, hostname, list[x].port, list[x].weight, + list[x].type) + == NULL) { - ptr->state.is_parsing= false; + ptr->state.is_parsing = false; return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - if (list[x].weight > 1) - { + if (list[x].weight > 1) { memcached_set_weighted_ketama(ptr, true); } } - ptr->state.is_parsing= false; + ptr->state.is_parsing = false; return run_distribution(ptr); } @@ -462,73 +367,67 @@ memcached_return_t memcached_server_push(memcached_st *shell, const memcached_se return MEMCACHED_INVALID_ARGUMENTS; } -memcached_return_t memcached_instance_push(memcached_st *ptr, const struct memcached_instance_st* list, uint32_t number_of_hosts) -{ - if (list == NULL) - { +memcached_return_t memcached_instance_push(memcached_st *ptr, + const struct memcached_instance_st *list, + uint32_t number_of_hosts) { + if (list == NULL) { return MEMCACHED_SUCCESS; } - uint32_t original_host_size= memcached_server_count(ptr); - uint32_t host_list_size= number_of_hosts +original_host_size; - memcached_instance_st* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st); + uint32_t original_host_size = memcached_server_count(ptr); + uint32_t host_list_size = number_of_hosts + original_host_size; + memcached_instance_st *new_host_list = libmemcached_xrealloc( + ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st); - if (new_host_list == NULL) - { + if (new_host_list == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } memcached_instance_set(ptr, new_host_list, host_list_size); // We don't bother with lookups for this operation - ptr->state.is_parsing= true; + ptr->state.is_parsing = true; // We use original_host_size since size will now point to the first new // instance allocated. - for (uint32_t x= 0; x < number_of_hosts; ++x, ++original_host_size) - { + for (uint32_t x = 0; x < number_of_hosts; ++x, ++original_host_size) { WATCHPOINT_ASSERT(list[x]._hostname[0] != 0); // We have extended the array, and now we will find it, and use it. - memcached_instance_st* instance= memcached_instance_fetch(ptr, original_host_size); + memcached_instance_st *instance = memcached_instance_fetch(ptr, original_host_size); WATCHPOINT_ASSERT(instance); - memcached_string_t hostname= { memcached_string_make_from_cstr(list[x]._hostname) }; - if (__instance_create_with(ptr, instance, - hostname, - list[x].port(), list[x].weight, list[x].type) == NULL) + memcached_string_t hostname = {memcached_string_make_from_cstr(list[x]._hostname)}; + if (__instance_create_with(ptr, instance, hostname, list[x].port(), list[x].weight, + list[x].type) + == NULL) { - ptr->state.is_parsing= false; + ptr->state.is_parsing = false; return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - if (list[x].weight > 1) - { + if (list[x].weight > 1) { memcached_set_weighted_ketama(ptr, true); } } - ptr->state.is_parsing= false; + ptr->state.is_parsing = false; return run_distribution(ptr); } -memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr, - const char *filename) -{ +memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr, const char *filename) { return memcached_server_add_unix_socket_with_weight(ptr, filename, 0); } memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *shell, const char *filename, - uint32_t weight) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr) - { - memcached_string_t _filename= { memcached_string_make_from_cstr(filename) }; - if (memcached_is_valid_filename(_filename) == false) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided")); + uint32_t weight) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr) { + memcached_string_t _filename = {memcached_string_make_from_cstr(filename)}; + if (memcached_is_valid_filename(_filename) == false) { + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid filename for socket provided")); } return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET); @@ -537,82 +436,63 @@ memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *sh return MEMCACHED_FAILURE; } -memcached_return_t memcached_server_add_udp(memcached_st *ptr, - const char *hostname, - in_port_t port) -{ +memcached_return_t memcached_server_add_udp(memcached_st *ptr, const char *hostname, + in_port_t port) { return memcached_server_add_udp_with_weight(ptr, hostname, port, 0); } -memcached_return_t memcached_server_add_udp_with_weight(memcached_st *shell, - const char *, - in_port_t, - uint32_t) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { +memcached_return_t memcached_server_add_udp_with_weight(memcached_st *shell, const char *, + in_port_t, uint32_t) { + Memcached *self = memcached2Memcached(shell); + if (self) { return memcached_set_error(*self, MEMCACHED_DEPRECATED, MEMCACHED_AT); } return MEMCACHED_INVALID_ARGUMENTS; } -memcached_return_t memcached_server_add(memcached_st *shell, - const char *hostname, - in_port_t port) -{ +memcached_return_t memcached_server_add(memcached_st *shell, const char *hostname, in_port_t port) { return memcached_server_add_with_weight(shell, hostname, port, 0); } -memcached_return_t memcached_server_add_with_weight(memcached_st *shell, - const char *hostname, - in_port_t port, - uint32_t weight) -{ - Memcached* ptr= memcached2Memcached(shell); - if (ptr == NULL) - { +memcached_return_t memcached_server_add_with_weight(memcached_st *shell, const char *hostname, + in_port_t port, uint32_t weight) { + Memcached *ptr = memcached2Memcached(shell); + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (port == 0) - { - port= MEMCACHED_DEFAULT_PORT; + if (port == 0) { + port = MEMCACHED_DEFAULT_PORT; } - size_t hostname_length= hostname ? strlen(hostname) : 0; - if (hostname_length == 0) - { - hostname= "localhost"; - hostname_length= memcached_literal_param_size("localhost"); + size_t hostname_length = hostname ? strlen(hostname) : 0; + if (hostname_length == 0) { + hostname = "localhost"; + hostname_length = memcached_literal_param_size("localhost"); } - memcached_string_t _hostname= { hostname, hostname_length }; + memcached_string_t _hostname = {hostname, hostname_length}; - if (memcached_is_valid_servername(_hostname) == false) - { - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); + if (memcached_is_valid_servername(_hostname) == false) { + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid hostname provided")); } - return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP); + return server_add(ptr, _hostname, port, weight, + _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET + : MEMCACHED_CONNECTION_TCP); } -memcached_return_t memcached_server_add_parsed(memcached_st *ptr, - const char *hostname, - size_t hostname_length, - in_port_t port, - uint32_t weight) -{ - char buffer[MEMCACHED_NI_MAXHOST]= { 0 }; +memcached_return_t memcached_server_add_parsed(memcached_st *ptr, const char *hostname, + size_t hostname_length, in_port_t port, + uint32_t weight) { + char buffer[MEMCACHED_NI_MAXHOST] = {0}; memcpy(buffer, hostname, hostname_length); - buffer[hostname_length]= 0; + buffer[hostname_length] = 0; - memcached_string_t _hostname= { buffer, hostname_length }; + memcached_string_t _hostname = {buffer, hostname_length}; - return server_add(ptr, _hostname, - port, - weight, - MEMCACHED_CONNECTION_TCP); + return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_TCP); } diff --git a/src/libmemcached/initialize_query.cc b/src/libmemcached/initialize_query.cc index 34daa4e9..ad4dfc15 100644 --- a/src/libmemcached/initialize_query.cc +++ b/src/libmemcached/initialize_query.cc @@ -1,60 +1,34 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -memcached_return_t initialize_query(Memcached *self, bool increment_query_id) -{ - if (self == NULL) - { +memcached_return_t initialize_query(Memcached *self, bool increment_query_id) { + if (self == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (increment_query_id) - { + if (increment_query_id) { self->query_id++; } - if (self->state.is_time_for_rebuild) - { + if (self->state.is_time_for_rebuild) { memcached_reset(self); } - if (memcached_server_count(self) == 0) - { + if (memcached_server_count(self) == 0) { return memcached_set_error(*self, MEMCACHED_NO_SERVERS, MEMCACHED_AT); } @@ -64,15 +38,12 @@ memcached_return_t initialize_query(Memcached *self, bool increment_query_id) return MEMCACHED_SUCCESS; } -memcached_return_t initialize_const_query(const Memcached *self) -{ - if (self == NULL) - { +memcached_return_t initialize_const_query(const Memcached *self) { + if (self == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (memcached_server_count(self) == 0) - { + if (memcached_server_count(self) == 0) { return MEMCACHED_NO_SERVERS; } diff --git a/src/libmemcached/initialize_query.h b/src/libmemcached/initialize_query.h index 105283c0..96652338 100644 --- a/src/libmemcached/initialize_query.h +++ b/src/libmemcached/initialize_query.h @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/instance.cc b/src/libmemcached/instance.cc index 751188c7..266eccc7 100644 --- a/src/libmemcached/instance.cc +++ b/src/libmemcached/instance.cc @@ -1,170 +1,129 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static inline void _server_init(memcached_instance_st* self, Memcached *root, - const memcached_string_t& hostname, - in_port_t port, - uint32_t weight, memcached_connection_t type) -{ - self->options.is_shutting_down= false; - self->options.is_dead= false; - self->options.ready= false; - self->_events= 0; - self->_revents= 0; - self->cursor_active_= 0; - self->port_= port; - self->fd= INVALID_SOCKET; - self->io_bytes_sent= 0; - self->request_id= 0; - self->server_failure_counter= 0; - self->server_failure_counter_query_id= 0; - self->server_timeout_counter= 0; - self->server_timeout_counter_query_id= 0; - self->weight= weight ? weight : 1; // 1 is the default weight value - self->io_wait_count.read= 0; - self->io_wait_count.write= 0; - self->io_wait_count.timeouts= 0; - self->io_wait_count._bytes_read= 0; - self->major_version= UINT8_MAX; - self->micro_version= UINT8_MAX; - self->minor_version= UINT8_MAX; - self->type= type; - self->error_messages= NULL; - self->read_ptr= self->read_buffer; - self->read_buffer_length= 0; - self->write_buffer_offset= 0; - self->address_info= NULL; - self->address_info_next= NULL; - - self->state= MEMCACHED_SERVER_STATE_NEW; - self->next_retry= 0; - - self->root= root; - if (root) - { - self->version= ++root->server_info.version; - } - else - { - self->version= UINT_MAX; +static inline void _server_init(memcached_instance_st *self, Memcached *root, + const memcached_string_t &hostname, in_port_t port, uint32_t weight, + memcached_connection_t type) { + self->options.is_shutting_down = false; + self->options.is_dead = false; + self->options.ready = false; + self->_events = 0; + self->_revents = 0; + self->cursor_active_ = 0; + self->port_ = port; + self->fd = INVALID_SOCKET; + self->io_bytes_sent = 0; + self->request_id = 0; + self->server_failure_counter = 0; + self->server_failure_counter_query_id = 0; + self->server_timeout_counter = 0; + self->server_timeout_counter_query_id = 0; + self->weight = weight ? weight : 1; // 1 is the default weight value + self->io_wait_count.read = 0; + self->io_wait_count.write = 0; + self->io_wait_count.timeouts = 0; + self->io_wait_count._bytes_read = 0; + self->major_version = UINT8_MAX; + self->micro_version = UINT8_MAX; + self->minor_version = UINT8_MAX; + self->type = type; + self->error_messages = NULL; + self->read_ptr = self->read_buffer; + self->read_buffer_length = 0; + self->write_buffer_offset = 0; + self->address_info = NULL; + self->address_info_next = NULL; + + self->state = MEMCACHED_SERVER_STATE_NEW; + self->next_retry = 0; + + self->root = root; + if (root) { + self->version = ++root->server_info.version; + } else { + self->version = UINT_MAX; } - self->limit_maxbytes= 0; + self->limit_maxbytes = 0; self->hostname(hostname); } -static memcached_instance_st* _server_create(memcached_instance_st* self, const memcached_st *memc) -{ - if (self == NULL) - { - self= libmemcached_xmalloc(memc, memcached_instance_st); +static memcached_instance_st *_server_create(memcached_instance_st *self, + const memcached_st *memc) { + if (self == NULL) { + self = libmemcached_xmalloc(memc, memcached_instance_st); - if (self == NULL) - { + if (self == NULL) { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ } - self->options.is_allocated= true; - } - else - { - self->options.is_allocated= false; + self->options.is_allocated = true; + } else { + self->options.is_allocated = false; } - self->options.is_initialized= true; + self->options.is_initialized = true; return self; } -void memcached_instance_st::events(short arg) -{ - if ((_events | arg) == _events) - { +void memcached_instance_st::events(short arg) { + if ((_events | arg) == _events) { return; } - _events|= arg; + _events |= arg; } -void memcached_instance_st::revents(short arg) -{ - if (arg) - { - options.ready= true; +void memcached_instance_st::revents(short arg) { + if (arg) { + options.ready = true; } - _revents= arg; - _events&= short(~arg); + _revents = arg; + _events &= short(~arg); } -memcached_instance_st* __instance_create_with(memcached_st *memc, - memcached_instance_st* self, - const memcached_string_t& hostname, - const in_port_t port, - uint32_t weight, - const memcached_connection_t type) -{ - if (memcached_is_valid_servername(hostname) == false) - { - memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); +memcached_instance_st *__instance_create_with(memcached_st *memc, memcached_instance_st *self, + const memcached_string_t &hostname, + const in_port_t port, uint32_t weight, + const memcached_connection_t type) { + if (memcached_is_valid_servername(hostname) == false) { + memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid hostname provided")); return NULL; } - self= _server_create(self, memc); + self = _server_create(self, memc); - if (self == NULL) - { + if (self == NULL) { return NULL; } _server_init(self, const_cast(memc), hostname, port, weight, type); - if (memc and memcached_is_udp(memc)) - { - self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH; + if (memc and memcached_is_udp(memc)) { + self->write_buffer_offset = UDP_DATAGRAM_HEADER_LENGTH; memcached_io_init_udp_header(self, 0); } return self; } -void __instance_free(memcached_instance_st* self) -{ +void __instance_free(memcached_instance_st *self) { memcached_quit_server(self, false); self->clear_addrinfo(); @@ -172,47 +131,36 @@ void __instance_free(memcached_instance_st* self) memcached_error_free(*self); - if (memcached_is_allocated(self)) - { + if (memcached_is_allocated(self)) { libmemcached_free(self->root, self); - } - else - { - self->options.is_initialized= false; + } else { + self->options.is_initialized = false; } } -void memcached_instance_free(memcached_instance_st* self) -{ - if (self) - { +void memcached_instance_free(memcached_instance_st *self) { + if (self) { __instance_free(self); } } -memcached_return_t memcached_server_cursor(const memcached_st* shell, - const memcached_server_fn *callback, - void *context, - uint32_t number_of_callbacks) -{ - const Memcached* memc= memcached2Memcached(shell); +memcached_return_t memcached_server_cursor(const memcached_st *shell, + const memcached_server_fn *callback, void *context, + uint32_t number_of_callbacks) { + const Memcached *memc = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_const_query(memc))) - { + if (memcached_failed(rc = initialize_const_query(memc))) { return rc; } - size_t errors= 0; - for (uint32_t x= 0; x < memcached_instance_list_count(memc); x++) - { - memcached_instance_st* instance= memcached_instance_by_position(memc, x); + size_t errors = 0; + for (uint32_t x = 0; x < memcached_instance_list_count(memc); x++) { + memcached_instance_st *instance = memcached_instance_by_position(memc, x); - for (uint32_t y= 0; y < number_of_callbacks; y++) - { - memcached_return_t ret= (*callback[y])(memc, instance, context); + for (uint32_t y = 0; y < number_of_callbacks; y++) { + memcached_return_t ret = (*callback[y])(memc, instance, context); - if (memcached_failed(ret)) - { + if (memcached_failed(ret)) { errors++; continue; } @@ -223,139 +171,112 @@ memcached_return_t memcached_server_cursor(const memcached_st* shell, } memcached_return_t memcached_server_execute(memcached_st *memc, - memcached_server_execute_fn callback, - void *context) -{ - if (callback == NULL) - { + memcached_server_execute_fn callback, void *context) { + if (callback == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - bool some_errors= false;; - for (uint32_t x= 0; x < memcached_instance_list_count(memc); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + bool some_errors = false; + ; + for (uint32_t x = 0; x < memcached_instance_list_count(memc); x++) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); - memcached_return_t rc= (*callback)(memc, instance, context); - if (rc == MEMCACHED_INVALID_ARGUMENTS) - { + memcached_return_t rc = (*callback)(memc, instance, context); + if (rc == MEMCACHED_INVALID_ARGUMENTS) { return rc; - } - else if (memcached_fatal(rc)) - { - some_errors= true; + } else if (memcached_fatal(rc)) { + some_errors = true; } } - (void)some_errors; + (void) some_errors; return MEMCACHED_SUCCESS; } -const memcached_instance_st * memcached_server_by_key(memcached_st *shell, - const char *key, - size_t key_length, - memcached_return_t *error) -{ - Memcached* memc= memcached2Memcached(shell); +const memcached_instance_st *memcached_server_by_key(memcached_st *shell, const char *key, + size_t key_length, memcached_return_t *error) { + Memcached *memc = memcached2Memcached(shell); memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - memcached_return_t rc; - if (memcached_failed(rc= initialize_const_query(memc))) - { - *error= rc; + if (memcached_failed(rc = initialize_const_query(memc))) { + *error = rc; return NULL; } - if (memcached_failed((memcached_key_test(*memc, (const char **)&key, &key_length, 1)))) - { - *error= memcached_last_error(memc); + if (memcached_failed((memcached_key_test(*memc, (const char **) &key, &key_length, 1)))) { + *error = memcached_last_error(memc); return NULL; } - uint32_t server_key= memcached_generate_hash(memc, key, key_length); + uint32_t server_key = memcached_generate_hash(memc, key, key_length); return memcached_instance_by_position(memc, server_key); } /* If we do not have a valid object to clone from, we toss an error. */ -static memcached_instance_st* memcached_instance_clone(memcached_instance_st* source) -{ +static memcached_instance_st *memcached_instance_clone(memcached_instance_st *source) { /* We just do a normal create if source is missing */ - if (source == NULL) - { + if (source == NULL) { return NULL; } - memcached_string_t hostname_= { memcached_string_make_from_cstr(source->hostname()) }; - return __instance_create_with(source->root, - NULL, - hostname_, - source->port(), source->weight, + memcached_string_t hostname_ = {memcached_string_make_from_cstr(source->hostname())}; + return __instance_create_with(source->root, NULL, hostname_, source->port(), source->weight, source->type); } -void set_last_disconnected_host(memcached_instance_st* self) -{ +void set_last_disconnected_host(memcached_instance_st *self) { assert(self->root); - if (self->root) - { - if (memcached_server_get_last_disconnect(self->root) and - memcached_server_get_last_disconnect(self->root)->version == self->version) + if (self->root) { + if (memcached_server_get_last_disconnect(self->root) + and memcached_server_get_last_disconnect(self->root)->version == self->version) { return; } // const_cast - memcached_st *root= (memcached_st *)self->root; + memcached_st *root = (memcached_st *) self->root; - memcached_instance_free((memcached_instance_st*)(root->last_disconnected_server)); + memcached_instance_free((memcached_instance_st *) (root->last_disconnected_server)); // We set is_parsing so that no lookup happens - root->state.is_parsing= true; - root->last_disconnected_server= memcached_instance_clone(self); - root->state.is_parsing= false; + root->state.is_parsing = true; + root->last_disconnected_server = memcached_instance_clone(self); + root->state.is_parsing = false; - ((memcached_instance_st*)memcached_server_get_last_disconnect(root))->version= self->version; + ((memcached_instance_st *) memcached_server_get_last_disconnect(root))->version = self->version; } } -const memcached_instance_st * memcached_server_get_last_disconnect(const memcached_st *shell) -{ - const Memcached* self= memcached2Memcached(shell); - if (self) - { - return (const memcached_instance_st *)self->last_disconnected_server; +const memcached_instance_st *memcached_server_get_last_disconnect(const memcached_st *shell) { + const Memcached *self = memcached2Memcached(shell); + if (self) { + return (const memcached_instance_st *) self->last_disconnected_server; } return 0; } -void memcached_instance_next_retry(const memcached_instance_st * self, const time_t absolute_time) -{ +void memcached_instance_next_retry(const memcached_instance_st *self, const time_t absolute_time) { WATCHPOINT_ASSERT(self); - if (self) - { - ((memcached_instance_st*)self)->next_retry= absolute_time; + if (self) { + ((memcached_instance_st *) self)->next_retry = absolute_time; } } -bool memcached_instance_st::valid() const -{ - if (fd == INVALID_SOCKET) - { +bool memcached_instance_st::valid() const { + if (fd == INVALID_SOCKET) { return false; } return true; } -bool memcached_instance_st::is_shutting_down() const -{ +bool memcached_instance_st::is_shutting_down() const { return options.is_shutting_down; } diff --git a/src/libmemcached/instance.hpp b/src/libmemcached/instance.hpp index 63bf0835..57f68786 100644 --- a/src/libmemcached/instance.hpp +++ b/src/libmemcached/instance.hpp @@ -1,89 +1,55 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifndef WIN32 -# ifdef HAVE_NETDB_H -# include -# endif +# ifdef HAVE_NETDB_H +# include +# endif #endif #ifdef NI_MAXHOST -# define MEMCACHED_NI_MAXHOST NI_MAXHOST +# define MEMCACHED_NI_MAXHOST NI_MAXHOST #else -# define MEMCACHED_NI_MAXHOST 1025 +# define MEMCACHED_NI_MAXHOST 1025 #endif #ifdef NI_MAXSERV -# define MEMCACHED_NI_MAXSERV NI_MAXSERV +# define MEMCACHED_NI_MAXSERV NI_MAXSERV #else -# define MEMCACHED_NI_MAXSERV 32 +# define MEMCACHED_NI_MAXSERV 32 #endif #include "libmemcached/string.hpp" // @todo Complete class transformation struct memcached_instance_st { - in_port_t port() const - { - return port_; - } + in_port_t port() const { return port_; } - void port(in_port_t arg) - { - port_= arg; - } + void port(in_port_t arg) { port_ = arg; } - void mark_server_as_clean() - { - server_failure_counter= 0; - server_timeout_counter= 0; - next_retry= 0; + void mark_server_as_clean() { + server_failure_counter = 0; + server_timeout_counter = 0; + next_retry = 0; } - void disable() - { - } + void disable() {} - void enable() - { - } + void enable() {} bool valid() const; @@ -93,10 +59,7 @@ struct memcached_instance_st { void close_socket(); void reset_socket(); - uint32_t response_count() const - { - return cursor_active_; - } + uint32_t response_count() const { return cursor_active_; } struct { bool is_allocated; @@ -109,32 +72,19 @@ struct memcached_instance_st { short _events; short _revents; - short events(void) - { - return _events; - } + short events(void) { return _events; } - short revents(void) - { - return _revents; - } + short revents(void) { return _revents; } - const char* hostname() - { - return _hostname; - } + const char *hostname() { return _hostname; } - void hostname(const memcached_string_t& hostname_) - { - if (hostname_.size) - { + void hostname(const memcached_string_t &hostname_) { + if (hostname_.size) { memcpy(_hostname, hostname_.c_str, hostname_.size); - _hostname[hostname_.size]= 0; - } - else - { + _hostname[hostname_.size] = 0; + } else { memcpy(_hostname, memcached_literal_param("localhost")); - _hostname[memcached_literal_param_size("localhost")]= 0; + _hostname[memcached_literal_param_size("localhost")] = 0; } } @@ -176,24 +126,21 @@ struct memcached_instance_st { char write_buffer[MEMCACHED_MAX_BUFFER]; char _hostname[MEMCACHED_NI_MAXHOST]; - void clear_addrinfo() - { - if (address_info) - { + void clear_addrinfo() { + if (address_info) { freeaddrinfo(address_info); - address_info= NULL; - address_info_next= NULL; + address_info = NULL; + address_info_next = NULL; } } }; -memcached_instance_st* __instance_create_with(memcached_st *memc, - memcached_instance_st* self, - const memcached_string_t& _hostname, - const in_port_t port, - uint32_t weight, +memcached_instance_st *__instance_create_with(memcached_st *memc, memcached_instance_st *self, + const memcached_string_t &_hostname, + const in_port_t port, uint32_t weight, const memcached_connection_t type); -memcached_return_t memcached_instance_push(memcached_st *ptr, const memcached_instance_st*, uint32_t); +memcached_return_t memcached_instance_push(memcached_st *ptr, const memcached_instance_st *, + uint32_t); void __instance_free(memcached_instance_st *); diff --git a/src/libmemcached/internal.h b/src/libmemcached/internal.h index 67ae67d5..ac7ccd31 100644 --- a/src/libmemcached/internal.h +++ b/src/libmemcached/internal.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/io.cc b/src/libmemcached/io.cc index 79007aaf..087ae606 100644 --- a/src/libmemcached/io.cc +++ b/src/libmemcached/io.cc @@ -1,59 +1,32 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #ifdef HAVE_SYS_SOCKET_H -# include +# include #endif -void initialize_binary_request(memcached_instance_st* server, protocol_binary_request_header& header) -{ +void initialize_binary_request(memcached_instance_st *server, + protocol_binary_request_header &header) { server->request_id++; - header.request.magic= PROTOCOL_BINARY_REQ; - header.request.opaque= htons(server->request_id); + header.request.magic = PROTOCOL_BINARY_REQ; + header.request.opaque = htons(server->request_id); } -enum memc_read_or_write { - MEM_READ, - MEM_WRITE -}; +enum memc_read_or_write { MEM_READ, MEM_WRITE }; /** * Try to fill the input buffer for a server with as much @@ -61,38 +34,29 @@ enum memc_read_or_write { * * @param instance the server to pack */ -static bool repack_input_buffer(memcached_instance_st* instance) -{ - if (instance->read_ptr != instance->read_buffer) - { +static bool repack_input_buffer(memcached_instance_st *instance) { + if (instance->read_ptr != instance->read_buffer) { /* Move all of the data to the beginning of the buffer so ** that we can fit more data into the buffer... - */ + */ memmove(instance->read_buffer, instance->read_ptr, instance->read_buffer_length); - instance->read_ptr= instance->read_buffer; + instance->read_ptr = instance->read_buffer; } /* There is room in the buffer, try to fill it! */ - if (instance->read_buffer_length != MEMCACHED_MAX_BUFFER) - { + if (instance->read_buffer_length != MEMCACHED_MAX_BUFFER) { do { /* Just try a single read to grab what's available */ ssize_t nr; - if ((nr= ::recv(instance->fd, - instance->read_ptr + instance->read_buffer_length, - MEMCACHED_MAX_BUFFER - instance->read_buffer_length, - MSG_NOSIGNAL)) <= 0) + if ((nr = ::recv(instance->fd, instance->read_ptr + instance->read_buffer_length, + MEMCACHED_MAX_BUFFER - instance->read_buffer_length, MSG_NOSIGNAL)) + <= 0) { - if (nr == 0) - { + if (nr == 0) { memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT); - } - else - { - switch (get_socket_errno()) - { - case EINTR: - continue; + } else { + switch (get_socket_errno()) { + case EINTR: continue; #if EWOULDBLOCK != EAGAIN case EWOULDBLOCK: @@ -103,16 +67,14 @@ static bool repack_input_buffer(memcached_instance_st* instance) #endif break; // No IO is fine, we can just move on - default: - memcached_set_errno(*instance, get_socket_errno(), MEMCACHED_AT); + default: memcached_set_errno(*instance, get_socket_errno(), MEMCACHED_AT); } } break; - } - else // We read data, append to our read buffer + } else // We read data, append to our read buffer { - instance->read_buffer_length+= size_t(nr); + instance->read_buffer_length += size_t(nr); return true; } @@ -132,35 +94,30 @@ static bool repack_input_buffer(memcached_instance_st* instance) * @param instance the server to star processing iput messages for * @return true if we processed anything, false otherwise */ -static bool process_input_buffer(memcached_instance_st* instance) -{ +static bool process_input_buffer(memcached_instance_st *instance) { /* ** We might be able to process some of the response messages if we ** have a callback set up - */ - if (instance->root->callbacks != NULL) - { + */ + if (instance->root->callbacks != NULL) { /* * We might have responses... try to read them out and fire * callbacks - */ - memcached_callback_st cb= *instance->root->callbacks; + */ + memcached_callback_st cb = *instance->root->callbacks; - memcached_set_processing_input((Memcached *)instance->root, true); + memcached_set_processing_input((Memcached *) instance->root, true); char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - Memcached *root= (Memcached *)instance->root; - memcached_return_t error= memcached_response(instance, buffer, sizeof(buffer), &root->result); + Memcached *root = (Memcached *) instance->root; + memcached_return_t error = memcached_response(instance, buffer, sizeof(buffer), &root->result); 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])(instance->root, &root->result, cb.context); - if (error != MEMCACHED_SUCCESS) - { + if (error == MEMCACHED_SUCCESS) { + for (unsigned int x = 0; x < cb.number_of_callback; x++) { + error = (*cb.callback[x])(instance->root, &root->result, cb.context); + if (error != MEMCACHED_SUCCESS) { break; } } @@ -174,9 +131,7 @@ static bool process_input_buffer(memcached_instance_st* instance) return false; } -static memcached_return_t io_wait(memcached_instance_st* instance, - const short events) -{ +static memcached_return_t io_wait(memcached_instance_st *instance, const short events) { /* ** We are going to block on write, but at least on Solaris we might block ** on write if we haven't read anything from our input buffer.. @@ -184,89 +139,81 @@ static memcached_return_t io_wait(memcached_instance_st* instance, ** application layer (just sending a lot of data etc) ** The test is moved down in the purge function to avoid duplication of ** the test. - */ - if (events & POLLOUT) - { - if (memcached_purge(instance) == false) - { + */ + if (events & POLLOUT) { + if (memcached_purge(instance) == false) { return MEMCACHED_FAILURE; } } struct pollfd fds; - fds.fd= instance->fd; - fds.events= events; - fds.revents= 0; + fds.fd = instance->fd; + fds.events = events; + fds.revents = 0; - if (fds.events & POLLOUT) /* write */ - { + if (fds.events & POLLOUT) /* write */ { instance->io_wait_count.write++; - } - else - { + } else { instance->io_wait_count.read++; } - if (instance->root->poll_timeout == 0) // Mimic 0 causes timeout behavior (not all platforms do this) + if (instance->root->poll_timeout + == 0) // Mimic 0 causes timeout behavior (not all platforms do this) { - return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("poll_timeout() was set to zero")); + return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_literal_param("poll_timeout() was set to zero")); } - size_t loop_max= 5; + size_t loop_max = 5; while (--loop_max) // While loop is for ERESTART or EINTR { - int active_fd= poll(&fds, 1, instance->root->poll_timeout); + int active_fd = poll(&fds, 1, instance->root->poll_timeout); - if (active_fd >= 1) - { - assert_msg(active_fd == 1 , "poll() returned an unexpected number of active file descriptors"); - if (fds.revents & POLLIN or fds.revents & POLLOUT) - { + if (active_fd >= 1) { + assert_msg(active_fd == 1, "poll() returned an unexpected number of active file descriptors"); + if (fds.revents & POLLIN or fds.revents & POLLOUT) { return MEMCACHED_SUCCESS; } - if (fds.revents & POLLHUP) - { - return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, + if (fds.revents & POLLHUP) { + return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, memcached_literal_param("poll() detected hang up")); } - if (fds.revents & POLLERR) - { - int local_errno= EINVAL; + if (fds.revents & POLLERR) { + int local_errno = EINVAL; int err; - socklen_t len= sizeof (err); - if (getsockopt(instance->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) - { + socklen_t len = sizeof(err); + if (getsockopt(instance->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0) { if (err == 0) // treat this as EINTR { continue; } - local_errno= err; + local_errno = err; } memcached_quit_server(instance, true); return memcached_set_errno(*instance, local_errno, MEMCACHED_AT, memcached_literal_param("poll() returned POLLHUP")); } - - return memcached_set_error(*instance, MEMCACHED_FAILURE, MEMCACHED_AT, memcached_literal_param("poll() returned a value that was not dealt with")); + + return memcached_set_error( + *instance, MEMCACHED_FAILURE, MEMCACHED_AT, + memcached_literal_param("poll() returned a value that was not dealt with")); } - if (active_fd == 0) - { - return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("No active_fd were found")); + if (active_fd == 0) { + return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, + memcached_literal_param("No active_fd were found")); } // Only an error should result in this code being called. - int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno - assert_msg(active_fd == -1 , "poll() returned an unexpected value"); - switch (local_errno) - { + int local_errno = get_socket_errno(); // We cache in case memcached_quit_server() modifies errno + assert_msg(active_fd == -1, "poll() returned an unexpected value"); + switch (local_errno) { #ifdef __linux case ERESTART: #endif - case EINTR: - continue; + case EINTR: continue; case EFAULT: case ENOMEM: @@ -274,7 +221,9 @@ static memcached_return_t io_wait(memcached_instance_st* instance, break; case EINVAL: - memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid")); + memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param( + "RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid")); break; default: @@ -286,36 +235,33 @@ static memcached_return_t io_wait(memcached_instance_st* instance, memcached_quit_server(instance, true); - if (memcached_has_error(instance)) - { + if (memcached_has_error(instance)) { return memcached_instance_error_return(instance); } - return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, - memcached_literal_param("number of attempts to call io_wait() failed")); + return memcached_set_error( + *instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, + memcached_literal_param("number of attempts to call io_wait() failed")); } -static bool io_flush(memcached_instance_st* instance, - const bool with_flush, - memcached_return_t& error) -{ +static bool io_flush(memcached_instance_st *instance, const bool with_flush, + memcached_return_t &error) { /* ** We might want to purge the input buffer if we haven't consumed ** any output yet... The test for the limits is the purge is inline ** in the purge function to avoid duplicating the logic.. - */ + */ { WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET); - if (memcached_purge(instance) == false) - { + if (memcached_purge(instance) == false) { return false; } } - char *local_write_ptr= instance->write_buffer; - size_t write_length= instance->write_buffer_offset; + char *local_write_ptr = instance->write_buffer; + size_t write_length = instance->write_buffer_offset; - error= MEMCACHED_SUCCESS; + error = MEMCACHED_SUCCESS; WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET); @@ -323,112 +269,95 @@ static bool io_flush(memcached_instance_st* instance, #if defined(DEBUG) if (write_length == MEMCACHED_MAX_BUFFER) WATCHPOINT_ASSERT(instance->write_buffer == local_write_ptr); - WATCHPOINT_ASSERT((instance->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length)); + WATCHPOINT_ASSERT((instance->write_buffer + MEMCACHED_MAX_BUFFER) + >= (local_write_ptr + write_length)); #endif - while (write_length) - { + while (write_length) { WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET); WATCHPOINT_ASSERT(write_length > 0); int flags; - if (with_flush) - { - flags= MSG_NOSIGNAL; - } - else - { - flags= MSG_NOSIGNAL|MSG_MORE; + if (with_flush) { + flags = MSG_NOSIGNAL; + } else { + flags = MSG_NOSIGNAL | MSG_MORE; } - ssize_t sent_length= ::send(instance->fd, local_write_ptr, write_length, flags); - int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno + ssize_t sent_length = ::send(instance->fd, local_write_ptr, write_length, flags); + int local_errno = get_socket_errno(); // We cache in case memcached_quit_server() modifies errno - if (sent_length == SOCKET_ERROR) - { + if (sent_length == SOCKET_ERROR) { #if 0 // @todo I should look at why we hit this bit of code hard frequently WATCHPOINT_ERRNO(get_socket_errno()); WATCHPOINT_NUMBER(get_socket_errno()); #endif - switch (get_socket_errno()) - { - case ENOBUFS: - continue; + switch (get_socket_errno()) { + case ENOBUFS: continue; #if EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif - case EAGAIN: - { - /* - * We may be blocked on write because the input buffer - * is full. Let's check if we have room in our input - * buffer for more data and retry the write before - * waiting.. + case EAGAIN: { + /* + * We may be blocked on write because the input buffer + * is full. Let's check if we have room in our input + * buffer for more data and retry the write before + * waiting.. */ - if (repack_input_buffer(instance) or process_input_buffer(instance)) - { - continue; - } - - memcached_return_t rc= io_wait(instance, POLLOUT); - if (memcached_success(rc)) - { - continue; - } - else if (rc == MEMCACHED_TIMEOUT) - { - return false; - } + if (repack_input_buffer(instance) or process_input_buffer(instance)) { + continue; + } - memcached_quit_server(instance, true); - error= memcached_set_errno(*instance, local_errno, MEMCACHED_AT); + memcached_return_t rc = io_wait(instance, POLLOUT); + if (memcached_success(rc)) { + continue; + } else if (rc == MEMCACHED_TIMEOUT) { return false; } + + memcached_quit_server(instance, true); + error = memcached_set_errno(*instance, local_errno, MEMCACHED_AT); + return false; + } case ENOTCONN: case EPIPE: default: memcached_quit_server(instance, true); - error= memcached_set_errno(*instance, local_errno, MEMCACHED_AT); + error = memcached_set_errno(*instance, local_errno, MEMCACHED_AT); WATCHPOINT_ASSERT(instance->fd == INVALID_SOCKET); return false; } } - instance->io_bytes_sent+= uint32_t(sent_length); + instance->io_bytes_sent += uint32_t(sent_length); - local_write_ptr+= sent_length; - write_length-= uint32_t(sent_length); + local_write_ptr += sent_length; + write_length -= uint32_t(sent_length); } WATCHPOINT_ASSERT(write_length == 0); - instance->write_buffer_offset= 0; + instance->write_buffer_offset = 0; return true; } -memcached_return_t memcached_io_wait_for_write(memcached_instance_st* instance) -{ +memcached_return_t memcached_io_wait_for_write(memcached_instance_st *instance) { return io_wait(instance, POLLOUT); } -memcached_return_t memcached_io_wait_for_read(memcached_instance_st* instance) -{ +memcached_return_t memcached_io_wait_for_read(memcached_instance_st *instance) { return io_wait(instance, POLLIN); } -static memcached_return_t _io_fill(memcached_instance_st* instance) -{ +static memcached_return_t _io_fill(memcached_instance_st *instance) { ssize_t data_read; - do - { - data_read= ::recv(instance->fd, instance->read_buffer, MEMCACHED_MAX_BUFFER, MSG_NOSIGNAL); - int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno + do { + data_read = ::recv(instance->fd, instance->read_buffer, MEMCACHED_MAX_BUFFER, MSG_NOSIGNAL); + int local_errno = get_socket_errno(); // We cache in case memcached_quit_server() modifies errno - if (data_read == SOCKET_ERROR) - { - switch (get_socket_errno()) - { + if (data_read == SOCKET_ERROR) { + switch (get_socket_errno()) { case EINTR: // We just retry continue; @@ -440,16 +369,15 @@ static memcached_return_t _io_fill(memcached_instance_st* instance) #ifdef __linux case ERESTART: #endif - { - memcached_return_t io_wait_ret; - if (memcached_success(io_wait_ret= io_wait(instance, POLLIN))) - { - continue; - } - - return io_wait_ret; + { + memcached_return_t io_wait_ret; + if (memcached_success(io_wait_ret = io_wait(instance, POLLIN))) { + continue; } + return io_wait_ret; + } + /* fall through */ case ENOTCONN: // Programmer Error @@ -471,9 +399,7 @@ static memcached_return_t _io_fill(memcached_instance_st* instance) } return memcached_instance_error_return(instance); - } - else if (data_read == 0) - { + } else if (data_read == 0) { /* EOF. Any data received so far is incomplete so discard it. This always reads by byte in case of TCP @@ -484,59 +410,55 @@ static memcached_return_t _io_fill(memcached_instance_st* instance) it will return EGAIN if data is not immediatly available. */ memcached_quit_server(instance, true); - return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, - memcached_literal_param("::rec() returned zero, server has disconnected")); + return memcached_set_error( + *instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT, + memcached_literal_param("::rec() returned zero, server has disconnected")); } - instance->io_wait_count._bytes_read+= data_read; + instance->io_wait_count._bytes_read += data_read; } while (data_read <= 0); - instance->io_bytes_sent= 0; - instance->read_buffer_length= (size_t) data_read; - instance->read_ptr= instance->read_buffer; + instance->io_bytes_sent = 0; + instance->read_buffer_length = (size_t) data_read; + instance->read_ptr = instance->read_buffer; return MEMCACHED_SUCCESS; } -memcached_return_t memcached_io_read(memcached_instance_st* instance, - void *buffer, size_t length, ssize_t& nread) -{ +memcached_return_t memcached_io_read(memcached_instance_st *instance, void *buffer, size_t length, + ssize_t &nread) { assert(memcached_is_udp(instance->root) == false); - assert_msg(instance, "Programmer error, memcached_io_read() recieved an invalid Instance"); // Programmer error - char *buffer_ptr= static_cast(buffer); + assert_msg( + instance, + "Programmer error, memcached_io_read() recieved an invalid Instance"); // Programmer error + char *buffer_ptr = static_cast(buffer); - if (instance->fd == INVALID_SOCKET) - { + if (instance->fd == INVALID_SOCKET) { #if 0 assert_msg(int(instance->state) <= int(MEMCACHED_SERVER_STATE_ADDRINFO), "Programmer error, invalid socket state"); #endif return MEMCACHED_CONNECTION_FAILURE; } - while (length) - { - if (instance->read_buffer_length == 0) - { + while (length) { + if (instance->read_buffer_length == 0) { memcached_return_t io_fill_ret; - if (memcached_fatal(io_fill_ret= _io_fill(instance))) - { - nread= -1; + if (memcached_fatal(io_fill_ret = _io_fill(instance))) { + nread = -1; return io_fill_ret; } } - if (length > 1) - { - size_t difference= (length > instance->read_buffer_length) ? instance->read_buffer_length : length; + if (length > 1) { + size_t difference = + (length > instance->read_buffer_length) ? instance->read_buffer_length : length; memcpy(buffer_ptr, instance->read_ptr, difference); length -= difference; - instance->read_ptr+= difference; - instance->read_buffer_length-= difference; - buffer_ptr+= difference; - } - else - { - *buffer_ptr= *instance->read_ptr; + instance->read_ptr += difference; + instance->read_buffer_length -= difference; + buffer_ptr += difference; + } else { + *buffer_ptr = *instance->read_ptr; instance->read_ptr++; instance->read_buffer_length--; buffer_ptr++; @@ -544,31 +466,27 @@ memcached_return_t memcached_io_read(memcached_instance_st* instance, } } - nread= ssize_t(buffer_ptr - (char*)buffer); + nread = ssize_t(buffer_ptr - (char *) buffer); return MEMCACHED_SUCCESS; } -memcached_return_t memcached_io_slurp(memcached_instance_st* instance) -{ +memcached_return_t memcached_io_slurp(memcached_instance_st *instance) { assert_msg(instance, "Programmer error, invalid Instance"); assert(memcached_is_udp(instance->root) == false); - if (instance->fd == INVALID_SOCKET) - { - assert_msg(int(instance->state) <= int(MEMCACHED_SERVER_STATE_ADDRINFO), "Invalid socket state"); + if (instance->fd == INVALID_SOCKET) { + assert_msg(int(instance->state) <= int(MEMCACHED_SERVER_STATE_ADDRINFO), + "Invalid socket state"); return MEMCACHED_CONNECTION_FAILURE; } ssize_t data_read; char buffer[MEMCACHED_MAX_BUFFER]; - do - { - data_read= ::recv(instance->fd, instance->read_buffer, sizeof(buffer), MSG_NOSIGNAL); - if (data_read == SOCKET_ERROR) - { - switch (get_socket_errno()) - { + do { + data_read = ::recv(instance->fd, instance->read_buffer, sizeof(buffer), MSG_NOSIGNAL); + if (data_read == SOCKET_ERROR) { + switch (get_socket_errno()) { case EINTR: // We just retry continue; @@ -580,8 +498,7 @@ memcached_return_t memcached_io_slurp(memcached_instance_st* instance) #ifdef __linux case ERESTART: #endif - if (memcached_success(io_wait(instance, POLLIN))) - { + if (memcached_success(io_wait(instance, POLLIN))) { continue; } return MEMCACHED_IN_PROGRESS; @@ -598,8 +515,7 @@ memcached_return_t memcached_io_slurp(memcached_instance_st* instance) case EINVAL: case EFAULT: case ECONNREFUSED: - default: - return MEMCACHED_CONNECTION_FAILURE; // We want this! + default: return MEMCACHED_CONNECTION_FAILURE; // We want this! } } } while (data_read > 0); @@ -607,103 +523,86 @@ memcached_return_t memcached_io_slurp(memcached_instance_st* instance) return MEMCACHED_CONNECTION_FAILURE; } -static bool _io_write(memcached_instance_st* instance, - const void *buffer, size_t length, bool with_flush, - size_t& written) -{ +static bool _io_write(memcached_instance_st *instance, const void *buffer, size_t length, + bool with_flush, size_t &written) { assert(instance->fd != INVALID_SOCKET); assert(memcached_is_udp(instance->root) == false); - const char *buffer_ptr= static_cast(buffer); + const char *buffer_ptr = static_cast(buffer); - const size_t original_length= length; + const size_t original_length = length; - while (length) - { + while (length) { char *write_ptr; - size_t buffer_end= MEMCACHED_MAX_BUFFER; - size_t should_write= buffer_end -instance->write_buffer_offset; - should_write= (should_write < length) ? should_write : length; + size_t buffer_end = MEMCACHED_MAX_BUFFER; + size_t should_write = buffer_end - instance->write_buffer_offset; + should_write = (should_write < length) ? should_write : length; - write_ptr= instance->write_buffer + instance->write_buffer_offset; + write_ptr = instance->write_buffer + instance->write_buffer_offset; memcpy(write_ptr, buffer_ptr, should_write); - instance->write_buffer_offset+= should_write; - buffer_ptr+= should_write; - length-= should_write; + instance->write_buffer_offset += should_write; + buffer_ptr += should_write; + length -= should_write; - if (instance->write_buffer_offset == buffer_end) - { + if (instance->write_buffer_offset == buffer_end) { WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET); memcached_return_t rc; - if (io_flush(instance, with_flush, rc) == false) - { - written= original_length -length; + if (io_flush(instance, with_flush, rc) == false) { + written = original_length - length; return false; } } } - if (with_flush) - { + if (with_flush) { memcached_return_t rc; WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET); - if (io_flush(instance, with_flush, rc) == false) - { - written= original_length -length; + if (io_flush(instance, with_flush, rc) == false) { + written = original_length - length; return false; } } - written= original_length -length; + written = original_length - length; return true; } -bool memcached_io_write(memcached_instance_st* instance) -{ +bool memcached_io_write(memcached_instance_st *instance) { size_t written; return _io_write(instance, NULL, 0, true, written); } -ssize_t memcached_io_write(memcached_instance_st* instance, - const void *buffer, const size_t length, const bool with_flush) -{ +ssize_t memcached_io_write(memcached_instance_st *instance, const void *buffer, const size_t length, + const bool with_flush) { size_t written; - if (_io_write(instance, buffer, length, with_flush, written) == false) - { + if (_io_write(instance, buffer, length, with_flush, written) == false) { return -1; } return ssize_t(written); } -bool memcached_io_writev(memcached_instance_st* instance, - libmemcached_io_vector_st vector[], - const size_t number_of, const bool with_flush) -{ - ssize_t complete_total= 0; - ssize_t total= 0; +bool memcached_io_writev(memcached_instance_st *instance, libmemcached_io_vector_st vector[], + const size_t number_of, const bool with_flush) { + ssize_t complete_total = 0; + ssize_t total = 0; - for (size_t x= 0; x < number_of; x++, vector++) - { - complete_total+= vector->length; - if (vector->length) - { + for (size_t x = 0; x < number_of; x++, vector++) { + complete_total += vector->length; + if (vector->length) { size_t written; - if ((_io_write(instance, vector->buffer, vector->length, false, written)) == false) - { + if ((_io_write(instance, vector->buffer, vector->length, false, written)) == false) { return false; } - total+= written; + total += written; } } - if (with_flush) - { - if (memcached_io_write(instance) == false) - { + if (with_flush) { + if (memcached_io_write(instance) == false) { return false; } } @@ -711,93 +610,78 @@ bool memcached_io_writev(memcached_instance_st* instance, return (complete_total == total); } -void memcached_instance_st::start_close_socket() -{ - if (fd != INVALID_SOCKET) - { +void memcached_instance_st::start_close_socket() { + if (fd != INVALID_SOCKET) { shutdown(fd, SHUT_WR); - options.is_shutting_down= true; + options.is_shutting_down = true; } } -void memcached_instance_st::reset_socket() -{ - if (fd != INVALID_SOCKET) - { - (void)closesocket(fd); - fd= INVALID_SOCKET; +void memcached_instance_st::reset_socket() { + if (fd != INVALID_SOCKET) { + (void) closesocket(fd); + fd = INVALID_SOCKET; } } -void memcached_instance_st::close_socket() -{ - if (fd != INVALID_SOCKET) - { - int shutdown_options= SHUT_RD; - if (options.is_shutting_down == false) - { - shutdown_options= SHUT_RDWR; +void memcached_instance_st::close_socket() { + if (fd != INVALID_SOCKET) { + int shutdown_options = SHUT_RD; + if (options.is_shutting_down == false) { + shutdown_options = SHUT_RDWR; } /* in case of death shutdown to avoid blocking at close() */ - if (shutdown(fd, shutdown_options) == SOCKET_ERROR and get_socket_errno() != ENOTCONN) - { + if (shutdown(fd, shutdown_options) == SOCKET_ERROR and get_socket_errno() != ENOTCONN) { WATCHPOINT_NUMBER(fd); WATCHPOINT_ERRNO(get_socket_errno()); WATCHPOINT_ASSERT(get_socket_errno()); } reset_socket(); - state= MEMCACHED_SERVER_STATE_NEW; + state = MEMCACHED_SERVER_STATE_NEW; } - state= MEMCACHED_SERVER_STATE_NEW; - cursor_active_= 0; - io_bytes_sent= 0; - write_buffer_offset= size_t(root and memcached_is_udp(root) ? UDP_DATAGRAM_HEADER_LENGTH : 0); - read_buffer_length= 0; - read_ptr= read_buffer; - options.is_shutting_down= false; + state = MEMCACHED_SERVER_STATE_NEW; + cursor_active_ = 0; + io_bytes_sent = 0; + write_buffer_offset = size_t(root and memcached_is_udp(root) ? UDP_DATAGRAM_HEADER_LENGTH : 0); + read_buffer_length = 0; + read_ptr = read_buffer; + options.is_shutting_down = false; memcached_server_response_reset(this); // We reset the version so that if we end up talking to a different server // we don't have stale server version information. - major_version= minor_version= micro_version= UINT8_MAX; + major_version = minor_version = micro_version = UINT8_MAX; } -memcached_instance_st* memcached_io_get_readable_server(Memcached *memc, memcached_return_t&) -{ +memcached_instance_st *memcached_io_get_readable_server(Memcached *memc, memcached_return_t &) { #define MAX_SERVERS_TO_POLL 100 struct pollfd fds[MAX_SERVERS_TO_POLL]; - nfds_t host_index= 0; + nfds_t host_index = 0; - for (uint32_t x= 0; x < memcached_server_count(memc) and host_index < MAX_SERVERS_TO_POLL; ++x) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + for (uint32_t x = 0; x < memcached_server_count(memc) and host_index < MAX_SERVERS_TO_POLL; ++x) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); - if (instance->read_buffer_length > 0) /* I have data in the buffer */ - { + if (instance->read_buffer_length > 0) /* I have data in the buffer */ { return instance; } - if (instance->response_count() > 0) - { - fds[host_index].events= POLLIN; - fds[host_index].revents= 0; - fds[host_index].fd= instance->fd; + if (instance->response_count() > 0) { + fds[host_index].events = POLLIN; + fds[host_index].revents = 0; + fds[host_index].fd = instance->fd; ++host_index; } } - if (host_index < 2) - { + if (host_index < 2) { /* We have 0 or 1 server with pending events.. */ - for (uint32_t x= 0; x< memcached_server_count(memc); ++x) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + for (uint32_t x = 0; x < memcached_server_count(memc); ++x) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); - if (instance->response_count() > 0) - { + if (instance->response_count() > 0) { return instance; } } @@ -805,26 +689,20 @@ memcached_instance_st* memcached_io_get_readable_server(Memcached *memc, memcach return NULL; } - int error= poll(fds, host_index, memc->poll_timeout); - switch (error) - { + int error = poll(fds, host_index, memc->poll_timeout); + switch (error) { case -1: memcached_set_errno(*memc, get_socket_errno(), MEMCACHED_AT); /* FALLTHROUGH */ - case 0: - break; + case 0: break; default: - for (nfds_t x= 0; x < host_index; ++x) - { - if (fds[x].revents & POLLIN) - { - for (uint32_t y= 0; y < memcached_server_count(memc); ++y) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, y); + for (nfds_t x = 0; x < host_index; ++x) { + if (fds[x].revents & POLLIN) { + for (uint32_t y = 0; y < memcached_server_count(memc); ++y) { + memcached_instance_st *instance = memcached_instance_fetch(memc, y); - if (instance->fd == fds[x].fd) - { + if (instance->fd == fds[x].fd) { return instance; } } @@ -838,8 +716,7 @@ memcached_instance_st* memcached_io_get_readable_server(Memcached *memc, memcach /* Eventually we will just kill off the server with the problem. */ -void memcached_io_reset(memcached_instance_st* instance) -{ +void memcached_io_reset(memcached_instance_st *instance) { memcached_quit_server(instance, true); } @@ -847,63 +724,52 @@ void memcached_io_reset(memcached_instance_st* instance) * Read a given number of bytes from the server and place it into a specific * buffer. Reset the IO channel on this server if an error occurs. */ -memcached_return_t memcached_safe_read(memcached_instance_st* instance, - void *dta, - const size_t size) -{ - size_t offset= 0; - char *data= static_cast(dta); - - while (offset < size) - { +memcached_return_t memcached_safe_read(memcached_instance_st *instance, void *dta, + const size_t size) { + size_t offset = 0; + char *data = static_cast(dta); + + while (offset < size) { ssize_t nread; memcached_return_t rc; - while (memcached_continue(rc= memcached_io_read(instance, data + offset, size - offset, nread))) { }; + while ( + memcached_continue(rc = memcached_io_read(instance, data + offset, size - offset, nread))) { + }; - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { return rc; } - offset+= size_t(nread); + offset += size_t(nread); } return MEMCACHED_SUCCESS; } -memcached_return_t memcached_io_readline(memcached_instance_st* instance, - char *buffer_ptr, - size_t size, - size_t& total_nr) -{ - total_nr= 0; - bool line_complete= false; +memcached_return_t memcached_io_readline(memcached_instance_st *instance, char *buffer_ptr, + size_t size, size_t &total_nr) { + total_nr = 0; + bool line_complete = false; - while (line_complete == false) - { - if (instance->read_buffer_length == 0) - { + while (line_complete == false) { + if (instance->read_buffer_length == 0) { /* * We don't have any data in the buffer, so let's fill the read * buffer. Call the standard read function to avoid duplicating * the logic. - */ + */ ssize_t nread; - memcached_return_t rc= memcached_io_read(instance, buffer_ptr, 1, nread); - if (memcached_failed(rc) and rc == MEMCACHED_IN_PROGRESS) - { + memcached_return_t rc = memcached_io_read(instance, buffer_ptr, 1, nread); + if (memcached_failed(rc) and rc == MEMCACHED_IN_PROGRESS) { memcached_quit_server(instance, true); return memcached_set_error(*instance, rc, MEMCACHED_AT); - } - else if (memcached_failed(rc)) - { + } else if (memcached_failed(rc)) { return rc; } - if (*buffer_ptr == '\n') - { - line_complete= true; + if (*buffer_ptr == '\n') { + line_complete = true; } ++buffer_ptr; @@ -911,11 +777,9 @@ memcached_return_t memcached_io_readline(memcached_instance_st* instance, } /* Now let's look in the buffer and copy as we go! */ - while (instance->read_buffer_length and total_nr < size and line_complete == false) - { + while (instance->read_buffer_length and total_nr < size and line_complete == false) { *buffer_ptr = *instance->read_ptr; - if (*buffer_ptr == '\n') - { + if (*buffer_ptr == '\n') { line_complete = true; } --instance->read_buffer_length; @@ -924,8 +788,7 @@ memcached_return_t memcached_io_readline(memcached_instance_st* instance, ++buffer_ptr; } - if (total_nr == size) - { + if (total_nr == size) { return MEMCACHED_PROTOCOL_ERROR; } } diff --git a/src/libmemcached/io.h b/src/libmemcached/io.h index c2711e28..5cbca8f7 100644 --- a/src/libmemcached/io.h +++ b/src/libmemcached/io.h @@ -1,45 +1,21 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -struct libmemcached_io_vector_st -{ +struct libmemcached_io_vector_st { const void *buffer; size_t length; }; diff --git a/src/libmemcached/io.hpp b/src/libmemcached/io.hpp index 5480239f..bd7635c9 100644 --- a/src/libmemcached/io.hpp +++ b/src/libmemcached/io.hpp @@ -1,73 +1,45 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -void initialize_binary_request(memcached_instance_st* server, protocol_binary_request_header&); +void initialize_binary_request(memcached_instance_st *server, protocol_binary_request_header &); -bool memcached_io_write(memcached_instance_st* ptr); +bool memcached_io_write(memcached_instance_st *ptr); -ssize_t memcached_io_write(memcached_instance_st* ptr, - const void *buffer, size_t length, bool with_flush); +ssize_t memcached_io_write(memcached_instance_st *ptr, const void *buffer, size_t length, + bool with_flush); -bool memcached_io_writev(memcached_instance_st* ptr, - libmemcached_io_vector_st vector[], +bool memcached_io_writev(memcached_instance_st *ptr, libmemcached_io_vector_st vector[], const size_t number_of, const bool with_flush); -memcached_return_t memcached_io_wait_for_write(memcached_instance_st*); -memcached_return_t memcached_io_wait_for_read(memcached_instance_st*); +memcached_return_t memcached_io_wait_for_write(memcached_instance_st *); +memcached_return_t memcached_io_wait_for_read(memcached_instance_st *); -void memcached_io_reset(memcached_instance_st* ptr); +void memcached_io_reset(memcached_instance_st *ptr); -memcached_return_t memcached_io_read(memcached_instance_st* ptr, - void *buffer, size_t length, ssize_t& nread); +memcached_return_t memcached_io_read(memcached_instance_st *ptr, void *buffer, size_t length, + ssize_t &nread); /* Read a line (terminated by '\n') into the buffer */ -memcached_return_t memcached_io_readline(memcached_instance_st* ptr, - char *buffer_ptr, - size_t size, - size_t& total); +memcached_return_t memcached_io_readline(memcached_instance_st *ptr, char *buffer_ptr, size_t size, + size_t &total); /* Read n bytes of data from the server and store them in dta */ -memcached_return_t memcached_safe_read(memcached_instance_st* ptr, - void *dta, - const size_t size); +memcached_return_t memcached_safe_read(memcached_instance_st *ptr, void *dta, const size_t size); -memcached_instance_st* memcached_io_get_readable_server(memcached_st *memc, memcached_return_t&); +memcached_instance_st *memcached_io_get_readable_server(memcached_st *memc, memcached_return_t &); -memcached_return_t memcached_io_slurp(memcached_instance_st* ptr); +memcached_return_t memcached_io_slurp(memcached_instance_st *ptr); diff --git a/src/libmemcached/is.h b/src/libmemcached/is.h index 54f2c2b2..7d9fed9a 100644 --- a/src/libmemcached/is.h +++ b/src/libmemcached/is.h @@ -1,84 +1,66 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -/* These are private */ -#define memcached_is_allocated(__object) ((__object)->options.is_allocated) -#define memcached_is_encrypted(__object) ((__object)->hashkit._key) -#define memcached_is_initialized(__object) ((__object)->options.is_initialized) -#define memcached_is_purging(__object) ((__object)->state.is_purging) +/* These are private */ +#define memcached_is_allocated(__object) ((__object)->options.is_allocated) +#define memcached_is_encrypted(__object) ((__object)->hashkit._key) +#define memcached_is_initialized(__object) ((__object)->options.is_initialized) +#define memcached_is_purging(__object) ((__object)->state.is_purging) #define memcached_is_processing_input(__object) ((__object)->state.is_processing_input) -#define memcached_is_aes(__object) ((__object)->flags.is_aes) -#define memcached_is_udp(__object) ((__object)->flags.use_udp) -#define memcached_is_verify_key(__object) ((__object)->flags.verify_key) -#define memcached_is_binary(__object) ((__object)->flags.binary_protocol) -#define memcached_is_fetching_version(__object) ((__object)->flags.is_fetching_version) -#define memcached_is_buffering(__object) ((__object)->flags.buffer_requests) -#define memcached_is_replying(__object) ((__object)->flags.reply) -#define memcached_is_cas(__object) ((__object)->flags.reply) +#define memcached_is_aes(__object) ((__object)->flags.is_aes) +#define memcached_is_udp(__object) ((__object)->flags.use_udp) +#define memcached_is_verify_key(__object) ((__object)->flags.verify_key) +#define memcached_is_binary(__object) ((__object)->flags.binary_protocol) +#define memcached_is_fetching_version(__object) ((__object)->flags.is_fetching_version) +#define memcached_is_buffering(__object) ((__object)->flags.buffer_requests) +#define memcached_is_replying(__object) ((__object)->flags.reply) +#define memcached_is_cas(__object) ((__object)->flags.reply) #define memcached_is_randomize_replica_read(__object) ((__object)->flags.randomize_replica_read) -#define memcached_is_no_block(__object) ((__object)->flags.no_block) -#define memcached_is_hash_with_namespace(__object) ((__object)->flags.hash_with_namespace) -#define memcached_is_tcp_nodelay(__object) ((__object)->flags.tcp_nodelay) -#define memcached_is_auto_eject_hosts(__object) ((__object)->flags.auto_eject_hosts) -#define memcached_is_use_sort_hosts(__object) ((__object)->flags.use_sort_hosts) +#define memcached_is_no_block(__object) ((__object)->flags.no_block) +#define memcached_is_hash_with_namespace(__object) ((__object)->flags.hash_with_namespace) +#define memcached_is_tcp_nodelay(__object) ((__object)->flags.tcp_nodelay) +#define memcached_is_auto_eject_hosts(__object) ((__object)->flags.auto_eject_hosts) +#define memcached_is_use_sort_hosts(__object) ((__object)->flags.use_sort_hosts) #define memcached_is_ready(__object) ((__object)->options.ready) #define memcached_is_weighted_ketama(__object) ((__object)->ketama.weighted_) -#define memcached_set_ready(__object, __flag) ((__object)->options.ready= (__flag)) +#define memcached_set_ready(__object, __flag) ((__object)->options.ready = (__flag)) -#define memcached_set_aes(__object, __flag) ((__object).flags.is_aes= __flag) -#define memcached_set_udp(__object, __flag) ((__object).flags.use_udp= __flag) -#define memcached_set_verify_key(__object, __flag) ((__object).flags.verify_key= __flag) -#define memcached_set_binary(__object, __flag) ((__object).flags.binary_protocol= __flag) -#define memcached_set_fetching_version(__object, __flag) ((__object).flags.is_fetching_version= __flag) -#define memcached_set_buffering(__object, __flag) ((__object).flags.buffer_requests= __flag) -#define memcached_set_replying(__object, __flag) ((__object).flags.reply= __flag) -#define memcached_set_cas(__object, __flag) ((__object).flags.reply= __flag) -#define memcached_set_randomize_replica_read(__object, __flag) ((__object).flags.randomize_replica_read= __flag) -#define memcached_set_no_block(__object, __flag) ((__object).flags.no_block= __flag) -#define memcached_set_hash_with_namespace(__object, __flag) ((__object).flags.hash_with_namespace= __flag) -#define memcached_set_tcp_nodelay(__object, __flag) ((__object).flags.tcp_nodelay= __flag) -#define memcached_set_auto_eject_hosts(__object, __flag) ((__object).flags.auto_eject_hosts= __flag) -#define memcached_set_use_sort_hosts(__object, __flag) ((__object).flags.use_sort_hosts= __flag) +#define memcached_set_aes(__object, __flag) ((__object).flags.is_aes = __flag) +#define memcached_set_udp(__object, __flag) ((__object).flags.use_udp = __flag) +#define memcached_set_verify_key(__object, __flag) ((__object).flags.verify_key = __flag) +#define memcached_set_binary(__object, __flag) ((__object).flags.binary_protocol = __flag) +#define memcached_set_fetching_version(__object, __flag) \ + ((__object).flags.is_fetching_version = __flag) +#define memcached_set_buffering(__object, __flag) ((__object).flags.buffer_requests = __flag) +#define memcached_set_replying(__object, __flag) ((__object).flags.reply = __flag) +#define memcached_set_cas(__object, __flag) ((__object).flags.reply = __flag) +#define memcached_set_randomize_replica_read(__object, __flag) \ + ((__object).flags.randomize_replica_read = __flag) +#define memcached_set_no_block(__object, __flag) ((__object).flags.no_block = __flag) +#define memcached_set_hash_with_namespace(__object, __flag) \ + ((__object).flags.hash_with_namespace = __flag) +#define memcached_set_tcp_nodelay(__object, __flag) ((__object).flags.tcp_nodelay = __flag) +#define memcached_set_auto_eject_hosts(__object, __flag) \ + ((__object).flags.auto_eject_hosts = __flag) +#define memcached_set_use_sort_hosts(__object, __flag) ((__object).flags.use_sort_hosts = __flag) #define memcached_has_root(__object) ((__object)->root) @@ -86,10 +68,12 @@ #define memcached_has_replicas(__object) ((__object)->root->number_of_replicas) -#define memcached_set_processing_input(__object, __value) ((__object)->state.is_processing_input= (__value)) -#define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized= (__value)) -#define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated= (__value)) +#define memcached_set_processing_input(__object, __value) \ + ((__object)->state.is_processing_input = (__value)) +#define memcached_set_initialized(__object, __value) \ + ((__object)->options.is_initialized = (__value)) +#define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated = (__value)) -#define memcached_set_weighted_ketama(__object, __value) ((__object)->ketama.weighted_= (__value)) +#define memcached_set_weighted_ketama(__object, __value) ((__object)->ketama.weighted_ = (__value)) #define memcached2Memcached(__obj) (__obj) diff --git a/src/libmemcached/key.cc b/src/libmemcached/key.cc index 0848c796..d6ff8a5d 100644 --- a/src/libmemcached/key.cc +++ b/src/libmemcached/key.cc @@ -1,46 +1,22 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool) -{ - if (key_length == 0) - { +static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool) { + if (key_length == 0) { return MEMCACHED_BAD_KEY_PROVIDED; } @@ -56,8 +32,7 @@ static inline memcached_return_t memcached_validate_key_length(size_t key_length else #endif { - if (key_length >= MEMCACHED_MAX_KEY) - { + if (key_length >= MEMCACHED_MAX_KEY) { return MEMCACHED_BAD_KEY_PROVIDED; } } @@ -65,42 +40,38 @@ static inline memcached_return_t memcached_validate_key_length(size_t key_length return MEMCACHED_SUCCESS; } -memcached_return_t memcached_key_test(memcached_st &memc, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys) -{ - if (number_of_keys == 0) - { - return memcached_set_error(memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Numbers of keys provided was zero")); +memcached_return_t memcached_key_test(memcached_st &memc, const char *const *keys, + const size_t *key_length, size_t number_of_keys) { + if (number_of_keys == 0) { + return memcached_set_error(memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Numbers of keys provided was zero")); } - if (keys == NULL or key_length == NULL) - { - return memcached_set_error(memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("Key was NULL or length of key was zero.")); + if (keys == NULL or key_length == NULL) { + return memcached_set_error(memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, + memcached_literal_param("Key was NULL or length of key was zero.")); } - const bool is_binary= memcached_flag(memc, MEMCACHED_FLAG_BINARY_PROTOCOL); + const bool is_binary = memcached_flag(memc, MEMCACHED_FLAG_BINARY_PROTOCOL); // If we don't need to verify the key, or we are using the binary protoocol, // we just check the size of the key - for (size_t x= 0; x < number_of_keys; ++x) - { + for (size_t x = 0; x < number_of_keys; ++x) { // We should set binary key, but the memcached server is broken for // longer keys at the moment. - memcached_return_t rc= memcached_validate_key_length(*(key_length +x), false /* memc.flags.binary_protocol */); - if (memcached_failed(rc)) - { - return memcached_set_error(memc, rc, MEMCACHED_AT, memcached_literal_param("Key provided was too long.")); + memcached_return_t rc = + memcached_validate_key_length(*(key_length + x), false /* memc.flags.binary_protocol */); + if (memcached_failed(rc)) { + return memcached_set_error(memc, rc, MEMCACHED_AT, + memcached_literal_param("Key provided was too long.")); } - if (memc.flags.verify_key and is_binary == false) - { - for (size_t y= 0; y < *(key_length +x); ++y) - { - if ((isgraph(keys[x][y])) == 0) - { - return memcached_set_error(memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("Key provided had invalid character.")); + if (memc.flags.verify_key and is_binary == false) { + for (size_t y = 0; y < *(key_length + x); ++y) { + if ((isgraph(keys[x][y])) == 0) { + return memcached_set_error( + memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, + memcached_literal_param("Key provided had invalid character.")); } } } @@ -108,4 +79,3 @@ memcached_return_t memcached_key_test(memcached_st &memc, return MEMCACHED_SUCCESS; } - diff --git a/src/libmemcached/key.hpp b/src/libmemcached/key.hpp index 811a4b7b..93c91581 100644 --- a/src/libmemcached/key.hpp +++ b/src/libmemcached/key.hpp @@ -1,43 +1,19 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -memcached_return_t memcached_key_test(memcached_st& memc, - const char * const *keys, - const size_t *key_length, - size_t number_of_keys); - +memcached_return_t memcached_key_test(memcached_st &memc, const char *const *keys, + const size_t *key_length, size_t number_of_keys); diff --git a/src/libmemcached/libmemcached_probes.h b/src/libmemcached/libmemcached_probes.h index 9dba8aa1..02a09bee 100644 --- a/src/libmemcached/libmemcached_probes.h +++ b/src/libmemcached/libmemcached_probes.h @@ -1,43 +1,20 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once - /* * This file contains the definition of the various probes supported by * libmemcached. Currently it only support DTRACE, but just create an @@ -50,7 +27,7 @@ * Create the DTrace probes on the system using it (to support both Solaris * and MacOS X */ -#include "libmemcached/dtrace_probes.h" +# include "libmemcached/dtrace_probes.h" #else /* @@ -58,61 +35,61 @@ * ifdefs when we want to use the probes. */ -#define LIBMEMCACHED_MEMCACHED_ADD_END() -#define LIBMEMCACHED_MEMCACHED_ADD_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_ADD_START() -#define LIBMEMCACHED_MEMCACHED_ADD_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_CONNECT_END() -#define LIBMEMCACHED_MEMCACHED_CONNECT_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_CONNECT_START() -#define LIBMEMCACHED_MEMCACHED_CONNECT_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DECREMENT_END() -#define LIBMEMCACHED_MEMCACHED_DECREMENT_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DECREMENT_START() -#define LIBMEMCACHED_MEMCACHED_DECREMENT_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END() -#define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START() -#define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DELETE_END() -#define LIBMEMCACHED_MEMCACHED_DELETE_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_DELETE_START() -#define LIBMEMCACHED_MEMCACHED_DELETE_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_FLUSH_END() -#define LIBMEMCACHED_MEMCACHED_FLUSH_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_FLUSH_START() -#define LIBMEMCACHED_MEMCACHED_FLUSH_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_GET_END() -#define LIBMEMCACHED_MEMCACHED_GET_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_GET_START() -#define LIBMEMCACHED_MEMCACHED_GET_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_TOUCH_END() -#define LIBMEMCACHED_MEMCACHED_TOUCH_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_TOUCH_START() -#define LIBMEMCACHED_MEMCACHED_TOUCH_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_INCREMENT_END() -#define LIBMEMCACHED_MEMCACHED_INCREMENT_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_INCREMENT_START() -#define LIBMEMCACHED_MEMCACHED_INCREMENT_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END() -#define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START() -#define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_MGET_END() -#define LIBMEMCACHED_MEMCACHED_MGET_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_MGET_START() -#define LIBMEMCACHED_MEMCACHED_MGET_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_REPLACE_END() -#define LIBMEMCACHED_MEMCACHED_REPLACE_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_REPLACE_START() -#define LIBMEMCACHED_MEMCACHED_REPLACE_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_SERVER_ADD_END() -#define LIBMEMCACHED_MEMCACHED_SERVER_ADD_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_SERVER_ADD_START() -#define LIBMEMCACHED_MEMCACHED_SERVER_ADD_START_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_SET_END() -#define LIBMEMCACHED_MEMCACHED_SET_END_ENABLED() (0) -#define LIBMEMCACHED_MEMCACHED_SET_START() -#define LIBMEMCACHED_MEMCACHED_SET_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_ADD_END() +# define LIBMEMCACHED_MEMCACHED_ADD_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_ADD_START() +# define LIBMEMCACHED_MEMCACHED_ADD_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_CONNECT_END() +# define LIBMEMCACHED_MEMCACHED_CONNECT_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_CONNECT_START() +# define LIBMEMCACHED_MEMCACHED_CONNECT_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DECREMENT_END() +# define LIBMEMCACHED_MEMCACHED_DECREMENT_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DECREMENT_START() +# define LIBMEMCACHED_MEMCACHED_DECREMENT_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END() +# define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START() +# define LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DELETE_END() +# define LIBMEMCACHED_MEMCACHED_DELETE_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_DELETE_START() +# define LIBMEMCACHED_MEMCACHED_DELETE_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_FLUSH_END() +# define LIBMEMCACHED_MEMCACHED_FLUSH_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_FLUSH_START() +# define LIBMEMCACHED_MEMCACHED_FLUSH_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_GET_END() +# define LIBMEMCACHED_MEMCACHED_GET_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_GET_START() +# define LIBMEMCACHED_MEMCACHED_GET_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_TOUCH_END() +# define LIBMEMCACHED_MEMCACHED_TOUCH_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_TOUCH_START() +# define LIBMEMCACHED_MEMCACHED_TOUCH_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_INCREMENT_END() +# define LIBMEMCACHED_MEMCACHED_INCREMENT_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_INCREMENT_START() +# define LIBMEMCACHED_MEMCACHED_INCREMENT_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END() +# define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START() +# define LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_MGET_END() +# define LIBMEMCACHED_MEMCACHED_MGET_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_MGET_START() +# define LIBMEMCACHED_MEMCACHED_MGET_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_REPLACE_END() +# define LIBMEMCACHED_MEMCACHED_REPLACE_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_REPLACE_START() +# define LIBMEMCACHED_MEMCACHED_REPLACE_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_SERVER_ADD_END() +# define LIBMEMCACHED_MEMCACHED_SERVER_ADD_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_SERVER_ADD_START() +# define LIBMEMCACHED_MEMCACHED_SERVER_ADD_START_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_SET_END() +# define LIBMEMCACHED_MEMCACHED_SET_END_ENABLED() (0) +# define LIBMEMCACHED_MEMCACHED_SET_START() +# define LIBMEMCACHED_MEMCACHED_SET_START_ENABLED() (0) #endif diff --git a/src/libmemcached/memcached.cc b/src/libmemcached/memcached.cc index c39ca019..7c80e5ef 100644 --- a/src/libmemcached/memcached.cc +++ b/src/libmemcached/memcached.cc @@ -1,135 +1,110 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/options.hpp" #include "libmemcached/virtual_bucket.h" -static inline bool _memcached_init(Memcached *self) -{ - self->state.is_purging= false; - self->state.is_processing_input= false; - self->state.is_time_for_rebuild= false; - self->state.is_parsing= false; - - self->flags.auto_eject_hosts= false; - self->flags.binary_protocol= false; - self->flags.buffer_requests= false; - self->flags.hash_with_namespace= false; - self->flags.no_block= false; - self->flags.reply= true; - self->flags.randomize_replica_read= false; - self->flags.support_cas= false; - self->flags.tcp_nodelay= false; - self->flags.use_sort_hosts= false; - self->flags.use_udp= false; - self->flags.verify_key= false; - self->flags.tcp_keepalive= false; - self->flags.is_aes= false; - self->flags.is_fetching_version= false; - - self->virtual_bucket= NULL; - - self->distribution= MEMCACHED_DISTRIBUTION_MODULA; - - if (hashkit_create(&self->hashkit) == NULL) - { +static inline bool _memcached_init(Memcached *self) { + self->state.is_purging = false; + self->state.is_processing_input = false; + self->state.is_time_for_rebuild = false; + self->state.is_parsing = false; + + self->flags.auto_eject_hosts = false; + self->flags.binary_protocol = false; + self->flags.buffer_requests = false; + self->flags.hash_with_namespace = false; + self->flags.no_block = false; + self->flags.reply = true; + self->flags.randomize_replica_read = false; + self->flags.support_cas = false; + self->flags.tcp_nodelay = false; + self->flags.use_sort_hosts = false; + self->flags.use_udp = false; + self->flags.verify_key = false; + self->flags.tcp_keepalive = false; + self->flags.is_aes = false; + self->flags.is_fetching_version = false; + + self->virtual_bucket = NULL; + + self->distribution = MEMCACHED_DISTRIBUTION_MODULA; + + if (hashkit_create(&self->hashkit) == NULL) { return false; } - self->server_info.version= 0; + self->server_info.version = 0; - self->ketama.continuum= NULL; - self->ketama.continuum_count= 0; - self->ketama.continuum_points_counter= 0; - self->ketama.next_distribution_rebuild= 0; - self->ketama.weighted_= false; + self->ketama.continuum = NULL; + self->ketama.continuum_count = 0; + self->ketama.continuum_points_counter = 0; + self->ketama.next_distribution_rebuild = 0; + self->ketama.weighted_ = false; - self->number_of_hosts= 0; - self->servers= NULL; - self->last_disconnected_server= NULL; + self->number_of_hosts = 0; + self->servers = NULL; + self->last_disconnected_server = NULL; - self->snd_timeout= 0; - self->rcv_timeout= 0; - self->server_failure_limit= MEMCACHED_SERVER_FAILURE_LIMIT; - self->server_timeout_limit= MEMCACHED_SERVER_TIMEOUT_LIMIT; - self->query_id= 1; // 0 is considered invalid + self->snd_timeout = 0; + self->rcv_timeout = 0; + self->server_failure_limit = MEMCACHED_SERVER_FAILURE_LIMIT; + self->server_timeout_limit = MEMCACHED_SERVER_TIMEOUT_LIMIT; + self->query_id = 1; // 0 is considered invalid /* TODO, Document why we picked these defaults */ - self->io_msg_watermark= 500; - self->io_bytes_watermark= 65 * 1024; + self->io_msg_watermark = 500; + self->io_bytes_watermark = 65 * 1024; - self->tcp_keepidle= 0; + self->tcp_keepidle = 0; - self->io_key_prefetch= 0; - self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT; - self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT; - self->retry_timeout= MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT; - self->dead_timeout= MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT; + self->io_key_prefetch = 0; + self->poll_timeout = MEMCACHED_DEFAULT_TIMEOUT; + self->connect_timeout = MEMCACHED_DEFAULT_CONNECT_TIMEOUT; + self->retry_timeout = MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT; + self->dead_timeout = MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT; - self->send_size= -1; - self->recv_size= -1; + self->send_size = -1; + self->recv_size = -1; - self->user_data= NULL; - self->number_of_replicas= 0; + self->user_data = NULL; + self->number_of_replicas = 0; - self->allocators= memcached_allocators_return_default(); + self->allocators = memcached_allocators_return_default(); - self->on_clone= NULL; - self->on_cleanup= NULL; - self->get_key_failure= NULL; - self->delete_trigger= NULL; - self->callbacks= NULL; - self->sasl.callbacks= NULL; - self->sasl.is_allocated= false; + self->on_clone = NULL; + self->on_cleanup = NULL; + self->get_key_failure = NULL; + self->delete_trigger = NULL; + self->callbacks = NULL; + self->sasl.callbacks = NULL; + self->sasl.is_allocated = false; - self->error_messages= NULL; - self->_namespace= NULL; - self->configure.initial_pool_size= 1; - self->configure.max_pool_size= 1; - self->configure.version= -1; - self->configure.filename= NULL; + self->error_messages = NULL; + self->_namespace = NULL; + self->configure.initial_pool_size = 1; + self->configure.max_pool_size = 1; + self->configure.version = -1; + self->configure.filename = NULL; return true; } -static void __memcached_free(Memcached *ptr, bool release_st) -{ +static void __memcached_free(Memcached *ptr, bool release_st) { /* If we have anything open, lets close it now */ send_quit(ptr); memcached_instance_list_free(memcached_instance_list(ptr), memcached_instance_list_count(ptr)); @@ -137,67 +112,56 @@ static void __memcached_free(Memcached *ptr, bool release_st) memcached_virtual_bucket_free(ptr); - memcached_instance_free((memcached_instance_st*)ptr->last_disconnected_server); + memcached_instance_free((memcached_instance_st *) ptr->last_disconnected_server); - if (ptr->on_cleanup) - { + if (ptr->on_cleanup) { ptr->on_cleanup(ptr); } libmemcached_free(ptr, ptr->ketama.continuum); - ptr->ketama.continuum= NULL; + ptr->ketama.continuum = NULL; memcached_array_free(ptr->_namespace); - ptr->_namespace= NULL; + ptr->_namespace = NULL; memcached_error_free(*ptr); - if (LIBMEMCACHED_WITH_SASL_SUPPORT and ptr->sasl.callbacks) - { + if (LIBMEMCACHED_WITH_SASL_SUPPORT and ptr->sasl.callbacks) { memcached_destroy_sasl_auth_data(ptr); } - if (release_st) - { + if (release_st) { memcached_array_free(ptr->configure.filename); - ptr->configure.filename= NULL; + ptr->configure.filename = NULL; } hashkit_free(&ptr->hashkit); - if (memcached_is_allocated(ptr) and release_st) - { + if (memcached_is_allocated(ptr) and release_st) { libmemcached_free(ptr, ptr); } } -memcached_st *memcached_create(memcached_st *shell) -{ - if (shell) - { - shell->options.is_allocated= false; - } - else - { - shell= (memcached_st *)libmemcached_xmalloc(NULL, memcached_st); +memcached_st *memcached_create(memcached_st *shell) { + if (shell) { + shell->options.is_allocated = false; + } else { + shell = (memcached_st *) libmemcached_xmalloc(NULL, memcached_st); - if (shell == NULL) - { + if (shell == NULL) { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ } - shell->options.is_allocated= true; + shell->options.is_allocated = true; } - if (_memcached_init(shell) == false) - { + if (_memcached_init(shell) == false) { memcached_free(shell); return NULL; } - Memcached* memc= memcached2Memcached(shell); - if (memcached_result_create(shell, &memc->result) == NULL) - { + Memcached *memc = memcached2Memcached(shell); + if (memcached_result_create(shell, &memc->result) == NULL) { memcached_free(shell); return NULL; } @@ -207,46 +171,38 @@ memcached_st *memcached_create(memcached_st *shell) return shell; } -memcached_st *memcached(const char *string, size_t length) -{ - if (length == 0 and string) - { +memcached_st *memcached(const char *string, size_t length) { + if (length == 0 and string) { return NULL; } - if (length and string == NULL) - { + if (length and string == NULL) { return NULL; } - if (length == 0) - { - if (bool(getenv("LIBMEMCACHED"))) - { - string= getenv("LIBMEMCACHED"); - length= string ? strlen(string) : 0; + if (length == 0) { + if (bool(getenv("LIBMEMCACHED"))) { + string = getenv("LIBMEMCACHED"); + length = string ? strlen(string) : 0; } } - memcached_st *memc= memcached_create(NULL); - if (memc == NULL) - { + memcached_st *memc = memcached_create(NULL); + if (memc == NULL) { return NULL; } - if (length == 0 or string == NULL) - { + if (length == 0 or string == NULL) { return memc; } - memcached_return_t rc= memcached_parse_configuration(memc, string, length); - if (memcached_success(rc) and memcached_parse_filename(memc)) - { - rc= memcached_parse_configure_file(*memc, memcached_parse_filename(memc), memcached_parse_filename_length(memc)); + memcached_return_t rc = memcached_parse_configuration(memc, string, length); + if (memcached_success(rc) and memcached_parse_filename(memc)) { + rc = memcached_parse_configure_file(*memc, memcached_parse_filename(memc), + memcached_parse_filename_length(memc)); } - - if (memcached_failed(rc)) - { + + if (memcached_failed(rc)) { memcached_free(memc); return NULL; } @@ -254,39 +210,34 @@ memcached_st *memcached(const char *string, size_t length) return memc; } -memcached_return_t memcached_reset(memcached_st *shell) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_reset(memcached_st *shell) { + Memcached *ptr = memcached2Memcached(shell); WATCHPOINT_ASSERT(ptr); - if (ptr == NULL) - { + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - bool stored_is_allocated= memcached_is_allocated(ptr); - uint64_t query_id= ptr->query_id; + bool stored_is_allocated = memcached_is_allocated(ptr); + uint64_t query_id = ptr->query_id; __memcached_free(ptr, false); memcached_create(ptr); memcached_set_allocated(ptr, stored_is_allocated); - ptr->query_id= query_id; + ptr->query_id = query_id; - if (ptr->configure.filename) - { + if (ptr->configure.filename) { return memcached_parse_configure_file(*ptr, memcached_param_array(ptr->configure.filename)); } return MEMCACHED_SUCCESS; } -void memcached_servers_reset(memcached_st *shell) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { +void memcached_servers_reset(memcached_st *shell) { + Memcached *self = memcached2Memcached(shell); + if (self) { libmemcached_free(self, self->ketama.continuum); - self->ketama.continuum= NULL; - self->ketama.continuum_count= 0; - self->ketama.continuum_points_counter= 0; + self->ketama.continuum = NULL; + self->ketama.continuum_count = 0; + self->ketama.continuum_points_counter = 0; memcached_instance_list_free(memcached_instance_list(self), self->number_of_hosts); memcached_instance_set(self, NULL, 0); @@ -295,20 +246,16 @@ void memcached_servers_reset(memcached_st *shell) } } -void memcached_reset_last_disconnected_server(memcached_st *shell) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { - memcached_instance_free((memcached_instance_st*)self->last_disconnected_server); - self->last_disconnected_server= NULL; +void memcached_reset_last_disconnected_server(memcached_st *shell) { + Memcached *self = memcached2Memcached(shell); + if (self) { + memcached_instance_free((memcached_instance_st *) self->last_disconnected_server); + self->last_disconnected_server = NULL; } } -void memcached_free(memcached_st *ptr) -{ - if (ptr) - { +void memcached_free(memcached_st *ptr) { + if (ptr) { __memcached_free(ptr, true); } } @@ -318,115 +265,99 @@ void memcached_free(memcached_st *ptr) If source is NULL the call is the same as if a memcached_create() was called. */ -memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) -{ - if (source == NULL) - { +memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) { + if (source == NULL) { return memcached_create(clone); } - if (clone and memcached_is_allocated(clone)) - { + if (clone and memcached_is_allocated(clone)) { return NULL; } - memcached_st *new_clone= memcached_create(clone); + memcached_st *new_clone = memcached_create(clone); - if (new_clone == NULL) - { + if (new_clone == NULL) { return NULL; } - new_clone->flags= source->flags; - new_clone->send_size= source->send_size; - new_clone->recv_size= source->recv_size; - new_clone->poll_timeout= source->poll_timeout; - new_clone->connect_timeout= source->connect_timeout; - new_clone->retry_timeout= source->retry_timeout; - new_clone->dead_timeout= source->dead_timeout; - new_clone->distribution= source->distribution; + new_clone->flags = source->flags; + new_clone->send_size = source->send_size; + new_clone->recv_size = source->recv_size; + new_clone->poll_timeout = source->poll_timeout; + new_clone->connect_timeout = source->connect_timeout; + new_clone->retry_timeout = source->retry_timeout; + new_clone->dead_timeout = source->dead_timeout; + new_clone->distribution = source->distribution; - if (hashkit_clone(&new_clone->hashkit, &source->hashkit) == NULL) - { + if (hashkit_clone(&new_clone->hashkit, &source->hashkit) == NULL) { memcached_free(new_clone); return NULL; } - new_clone->user_data= source->user_data; + new_clone->user_data = source->user_data; - new_clone->snd_timeout= source->snd_timeout; - new_clone->rcv_timeout= source->rcv_timeout; + new_clone->snd_timeout = source->snd_timeout; + new_clone->rcv_timeout = source->rcv_timeout; - new_clone->on_clone= source->on_clone; - new_clone->on_cleanup= source->on_cleanup; + new_clone->on_clone = source->on_clone; + new_clone->on_cleanup = source->on_cleanup; - new_clone->allocators= source->allocators; + new_clone->allocators = source->allocators; - new_clone->get_key_failure= source->get_key_failure; - new_clone->delete_trigger= source->delete_trigger; - new_clone->server_failure_limit= source->server_failure_limit; - new_clone->server_timeout_limit= source->server_timeout_limit; - new_clone->io_msg_watermark= source->io_msg_watermark; - new_clone->io_bytes_watermark= source->io_bytes_watermark; - new_clone->io_key_prefetch= source->io_key_prefetch; - new_clone->number_of_replicas= source->number_of_replicas; - new_clone->tcp_keepidle= source->tcp_keepidle; + new_clone->get_key_failure = source->get_key_failure; + new_clone->delete_trigger = source->delete_trigger; + new_clone->server_failure_limit = source->server_failure_limit; + new_clone->server_timeout_limit = source->server_timeout_limit; + new_clone->io_msg_watermark = source->io_msg_watermark; + new_clone->io_bytes_watermark = source->io_bytes_watermark; + new_clone->io_key_prefetch = source->io_key_prefetch; + new_clone->number_of_replicas = source->number_of_replicas; + new_clone->tcp_keepidle = source->tcp_keepidle; - if (memcached_server_count(source)) - { - if (memcached_failed(memcached_push(new_clone, source))) - { + if (memcached_server_count(source)) { + if (memcached_failed(memcached_push(new_clone, source))) { return NULL; } } + new_clone->_namespace = memcached_array_clone(new_clone, source->_namespace); + new_clone->configure.filename = memcached_array_clone(new_clone, source->_namespace); + new_clone->configure.version = source->configure.version; - new_clone->_namespace= memcached_array_clone(new_clone, source->_namespace); - new_clone->configure.filename= memcached_array_clone(new_clone, source->_namespace); - new_clone->configure.version= source->configure.version; - - if (LIBMEMCACHED_WITH_SASL_SUPPORT and source->sasl.callbacks) - { - if (memcached_failed(memcached_clone_sasl(new_clone, source))) - { + if (LIBMEMCACHED_WITH_SASL_SUPPORT and source->sasl.callbacks) { + if (memcached_failed(memcached_clone_sasl(new_clone, source))) { memcached_free(new_clone); return NULL; } } - if (memcached_failed(run_distribution(new_clone))) - { + if (memcached_failed(run_distribution(new_clone))) { memcached_free(new_clone); return NULL; } - if (source->on_clone) - { + if (source->on_clone) { source->on_clone(new_clone, source); } return new_clone; } -void *memcached_get_user_data(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { +void *memcached_get_user_data(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { return memc->user_data; } return NULL; } -void *memcached_set_user_data(memcached_st *shell, void *data) -{ - Memcached* memc= memcached2Memcached(shell); - if (memc) - { - void *ret= memc->user_data; - memc->user_data= data; +void *memcached_set_user_data(memcached_st *shell, void *data) { + Memcached *memc = memcached2Memcached(shell); + if (memc) { + void *ret = memc->user_data; + memc->user_data = data; return ret; } @@ -434,62 +365,53 @@ void *memcached_set_user_data(memcached_st *shell, void *data) return NULL; } -memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source) -{ - return memcached_instance_push(destination, (memcached_instance_st*)source->servers, source->number_of_hosts); +memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source) { + return memcached_instance_push(destination, (memcached_instance_st *) source->servers, + source->number_of_hosts); } -memcached_instance_st* memcached_instance_fetch(Memcached *ptr, uint32_t server_key) -{ - if (ptr == NULL) - { +memcached_instance_st *memcached_instance_fetch(Memcached *ptr, uint32_t server_key) { + if (ptr == NULL) { return NULL; } return &ptr->servers[server_key]; } -const memcached_instance_st * memcached_server_instance_by_position(const memcached_st *shell, uint32_t server_key) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { +const memcached_instance_st *memcached_server_instance_by_position(const memcached_st *shell, + uint32_t server_key) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { return &memc->servers[server_key]; } return NULL; } -memcached_instance_st* memcached_instance_by_position(const memcached_st *shell, uint32_t server_key) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { +memcached_instance_st *memcached_instance_by_position(const memcached_st *shell, + uint32_t server_key) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { return &memc->servers[server_key]; } return NULL; } -uint64_t memcached_query_id(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { +uint64_t memcached_query_id(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { return memc->query_id; } return 0; } -memcached_instance_st* memcached_instance_list(const memcached_st *shell) -{ - const Memcached* memc= memcached2Memcached(shell); - if (memc) - { - return (memcached_instance_st*)memc->servers; +memcached_instance_st *memcached_instance_list(const memcached_st *shell) { + const Memcached *memc = memcached2Memcached(shell); + if (memc) { + return (memcached_instance_st *) memc->servers; } return NULL; } - diff --git a/src/libmemcached/memcached.h b/src/libmemcached/memcached.h index 01126042..c0878dc3 100644 --- a/src/libmemcached/memcached.h +++ b/src/libmemcached/memcached.h @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once diff --git a/src/libmemcached/memcached.hpp b/src/libmemcached/memcached.hpp index 75275be1..6ebb5c4d 100644 --- a/src/libmemcached/memcached.hpp +++ b/src/libmemcached/memcached.hpp @@ -1,40 +1,18 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include "libmemcached-1.0/memcached.hpp" - diff --git a/src/libmemcached/memory.h b/src/libmemcached/memory.h index 2a805455..f254cadd 100644 --- a/src/libmemcached/memory.h +++ b/src/libmemcached/memory.h @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once @@ -41,21 +20,17 @@ #include "libmemcached/common.h" #ifdef __cplusplus -#include -#include +# include +# include #else -#include -#include +# include +# include #endif -static inline void libmemcached_free(const memcached_st *self, void *mem) -{ - if (self) - { +static inline void libmemcached_free(const memcached_st *self, void *mem) { + if (self) { self->allocators.free(self, mem, self->allocators.context); - } - else if (mem) - { + } else if (mem) { #ifdef __cplusplus std::free(mem); #else @@ -64,10 +39,8 @@ static inline void libmemcached_free(const memcached_st *self, void *mem) } } -static inline void *libmemcached_malloc(const memcached_st *self, const size_t size) -{ - if (self) - { +static inline void *libmemcached_malloc(const memcached_st *self, const size_t size) { + if (self) { return self->allocators.malloc(self, size, self->allocators.context); } @@ -77,35 +50,36 @@ static inline void *libmemcached_malloc(const memcached_st *self, const size_t s return malloc(size); #endif } -#define libmemcached_xmalloc(__memcachd_st, __type) ((__type *)libmemcached_malloc((__memcachd_st), sizeof(__type))) +#define libmemcached_xmalloc(__memcachd_st, __type) \ + ((__type *) libmemcached_malloc((__memcachd_st), sizeof(__type))) -static inline void *libmemcached_realloc(const memcached_st *self, void *mem, size_t nmemb, const size_t size) -{ - if (self) - { +static inline void *libmemcached_realloc(const memcached_st *self, void *mem, size_t nmemb, + const size_t size) { + if (self) { return self->allocators.realloc(self, mem, nmemb * size, self->allocators.context); } #ifdef __cplusplus - return std::realloc(mem, size); + return std::realloc(mem, size); #else - return realloc(mem, size); + return realloc(mem, size); #endif } -#define libmemcached_xrealloc(__memcachd_st, __mem, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), (__mem), (__nelem), sizeof(__type))) -#define libmemcached_xvalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), NULL, (__nelem), sizeof(__type))) +#define libmemcached_xrealloc(__memcachd_st, __mem, __nelem, __type) \ + ((__type *) libmemcached_realloc((__memcachd_st), (__mem), (__nelem), sizeof(__type))) +#define libmemcached_xvalloc(__memcachd_st, __nelem, __type) \ + ((__type *) libmemcached_realloc((__memcachd_st), NULL, (__nelem), sizeof(__type))) -static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size) -{ - if (self) - { +static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size) { + if (self) { return self->allocators.calloc(self, nelem, size, self->allocators.context); } #ifdef __cplusplus - return std::calloc(nelem, size); + return std::calloc(nelem, size); #else - return calloc(nelem, size); + return calloc(nelem, size); #endif } -#define libmemcached_xcalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_calloc((__memcachd_st), (__nelem), sizeof(__type))) +#define libmemcached_xcalloc(__memcachd_st, __nelem, __type) \ + ((__type *) libmemcached_calloc((__memcachd_st), (__nelem), sizeof(__type))) diff --git a/src/libmemcached/namespace.cc b/src/libmemcached/namespace.cc index 2ddd38ed..8cf54347 100644 --- a/src/libmemcached/namespace.cc +++ b/src/libmemcached/namespace.cc @@ -1,92 +1,62 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/assert.hpp" -memcached_return_t memcached_set_namespace(Memcached& memc, const char *key, size_t key_length) -{ - if (key and key_length == 0) - { +memcached_return_t memcached_set_namespace(Memcached &memc, const char *key, size_t key_length) { + if (key and key_length == 0) { WATCHPOINT_ASSERT(key_length); - return memcached_set_error(memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid namespace, namespace string had value but length was 0")); - } - else if (key_length and key == NULL) - { + return memcached_set_error( + memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid namespace, namespace string had value but length was 0")); + } else if (key_length and key == NULL) { WATCHPOINT_ASSERT(key); - return memcached_set_error(memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid namespace, namespace string length was > 1 but namespace string was null ")); - } - else if (key and key_length) - { - bool orig= memc.flags.verify_key; - memc.flags.verify_key= true; - if (memcached_failed(memcached_key_test(memc, (const char **)&key, &key_length, 1))) - { - memc.flags.verify_key= orig; + return memcached_set_error( + memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "Invalid namespace, namespace string length was > 1 but namespace string was null ")); + } else if (key and key_length) { + bool orig = memc.flags.verify_key; + memc.flags.verify_key = true; + if (memcached_failed(memcached_key_test(memc, (const char **) &key, &key_length, 1))) { + memc.flags.verify_key = orig; return memcached_last_error(&memc); } - memc.flags.verify_key= orig; + memc.flags.verify_key = orig; - if ((key_length > MEMCACHED_MAX_NAMESPACE -1)) - { + if ((key_length > MEMCACHED_MAX_NAMESPACE - 1)) { return memcached_set_error(memc, MEMCACHED_KEY_TOO_BIG, MEMCACHED_AT); } memcached_array_free(memc._namespace); - memc._namespace= memcached_strcpy(&memc, key, key_length); + memc._namespace = memcached_strcpy(&memc, key, key_length); - if (memc._namespace == NULL) - { + if (memc._namespace == NULL) { return memcached_set_error(memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - } - else - { + } else { memcached_array_free(memc._namespace); - memc._namespace= NULL; + memc._namespace = NULL; } return MEMCACHED_SUCCESS; } -const char * memcached_get_namespace(Memcached& memc) -{ - if (memc._namespace == NULL) - { +const char *memcached_get_namespace(Memcached &memc) { + if (memc._namespace == NULL) { return NULL; } diff --git a/src/libmemcached/namespace.h b/src/libmemcached/namespace.h index 0c98ce4c..03bc7f36 100644 --- a/src/libmemcached/namespace.h +++ b/src/libmemcached/namespace.h @@ -1,46 +1,24 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifdef __cplusplus -memcached_return_t memcached_set_namespace(Memcached&, const char *str, size_t length); +memcached_return_t memcached_set_namespace(Memcached &, const char *str, size_t length); -const char * memcached_get_namespace(Memcached&); +const char *memcached_get_namespace(Memcached &); #endif // __cplusplus diff --git a/src/libmemcached/options.cc b/src/libmemcached/options.cc index c15187bb..455a8749 100644 --- a/src/libmemcached/options.cc +++ b/src/libmemcached/options.cc @@ -1,76 +1,49 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/options.hpp" #include "libmemcached/csl/context.h" -const char *memcached_parse_filename(memcached_st *memc) -{ +const char *memcached_parse_filename(memcached_st *memc) { assert_msg(memc, "Invalid memcached_st"); return memcached_array_string(memc->configure.filename); } -size_t memcached_parse_filename_length(memcached_st *memc) -{ +size_t memcached_parse_filename_length(memcached_st *memc) { return memcached_array_size(memc->configure.filename); } -static memcached_return_t _parse_file_options(memcached_st& self, memcached_array_st *real_name) -{ - FILE *fp= fopen(memcached_array_string(real_name), "r"); - if (not fp) - { - memcached_string_t error_message= memcached_array_to_string(real_name); - memcached_return_t rc= memcached_set_errno(self, errno, MEMCACHED_AT, error_message); +static memcached_return_t _parse_file_options(memcached_st &self, memcached_array_st *real_name) { + FILE *fp = fopen(memcached_array_string(real_name), "r"); + if (not fp) { + memcached_string_t error_message = memcached_array_to_string(real_name); + memcached_return_t rc = memcached_set_errno(self, errno, MEMCACHED_AT, error_message); return rc; } char buffer[BUFSIZ]; - memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS; - while (fgets(buffer, sizeof(buffer), fp)) - { - size_t length= strlen(buffer); - + memcached_return_t rc = MEMCACHED_INVALID_ARGUMENTS; + while (fgets(buffer, sizeof(buffer), fp)) { + size_t length = strlen(buffer); + if (length == 1 and buffer[0] == '\n') continue; - if (memcached_failed(rc= memcached_parse_configuration(&self, buffer, length))) + if (memcached_failed(rc = memcached_parse_configuration(&self, buffer, length))) break; } fclose(fp); @@ -78,43 +51,37 @@ static memcached_return_t _parse_file_options(memcached_st& self, memcached_arra return rc; } -memcached_return_t libmemcached_check_configuration(const char *option_string, size_t length, char *error_buffer, size_t error_buffer_size) -{ +memcached_return_t libmemcached_check_configuration(const char *option_string, size_t length, + char *error_buffer, size_t error_buffer_size) { memcached_st memc, *memc_ptr; - if (option_string == NULL or length == 0) - { + if (option_string == NULL or length == 0) { return MEMCACHED_INVALID_ARGUMENTS; } - if (error_buffer and error_buffer_size) - { - error_buffer[0]= 0; + if (error_buffer and error_buffer_size) { + error_buffer[0] = 0; } - if (not (memc_ptr= memcached_create(&memc))) - { + if (not(memc_ptr = memcached_create(&memc))) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - memcached_return_t rc= memcached_parse_configuration(memc_ptr, option_string, length); - if (memcached_failed(rc) and error_buffer and error_buffer_size) - { + memcached_return_t rc = memcached_parse_configuration(memc_ptr, option_string, length); + if (memcached_failed(rc) and error_buffer and error_buffer_size) { strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size); - error_buffer[error_buffer_size -1]= 0; + error_buffer[error_buffer_size - 1] = 0; } - bool has_filename= memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE); - if (memcached_success(rc) and has_filename) - { + bool has_filename = memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE); + if (memcached_success(rc) and has_filename) { assert_msg(memcached_parse_filename(memc_ptr), "Invalid configuration file"); assert_msg(memcached_parse_filename_length(memc_ptr), "Invalid configuration file"); - rc= _parse_file_options(*memc_ptr, memc_ptr->configure.filename); + rc = _parse_file_options(*memc_ptr, memc_ptr->configure.filename); - if (memcached_failed(rc) and error_buffer and error_buffer_size) - { + if (memcached_failed(rc) and error_buffer and error_buffer_size) { strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size); - error_buffer[error_buffer_size -1]= 0; + error_buffer[error_buffer_size - 1] = 0; } } @@ -123,11 +90,10 @@ memcached_return_t libmemcached_check_configuration(const char *option_string, s return rc; } -memcached_return_t memcached_parse_configuration(memcached_st *self, char const *option_string, size_t length) -{ +memcached_return_t memcached_parse_configuration(memcached_st *self, char const *option_string, + size_t length) { WATCHPOINT_ASSERT(self); - if (not self) - { + if (not self) { return MEMCACHED_INVALID_ARGUMENTS; } @@ -139,45 +105,41 @@ memcached_return_t memcached_parse_configuration(memcached_st *self, char const return rc; } -void memcached_set_configuration_file(memcached_st *self, const char *filename, size_t filename_length) -{ +void memcached_set_configuration_file(memcached_st *self, const char *filename, + size_t filename_length) { assert_msg(filename, "Invalid filename"); assert_msg(filename_length, "Invalid filename_length"); memcached_array_free(self->configure.filename); - self->configure.filename= memcached_strcpy(self, filename, filename_length); + self->configure.filename = memcached_strcpy(self, filename, filename_length); } -memcached_return_t memcached_parse_configure_file(memcached_st& self, const char *filename, size_t length) -{ - if (not filename) - { +memcached_return_t memcached_parse_configure_file(memcached_st &self, const char *filename, + size_t length) { + if (not filename) { return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); } WATCHPOINT_ASSERT(self); - if (not length) - { + if (not length) { return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); } - memcached_array_st *tmp_array= memcached_strcpy(&self, filename, length); + memcached_array_st *tmp_array = memcached_strcpy(&self, filename, length); - if (not tmp_array) - { + if (not tmp_array) { return memcached_set_error(self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - memcached_return_t rc= memcached_parse_configure_file(self, *tmp_array); + memcached_return_t rc = memcached_parse_configure_file(self, *tmp_array); memcached_array_free(tmp_array); return rc; } -memcached_return_t memcached_parse_configure_file(memcached_st& self, memcached_array_st& filename) -{ +memcached_return_t memcached_parse_configure_file(memcached_st &self, + memcached_array_st &filename) { WATCHPOINT_ASSERT(memcached_array_size(&filename)); - if (not memcached_array_size(&filename)) - { + if (not memcached_array_size(&filename)) { return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); } diff --git a/src/libmemcached/options.hpp b/src/libmemcached/options.hpp index bc717322..645d97b5 100644 --- a/src/libmemcached/options.hpp +++ b/src/libmemcached/options.hpp @@ -1,56 +1,37 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once LIBMEMCACHED_LOCAL - void memcached_set_configuration_file(memcached_st *self, const char *filename, size_t filename_length); +void memcached_set_configuration_file(memcached_st *self, const char *filename, + size_t filename_length); LIBMEMCACHED_LOCAL - const char *memcached_parse_filename(memcached_st *memc); +const char *memcached_parse_filename(memcached_st *memc); LIBMEMCACHED_LOCAL - memcached_return_t memcached_parse_configuration(memcached_st *ptr, const char *option_string, size_t length); +memcached_return_t memcached_parse_configuration(memcached_st *ptr, const char *option_string, + size_t length); LIBMEMCACHED_LOCAL - size_t memcached_parse_filename_length(memcached_st *memc); +size_t memcached_parse_filename_length(memcached_st *memc); LIBMEMCACHED_LOCAL - memcached_return_t memcached_parse_configure_file(memcached_st&, const char *filename, size_t length); +memcached_return_t memcached_parse_configure_file(memcached_st &, const char *filename, + size_t length); LIBMEMCACHED_LOCAL - memcached_return_t memcached_parse_configure_file(memcached_st&, memcached_array_st& filename); +memcached_return_t memcached_parse_configure_file(memcached_st &, memcached_array_st &filename); diff --git a/src/libmemcached/parse.cc b/src/libmemcached/parse.cc index ef37e539..37380465 100644 --- a/src/libmemcached/parse.cc +++ b/src/libmemcached/parse.cc @@ -1,121 +1,82 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -/* - I debated about putting this in the client library since it does an - action I don't really believe belongs in the library. - - Frankly its too damn useful not to be here though. +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #include "libmemcached/common.h" -memcached_server_list_st memcached_servers_parse(const char *server_strings) -{ +memcached_server_list_st memcached_servers_parse(const char *server_strings) { char *string; const char *begin_ptr; const char *end_ptr; - memcached_server_st *servers= NULL; + memcached_server_st *servers = NULL; memcached_return_t rc; WATCHPOINT_ASSERT(server_strings); - end_ptr= server_strings + strlen(server_strings); + end_ptr = server_strings + strlen(server_strings); - for (begin_ptr= server_strings, string= (char *)index(server_strings, ','); - begin_ptr != end_ptr; - string= (char *)index(begin_ptr, ',')) + for (begin_ptr = server_strings, string = (char *) index(server_strings, ','); + begin_ptr != end_ptr; string = (char *) index(begin_ptr, ',')) { char buffer[HUGE_STRING_LEN]; char *ptr, *ptr2; - uint32_t weight= 0; - - if (string) - { - memcpy(buffer, begin_ptr, (size_t) (string - begin_ptr)); - buffer[(unsigned int)(string - begin_ptr)]= 0; - begin_ptr= string+1; - } - else - { - size_t length= strlen(begin_ptr); + uint32_t weight = 0; + + if (string) { + memcpy(buffer, begin_ptr, (size_t)(string - begin_ptr)); + buffer[(unsigned int) (string - begin_ptr)] = 0; + begin_ptr = string + 1; + } else { + size_t length = strlen(begin_ptr); memcpy(buffer, begin_ptr, length); - buffer[length]= 0; - begin_ptr= end_ptr; + buffer[length] = 0; + begin_ptr = end_ptr; } - ptr= index(buffer, ':'); + ptr = index(buffer, ':'); - in_port_t port= 0; - if (ptr) - { - ptr[0]= 0; + in_port_t port = 0; + if (ptr) { + ptr[0] = 0; ptr++; - errno= 0; - port= (in_port_t) strtoul(ptr, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + port = (in_port_t) strtoul(ptr, (char **) NULL, 10); + if (errno != 0) { memcached_server_free(servers); return NULL; } - ptr2= index(ptr, ' '); - if (! ptr2) - ptr2= index(ptr, ':'); + ptr2 = index(ptr, ' '); + if (!ptr2) + ptr2 = index(ptr, ':'); - if (ptr2) - { + if (ptr2) { ptr2++; - errno= 0; - weight= uint32_t(strtoul(ptr2, (char **)NULL, 10)); - if (errno != 0) - { + errno = 0; + weight = uint32_t(strtoul(ptr2, (char **) NULL, 10)); + if (errno != 0) { memcached_server_free(servers); return NULL; } } } - servers= memcached_server_list_append_with_weight(servers, buffer, port, weight, &rc); + servers = memcached_server_list_append_with_weight(servers, buffer, port, weight, &rc); - if (isspace(*begin_ptr)) - { + if (isspace(*begin_ptr)) { begin_ptr++; } } diff --git a/src/libmemcached/poll.cc b/src/libmemcached/poll.cc index 6fdf242c..b8324caa 100644 --- a/src/libmemcached/poll.cc +++ b/src/libmemcached/poll.cc @@ -1,85 +1,75 @@ -/* LibMemcached - * Copyright (C) 2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker, Trond Norbye - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: Implementation of poll by using select - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #if defined(_WIN32) -#include "libmemcached/poll.h" +# include "libmemcached/poll.h" -#include -#include +# include +# include -int poll(struct pollfd fds[], nfds_t nfds, int tmo) -{ +int poll(struct pollfd fds[], nfds_t nfds, int tmo) { fd_set readfds, writefds, errorfds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&errorfds); - int maxfd= 0; + int maxfd = 0; - for (nfds_t x= 0; x < nfds; ++x) - { - if (fds[x].events & (POLLIN | POLLOUT)) - { -#ifndef _WIN32 - if (fds[x].fd > maxfd) - { - maxfd= fds[x].fd; + for (nfds_t x = 0; x < nfds; ++x) { + if (fds[x].events & (POLLIN | POLLOUT)) { +# ifndef _WIN32 + if (fds[x].fd > maxfd) { + maxfd = fds[x].fd; } -#endif - if (fds[x].events & POLLIN) - { +# endif + if (fds[x].events & POLLIN) { FD_SET(fds[x].fd, &readfds); } - if (fds[x].events & POLLOUT) - { + if (fds[x].events & POLLOUT) { FD_SET(fds[x].fd, &writefds); } } } - struct timeval timeout= { .tv_sec = tmo / 1000, - .tv_usec= (tmo % 1000) * 1000 }; - struct timeval *tp= &timeout; - if (tmo == -1) - { - tp= NULL; + struct timeval timeout = {.tv_sec = tmo / 1000, .tv_usec = (tmo % 1000) * 1000}; + struct timeval *tp = &timeout; + if (tmo == -1) { + tp = NULL; } - int ret= select(maxfd + 1, &readfds, &writefds, &errorfds, tp); - if (ret <= 0) - { + int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, tp); + if (ret <= 0) { return ret; } /* Iterate through all of them because I need to clear the revent map */ - for (nfds_t x= 0; x < nfds; ++x) - { - fds[x].revents= 0; - if (FD_ISSET(fds[x].fd, &readfds)) - { + for (nfds_t x = 0; x < nfds; ++x) { + fds[x].revents = 0; + if (FD_ISSET(fds[x].fd, &readfds)) { fds[x].revents |= POLLIN; } - if (FD_ISSET(fds[x].fd, &writefds)) - { + if (FD_ISSET(fds[x].fd, &writefds)) { fds[x].revents |= POLLOUT; } - if (FD_ISSET(fds[x].fd, &errorfds)) - { + if (FD_ISSET(fds[x].fd, &errorfds)) { fds[x].revents |= POLLERR; } } - return ret; + return ret; } #endif // defined(_WIN32) diff --git a/src/libmemcached/poll.h b/src/libmemcached/poll.h index 46636c40..fab30d1f 100644 --- a/src/libmemcached/poll.h +++ b/src/libmemcached/poll.h @@ -1,48 +1,50 @@ -/* LibMemcached - * Copyright (C) 2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker, Trond Norbye - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: Implementation of poll by using select - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #if defined(_WIN32) -#include +# include -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif -typedef struct pollfd -{ -#if defined(_WIN32) +typedef struct pollfd { +# if defined(_WIN32) SOCKET fd; -#else +# else int fd; -#endif +# endif short events; short revents; } pollfd_t; typedef int nfds_t; -#define POLLIN 0x0001 -#define POLLOUT 0x0004 -#define POLLERR 0x0008 -#define POLLHUP 0x010 /* Hung up. */ -#define POLLNVAL 0x020 /* Invalid polling request. */ +# define POLLIN 0x0001 +# define POLLOUT 0x0004 +# define POLLERR 0x0008 +# define POLLHUP 0x010 /* Hung up. */ +# define POLLNVAL 0x020 /* Invalid polling request. */ int poll(struct pollfd fds[], nfds_t nfds, int tmo); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif #endif // defined(_WIN32) diff --git a/src/libmemcached/purge.cc b/src/libmemcached/purge.cc index 10c133f2..a347e9cb 100644 --- a/src/libmemcached/purge.cc +++ b/src/libmemcached/purge.cc @@ -1,159 +1,118 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -#define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value)) +#define memcached_set_purging(__object, __value) ((__object)->state.is_purging = (__value)) -class Purge -{ +class Purge { public: - Purge(Memcached* arg) : - _memc(arg) - { + Purge(Memcached *arg) + : _memc(arg) { memcached_set_purging(_memc, true); } - ~Purge() - { - memcached_set_purging(_memc, false); - } + ~Purge() { memcached_set_purging(_memc, false); } private: - Memcached* _memc; + Memcached *_memc; }; -class PollTimeout -{ +class PollTimeout { public: - PollTimeout(Memcached* arg, int32_t ms = 50) : - _timeout(arg->poll_timeout), - _origin(arg->poll_timeout) - { + PollTimeout(Memcached *arg, int32_t ms = 50) + : _timeout(arg->poll_timeout) + , _origin(arg->poll_timeout) { _origin = ms; } - ~PollTimeout() - { - _origin= _timeout; - } + ~PollTimeout() { _origin = _timeout; } private: int32_t _timeout; - int32_t& _origin; + int32_t &_origin; }; -bool memcached_purge(memcached_instance_st* ptr) -{ - Memcached *root= (Memcached *)ptr->root; +bool memcached_purge(memcached_instance_st *ptr) { + Memcached *root = (Memcached *) ptr->root; if (memcached_is_purging(ptr->root) || /* already purging */ - (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark && - ptr->io_bytes_sent < ptr->root->io_bytes_watermark) || - (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark && - memcached_server_response_count(ptr) < 2)) + (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark + && ptr->io_bytes_sent < ptr->root->io_bytes_watermark) + || (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark + && memcached_server_response_count(ptr) < 2)) { return true; } /* memcached_io_write and memcached_response may call memcached_purge - so we need to be able stop any recursion.. + so we need to be able stop any recursion.. */ Purge set_purge(root); WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); - /* + /* Force a flush of the buffer to ensure that we don't have the n-1 pending - requests buffered up.. + requests buffered up.. */ - if (memcached_io_write(ptr) == false) - { + if (memcached_io_write(ptr) == false) { memcached_io_reset(ptr); memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); return false; } WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); - bool is_successful= true; - uint32_t no_msg= memcached_server_response_count(ptr); - if (no_msg > 1) - { + bool is_successful = true; + uint32_t no_msg = memcached_server_response_count(ptr); + if (no_msg > 1) { memcached_result_st result; /* * We need to increase the timeout, because we might be waiting for * data to be sent from the server (the commands was in the output buffer * and just flushed - */ + */ PollTimeout poll_timeout(ptr->root); - memcached_result_st* result_ptr= memcached_result_create(root, &result); + memcached_result_st *result_ptr = memcached_result_create(root, &result); assert(result_ptr); - for (uint32_t x= 0; x < no_msg - 1; x++) - { + for (uint32_t x = 0; x < no_msg - 1; x++) { memcached_result_reset(result_ptr); - memcached_return_t rc= memcached_read_one_response(ptr, result_ptr); + memcached_return_t rc = memcached_read_one_response(ptr, result_ptr); /* * Purge doesn't care for what kind of command results that is received. * The only kind of errors I care about if is I'm out of sync with the * protocol or have problems reading data from the network.. */ - if (rc== MEMCACHED_PROTOCOL_ERROR or rc == MEMCACHED_UNKNOWN_READ_FAILURE or rc == MEMCACHED_READ_FAILURE) + if (rc == MEMCACHED_PROTOCOL_ERROR or rc == MEMCACHED_UNKNOWN_READ_FAILURE + or rc == MEMCACHED_READ_FAILURE) { WATCHPOINT_ERROR(rc); - is_successful= false; + is_successful = false; } if (rc == MEMCACHED_TIMEOUT) { break; } - if (ptr->root->callbacks != NULL) - { + if (ptr->root->callbacks != NULL) { memcached_callback_st cb = *ptr->root->callbacks; - if (memcached_success(rc)) - { - for (uint32_t y= 0; y < cb.number_of_callback; y++) - { - if (memcached_fatal((*cb.callback[y])(ptr->root, result_ptr, cb.context))) - { + if (memcached_success(rc)) { + for (uint32_t y = 0; y < cb.number_of_callback; y++) { + if (memcached_fatal((*cb.callback[y])(ptr->root, result_ptr, cb.context))) { break; } } diff --git a/src/libmemcached/quit.cc b/src/libmemcached/quit.cc index ad16d2c1..195582c4 100644 --- a/src/libmemcached/quit.cc +++ b/src/libmemcached/quit.cc @@ -1,101 +1,67 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" namespace { - memcached_return_t send_quit_message(memcached_instance_st* instance) - { - memcached_return_t rc; - if (instance->root->flags.binary_protocol) - { - protocol_binary_request_quit request= {}; // = {.bytes= {0}}; +memcached_return_t send_quit_message(memcached_instance_st *instance) { + memcached_return_t rc; + if (instance->root->flags.binary_protocol) { + protocol_binary_request_quit request = {}; // = {.bytes= {0}}; - initialize_binary_request(instance, request.message.header); + initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT; - request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) } - }; + libmemcached_io_vector_st vector[] = {{request.bytes, sizeof(request.bytes)}}; - rc= memcached_vdo(instance, vector, 1, true); - } - else - { - libmemcached_io_vector_st vector[]= - { - { memcached_literal_param("quit\r\n") } - }; + rc = memcached_vdo(instance, vector, 1, true); + } else { + libmemcached_io_vector_st vector[] = {{memcached_literal_param("quit\r\n")}}; - rc= memcached_vdo(instance, vector, 1, true); - } - - return rc; + rc = memcached_vdo(instance, vector, 1, true); } - void drain_instance(memcached_instance_st* instance) - { - /* read until socket is closed, or there is an error - * closing the socket before all data is read - * results in server throwing away all data which is - * not read - * - * In .40 we began to only do this if we had been doing buffered - * requests of had replication enabled. - */ - if (instance->root->flags.buffer_requests or instance->root->number_of_replicas) - { - memcached_io_slurp(instance); - } + return rc; +} - /* - * memcached_io_read may call memcached_quit_server with io_death if - * it encounters problems, but we don't care about those occurences. - * The intention of that loop is to drain the data sent from the - * server to ensure that the server processed all of the data we - * sent to the server. - */ - instance->server_failure_counter= 0; - instance->server_timeout_counter= 0; +void drain_instance(memcached_instance_st *instance) { + /* read until socket is closed, or there is an error + * closing the socket before all data is read + * results in server throwing away all data which is + * not read + * + * In .40 we began to only do this if we had been doing buffered + * requests of had replication enabled. + */ + if (instance->root->flags.buffer_requests or instance->root->number_of_replicas) { + memcached_io_slurp(instance); } + + /* + * memcached_io_read may call memcached_quit_server with io_death if + * it encounters problems, but we don't care about those occurences. + * The intention of that loop is to drain the data sent from the + * server to ensure that the server processed all of the data we + * sent to the server. + */ + instance->server_failure_counter = 0; + instance->server_timeout_counter = 0; } +} // namespace /* This closes all connections (forces flush of input as well). @@ -106,11 +72,10 @@ namespace { will force data to be completed. */ -void memcached_quit_server(memcached_instance_st* instance, bool io_death) -{ - if (instance->valid()) - { - if (io_death == false and memcached_is_udp(instance->root) == false and instance->is_shutting_down() == false) +void memcached_quit_server(memcached_instance_st *instance, bool io_death) { + if (instance->valid()) { + if (io_death == false and memcached_is_udp(instance->root) == false + and instance->is_shutting_down() == false) { send_quit_message(instance); @@ -121,8 +86,7 @@ void memcached_quit_server(memcached_instance_st* instance, bool io_death) instance->close_socket(); - if (io_death and memcached_is_udp(instance->root)) - { + if (io_death and memcached_is_udp(instance->root)) { /* If using UDP, we should stop using the server briefly on every IO failure. If using TCP, it may be that the connection went down a @@ -134,22 +98,18 @@ void memcached_quit_server(memcached_instance_st* instance, bool io_death) } } -void send_quit(Memcached *memc) -{ - for (uint32_t x= 0; x < memcached_server_count(memc); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); +void send_quit(Memcached *memc) { + for (uint32_t x = 0; x < memcached_server_count(memc); x++) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); memcached_quit_server(instance, false); } } -void memcached_quit(memcached_st *shell) -{ - Memcached* memc= memcached2Memcached(shell); +void memcached_quit(memcached_st *shell) { + Memcached *memc = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(memc, true))) - { + if (memcached_failed(rc = initialize_query(memc, true))) { return; } diff --git a/src/libmemcached/quit.hpp b/src/libmemcached/quit.hpp index a48a8a7e..5356fbb7 100644 --- a/src/libmemcached/quit.hpp +++ b/src/libmemcached/quit.hpp @@ -1,41 +1,20 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -void memcached_quit_server(memcached_instance_st* ptr, bool io_death); +void memcached_quit_server(memcached_instance_st *ptr, bool io_death); void send_quit(memcached_st *ptr); diff --git a/src/libmemcached/response.cc b/src/libmemcached/response.cc index 8c4eb988..ffd361e3 100644 --- a/src/libmemcached/response.cc +++ b/src/libmemcached/response.cc @@ -1,146 +1,113 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include "libmemcached/string.hpp" -static memcached_return_t textual_value_fetch(memcached_instance_st* instance, - char *buffer, - memcached_result_st *result) -{ +static memcached_return_t textual_value_fetch(memcached_instance_st *instance, char *buffer, + memcached_result_st *result) { char *next_ptr; - ssize_t read_length= 0; + ssize_t read_length = 0; size_t value_length; WATCHPOINT_ASSERT(instance->root); - char *end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; + char *end_ptr = buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; memcached_result_reset(result); - char *string_ptr= buffer; - string_ptr+= 6; /* "VALUE " */ - + char *string_ptr = buffer; + string_ptr += 6; /* "VALUE " */ // Just used for cases of AES decrypt currently - memcached_return_t rc= MEMCACHED_SUCCESS; + memcached_return_t rc = MEMCACHED_SUCCESS; /* We load the key */ { - char *key= result->item_key; - result->key_length= 0; + char *key = result->item_key; + result->key_length = 0; - for (ptrdiff_t prefix_length= memcached_array_size(instance->root->_namespace); !(iscntrl(*string_ptr) || isspace(*string_ptr)) ; string_ptr++) + for (ptrdiff_t prefix_length = memcached_array_size(instance->root->_namespace); + !(iscntrl(*string_ptr) || isspace(*string_ptr)); string_ptr++) { - if (prefix_length == 0) - { - *key= *string_ptr; + if (prefix_length == 0) { + *key = *string_ptr; key++; result->key_length++; - } - else + } else prefix_length--; } - result->item_key[result->key_length]= 0; + result->item_key[result->key_length] = 0; } - if (end_ptr == string_ptr) - { + if (end_ptr == string_ptr) { goto read_error; } /* Flags fetch move past space */ string_ptr++; - if (end_ptr == string_ptr) - { + if (end_ptr == string_ptr) { goto read_error; } - for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {}; - errno= 0; - result->item_flags= (uint32_t) strtoul(next_ptr, &string_ptr, 10); + for (next_ptr = string_ptr; isdigit(*string_ptr); string_ptr++) { + }; + errno = 0; + result->item_flags = (uint32_t) strtoul(next_ptr, &string_ptr, 10); - if (errno != 0 or end_ptr == string_ptr) - { + if (errno != 0 or end_ptr == string_ptr) { goto read_error; } /* Length fetch move past space*/ string_ptr++; - if (end_ptr == string_ptr) - { + if (end_ptr == string_ptr) { goto read_error; } - for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {}; - errno= 0; - value_length= (size_t)strtoull(next_ptr, &string_ptr, 10); + for (next_ptr = string_ptr; isdigit(*string_ptr); string_ptr++) { + }; + errno = 0; + value_length = (size_t) strtoull(next_ptr, &string_ptr, 10); - if (errno != 0 or end_ptr == string_ptr) - { + if (errno != 0 or end_ptr == string_ptr) { goto read_error; } /* Skip spaces */ - if (*string_ptr == '\r') - { + if (*string_ptr == '\r') { /* Skip past the \r\n */ - string_ptr+= 2; - } - else - { + string_ptr += 2; + } else { string_ptr++; - for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {}; - errno= 0; - result->item_cas= strtoull(next_ptr, &string_ptr, 10); + for (next_ptr = string_ptr; isdigit(*string_ptr); string_ptr++) { + }; + errno = 0; + result->item_cas = strtoull(next_ptr, &string_ptr, 10); } - if (errno != 0 or end_ptr < string_ptr) - { + if (errno != 0 or end_ptr < string_ptr) { goto read_error; } /* We add two bytes so that we can walk the \r\n */ - if (memcached_failed(memcached_string_check(&result->value, value_length +2))) - { + if (memcached_failed(memcached_string_check(&result->value, value_length + 2))) { return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } { - char *value_ptr= memcached_string_value_mutable(&result->value); + char *value_ptr = memcached_string_value_mutable(&result->value); /* We read the \r\n into the string since not doing so is more cycles then the waster of memory to do so. @@ -148,55 +115,50 @@ static memcached_return_t textual_value_fetch(memcached_instance_st* instance, We are null terminating through, which will most likely make some people lazy about using the return length. */ - size_t to_read= (value_length) + 2; - memcached_return_t rrc= memcached_io_read(instance, value_ptr, to_read, read_length); - if (memcached_failed(rrc) and rrc == MEMCACHED_IN_PROGRESS) - { + size_t to_read = (value_length) + 2; + memcached_return_t rrc = memcached_io_read(instance, value_ptr, to_read, read_length); + if (memcached_failed(rrc) and rrc == MEMCACHED_IN_PROGRESS) { memcached_quit_server(instance, true); return memcached_set_error(*instance, MEMCACHED_IN_PROGRESS, MEMCACHED_AT); - } - else if (memcached_failed(rrc)) - { + } else if (memcached_failed(rrc)) { return rrc; } } - if (read_length != (ssize_t)(value_length + 2)) - { + if (read_length != (ssize_t)(value_length + 2)) { goto read_error; } /* This next bit blows the API, but this is internal....*/ { char *char_ptr; - char_ptr= memcached_string_value_mutable(&result->value);; - char_ptr[value_length]= 0; - char_ptr[value_length +1]= 0; + char_ptr = memcached_string_value_mutable(&result->value); + ; + char_ptr[value_length] = 0; + char_ptr[value_length + 1] = 0; memcached_string_set_length(&result->value, value_length); } - if (memcached_is_encrypted(instance->root) and memcached_result_length(result)) - { + if (memcached_is_encrypted(instance->root) and memcached_result_length(result)) { hashkit_string_st *destination; - if ((destination= hashkit_decrypt(&instance->root->hashkit, - memcached_result_value(result), memcached_result_length(result))) == NULL) - { - rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, - MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed")); - } - else + if ((destination = hashkit_decrypt(&instance->root->hashkit, memcached_result_value(result), + memcached_result_length(result))) + == NULL) { + rc = memcached_set_error(*instance->root, MEMCACHED_FAILURE, MEMCACHED_AT, + memcached_literal_param("hashkit_decrypt() failed")); + } else { memcached_result_reset_value(result); - if (memcached_failed(memcached_result_set_value(result, hashkit_string_c_str(destination), hashkit_string_length(destination)))) + if (memcached_failed(memcached_result_set_value(result, hashkit_string_c_str(destination), + hashkit_string_length(destination)))) { - rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, - MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed")); + rc = memcached_set_error(*instance->root, MEMCACHED_FAILURE, MEMCACHED_AT, + memcached_literal_param("hashkit_decrypt() failed")); } } - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { memcached_result_reset(result); } hashkit_string_free(destination); @@ -210,168 +172,158 @@ read_error: return MEMCACHED_PARTIAL_READ; } -static memcached_return_t textual_read_one_response(memcached_instance_st* instance, - char *buffer, const size_t buffer_length, - memcached_result_st *result) -{ +static memcached_return_t textual_read_one_response(memcached_instance_st *instance, char *buffer, + const size_t buffer_length, + memcached_result_st *result) { size_t total_read; - memcached_return_t rc= memcached_io_readline(instance, buffer, buffer_length, total_read); + memcached_return_t rc = memcached_io_readline(instance, buffer, buffer_length, total_read); - if (memcached_failed(rc)) - { + if (memcached_failed(rc)) { return rc; } assert(total_read); - switch(buffer[0]) - { - case 'V': + switch (buffer[0]) { + case 'V': { + // VALUE + if (buffer[1] == 'A' and buffer[2] == 'L' and buffer[3] == 'U' and buffer[4] == 'E') /* VALUE */ { - // VALUE - if (buffer[1] == 'A' and buffer[2] == 'L' and buffer[3] == 'U' and buffer[4] == 'E') /* VALUE */ - { - /* We add back in one because we will need to search for END */ - memcached_server_response_increment(instance); - return textual_value_fetch(instance, buffer, result); - } - // VERSION - else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'S' and buffer[4] == 'I' and buffer[5] == 'O' and buffer[6] == 'N') /* VERSION */ - { - /* Find the space, and then move one past it to copy version */ - char *response_ptr= index(buffer, ' '); - - char *endptr; - errno= 0; - long int version= strtol(response_ptr, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX or version == 0) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); - } - instance->major_version= uint8_t(version); - - endptr++; - errno= 0; - version= strtol(endptr, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version")); - } - instance->minor_version= uint8_t(version); + /* We add back in one because we will need to search for END */ + memcached_server_response_increment(instance); + return textual_value_fetch(instance, buffer, result); + } + // VERSION + else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'S' and buffer[4] == 'I' + and buffer[5] == 'O' and buffer[6] == 'N') /* VERSION */ + { + /* Find the space, and then move one past it to copy version */ + char *response_ptr = index(buffer, ' '); + + char *endptr; + errno = 0; + long int version = strtol(response_ptr, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX + or version == 0) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse major version")); + } + instance->major_version = uint8_t(version); + + endptr++; + errno = 0; + version = strtol(endptr, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse minor version")); + } + instance->minor_version = uint8_t(version); + + endptr++; + errno = 0; + version = strtol(endptr, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse micro version")); + } + instance->micro_version = uint8_t(version); - endptr++; - errno= 0; - version= strtol(endptr, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); - } - instance->micro_version= uint8_t(version); + return MEMCACHED_SUCCESS; + } + } break; - return MEMCACHED_SUCCESS; - } + case 'O': { + // OK + if (buffer[1] == 'K') { + return MEMCACHED_SUCCESS; } - break; + } break; - case 'O': + case 'S': { + // STAT + if (buffer[1] == 'T' and buffer[2] == 'A' and buffer[3] == 'T') /* STORED STATS */ { + memcached_server_response_increment(instance); + return MEMCACHED_STAT; + } + // SERVER_ERROR + else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'V' and buffer[4] == 'E' + and buffer[5] == 'R' and buffer[6] == '_' and buffer[7] == 'E' and buffer[8] == 'R' + and buffer[9] == 'R' and buffer[10] == 'O' and buffer[11] == 'R') { - // OK - if (buffer[1] == 'K') - { - return MEMCACHED_SUCCESS; + if (total_read == memcached_literal_param_size("SERVER_ERROR")) { + return MEMCACHED_SERVER_ERROR; } - } - break; - case 'S': - { - // STAT - if (buffer[1] == 'T' and buffer[2] == 'A' and buffer[3] == 'T') /* STORED STATS */ + if (total_read >= memcached_literal_param_size("SERVER_ERROR object too large for cache") + and (memcmp(buffer, memcached_literal_param("SERVER_ERROR object too large for cache")) + == 0)) { - memcached_server_response_increment(instance); - return MEMCACHED_STAT; + return MEMCACHED_E2BIG; } - // SERVER_ERROR - else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'V' and buffer[4] == 'E' and buffer[5] == 'R' - and buffer[6] == '_' - and buffer[7] == 'E' and buffer[8] == 'R' and buffer[9] == 'R' and buffer[10] == 'O' and buffer[11] == 'R' ) - { - if (total_read == memcached_literal_param_size("SERVER_ERROR")) - { - return MEMCACHED_SERVER_ERROR; - } - if (total_read >= memcached_literal_param_size("SERVER_ERROR object too large for cache") and - (memcmp(buffer, memcached_literal_param("SERVER_ERROR object too large for cache")) == 0)) - { - return MEMCACHED_E2BIG; - } - - if (total_read >= memcached_literal_param_size("SERVER_ERROR out of memory") and - ((memcmp(buffer, memcached_literal_param("SERVER_ERROR out of memory")) == 0) or - (memcmp(buffer, memcached_literal_param("SERVER_ERROR Out of memory")) == 0))) - { - return MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE; - } + if (total_read >= memcached_literal_param_size("SERVER_ERROR out of memory") + and ((memcmp(buffer, memcached_literal_param("SERVER_ERROR out of memory")) == 0) + or (memcmp(buffer, memcached_literal_param("SERVER_ERROR Out of memory")) == 0))) + { + return MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE; + } - // Move past the basic error message and whitespace - char *startptr= buffer + memcached_literal_param_size("SERVER_ERROR"); - if (startptr[0] == ' ') - { - startptr++; - } + // Move past the basic error message and whitespace + char *startptr = buffer + memcached_literal_param_size("SERVER_ERROR"); + if (startptr[0] == ' ') { + startptr++; + } - char *endptr= startptr; - while (*endptr != '\r' && *endptr != '\n') endptr++; + char *endptr = startptr; + while (*endptr != '\r' && *endptr != '\n') endptr++; - return memcached_set_error(*instance, MEMCACHED_SERVER_ERROR, MEMCACHED_AT, startptr, size_t(endptr - startptr)); - } - // STORED - else if (buffer[1] == 'T' and buffer[2] == 'O' and buffer[3] == 'R') // and buffer[4] == 'E' and buffer[5] == 'D') - { - return MEMCACHED_STORED; - } + return memcached_set_error(*instance, MEMCACHED_SERVER_ERROR, MEMCACHED_AT, startptr, + size_t(endptr - startptr)); + } + // STORED + else if (buffer[1] == 'T' and buffer[2] == 'O' + and buffer[3] == 'R') // and buffer[4] == 'E' and buffer[5] == 'D') + { + return MEMCACHED_STORED; } - break; + } break; - case 'D': + case 'D': { + // DELETED + if (buffer[1] == 'E' and buffer[2] == 'L' and buffer[3] == 'E' and buffer[4] == 'T' + and buffer[5] == 'E' and buffer[6] == 'D') { - // DELETED - if (buffer[1] == 'E' and buffer[2] == 'L' and buffer[3] == 'E' and buffer[4] == 'T' and buffer[5] == 'E' and buffer[6] == 'D') - { - return MEMCACHED_DELETED; - } + return MEMCACHED_DELETED; } - break; + } break; - case 'N': + case 'N': { + // NOT_FOUND + if (buffer[1] == 'O' and buffer[2] == 'T' and buffer[3] == '_' and buffer[4] == 'F' + and buffer[5] == 'O' and buffer[6] == 'U' and buffer[7] == 'N' and buffer[8] == 'D') { - // NOT_FOUND - if (buffer[1] == 'O' and buffer[2] == 'T' - and buffer[3] == '_' - and buffer[4] == 'F' and buffer[5] == 'O' and buffer[6] == 'U' and buffer[7] == 'N' and buffer[8] == 'D') - { - return MEMCACHED_NOTFOUND; - } - // NOT_STORED - else if (buffer[1] == 'O' and buffer[2] == 'T' - and buffer[3] == '_' - and buffer[4] == 'S' and buffer[5] == 'T' and buffer[6] == 'O' and buffer[7] == 'R' and buffer[8] == 'E' and buffer[9] == 'D') - { - return MEMCACHED_NOTSTORED; - } + return MEMCACHED_NOTFOUND; + } + // NOT_STORED + else if (buffer[1] == 'O' and buffer[2] == 'T' and buffer[3] == '_' and buffer[4] == 'S' + and buffer[5] == 'T' and buffer[6] == 'O' and buffer[7] == 'R' and buffer[8] == 'E' + and buffer[9] == 'D') + { + return MEMCACHED_NOTSTORED; } - break; + } break; case 'E': /* PROTOCOL ERROR or END */ - { - // END - if (buffer[1] == 'N' and buffer[2] == 'D') - { - return MEMCACHED_END; - } + { + // END + if (buffer[1] == 'N' and buffer[2] == 'D') { + return MEMCACHED_END; + } #if 0 // PROTOCOL_ERROR else if (buffer[1] == 'R' and buffer[2] == 'O' and buffer[3] == 'T' and buffer[4] == 'O' and buffer[5] == 'C' and buffer[6] == 'O' and buffer[7] == 'L' @@ -381,62 +333,59 @@ static memcached_return_t textual_read_one_response(memcached_instance_st* insta return MEMCACHED_PROTOCOL_ERROR; } #endif - // ERROR - else if (buffer[1] == 'R' and buffer[2] == 'R' and buffer[3] == 'O' and buffer[4] == 'R') - { - return MEMCACHED_ERROR; - } - // EXISTS - else if (buffer[1] == 'X' and buffer[2] == 'I' and buffer[3] == 'S' and buffer[4] == 'T' and buffer[5] == 'S') - { - return MEMCACHED_DATA_EXISTS; - } + // ERROR + else if (buffer[1] == 'R' and buffer[2] == 'R' and buffer[3] == 'O' and buffer[4] == 'R') + { + return MEMCACHED_ERROR; } - break; + // EXISTS + else if (buffer[1] == 'X' and buffer[2] == 'I' and buffer[3] == 'S' and buffer[4] == 'T' + and buffer[5] == 'S') + { + return MEMCACHED_DATA_EXISTS; + } + } break; case 'T': /* TOUCHED */ + { + // TOUCHED + if (buffer[1] == 'O' and buffer[2] == 'U' and buffer[3] == 'C' and buffer[4] == 'H' + and buffer[5] == 'E' and buffer[6] == 'D') { - // TOUCHED - if (buffer[1] == 'O' and buffer[2] == 'U' and buffer[3] == 'C' and buffer[4] == 'H' and buffer[5] == 'E' and buffer[6] == 'D') - { - return MEMCACHED_SUCCESS; - } + return MEMCACHED_SUCCESS; } - break; + } break; case 'I': /* ITEM */ - { - // ITEM - if (buffer[1] == 'T' and buffer[2] == 'E' and buffer[3] == 'M') - { - /* We add back in one because we will need to search for END */ - memcached_server_response_increment(instance); - return MEMCACHED_ITEM; - } + { + // ITEM + if (buffer[1] == 'T' and buffer[2] == 'E' and buffer[3] == 'M') { + /* We add back in one because we will need to search for END */ + memcached_server_response_increment(instance); + return MEMCACHED_ITEM; } - break; + } break; case 'C': /* CLIENT ERROR */ + { + // CLIENT_ERROR + if (buffer[1] == 'L' and buffer[2] == 'I' and buffer[3] == 'E' and buffer[4] == 'N' + and buffer[5] == 'T' and buffer[6] == '_' and buffer[7] == 'E' and buffer[8] == 'R' + and buffer[9] == 'R' and buffer[10] == 'O' and buffer[11] == 'R') { - // CLIENT_ERROR - if (buffer[1] == 'L' and buffer[2] == 'I' and buffer[3] == 'E' and buffer[4] == 'N' and buffer[5] == 'T' - and buffer[6] == '_' - and buffer[7] == 'E' and buffer[8] == 'R' and buffer[9] == 'R' and buffer[10] == 'O' and buffer[11] == 'R') - { - // Move past the basic error message and whitespace - char *startptr= buffer + memcached_literal_param_size("CLIENT_ERROR"); - if (startptr[0] == ' ') - { - startptr++; - } + // Move past the basic error message and whitespace + char *startptr = buffer + memcached_literal_param_size("CLIENT_ERROR"); + if (startptr[0] == ' ') { + startptr++; + } - char *endptr= startptr; - while (*endptr != '\r' && *endptr != '\n') endptr++; + char *endptr = startptr; + while (*endptr != '\r' && *endptr != '\n') endptr++; - return memcached_set_error(*instance, MEMCACHED_CLIENT_ERROR, MEMCACHED_AT, startptr, size_t(endptr - startptr)); - } + return memcached_set_error(*instance, MEMCACHED_CLIENT_ERROR, MEMCACHED_AT, startptr, + size_t(endptr - startptr)); } - break; + } break; case '0': /* INCR/DECR response */ case '1': /* INCR/DECR response */ @@ -448,40 +397,34 @@ static memcached_return_t textual_read_one_response(memcached_instance_st* insta case '7': /* INCR/DECR response */ case '8': /* INCR/DECR response */ case '9': /* INCR/DECR response */ - { - errno= 0; - unsigned long long int auto_return_value= strtoull(buffer, (char **)NULL, 10); - - if (auto_return_value == ULLONG_MAX and errno == ERANGE) - { - result->numeric_value= UINT64_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, - memcached_literal_param("Numeric response was out of range")); - } - else if (errno == EINVAL) - { - result->numeric_value= UINT64_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, - memcached_literal_param("Numeric response was out of range")); - } - else if (errno != 0) - { - result->numeric_value= UINT64_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, - memcached_literal_param("Numeric response was out of range")); - } + { + errno = 0; + unsigned long long int auto_return_value = strtoull(buffer, (char **) NULL, 10); + + if (auto_return_value == ULLONG_MAX and errno == ERANGE) { + result->numeric_value = UINT64_MAX; + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Numeric response was out of range")); + } else if (errno == EINVAL) { + result->numeric_value = UINT64_MAX; + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Numeric response was out of range")); + } else if (errno != 0) { + result->numeric_value = UINT64_MAX; + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Numeric response was out of range")); + } - result->numeric_value= uint64_t(auto_return_value); + result->numeric_value = uint64_t(auto_return_value); - WATCHPOINT_STRING(buffer); - return MEMCACHED_SUCCESS; - } + WATCHPOINT_STRING(buffer); + return MEMCACHED_SUCCESS; + } - default: - break; + default: break; } - buffer[total_read]= 0; + buffer[total_read] = 0; #if 0 if (total_read >= sizeof("STORSTORED") -1) { @@ -490,44 +433,41 @@ static memcached_return_t textual_read_one_response(memcached_instance_st* insta assert(memcmp(buffer,"STORSTORED", sizeof("STORSTORED") -1)); } #endif - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, - buffer, total_read); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, buffer, + total_read); } -static memcached_return_t binary_read_one_response(memcached_instance_st* instance, - char *buffer, const size_t buffer_length, - memcached_result_st *result) -{ +static memcached_return_t binary_read_one_response(memcached_instance_st *instance, char *buffer, + const size_t buffer_length, + memcached_result_st *result) { memcached_return_t rc; protocol_binary_response_header header; assert(memcached_is_binary(instance->root)); - if ((rc= memcached_safe_read(instance, &header.bytes, sizeof(header.bytes))) != MEMCACHED_SUCCESS) - { + if ((rc = memcached_safe_read(instance, &header.bytes, sizeof(header.bytes))) + != MEMCACHED_SUCCESS) { WATCHPOINT_ERROR(rc); return rc; } - if (header.response.magic != PROTOCOL_BINARY_RES) - { + if (header.response.magic != PROTOCOL_BINARY_RES) { return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } /* ** Convert the header to host local endian! - */ - header.response.keylen= ntohs(header.response.keylen); - header.response.status= ntohs(header.response.status); - header.response.bodylen= ntohl(header.response.bodylen); - header.response.cas= memcached_ntohll(header.response.cas); - uint32_t bodylen= header.response.bodylen; - - if (header.response.status == PROTOCOL_BINARY_RESPONSE_SUCCESS or - header.response.status == PROTOCOL_BINARY_RESPONSE_AUTH_CONTINUE) + */ + header.response.keylen = ntohs(header.response.keylen); + header.response.status = ntohs(header.response.status); + header.response.bodylen = ntohl(header.response.bodylen); + header.response.cas = memcached_ntohll(header.response.cas); + uint32_t bodylen = header.response.bodylen; + + if (header.response.status == PROTOCOL_BINARY_RESPONSE_SUCCESS + or header.response.status == PROTOCOL_BINARY_RESPONSE_AUTH_CONTINUE) { - switch (header.response.opcode) - { + switch (header.response.opcode) { case PROTOCOL_BINARY_CMD_GETKQ: /* * We didn't increment the response counter for the GETKQ packet @@ -535,145 +475,132 @@ static memcached_return_t binary_read_one_response(memcached_instance_st* instan */ memcached_server_response_increment(instance); /* fall through */ - case PROTOCOL_BINARY_CMD_GETK: - { - uint16_t keylen= header.response.keylen; - memcached_result_reset(result); - result->item_cas= header.response.cas; - - if ((rc= memcached_safe_read(instance, &result->item_flags, sizeof (result->item_flags))) != MEMCACHED_SUCCESS) - { - WATCHPOINT_ERROR(rc); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - - result->item_flags= ntohl(result->item_flags); - bodylen -= header.response.extlen; - - result->key_length= keylen; - if (memcached_failed(rc= memcached_safe_read(instance, result->item_key, keylen))) - { - WATCHPOINT_ERROR(rc); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - - // Only bother with doing this if key_length > 0 - if (result->key_length) - { - if (memcached_array_size(instance->root->_namespace) and memcached_array_size(instance->root->_namespace) >= result->key_length) - { - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); - } - - if (memcached_array_size(instance->root->_namespace)) - { - result->key_length-= memcached_array_size(instance->root->_namespace); - memmove(result->item_key, result->item_key +memcached_array_size(instance->root->_namespace), result->key_length); - } - } + case PROTOCOL_BINARY_CMD_GETK: { + uint16_t keylen = header.response.keylen; + memcached_result_reset(result); + result->item_cas = header.response.cas; - bodylen -= keylen; - if (memcached_failed(memcached_string_check(&result->value, bodylen))) - { - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } + if ((rc = memcached_safe_read(instance, &result->item_flags, sizeof(result->item_flags))) + != MEMCACHED_SUCCESS) + { + WATCHPOINT_ERROR(rc); + return MEMCACHED_UNKNOWN_READ_FAILURE; + } - char *vptr= memcached_string_value_mutable(&result->value); - if (memcached_failed(rc= memcached_safe_read(instance, vptr, bodylen))) - { - WATCHPOINT_ERROR(rc); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } + result->item_flags = ntohl(result->item_flags); + bodylen -= header.response.extlen; - memcached_string_set_length(&result->value, bodylen); + result->key_length = keylen; + if (memcached_failed(rc = memcached_safe_read(instance, result->item_key, keylen))) { + WATCHPOINT_ERROR(rc); + return MEMCACHED_UNKNOWN_READ_FAILURE; } - break; - case PROTOCOL_BINARY_CMD_INCREMENT: - case PROTOCOL_BINARY_CMD_DECREMENT: - { - if (bodylen != sizeof(uint64_t)) + // Only bother with doing this if key_length > 0 + if (result->key_length) { + if (memcached_array_size(instance->root->_namespace) + and memcached_array_size(instance->root->_namespace) >= result->key_length) { - result->numeric_value= UINT64_MAX; return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } - uint64_t val; - if ((rc= memcached_safe_read(instance, &val, sizeof(val))) != MEMCACHED_SUCCESS) - { - result->numeric_value= UINT64_MAX; - return MEMCACHED_UNKNOWN_READ_FAILURE; + if (memcached_array_size(instance->root->_namespace)) { + result->key_length -= memcached_array_size(instance->root->_namespace); + memmove(result->item_key, + result->item_key + memcached_array_size(instance->root->_namespace), + result->key_length); } + } - result->numeric_value= memcached_ntohll(val); + bodylen -= keylen; + if (memcached_failed(memcached_string_check(&result->value, bodylen))) { + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - break; - case PROTOCOL_BINARY_CMD_SASL_LIST_MECHS: - { - if (header.response.keylen != 0 || bodylen + 1 > buffer_length) - { - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - else - { - if ((rc= memcached_safe_read(instance, buffer, bodylen)) != MEMCACHED_SUCCESS) - { - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - } + char *vptr = memcached_string_value_mutable(&result->value); + if (memcached_failed(rc = memcached_safe_read(instance, vptr, bodylen))) { + WATCHPOINT_ERROR(rc); + return MEMCACHED_UNKNOWN_READ_FAILURE; } - break; - case PROTOCOL_BINARY_CMD_VERSION: - { - char version_buffer[32]; // @todo document this number - memset(version_buffer, 0, sizeof(version_buffer)); + memcached_string_set_length(&result->value, bodylen); + } break; - if (memcached_safe_read(instance, version_buffer, bodylen) != MEMCACHED_SUCCESS) - { - return MEMCACHED_UNKNOWN_READ_FAILURE; - } + case PROTOCOL_BINARY_CMD_INCREMENT: + case PROTOCOL_BINARY_CMD_DECREMENT: { + if (bodylen != sizeof(uint64_t)) { + result->numeric_value = UINT64_MAX; + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); + } - char *endptr; - errno= 0; - long int version= strtol(version_buffer, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX or version == 0) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); - } - instance->major_version= uint8_t(version); + uint64_t val; + if ((rc = memcached_safe_read(instance, &val, sizeof(val))) != MEMCACHED_SUCCESS) { + result->numeric_value = UINT64_MAX; + return MEMCACHED_UNKNOWN_READ_FAILURE; + } - endptr++; - errno= 0; - version= strtol(endptr, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version")); - } - instance->minor_version= uint8_t(version); + result->numeric_value = memcached_ntohll(val); + } break; - endptr++; - errno= 0; - version= strtol(endptr, &endptr, 10); - if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) - { - instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); + case PROTOCOL_BINARY_CMD_SASL_LIST_MECHS: { + if (header.response.keylen != 0 || bodylen + 1 > buffer_length) { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } else { + if ((rc = memcached_safe_read(instance, buffer, bodylen)) != MEMCACHED_SUCCESS) { + return MEMCACHED_UNKNOWN_READ_FAILURE; } - instance->micro_version= uint8_t(version); } - break; - - case PROTOCOL_BINARY_CMD_TOUCH: - { - rc= MEMCACHED_SUCCESS; - if (bodylen == 4) // The four byte read is a bug? - { - char touch_buffer[4]; // @todo document this number - rc= memcached_safe_read(instance, touch_buffer, sizeof(touch_buffer)); + } break; + + case PROTOCOL_BINARY_CMD_VERSION: { + char version_buffer[32]; // @todo document this number + memset(version_buffer, 0, sizeof(version_buffer)); + + if (memcached_safe_read(instance, version_buffer, bodylen) != MEMCACHED_SUCCESS) { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + + char *endptr; + errno = 0; + long int version = strtol(version_buffer, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX + or version == 0) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse major version")); + } + instance->major_version = uint8_t(version); + + endptr++; + errno = 0; + version = strtol(endptr, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse minor version")); + } + instance->minor_version = uint8_t(version); + + endptr++; + errno = 0; + version = strtol(endptr, &endptr, 10); + if (errno != 0 or version == LONG_MIN or version == LONG_MAX or version > UINT8_MAX) { + instance->major_version = instance->minor_version = instance->micro_version = UINT8_MAX; + return memcached_set_error( + *instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("strtol() failed to parse micro version")); + } + instance->micro_version = uint8_t(version); + } break; + + case PROTOCOL_BINARY_CMD_TOUCH: { + rc = MEMCACHED_SUCCESS; + if (bodylen == 4) // The four byte read is a bug? + { + char touch_buffer[4]; // @todo document this number + rc = memcached_safe_read(instance, touch_buffer, sizeof(touch_buffer)); #if 0 fprintf(stderr, "%s:%d %d %d %d %d %.*s(%d)\n", __FILE__, __LINE__, int(touch_buffer[0]), @@ -682,9 +609,9 @@ static memcached_return_t binary_read_one_response(memcached_instance_st* instan int(touch_buffer[3]), int(bodylen), touch_buffer, int(bodylen)); #endif - } - return memcached_set_error(*instance, rc, MEMCACHED_AT); } + return memcached_set_error(*instance, rc, MEMCACHED_AT); + } case PROTOCOL_BINARY_CMD_FLUSH: case PROTOCOL_BINARY_CMD_QUIT: @@ -693,135 +620,99 @@ static memcached_return_t binary_read_one_response(memcached_instance_st* instan case PROTOCOL_BINARY_CMD_REPLACE: case PROTOCOL_BINARY_CMD_APPEND: case PROTOCOL_BINARY_CMD_PREPEND: - case PROTOCOL_BINARY_CMD_DELETE: - { - WATCHPOINT_ASSERT(bodylen == 0); - return MEMCACHED_SUCCESS; - } + case PROTOCOL_BINARY_CMD_DELETE: { + WATCHPOINT_ASSERT(bodylen == 0); + return MEMCACHED_SUCCESS; + } - case PROTOCOL_BINARY_CMD_NOOP: - { - WATCHPOINT_ASSERT(bodylen == 0); - return MEMCACHED_END; - } + case PROTOCOL_BINARY_CMD_NOOP: { + WATCHPOINT_ASSERT(bodylen == 0); + return MEMCACHED_END; + } - case PROTOCOL_BINARY_CMD_STAT: - { - if (bodylen == 0) - { - return MEMCACHED_END; - } - else if (bodylen + 1 > buffer_length) + case PROTOCOL_BINARY_CMD_STAT: { + if (bodylen == 0) { + return MEMCACHED_END; + } else if (bodylen + 1 > buffer_length) { + /* not enough space in buffer.. should not happen... */ + return MEMCACHED_UNKNOWN_READ_FAILURE; + } else { + size_t keylen = header.response.keylen; + memset(buffer, 0, buffer_length); + if ((rc = memcached_safe_read(instance, buffer, keylen)) != MEMCACHED_SUCCESS + || (rc = memcached_safe_read(instance, buffer + keylen + 1, bodylen - keylen)) + != MEMCACHED_SUCCESS) { - /* not enough space in buffer.. should not happen... */ + WATCHPOINT_ERROR(rc); return MEMCACHED_UNKNOWN_READ_FAILURE; } - else - { - size_t keylen= header.response.keylen; - memset(buffer, 0, buffer_length); - if ((rc= memcached_safe_read(instance, buffer, keylen)) != MEMCACHED_SUCCESS || - (rc= memcached_safe_read(instance, buffer + keylen + 1, bodylen - keylen)) != MEMCACHED_SUCCESS) - { - WATCHPOINT_ERROR(rc); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - } } - break; + } break; case PROTOCOL_BINARY_CMD_SASL_AUTH: - case PROTOCOL_BINARY_CMD_SASL_STEP: - { - memcached_result_reset(result); - result->item_cas= header.response.cas; - - if (memcached_string_check(&result->value, - bodylen) != MEMCACHED_SUCCESS) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + case PROTOCOL_BINARY_CMD_SASL_STEP: { + memcached_result_reset(result); + result->item_cas = header.response.cas; - char *vptr= memcached_string_value_mutable(&result->value); - if ((rc= memcached_safe_read(instance, vptr, bodylen)) != MEMCACHED_SUCCESS) - { - WATCHPOINT_ERROR(rc); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } + if (memcached_string_check(&result->value, bodylen) != MEMCACHED_SUCCESS) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - memcached_string_set_length(&result->value, bodylen); - } - break; - default: - { - /* Command not implemented yet! */ - return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); + char *vptr = memcached_string_value_mutable(&result->value); + if ((rc = memcached_safe_read(instance, vptr, bodylen)) != MEMCACHED_SUCCESS) { + WATCHPOINT_ERROR(rc); + return MEMCACHED_UNKNOWN_READ_FAILURE; } + + memcached_string_set_length(&result->value, bodylen); + } break; + default: { + /* Command not implemented yet! */ + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } - } - else if (header.response.bodylen) - { + } + } else if (header.response.bodylen) { /* What should I do with the error message??? just discard it for now */ char hole[SMALL_STRING_LEN]; - while (bodylen > 0) - { - size_t nr= (bodylen > SMALL_STRING_LEN) ? SMALL_STRING_LEN : bodylen; - if ((rc= memcached_safe_read(instance, hole, nr)) != MEMCACHED_SUCCESS) - { + while (bodylen > 0) { + size_t nr = (bodylen > SMALL_STRING_LEN) ? SMALL_STRING_LEN : bodylen; + if ((rc = memcached_safe_read(instance, hole, nr)) != MEMCACHED_SUCCESS) { WATCHPOINT_ERROR(rc); return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } - bodylen-= (uint32_t) nr; + bodylen -= (uint32_t) nr; } /* This might be an error from one of the quiet commands.. if * so, just throw it away and get the next one. What about creating * a callback to the user with the error information? - */ - switch (header.response.opcode) - { + */ + switch (header.response.opcode) { case PROTOCOL_BINARY_CMD_SETQ: case PROTOCOL_BINARY_CMD_ADDQ: case PROTOCOL_BINARY_CMD_REPLACEQ: case PROTOCOL_BINARY_CMD_APPENDQ: - case PROTOCOL_BINARY_CMD_PREPENDQ: - return MEMCACHED_FETCH_NOTFINISHED; + case PROTOCOL_BINARY_CMD_PREPENDQ: return MEMCACHED_FETCH_NOTFINISHED; - default: - break; + default: break; } } - rc= MEMCACHED_SUCCESS; - if (header.response.status != 0) - { - switch (header.response.status) - { - case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT: - rc= MEMCACHED_NOTFOUND; - break; + rc = MEMCACHED_SUCCESS; + if (header.response.status != 0) { + switch (header.response.status) { + case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT: rc = MEMCACHED_NOTFOUND; break; - case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS: - rc= MEMCACHED_DATA_EXISTS; - break; + case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS: rc = MEMCACHED_DATA_EXISTS; break; - case PROTOCOL_BINARY_RESPONSE_NOT_STORED: - rc= MEMCACHED_NOTSTORED; - break; + case PROTOCOL_BINARY_RESPONSE_NOT_STORED: rc = MEMCACHED_NOTSTORED; break; - case PROTOCOL_BINARY_RESPONSE_E2BIG: - rc= MEMCACHED_E2BIG; - break; + case PROTOCOL_BINARY_RESPONSE_E2BIG: rc = MEMCACHED_E2BIG; break; - case PROTOCOL_BINARY_RESPONSE_ENOMEM: - rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE; - break; + case PROTOCOL_BINARY_RESPONSE_ENOMEM: rc = MEMCACHED_MEMORY_ALLOCATION_FAILURE; break; - case PROTOCOL_BINARY_RESPONSE_AUTH_CONTINUE: - rc= MEMCACHED_AUTH_CONTINUE; - break; + case PROTOCOL_BINARY_RESPONSE_AUTH_CONTINUE: rc = MEMCACHED_AUTH_CONTINUE; break; - case PROTOCOL_BINARY_RESPONSE_AUTH_ERROR: - rc= MEMCACHED_AUTH_FAILURE; - break; + case PROTOCOL_BINARY_RESPONSE_AUTH_ERROR: rc = MEMCACHED_AUTH_FAILURE; break; case PROTOCOL_BINARY_RESPONSE_EINVAL: case PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND: @@ -834,79 +725,65 @@ static memcached_return_t binary_read_one_response(memcached_instance_st* instan return rc; } -static memcached_return_t _read_one_response(memcached_instance_st* instance, - char *buffer, const size_t buffer_length, - memcached_result_st *result) -{ +static memcached_return_t _read_one_response(memcached_instance_st *instance, char *buffer, + const size_t buffer_length, + memcached_result_st *result) { memcached_server_response_decrement(instance); - if (result == NULL) - { - Memcached *root= (Memcached *)instance->root; + if (result == NULL) { + Memcached *root = (Memcached *) instance->root; result = &root->result; } memcached_return_t rc; - if (memcached_is_binary(instance->root)) - { + if (memcached_is_binary(instance->root)) { do { - rc= binary_read_one_response(instance, buffer, buffer_length, result); + rc = binary_read_one_response(instance, buffer, buffer_length, result); } while (rc == MEMCACHED_FETCH_NOTFINISHED); - } - else - { - rc= textual_read_one_response(instance, buffer, buffer_length, result); + } else { + rc = textual_read_one_response(instance, buffer, buffer_length, result); } - if (memcached_fatal(rc) && rc != MEMCACHED_TIMEOUT) - { + if (memcached_fatal(rc) && rc != MEMCACHED_TIMEOUT) { memcached_io_reset(instance); } return rc; } -memcached_return_t memcached_read_one_response(memcached_instance_st* instance, - memcached_result_st *result) -{ +memcached_return_t memcached_read_one_response(memcached_instance_st *instance, + memcached_result_st *result) { char buffer[SMALL_STRING_LEN]; - if (memcached_is_udp(instance->root)) - { + if (memcached_is_udp(instance->root)) { return memcached_set_error(*instance, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } - return _read_one_response(instance, buffer, sizeof(buffer), result); } -memcached_return_t memcached_response(memcached_instance_st* instance, - memcached_result_st *result) -{ +memcached_return_t memcached_response(memcached_instance_st *instance, + memcached_result_st *result) { char buffer[1024]; return memcached_response(instance, buffer, sizeof(buffer), result); } -memcached_return_t memcached_response(memcached_instance_st* instance, - char *buffer, size_t buffer_length, - memcached_result_st *result) -{ - if (memcached_is_udp(instance->root)) - { +memcached_return_t memcached_response(memcached_instance_st *instance, char *buffer, + size_t buffer_length, memcached_result_st *result) { + if (memcached_is_udp(instance->root)) { return memcached_set_error(*instance, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } /* We may have old commands in the buffer not sent, first purge */ if ((instance->root->flags.no_block) and (memcached_is_processing_input(instance->root) == false)) { - (void)memcached_io_write(instance); + (void) memcached_io_write(instance); } /* Before going into loop wait to see if we have any IO waiting for us */ - if (0) - { - memcached_return_t read_rc= memcached_io_wait_for_read(instance); + if (0) { + memcached_return_t read_rc = memcached_io_wait_for_read(instance); fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, memcached_strerror(NULL, read_rc)); } @@ -914,21 +791,21 @@ memcached_return_t memcached_response(memcached_instance_st* instance, * The previous implementation purged all pending requests and just * returned the last one. Purge all pending messages to ensure backwards * compatibility. - */ - if (memcached_is_binary(instance->root) == false and memcached_server_response_count(instance) > 1) - { + */ + if (memcached_is_binary(instance->root) == false + and memcached_server_response_count(instance) > 1) { memcached_result_st junked_result; - memcached_result_st *junked_result_ptr= memcached_result_create(instance->root, &junked_result); + memcached_result_st *junked_result_ptr = + memcached_result_create(instance->root, &junked_result); assert(junked_result_ptr); - while (memcached_server_response_count(instance) > 1) - { - memcached_return_t rc= _read_one_response(instance, buffer, buffer_length, junked_result_ptr); + while (memcached_server_response_count(instance) > 1) { + memcached_return_t rc = + _read_one_response(instance, buffer, buffer_length, junked_result_ptr); // @TODO should we return an error on another but a bad read case? - if (memcached_fatal(rc)) - { + if (memcached_fatal(rc)) { memcached_result_free(junked_result_ptr); return rc; } diff --git a/src/libmemcached/response.h b/src/libmemcached/response.h index c066f3b3..fad0c9c9 100644 --- a/src/libmemcached/response.h +++ b/src/libmemcached/response.h @@ -1,49 +1,25 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once /* Read a single response from the server */ -memcached_return_t memcached_read_one_response(memcached_instance_st* ptr, +memcached_return_t memcached_read_one_response(memcached_instance_st *ptr, memcached_result_st *result); -memcached_return_t memcached_response(memcached_instance_st* ptr, - memcached_result_st *result); +memcached_return_t memcached_response(memcached_instance_st *ptr, memcached_result_st *result); -memcached_return_t memcached_response(memcached_instance_st* ptr, - char *buffer, size_t buffer_length, - memcached_result_st *result); +memcached_return_t memcached_response(memcached_instance_st *ptr, char *buffer, + size_t buffer_length, memcached_result_st *result); diff --git a/src/libmemcached/result.cc b/src/libmemcached/result.cc index 7f2300cf..d57be515 100644 --- a/src/libmemcached/result.cc +++ b/src/libmemcached/result.cc @@ -1,189 +1,134 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - - /* - memcached_result_st are used to internally represent the return values from - memcached. We use a structure so that long term as identifiers are added - to memcached we will be able to absorb new attributes without having - to addjust the entire API. + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ + #include "libmemcached/common.h" -static inline void _result_init(memcached_result_st *self, - Memcached *memc) -{ - self->item_flags= 0; - self->item_expiration= 0; - self->key_length= 0; - self->item_cas= 0; - self->root= memc; - self->numeric_value= UINT64_MAX; - self->count= 0; - self->item_key[0]= 0; +static inline void _result_init(memcached_result_st *self, Memcached *memc) { + self->item_flags = 0; + self->item_expiration = 0; + self->key_length = 0; + self->item_cas = 0; + self->root = memc; + self->numeric_value = UINT64_MAX; + self->count = 0; + self->item_key[0] = 0; } -memcached_result_st *memcached_result_create(const memcached_st *shell, - memcached_result_st *ptr) -{ - const Memcached* memc= memcached2Memcached(shell); +memcached_result_st *memcached_result_create(const memcached_st *shell, memcached_result_st *ptr) { + const Memcached *memc = memcached2Memcached(shell); /* Saving malloc calls :) */ - if (ptr) - { - ptr->options.is_allocated= false; - } - else - { - ptr= libmemcached_xmalloc(memc, memcached_result_st); + if (ptr) { + ptr->options.is_allocated = false; + } else { + ptr = libmemcached_xmalloc(memc, memcached_result_st); - if (not ptr) - { + if (not ptr) { return NULL; } - ptr->options.is_allocated= true; + ptr->options.is_allocated = true; } - ptr->options.is_initialized= true; + ptr->options.is_initialized = true; - _result_init(ptr, (memcached_st *)memc); + _result_init(ptr, (memcached_st *) memc); - WATCHPOINT_SET(ptr->value.options.is_initialized= false); - memcached_string_create((memcached_st*)memc, &ptr->value, 0); + WATCHPOINT_SET(ptr->value.options.is_initialized = false); + memcached_string_create((memcached_st *) memc, &ptr->value, 0); WATCHPOINT_ASSERT_INITIALIZED(&ptr->value); WATCHPOINT_ASSERT(ptr->value.string == NULL); return ptr; } -void memcached_result_reset(memcached_result_st *ptr) -{ - ptr->key_length= 0; +void memcached_result_reset(memcached_result_st *ptr) { + ptr->key_length = 0; memcached_string_reset(&ptr->value); - ptr->item_flags= 0; - ptr->item_cas= 0; - ptr->item_expiration= 0; - ptr->numeric_value= UINT64_MAX; + ptr->item_flags = 0; + ptr->item_cas = 0; + ptr->item_expiration = 0; + ptr->numeric_value = UINT64_MAX; } -void memcached_result_free(memcached_result_st *ptr) -{ - if (ptr == NULL) - { +void memcached_result_free(memcached_result_st *ptr) { + if (ptr == NULL) { return; } memcached_string_free(&ptr->value); - ptr->numeric_value= UINT64_MAX; + ptr->numeric_value = UINT64_MAX; - if (memcached_is_allocated(ptr)) - { - WATCHPOINT_ASSERT(ptr->root); // Without a root, that means that result was not properly initialized. + if (memcached_is_allocated(ptr)) { + WATCHPOINT_ASSERT( + ptr->root); // Without a root, that means that result was not properly initialized. libmemcached_free(ptr->root, ptr); - } - else - { - ptr->count= 0; - ptr->options.is_initialized= false; + } else { + ptr->count = 0; + ptr->options.is_initialized = false; } } -void memcached_result_reset_value(memcached_result_st *ptr) -{ +void memcached_result_reset_value(memcached_result_st *ptr) { memcached_string_reset(&ptr->value); } -memcached_return_t memcached_result_set_value(memcached_result_st *ptr, - const char *value, - size_t length) -{ - if (memcached_failed(memcached_string_append(&ptr->value, value, length))) - { +memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, + size_t length) { + if (memcached_failed(memcached_string_append(&ptr->value, value, length))) { return memcached_set_errno(*ptr->root, errno, MEMCACHED_AT); } return MEMCACHED_SUCCESS; } -const char *memcached_result_key_value(const memcached_result_st *self) -{ +const char *memcached_result_key_value(const memcached_result_st *self) { return self->key_length ? self->item_key : NULL; } -size_t memcached_result_key_length(const memcached_result_st *self) -{ +size_t memcached_result_key_length(const memcached_result_st *self) { return self->key_length; } -const char *memcached_result_value(const memcached_result_st *self) -{ - const memcached_string_st *sptr= &self->value; +const char *memcached_result_value(const memcached_result_st *self) { + const memcached_string_st *sptr = &self->value; return memcached_string_value(sptr); } -size_t memcached_result_length(const memcached_result_st *self) -{ - const memcached_string_st *sptr= &self->value; +size_t memcached_result_length(const memcached_result_st *self) { + const memcached_string_st *sptr = &self->value; return memcached_string_length(sptr); } -char *memcached_result_take_value(memcached_result_st *self) -{ - memcached_string_st *sptr= &self->value; +char *memcached_result_take_value(memcached_result_st *self) { + memcached_string_st *sptr = &self->value; return memcached_string_take_value(sptr); } -uint32_t memcached_result_flags(const memcached_result_st *self) -{ +uint32_t memcached_result_flags(const memcached_result_st *self) { return self->item_flags; } -uint64_t memcached_result_cas(const memcached_result_st *self) -{ +uint64_t memcached_result_cas(const memcached_result_st *self) { return self->item_cas; } -void memcached_result_set_flags(memcached_result_st *self, uint32_t flags) -{ - self->item_flags= flags; +void memcached_result_set_flags(memcached_result_st *self, uint32_t flags) { + self->item_flags = flags; } -void memcached_result_set_expiration(memcached_result_st *self, time_t expiration) -{ - self->item_expiration= expiration; +void memcached_result_set_expiration(memcached_result_st *self, time_t expiration) { + self->item_expiration = expiration; } diff --git a/src/libmemcached/result.h b/src/libmemcached/result.h index 624ff5a8..4342c78b 100644 --- a/src/libmemcached/result.h +++ b/src/libmemcached/result.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once void memcached_result_reset_value(memcached_result_st *ptr); diff --git a/src/libmemcached/sasl.cc b/src/libmemcached/sasl.cc index d8247bd1..637ceeef 100644 --- a/src/libmemcached/sasl.cc +++ b/src/libmemcached/sasl.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" #include @@ -41,30 +19,25 @@ #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT -#if defined(HAVE_LIBSASL) && HAVE_LIBSASL -#include -#endif +# if defined(HAVE_LIBSASL) && HAVE_LIBSASL +# include +# endif -#define CAST_SASL_CB(cb) reinterpret_cast(reinterpret_cast(cb)) +# define CAST_SASL_CB(cb) reinterpret_cast(reinterpret_cast(cb)) -#include +# include -void memcached_set_sasl_callbacks(memcached_st *shell, - const sasl_callback_t *callbacks) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { - self->sasl.callbacks= const_cast(callbacks); - self->sasl.is_allocated= false; +void memcached_set_sasl_callbacks(memcached_st *shell, const sasl_callback_t *callbacks) { + Memcached *self = memcached2Memcached(shell); + if (self) { + self->sasl.callbacks = const_cast(callbacks); + self->sasl.is_allocated = false; } } -sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *shell) -{ - Memcached* self= memcached2Memcached(shell); - if (self) - { +sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *shell) { + Memcached *self = memcached2Memcached(shell); + if (self) { return self->sasl.callbacks; } @@ -78,95 +51,91 @@ sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *shell) * @param raddr remote address (out) * @return true on success false otherwise (errno contains more info) */ -static memcached_return_t resolve_names(memcached_instance_st& server, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length) -{ +static memcached_return_t resolve_names(memcached_instance_st &server, char *laddr, + size_t laddr_length, char *raddr, size_t raddr_length) { char host[MEMCACHED_NI_MAXHOST]; char port[MEMCACHED_NI_MAXSERV]; struct sockaddr_storage saddr; - socklen_t salen= sizeof(saddr); + socklen_t salen = sizeof(saddr); - if (getsockname(server.fd, (struct sockaddr *)&saddr, &salen) < 0) - { + if (getsockname(server.fd, (struct sockaddr *) &saddr, &salen) < 0) { return memcached_set_error(server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT); } - if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) + if (getnameinfo((struct sockaddr *) &saddr, salen, host, sizeof(host), port, sizeof(port), + NI_NUMERICHOST | NI_NUMERICSERV) + < 0) { return memcached_set_error(server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT); } - (void)snprintf(laddr, laddr_length, "%s;%s", host, port); - salen= sizeof(saddr); + (void) snprintf(laddr, laddr_length, "%s;%s", host, port); + salen = sizeof(saddr); - if (getpeername(server.fd, (struct sockaddr *)&saddr, &salen) < 0) - { + if (getpeername(server.fd, (struct sockaddr *) &saddr, &salen) < 0) { return memcached_set_error(server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT); } - if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), - port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) + if (getnameinfo((struct sockaddr *) &saddr, salen, host, sizeof(host), port, sizeof(port), + NI_NUMERICHOST | NI_NUMERICSERV) + < 0) { return memcached_set_error(server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT); } - (void)snprintf(raddr, raddr_length, "%s;%s", host, port); + (void) snprintf(raddr, raddr_length, "%s;%s", host, port); return MEMCACHED_SUCCESS; } extern "C" { -static void sasl_shutdown_function() -{ +static void sasl_shutdown_function() { sasl_done(); } static std::atomic sasl_startup_state(SASL_OK); -static pthread_mutex_t sasl_startup_state_LOCK= PTHREAD_MUTEX_INITIALIZER; -static pthread_once_t sasl_startup_once= PTHREAD_ONCE_INIT; -static void sasl_startup_function(void) -{ - sasl_startup_state= sasl_client_init(NULL); +static pthread_mutex_t sasl_startup_state_LOCK = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t sasl_startup_once = PTHREAD_ONCE_INIT; +static void sasl_startup_function(void) { + sasl_startup_state = sasl_client_init(NULL); - if (sasl_startup_state == SASL_OK) - { - (void)atexit(sasl_shutdown_function); + if (sasl_startup_state == SASL_OK) { + (void) atexit(sasl_shutdown_function); } } } // extern "C" -memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* server) -{ - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { +memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st *server) { + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { return MEMCACHED_NOT_SUPPORTED; } - if (server == NULL) - { + if (server == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } /* SANITY CHECK: SASL can only be used with the binary protocol */ - if (memcached_is_binary(server->root) == false) - { - return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("memcached_sasl_authenticate_connection() is not supported via the ASCII protocol")); + if (memcached_is_binary(server->root) == false) { + return memcached_set_error( + *server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param( + "memcached_sasl_authenticate_connection() is not supported via the ASCII protocol")); } /* Try to get the supported mech from the server. Servers without SASL * support will return UNKNOWN COMMAND, so we can just treat that * as authenticated - */ - protocol_binary_request_no_extras request= { }; + */ + protocol_binary_request_no_extras request = {}; initialize_binary_request(server, request.message.header); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_LIST_MECHS; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_SASL_LIST_MECHS; - if (memcached_io_write(server, request.bytes, sizeof(request.bytes), true) != sizeof(request.bytes)) - { + if (memcached_io_write(server, request.bytes, sizeof(request.bytes), true) + != sizeof(request.bytes)) { return MEMCACHED_WRITE_FAILURE; } assert_msg(server->fd != INVALID_SOCKET, "Programmer error, invalid socket"); @@ -174,18 +143,16 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* memcached_server_response_increment(server); char mech[MEMCACHED_MAX_BUFFER] = {0}; - memcached_return_t rc= memcached_response(server, mech, sizeof(mech) - 1, NULL); - if (memcached_failed(rc)) - { - if (rc == MEMCACHED_PROTOCOL_ERROR) - { + memcached_return_t rc = memcached_response(server, mech, sizeof(mech) - 1, NULL); + if (memcached_failed(rc)) { + if (rc == MEMCACHED_PROTOCOL_ERROR) { /* If the server doesn't support SASL it will return PROTOCOL_ERROR. * This error may also be returned for other errors, but let's assume * that the server don't support SASL and treat it as success and * let the client fail with the next operation if the error was * caused by another problem.... - */ - rc= MEMCACHED_SUCCESS; + */ + rc = MEMCACHED_SUCCESS; } return rc; @@ -196,70 +163,63 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* char laddr[MEMCACHED_NI_MAXHOST + MEMCACHED_NI_MAXSERV]; char raddr[MEMCACHED_NI_MAXHOST + MEMCACHED_NI_MAXSERV]; - if (memcached_failed(rc= resolve_names(*server, laddr, sizeof(laddr), raddr, sizeof(raddr)))) - { + if (memcached_failed(rc = resolve_names(*server, laddr, sizeof(laddr), raddr, sizeof(raddr)))) { return rc; } int pthread_error; - if ((pthread_error= pthread_once(&sasl_startup_once, sasl_startup_function)) != 0) - { + if ((pthread_error = pthread_once(&sasl_startup_once, sasl_startup_function)) != 0) { return memcached_set_errno(*server, pthread_error, MEMCACHED_AT); } - (void)pthread_mutex_lock(&sasl_startup_state_LOCK); - if (sasl_startup_state != SASL_OK) - { - const char *sasl_error_msg= sasl_errstring(sasl_startup_state, NULL, NULL); - return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + (void) pthread_mutex_lock(&sasl_startup_state_LOCK); + if (sasl_startup_state != SASL_OK) { + const char *sasl_error_msg = sasl_errstring(sasl_startup_state, NULL, NULL); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } - (void)pthread_mutex_unlock(&sasl_startup_state_LOCK); + (void) pthread_mutex_unlock(&sasl_startup_state_LOCK); sasl_conn_t *conn; int ret; - if ((ret= sasl_client_new("memcached", server->_hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK) + if ((ret = sasl_client_new("memcached", server->_hostname, laddr, raddr, + server->root->sasl.callbacks, 0, &conn)) + != SASL_OK) { - const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + const char *sasl_error_msg = sasl_errstring(ret, NULL, NULL); sasl_dispose(&conn); - return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } const char *data; const char *chosenmech; unsigned int len; - ret= sasl_client_start(conn, mech, NULL, &data, &len, &chosenmech); - if (ret != SASL_OK and ret != SASL_CONTINUE) - { - const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + ret = sasl_client_start(conn, mech, NULL, &data, &len, &chosenmech); + if (ret != SASL_OK and ret != SASL_CONTINUE) { + const char *sasl_error_msg = sasl_errstring(ret, NULL, NULL); sasl_dispose(&conn); - return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } - uint16_t keylen= (uint16_t)strlen(chosenmech); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_AUTH; - request.message.header.request.keylen= htons(keylen); - request.message.header.request.bodylen= htonl(len + keylen); + uint16_t keylen = (uint16_t) strlen(chosenmech); + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_SASL_AUTH; + request.message.header.request.keylen = htons(keylen); + request.message.header.request.bodylen = htonl(len + keylen); do { /* send the packet */ - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) }, - { chosenmech, keylen }, - { data, len } - }; + libmemcached_io_vector_st vector[] = { + {request.bytes, sizeof(request.bytes)}, {chosenmech, keylen}, {data, len}}; assert_msg(server->fd != INVALID_SOCKET, "Programmer error, invalid socket"); - if (memcached_io_writev(server, vector, 3, true) == false) - { - rc= MEMCACHED_WRITE_FAILURE; + if (memcached_io_writev(server, vector, 3, true) == false) { + rc = MEMCACHED_WRITE_FAILURE; break; } assert_msg(server->fd != INVALID_SOCKET, "Programmer error, invalid socket"); @@ -267,25 +227,23 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* /* read the response */ assert_msg(server->fd != INVALID_SOCKET, "Programmer error, invalid socket"); - rc= memcached_response(server, NULL, 0, NULL); - if (rc != MEMCACHED_AUTH_CONTINUE) - { + rc = memcached_response(server, NULL, 0, NULL); + if (rc != MEMCACHED_AUTH_CONTINUE) { break; } assert_msg(server->fd != INVALID_SOCKET, "Programmer error, invalid socket"); - ret= sasl_client_step(conn, memcached_result_value(&server->root->result), - (unsigned int)memcached_result_length(&server->root->result), - NULL, &data, &len); + ret = sasl_client_step(conn, memcached_result_value(&server->root->result), + (unsigned int) memcached_result_length(&server->root->result), NULL, + &data, &len); - if (ret != SASL_OK && ret != SASL_CONTINUE) - { - rc= MEMCACHED_AUTH_PROBLEM; + if (ret != SASL_OK && ret != SASL_CONTINUE) { + rc = MEMCACHED_AUTH_PROBLEM; break; } - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_STEP; - request.message.header.request.bodylen= htonl(len + keylen); + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_SASL_STEP; + request.message.header.request.bodylen = htonl(len + keylen); } while (true); /* Release resources */ @@ -294,171 +252,149 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* return memcached_set_error(*server, rc, MEMCACHED_AT); } -static int get_username(void *context, int id, const char **result, unsigned int *len) -{ - if (!context || !result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) - { +static int get_username(void *context, int id, const char **result, unsigned int *len) { + if (!context || !result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) { return SASL_BADPARAM; } - *result= (char *)context; - if (len) - { - *len= (unsigned int)strlen(*result); + *result = (char *) context; + if (len) { + *len = (unsigned int) strlen(*result); } return SASL_OK; } -static int get_password(sasl_conn_t *conn, void *context, int id, - sasl_secret_t **psecret) -{ - if (!conn || ! psecret || id != SASL_CB_PASS) - { +static int get_password(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret) { + if (!conn || !psecret || id != SASL_CB_PASS) { return SASL_BADPARAM; } - *psecret= (sasl_secret_t *)context; + *psecret = (sasl_secret_t *) context; return SASL_OK; } -memcached_return_t memcached_set_sasl_auth_data(memcached_st *shell, - const char *username, - const char *password) -{ - Memcached* ptr= memcached2Memcached(shell); - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { +memcached_return_t memcached_set_sasl_auth_data(memcached_st *shell, const char *username, + const char *password) { + Memcached *ptr = memcached2Memcached(shell); + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { return MEMCACHED_NOT_SUPPORTED; } - if (ptr == NULL or username == NULL or password == NULL) - { + if (ptr == NULL or username == NULL or password == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } memcached_return_t ret; - if (memcached_failed(ret= memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) - { - return memcached_set_error(*ptr, ret, MEMCACHED_AT, memcached_literal_param("Unable change to binary protocol which is required for SASL.")); + if (memcached_failed(ret = memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) { + return memcached_set_error( + *ptr, ret, MEMCACHED_AT, + memcached_literal_param("Unable change to binary protocol which is required for SASL.")); } memcached_destroy_sasl_auth_data(ptr); - sasl_callback_t *callbacks= libmemcached_xcalloc(ptr, 4, sasl_callback_t); - size_t password_length= strlen(password); - size_t username_length= strlen(username); - char *name= (char *)libmemcached_malloc(ptr, username_length +1); - sasl_secret_t *secret= (sasl_secret_t*)libmemcached_malloc(ptr, password_length +1 + sizeof(sasl_secret_t)); + sasl_callback_t *callbacks = libmemcached_xcalloc(ptr, 4, sasl_callback_t); + size_t password_length = strlen(password); + size_t username_length = strlen(username); + char *name = (char *) libmemcached_malloc(ptr, username_length + 1); + sasl_secret_t *secret = + (sasl_secret_t *) libmemcached_malloc(ptr, password_length + 1 + sizeof(sasl_secret_t)); - if (callbacks == NULL or name == NULL or secret == NULL) - { + if (callbacks == NULL or name == NULL or secret == NULL) { libmemcached_free(ptr, callbacks); libmemcached_free(ptr, name); libmemcached_free(ptr, secret); return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - secret->len= password_length; + secret->len = password_length; memcpy(secret->data, password, password_length); - secret->data[password_length]= 0; - - callbacks[0].id= SASL_CB_USER; - callbacks[0].proc= CAST_SASL_CB(get_username); - callbacks[0].context= strncpy(name, username, username_length +1); - callbacks[1].id= SASL_CB_AUTHNAME; - callbacks[1].proc= CAST_SASL_CB(get_username); - callbacks[1].context= name; - callbacks[2].id= SASL_CB_PASS; - callbacks[2].proc= CAST_SASL_CB(get_password); - callbacks[2].context= secret; - callbacks[3].id= SASL_CB_LIST_END; - - ptr->sasl.callbacks= callbacks; - ptr->sasl.is_allocated= true; + secret->data[password_length] = 0; + + callbacks[0].id = SASL_CB_USER; + callbacks[0].proc = CAST_SASL_CB(get_username); + callbacks[0].context = strncpy(name, username, username_length + 1); + callbacks[1].id = SASL_CB_AUTHNAME; + callbacks[1].proc = CAST_SASL_CB(get_username); + callbacks[1].context = name; + callbacks[2].id = SASL_CB_PASS; + callbacks[2].proc = CAST_SASL_CB(get_password); + callbacks[2].context = secret; + callbacks[3].id = SASL_CB_LIST_END; + + ptr->sasl.callbacks = callbacks; + ptr->sasl.is_allocated = true; return MEMCACHED_SUCCESS; } -memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *shell) -{ - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { +memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *shell) { + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { return MEMCACHED_NOT_SUPPORTED; } - Memcached* ptr= memcached2Memcached(shell); - if (ptr == NULL) - { + Memcached *ptr = memcached2Memcached(shell); + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (ptr->sasl.callbacks == NULL) - { + if (ptr->sasl.callbacks == NULL) { return MEMCACHED_SUCCESS; } - if (ptr->sasl.is_allocated) - { + if (ptr->sasl.is_allocated) { libmemcached_free(ptr, ptr->sasl.callbacks[0].context); libmemcached_free(ptr, ptr->sasl.callbacks[2].context); - libmemcached_free(ptr, (void*)ptr->sasl.callbacks); - ptr->sasl.is_allocated= false; + libmemcached_free(ptr, (void *) ptr->sasl.callbacks); + ptr->sasl.is_allocated = false; } - ptr->sasl.callbacks= NULL; + ptr->sasl.callbacks = NULL; return MEMCACHED_SUCCESS; } -memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source) -{ - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { +memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source) { + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { return MEMCACHED_NOT_SUPPORTED; } - if (clone == NULL or source == NULL) - { + if (clone == NULL or source == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } - if (source->sasl.callbacks == NULL) - { + if (source->sasl.callbacks == NULL) { return MEMCACHED_SUCCESS; } /* Hopefully we are using our own callback mechanisms.. */ - if (source->sasl.callbacks[0].id == SASL_CB_USER && - source->sasl.callbacks[0].proc == CAST_SASL_CB(get_username) && - source->sasl.callbacks[1].id == SASL_CB_AUTHNAME && - source->sasl.callbacks[1].proc == CAST_SASL_CB(get_username) && - source->sasl.callbacks[2].id == SASL_CB_PASS && - source->sasl.callbacks[2].proc == CAST_SASL_CB(get_password) && - source->sasl.callbacks[3].id == SASL_CB_LIST_END) + if (source->sasl.callbacks[0].id == SASL_CB_USER + && source->sasl.callbacks[0].proc == CAST_SASL_CB(get_username) + && source->sasl.callbacks[1].id == SASL_CB_AUTHNAME + && source->sasl.callbacks[1].proc == CAST_SASL_CB(get_username) + && source->sasl.callbacks[2].id == SASL_CB_PASS + && source->sasl.callbacks[2].proc == CAST_SASL_CB(get_password) + && source->sasl.callbacks[3].id == SASL_CB_LIST_END) { - sasl_secret_t *secret= (sasl_secret_t *)source->sasl.callbacks[2].context; - return memcached_set_sasl_auth_data(clone, - (const char*)source->sasl.callbacks[0].context, - (const char*)secret->data); + sasl_secret_t *secret = (sasl_secret_t *) source->sasl.callbacks[2].context; + return memcached_set_sasl_auth_data(clone, (const char *) source->sasl.callbacks[0].context, + (const char *) secret->data); } /* * But we're not. It may work if we know what the user tries to pass * into the list, but if we don't know the ID we don't know how to handle * the context... - */ - ptrdiff_t total= 0; + */ + ptrdiff_t total = 0; - while (source->sasl.callbacks[total].id != SASL_CB_LIST_END) - { - switch (source->sasl.callbacks[total].id) - { + while (source->sasl.callbacks[total].id != SASL_CB_LIST_END) { + switch (source->sasl.callbacks[total].id) { case SASL_CB_USER: case SASL_CB_AUTHNAME: - case SASL_CB_PASS: - break; + case SASL_CB_PASS: break; default: /* I don't know how to deal with this... */ return MEMCACHED_NOT_SUPPORTED; @@ -467,42 +403,35 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st ++total; } - sasl_callback_t *callbacks= libmemcached_xcalloc(clone, total +1, sasl_callback_t); - if (callbacks == NULL) - { + sasl_callback_t *callbacks = libmemcached_xcalloc(clone, total + 1, sasl_callback_t); + if (callbacks == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } memcpy(callbacks, source->sasl.callbacks, (total + 1) * sizeof(sasl_callback_t)); /* Now update the context... */ - for (ptrdiff_t x= 0; x < total; ++x) - { - if (callbacks[x].id == SASL_CB_USER || callbacks[x].id == SASL_CB_AUTHNAME) - { - callbacks[x].context= (sasl_callback_t*)libmemcached_malloc(clone, strlen((const char*)source->sasl.callbacks[x].context)); + for (ptrdiff_t x = 0; x < total; ++x) { + if (callbacks[x].id == SASL_CB_USER || callbacks[x].id == SASL_CB_AUTHNAME) { + callbacks[x].context = (sasl_callback_t *) libmemcached_malloc( + clone, strlen((const char *) source->sasl.callbacks[x].context)); - if (callbacks[x].context == NULL) - { + if (callbacks[x].context == NULL) { /* Failed to allocate memory, clean up previously allocated memory */ - for (ptrdiff_t y= 0; y < x; ++y) - { + for (ptrdiff_t y = 0; y < x; ++y) { libmemcached_free(clone, clone->sasl.callbacks[y].context); } libmemcached_free(clone, callbacks); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - strncpy((char*)callbacks[x].context, (const char*)source->sasl.callbacks[x].context, sizeof(callbacks[x].context)); - } - else - { - sasl_secret_t *src= (sasl_secret_t *)source->sasl.callbacks[x].context; - sasl_secret_t *n= (sasl_secret_t*)libmemcached_malloc(clone, src->len + 1 + sizeof(*n)); - if (n == NULL) - { + strncpy((char *) callbacks[x].context, (const char *) source->sasl.callbacks[x].context, + sizeof(callbacks[x].context)); + } else { + sasl_secret_t *src = (sasl_secret_t *) source->sasl.callbacks[x].context; + sasl_secret_t *n = (sasl_secret_t *) libmemcached_malloc(clone, src->len + 1 + sizeof(*n)); + if (n == NULL) { /* Failed to allocate memory, clean up previously allocated memory */ - for (ptrdiff_t y= 0; y < x; ++y) - { + for (ptrdiff_t y = 0; y < x; ++y) { libmemcached_free(clone, clone->sasl.callbacks[y].context); } @@ -510,34 +439,29 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } memcpy(n, src, src->len + 1 + sizeof(*n)); - callbacks[x].context= n; + callbacks[x].context = n; } } - clone->sasl.callbacks= callbacks; - clone->sasl.is_allocated= true; + clone->sasl.callbacks = callbacks; + clone->sasl.is_allocated = true; return MEMCACHED_SUCCESS; } #else -void memcached_set_sasl_callbacks(memcached_st *, const sasl_callback_t *) -{ -} +void memcached_set_sasl_callbacks(memcached_st *, const sasl_callback_t *) {} -sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *) -{ +sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *) { return NULL; } -memcached_return_t memcached_set_sasl_auth_data(memcached_st *, const char *, const char *) -{ +memcached_return_t memcached_set_sasl_auth_data(memcached_st *, const char *, const char *) { return MEMCACHED_NOT_SUPPORTED; } -memcached_return_t memcached_clone_sasl(memcached_st *, const memcached_st *) -{ +memcached_return_t memcached_clone_sasl(memcached_st *, const memcached_st *) { return MEMCACHED_NOT_SUPPORTED; } diff --git a/src/libmemcached/sasl.hpp b/src/libmemcached/sasl.hpp index bda6b227..f51b7d69 100644 --- a/src/libmemcached/sasl.hpp +++ b/src/libmemcached/sasl.hpp @@ -1,42 +1,20 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source); +memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source); -memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st* server); +memcached_return_t memcached_sasl_authenticate_connection(memcached_instance_st *server); diff --git a/src/libmemcached/server.cc b/src/libmemcached/server.cc index bb8420e3..bf028621 100644 --- a/src/libmemcached/server.cc +++ b/src/libmemcached/server.cc @@ -1,129 +1,89 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - /* - This is a partial implementation for fetching/creating memcached_server_st objects. + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ + #include "libmemcached/common.h" static inline void _server_init(memcached_server_st *self, Memcached *root, - const memcached_string_t& hostname, - in_port_t port, - uint32_t weight, memcached_connection_t type) -{ - self->options.is_shutting_down= false; - self->options.is_dead= false; - self->number_of_hosts= 0; - self->cursor_active= 0; - self->port= port; - self->io_bytes_sent= 0; - self->request_id= 0; - self->server_failure_counter= 0; - self->server_failure_counter_query_id= 0; - self->server_timeout_counter= 0; - self->server_timeout_counter_query_id= 0; - self->weight= weight ? weight : 1; // 1 is the default weight value - self->io_wait_count.read= 0; - self->io_wait_count.write= 0; - self->io_wait_count.timeouts= 0; - self->io_wait_count._bytes_read= 0; - self->major_version= UINT8_MAX; - self->micro_version= UINT8_MAX; - self->minor_version= UINT8_MAX; - self->type= type; - self->error_messages= NULL; - - self->state= MEMCACHED_SERVER_STATE_NEW; - self->next_retry= 0; - - self->root= root; - if (root) - { - self->version= ++root->server_info.version; - } - else - { - self->version= UINT_MAX; - } - self->limit_maxbytes= 0; + const memcached_string_t &hostname, in_port_t port, uint32_t weight, + memcached_connection_t type) { + self->options.is_shutting_down = false; + self->options.is_dead = false; + self->number_of_hosts = 0; + self->cursor_active = 0; + self->port = port; + self->io_bytes_sent = 0; + self->request_id = 0; + self->server_failure_counter = 0; + self->server_failure_counter_query_id = 0; + self->server_timeout_counter = 0; + self->server_timeout_counter_query_id = 0; + self->weight = weight ? weight : 1; // 1 is the default weight value + self->io_wait_count.read = 0; + self->io_wait_count.write = 0; + self->io_wait_count.timeouts = 0; + self->io_wait_count._bytes_read = 0; + self->major_version = UINT8_MAX; + self->micro_version = UINT8_MAX; + self->minor_version = UINT8_MAX; + self->type = type; + self->error_messages = NULL; + + self->state = MEMCACHED_SERVER_STATE_NEW; + self->next_retry = 0; + + self->root = root; + if (root) { + self->version = ++root->server_info.version; + } else { + self->version = UINT_MAX; + } + self->limit_maxbytes = 0; memcpy(self->hostname, hostname.c_str, hostname.size); - self->hostname[hostname.size]= 0; + self->hostname[hostname.size] = 0; } -static memcached_server_st *_server_create(memcached_server_st *self, const Memcached *memc) -{ - if (self == NULL) - { - self= libmemcached_xmalloc(memc, struct memcached_server_st); +static memcached_server_st *_server_create(memcached_server_st *self, const Memcached *memc) { + if (self == NULL) { + self = libmemcached_xmalloc(memc, struct memcached_server_st); - if (self == NULL) - { + if (self == NULL) { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ } - self->options.is_allocated= true; - } - else - { - self->options.is_allocated= false; + self->options.is_allocated = true; + } else { + self->options.is_allocated = false; } - self->options.is_initialized= true; + self->options.is_initialized = true; return self; } -memcached_server_st *__server_create_with(Memcached *memc, - memcached_server_st* allocated_instance, - const memcached_string_t& hostname, - const in_port_t port, - uint32_t weight, - const memcached_connection_t type) -{ - if (memcached_is_valid_servername(hostname) == false) - { - memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); +memcached_server_st *__server_create_with(Memcached *memc, memcached_server_st *allocated_instance, + const memcached_string_t &hostname, const in_port_t port, + uint32_t weight, const memcached_connection_t type) { + if (memcached_is_valid_servername(hostname) == false) { + memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid hostname provided")); return NULL; } - allocated_instance= _server_create(allocated_instance, memc); + allocated_instance = _server_create(allocated_instance, memc); - if (allocated_instance == NULL) - { + if (allocated_instance == NULL) { return NULL; } @@ -132,29 +92,22 @@ memcached_server_st *__server_create_with(Memcached *memc, return allocated_instance; } -void __server_free(memcached_server_st *self) -{ +void __server_free(memcached_server_st *self) { memcached_error_free(*self); - if (memcached_is_allocated(self)) - { + if (memcached_is_allocated(self)) { libmemcached_free(self->root, self); - } - else - { - self->options.is_initialized= false; + } else { + self->options.is_initialized = false; } } -void memcached_server_free(memcached_server_st *self) -{ - if (self == NULL) - { +void memcached_server_free(memcached_server_st *self) { + if (self == NULL) { return; } - if (memcached_server_list_count(self)) - { + if (memcached_server_list_count(self)) { memcached_server_list_free(self); return; } @@ -162,30 +115,25 @@ void memcached_server_free(memcached_server_st *self) __server_free(self); } -void memcached_server_error_reset(memcached_server_st *self) -{ +void memcached_server_error_reset(memcached_server_st *self) { WATCHPOINT_ASSERT(self); - if (self == NULL) - { + if (self == NULL) { return; } memcached_error_free(*self); } -uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count) -{ +uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count) { WATCHPOINT_ASSERT(servers); - if (servers == NULL) - { + if (servers == NULL) { return 0; } - return servers->number_of_hosts= count; + return servers->number_of_hosts = count; } -uint32_t memcached_server_count(const memcached_st *self) -{ +uint32_t memcached_server_count(const memcached_st *self) { WATCHPOINT_ASSERT(self); if (self == NULL) return 0; @@ -193,101 +141,82 @@ uint32_t memcached_server_count(const memcached_st *self) return self->number_of_hosts; } -const char *memcached_server_name(const memcached_instance_st * self) -{ +const char *memcached_server_name(const memcached_instance_st *self) { WATCHPOINT_ASSERT(self); - if (self) - { + if (self) { return self->_hostname; } return NULL; } -in_port_t memcached_server_port(const memcached_instance_st * self) -{ +in_port_t memcached_server_port(const memcached_instance_st *self) { WATCHPOINT_ASSERT(self); - if (self == NULL) - { + if (self == NULL) { return 0; } return self->port(); } -in_port_t memcached_server_srcport(const memcached_instance_st * self) -{ +in_port_t memcached_server_srcport(const memcached_instance_st *self) { WATCHPOINT_ASSERT(self); - if (self == NULL || self->fd == INVALID_SOCKET || (self->type != MEMCACHED_CONNECTION_TCP && self->type != MEMCACHED_CONNECTION_UDP)) + if (self == NULL || self->fd == INVALID_SOCKET + || (self->type != MEMCACHED_CONNECTION_TCP && self->type != MEMCACHED_CONNECTION_UDP)) { return 0; } struct sockaddr_in sin; - socklen_t addrlen= sizeof(sin); - if (getsockname(self->fd, (struct sockaddr*)&sin, &addrlen) != -1) - { + socklen_t addrlen = sizeof(sin); + if (getsockname(self->fd, (struct sockaddr *) &sin, &addrlen) != -1) { return ntohs(sin.sin_port); } return -1; } -uint32_t memcached_server_response_count(const memcached_instance_st * self) -{ +uint32_t memcached_server_response_count(const memcached_instance_st *self) { WATCHPOINT_ASSERT(self); - if (self == NULL) - { + if (self == NULL) { return 0; } return self->cursor_active_; } -const char *memcached_server_type(const memcached_instance_st * ptr) -{ - if (ptr) - { - switch (ptr->type) - { - case MEMCACHED_CONNECTION_TCP: - return "TCP"; +const char *memcached_server_type(const memcached_instance_st *ptr) { + if (ptr) { + switch (ptr->type) { + case MEMCACHED_CONNECTION_TCP: return "TCP"; - case MEMCACHED_CONNECTION_UDP: - return "UDP"; + case MEMCACHED_CONNECTION_UDP: return "UDP"; - case MEMCACHED_CONNECTION_UNIX_SOCKET: - return "SOCKET"; + case MEMCACHED_CONNECTION_UNIX_SOCKET: return "SOCKET"; } } return "UNKNOWN"; } -uint8_t memcached_server_major_version(const memcached_instance_st * instance) -{ - if (instance) - { +uint8_t memcached_server_major_version(const memcached_instance_st *instance) { + if (instance) { return instance->major_version; } return UINT8_MAX; } -uint8_t memcached_server_minor_version(const memcached_instance_st * instance) -{ - if (instance) - { +uint8_t memcached_server_minor_version(const memcached_instance_st *instance) { + if (instance) { return instance->minor_version; } return UINT8_MAX; } -uint8_t memcached_server_micro_version(const memcached_instance_st * instance) -{ - if (instance) - { +uint8_t memcached_server_micro_version(const memcached_instance_st *instance) { + if (instance) { return instance->micro_version; } diff --git a/src/libmemcached/server.hpp b/src/libmemcached/server.hpp index 582356d2..017242bd 100644 --- a/src/libmemcached/server.hpp +++ b/src/libmemcached/server.hpp @@ -1,104 +1,67 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifdef HAVE_SYS_TIME_H -#include +# include #endif #include -memcached_server_st *__server_create_with(memcached_st *memc, - memcached_server_st* self, - const memcached_string_t& hostname, - const in_port_t port, - uint32_t weight, - const memcached_connection_t type); +memcached_server_st *__server_create_with(memcached_st *memc, memcached_server_st *self, + const memcached_string_t &hostname, const in_port_t port, + uint32_t weight, const memcached_connection_t type); -memcached_return_t memcached_server_add_parsed(memcached_st *ptr, - const char *hostname, - size_t hostname_length, - in_port_t port, +memcached_return_t memcached_server_add_parsed(memcached_st *ptr, const char *hostname, + size_t hostname_length, in_port_t port, uint32_t weight); void __server_free(memcached_server_st *); -static inline bool memcached_is_valid_servername(const memcached_string_t& arg) -{ +static inline bool memcached_is_valid_servername(const memcached_string_t &arg) { return (arg.c_str != NULL or arg.size == 0) and arg.size < MEMCACHED_NI_MAXHOST; } -static inline bool memcached_is_valid_filename(const memcached_string_t& arg) -{ +static inline bool memcached_is_valid_filename(const memcached_string_t &arg) { return arg.c_str != NULL and arg.size > 0 and arg.size < MEMCACHED_NI_MAXHOST; } void memcached_instance_free(memcached_instance_st *); -void set_last_disconnected_host(memcached_instance_st* self); +void set_last_disconnected_host(memcached_instance_st *self); -static inline void memcached_mark_server_for_timeout(memcached_instance_st* server) -{ - if (server->state != MEMCACHED_SERVER_STATE_IN_TIMEOUT) - { - if (server->server_timeout_counter_query_id != server->root->query_id) - { +static inline void memcached_mark_server_for_timeout(memcached_instance_st *server) { + if (server->state != MEMCACHED_SERVER_STATE_IN_TIMEOUT) { + if (server->server_timeout_counter_query_id != server->root->query_id) { server->server_timeout_counter++; - server->server_timeout_counter_query_id= server->root->query_id; + server->server_timeout_counter_query_id = server->root->query_id; } - if (server->server_timeout_counter >= server->root->server_timeout_limit) - { + if (server->server_timeout_counter >= server->root->server_timeout_limit) { struct timeval next_time; - if (gettimeofday(&next_time, NULL) == 0) - { - server->next_retry= next_time.tv_sec +server->root->retry_timeout; - } - else - { - server->next_retry= 1; // Setting the value to 1 causes the timeout to occur immediately + if (gettimeofday(&next_time, NULL) == 0) { + server->next_retry = next_time.tv_sec + server->root->retry_timeout; + } else { + server->next_retry = 1; // Setting the value to 1 causes the timeout to occur immediately } - server->state= MEMCACHED_SERVER_STATE_IN_TIMEOUT; - if (server->server_failure_counter_query_id != server->root->query_id) - { + server->state = MEMCACHED_SERVER_STATE_IN_TIMEOUT; + if (server->server_failure_counter_query_id != server->root->query_id) { server->server_failure_counter++; - server->server_failure_counter_query_id= server->root->query_id; + server->server_failure_counter_query_id = server->root->query_id; } set_last_disconnected_host(server); } diff --git a/src/libmemcached/server_instance.h b/src/libmemcached/server_instance.h index 187f2744..ccf22e04 100644 --- a/src/libmemcached/server_instance.h +++ b/src/libmemcached/server_instance.h @@ -1,60 +1,37 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifdef __cplusplus -typedef struct memcached_instance_st* memcached_server_write_instance_st; +typedef struct memcached_instance_st *memcached_server_write_instance_st; #else -typedef void* memcached_server_write_instance_st; +typedef void *memcached_server_write_instance_st; #endif #ifdef __cplusplus extern "C" { #endif -typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context); +typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, + memcached_server_write_instance_st server, + void *context); -memcached_return_t memcached_server_execute(memcached_st *ptr, - memcached_server_execute_fn callback, +memcached_return_t memcached_server_execute(memcached_st *ptr, memcached_server_execute_fn callback, void *context); #ifdef __cplusplus } // extern "C" #endif - - diff --git a/src/libmemcached/server_list.cc b/src/libmemcached/server_list.cc index d8c9c40a..d5c39c34 100644 --- a/src/libmemcached/server_list.cc +++ b/src/libmemcached/server_list.cc @@ -1,90 +1,59 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -memcached_server_list_st -memcached_server_list_append_with_weight(memcached_server_list_st ptr, - const char *hostname, in_port_t port, - uint32_t weight, - memcached_return_t *error) -{ +memcached_server_list_st memcached_server_list_append_with_weight(memcached_server_list_st ptr, + const char *hostname, + in_port_t port, uint32_t weight, + memcached_return_t *error) { memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - if (hostname == NULL) - { - hostname= "localhost"; + if (hostname == NULL) { + hostname = "localhost"; } - if (hostname[0] == '/') - { + if (hostname[0] == '/') { port = 0; + } else if (port == 0) { + port = MEMCACHED_DEFAULT_PORT; } - else if (port == 0) - { - port= MEMCACHED_DEFAULT_PORT; - } - /* Increment count for hosts */ - uint32_t count= 1; - if (ptr != NULL) - { - count+= memcached_server_list_count(ptr); + uint32_t count = 1; + if (ptr != NULL) { + count += memcached_server_list_count(ptr); } - memcached_server_list_st new_host_list= (memcached_server_st*)realloc(ptr, sizeof(memcached_server_st) * count); - if (new_host_list == NULL) - { + memcached_server_list_st new_host_list = + (memcached_server_st *) realloc(ptr, sizeof(memcached_server_st) * count); + if (new_host_list == NULL) { #if 0 *error= memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); #endif return NULL; } - memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) }; + memcached_string_t _hostname = {memcached_string_make_from_cstr(hostname)}; /* @todo Check return type */ - if (__server_create_with(NULL, &new_host_list[count-1], _hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET) == NULL) + if (__server_create_with(NULL, &new_host_list[count - 1], _hostname, port, weight, + port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET) + == NULL) { #if 0 *error= memcached_set_errno(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); @@ -101,46 +70,37 @@ memcached_server_list_append_with_weight(memcached_server_list_st ptr, /* Backwards compatibility hack */ memcached_servers_set_count(new_host_list, count); - *error= MEMCACHED_SUCCESS; + *error = MEMCACHED_SUCCESS; return new_host_list; } -memcached_server_list_st -memcached_server_list_append(memcached_server_list_st ptr, - const char *hostname, in_port_t port, - memcached_return_t *error) -{ +memcached_server_list_st memcached_server_list_append(memcached_server_list_st ptr, + const char *hostname, in_port_t port, + memcached_return_t *error) { return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error); } -uint32_t memcached_server_list_count(const memcached_server_list_st self) -{ - return (self == NULL) - ? 0 - : self->number_of_hosts; +uint32_t memcached_server_list_count(const memcached_server_list_st self) { + return (self == NULL) ? 0 : self->number_of_hosts; } -uint32_t memcached_instance_list_count(const memcached_st* self) -{ - return (self == NULL) - ? 0 - : self->number_of_hosts; +uint32_t memcached_instance_list_count(const memcached_st *self) { + return (self == NULL) ? 0 : self->number_of_hosts; } -void memcached_instance_set(memcached_st* memc, memcached_instance_st* list, const uint32_t host_list_size) -{ +void memcached_instance_set(memcached_st *memc, memcached_instance_st *list, + const uint32_t host_list_size) { assert(memc); - memc->servers= list; - memc->number_of_hosts= host_list_size; + memc->servers = list; + memc->number_of_hosts = host_list_size; } -void memcached_server_list_free(memcached_server_list_st self) -{ - if (self) - { - for (uint32_t x= 0; x < memcached_server_list_count(self); x++) - { - assert_msg(not memcached_is_allocated(&self[x]), "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st"); +void memcached_server_list_free(memcached_server_list_st self) { + if (self) { + for (uint32_t x = 0; x < memcached_server_list_count(self); x++) { + assert_msg(not memcached_is_allocated(&self[x]), + "You have called memcached_server_list_free(), but you did not pass it a valid " + "memcached_server_list_st"); __server_free(&self[x]); } @@ -148,13 +108,12 @@ void memcached_server_list_free(memcached_server_list_st self) } } -void memcached_instance_list_free(memcached_instance_st* self, uint32_t instance_count) -{ - if (self) - { - for (uint32_t x= 0; x < instance_count; x++) - { - assert_msg(memcached_is_allocated(&self[x]) == false, "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st"); +void memcached_instance_list_free(memcached_instance_st *self, uint32_t instance_count) { + if (self) { + for (uint32_t x = 0; x < instance_count; x++) { + assert_msg(memcached_is_allocated(&self[x]) == false, + "You have called memcached_server_list_free(), but you did not pass it a valid " + "memcached_server_list_st"); __instance_free(&self[x]); } diff --git a/src/libmemcached/server_list.hpp b/src/libmemcached/server_list.hpp index 0280332c..2d515663 100644 --- a/src/libmemcached/server_list.hpp +++ b/src/libmemcached/server_list.hpp @@ -1,47 +1,26 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once -memcached_instance_st* memcached_instance_list(const memcached_st *); +memcached_instance_st *memcached_instance_list(const memcached_st *); -uint32_t memcached_instance_list_count(const memcached_st*); +uint32_t memcached_instance_list_count(const memcached_st *); uint32_t memcached_servers_set_count(memcached_server_list_st servers, uint32_t count); -void memcached_instance_list_free(memcached_instance_st* self, uint32_t count); +void memcached_instance_list_free(memcached_instance_st *self, uint32_t count); -void memcached_instance_set(memcached_st*, memcached_instance_st*, const uint32_t host_list_size); +void memcached_instance_set(memcached_st *, memcached_instance_st *, const uint32_t host_list_size); diff --git a/src/libmemcached/socket.hpp b/src/libmemcached/socket.hpp index a2ee941f..d1a33963 100644 --- a/src/libmemcached/socket.hpp +++ b/src/libmemcached/socket.hpp @@ -1,44 +1,20 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once - /* To hide the platform differences between MS Windows and Unix, I am * going to use the Microsoft way and #define the Microsoft-specific * functions to the unix way. Microsoft use a separate subsystem for sockets, @@ -47,34 +23,31 @@ * way without side effect ;-) */ #if defined(WIN32) || defined(__MINGW32__) -# include "win32/wrappers.h" -# define get_socket_errno() WSAGetLastError() +# include "win32/wrappers.h" +# define get_socket_errno() WSAGetLastError() #else -# include -# define INVALID_SOCKET -1 -# define SOCKET_ERROR -1 -# define closesocket(a) close(a) -# define get_socket_errno() errno +# include +# define INVALID_SOCKET -1 +# define SOCKET_ERROR -1 +# define closesocket(a) close(a) +# define get_socket_errno() errno #endif #ifdef __cplusplus -static inline void memcached_close_socket(memcached_socket_t& socket_fd) -{ +static inline void memcached_close_socket(memcached_socket_t &socket_fd) { closesocket(socket_fd); - socket_fd= INVALID_SOCKET; + socket_fd = INVALID_SOCKET; } #endif #ifndef HAVE_MSG_NOSIGNAL -# define MSG_NOSIGNAL 0 +# define MSG_NOSIGNAL 0 #endif #ifndef HAVE_MSG_DONTWAIT -# define MSG_DONTWAIT 0 +# define MSG_DONTWAIT 0 #endif #ifndef HAVE_MSG_MORE -# define MSG_MORE 0 +# define MSG_MORE 0 #endif - - diff --git a/src/libmemcached/stats.cc b/src/libmemcached/stats.cc index 75779215..e1e71c2d 100644 --- a/src/libmemcached/stats.cc +++ b/src/libmemcached/stats.cc @@ -1,342 +1,235 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static const char *memcached_stat_keys[] = { - "pid", - "uptime", - "time", - "version", - "pointer_size", - "rusage_user", - "rusage_system", - "curr_items", - "total_items", - "bytes", - "curr_connections", - "total_connections", - "connection_structures", - "cmd_get", - "cmd_set", - "get_hits", - "get_misses", - "evictions", - "bytes_read", - "bytes_written", - "limit_maxbytes", - "threads", - NULL -}; - -struct local_context -{ +static const char *memcached_stat_keys[] = {"pid", + "uptime", + "time", + "version", + "pointer_size", + "rusage_user", + "rusage_system", + "curr_items", + "total_items", + "bytes", + "curr_connections", + "total_connections", + "connection_structures", + "cmd_get", + "cmd_set", + "get_hits", + "get_misses", + "evictions", + "bytes_read", + "bytes_written", + "limit_maxbytes", + "threads", + NULL}; + +struct local_context { memcached_stat_fn func; void *context; const char *args; const size_t args_length; - local_context(memcached_stat_fn func_arg, - void *context_arg, - const char *args_arg, - const size_t args_length_arg) : - func(func_arg), - context(context_arg), - args(args_arg), - args_length(args_length_arg) - { } + local_context(memcached_stat_fn func_arg, void *context_arg, const char *args_arg, + const size_t args_length_arg) + : func(func_arg) + , context(context_arg) + , args(args_arg) + , args_length(args_length_arg) {} }; - -static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key, const char *value) -{ - - if (strlen(key) < 1) - { +static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key, + const char *value) { + if (strlen(key) < 1) { WATCHPOINT_STRING(key); return MEMCACHED_UNKNOWN_STAT_KEY; - } - else if (strcmp("pid", key) == 0) - { - errno= 0; - int64_t temp= strtoll(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (strcmp("pid", key) == 0) { + errno = 0; + int64_t temp = strtoll(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - if (temp <= INT32_MAX and ( sizeof(pid_t) == sizeof(int32_t) )) - { - memc_stat->pid= pid_t(temp); - } - else if (temp > -1) - { - memc_stat->pid= pid_t(temp); - } - else - { + if (temp <= INT32_MAX and (sizeof(pid_t) == sizeof(int32_t))) { + memc_stat->pid = pid_t(temp); + } else if (temp > -1) { + memc_stat->pid = pid_t(temp); + } else { // If we got a value less then -1 then something went wrong in the // protocol } - } - else if (not strcmp("uptime", key)) - { - errno= 0; - memc_stat->uptime= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("uptime", key)) { + errno = 0; + memc_stat->uptime = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("time", key)) - { - errno= 0; - memc_stat->time= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("time", key)) { + errno = 0; + memc_stat->time = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("version", key)) - { + } else if (not strcmp("version", key)) { memcpy(memc_stat->version, value, strlen(value)); - memc_stat->version[strlen(value)]= 0; - } - else if (not strcmp("pointer_size", key)) - { - errno= 0; - memc_stat->pointer_size= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + memc_stat->version[strlen(value)] = 0; + } else if (not strcmp("pointer_size", key)) { + errno = 0; + memc_stat->pointer_size = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("rusage_user", key)) - { + } else if (not strcmp("rusage_user", key)) { char *walk_ptr; - for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {}; - *walk_ptr= 0; + for (walk_ptr = (char *) value; (!ispunct(*walk_ptr)); walk_ptr++) { + }; + *walk_ptr = 0; walk_ptr++; - errno= 0; - memc_stat->rusage_user_seconds= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + memc_stat->rusage_user_seconds = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - errno= 0; - memc_stat->rusage_user_microseconds= strtoul(walk_ptr, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + memc_stat->rusage_user_microseconds = strtoul(walk_ptr, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("rusage_system", key)) - { + } else if (not strcmp("rusage_system", key)) { char *walk_ptr; - for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {}; - *walk_ptr= 0; + for (walk_ptr = (char *) value; (!ispunct(*walk_ptr)); walk_ptr++) { + }; + *walk_ptr = 0; walk_ptr++; - errno= 0; - memc_stat->rusage_system_seconds= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + memc_stat->rusage_system_seconds = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - errno= 0; - memc_stat->rusage_system_microseconds= strtoul(walk_ptr, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + memc_stat->rusage_system_microseconds = strtoul(walk_ptr, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("curr_items", key)) - { - errno= 0; - memc_stat->curr_items= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("curr_items", key)) { + errno = 0; + memc_stat->curr_items = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("total_items", key)) - { - errno= 0; - memc_stat->total_items= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("total_items", key)) { + errno = 0; + memc_stat->total_items = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("bytes_read", key)) - { - errno= 0; - memc_stat->bytes_read= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("bytes_read", key)) { + errno = 0; + memc_stat->bytes_read = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("bytes_written", key)) - { - errno= 0; - memc_stat->bytes_written= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("bytes_written", key)) { + errno = 0; + memc_stat->bytes_written = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("bytes", key)) - { - errno= 0; - memc_stat->bytes= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("bytes", key)) { + errno = 0; + memc_stat->bytes = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("curr_connections", key)) - { - errno= 0; - memc_stat->curr_connections= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("curr_connections", key)) { + errno = 0; + memc_stat->curr_connections = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("total_connections", key)) - { - errno= 0; - memc_stat->total_connections= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("total_connections", key)) { + errno = 0; + memc_stat->total_connections = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("connection_structures", key)) - { - errno= 0; - memc_stat->connection_structures= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("connection_structures", key)) { + errno = 0; + memc_stat->connection_structures = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("cmd_get", key)) - { - errno= 0; - memc_stat->cmd_get= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("cmd_get", key)) { + errno = 0; + memc_stat->cmd_get = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("cmd_set", key)) - { - errno= 0; - memc_stat->cmd_set= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("cmd_set", key)) { + errno = 0; + memc_stat->cmd_set = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("get_hits", key)) - { - errno= 0; - memc_stat->get_hits= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("get_hits", key)) { + errno = 0; + memc_stat->get_hits = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("get_misses", key)) - { - errno= 0; - memc_stat->get_misses= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("get_misses", key)) { + errno = 0; + memc_stat->get_misses = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("evictions", key)) - { - errno= 0; - memc_stat->evictions= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("evictions", key)) { + errno = 0; + memc_stat->evictions = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("limit_maxbytes", key)) - { - errno= 0; - memc_stat->limit_maxbytes= strtoull(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("limit_maxbytes", key)) { + errno = 0; + memc_stat->limit_maxbytes = strtoull(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if (not strcmp("threads", key)) - { - errno= 0; - memc_stat->threads= strtoul(value, (char **)NULL, 10); - if (errno != 0) - { + } else if (not strcmp("threads", key)) { + errno = 0; + memc_stat->threads = strtoul(value, (char **) NULL, 10); + if (errno != 0) { return MEMCACHED_FAILURE; } - } - else if ((strcmp("delete_misses", key) == 0 or /* New stats in the 1.3 beta */ - strcmp("delete_hits", key) == 0 or /* Just swallow them for now.. */ - strcmp("incr_misses", key) == 0 or - strcmp("incr_hits", key) == 0 or - strcmp("decr_misses", key) == 0 or - strcmp("decr_hits", key) == 0 or - strcmp("cas_misses", key) == 0 or - strcmp("cas_hits", key) == 0 or - strcmp("cas_badval", key) == 0 or - strcmp("cmd_flush", key) == 0 or - strcmp("accepting_conns", key) == 0 or - strcmp("listen_disabled_num", key) == 0 or - strcmp("conn_yields", key) == 0 or - strcmp("auth_cmds", key) == 0 or - strcmp("auth_errors", key) == 0 or - strcmp("reclaimed", key) == 0) == 0) + } else if ((strcmp("delete_misses", key) == 0 or /* New stats in the 1.3 beta */ + strcmp("delete_hits", key) == 0 or /* Just swallow them for now.. */ + strcmp("incr_misses", key) == 0 or strcmp("incr_hits", key) == 0 + or strcmp("decr_misses", key) == 0 or strcmp("decr_hits", key) == 0 + or strcmp("cas_misses", key) == 0 or strcmp("cas_hits", key) == 0 + or strcmp("cas_badval", key) == 0 or strcmp("cmd_flush", key) == 0 + or strcmp("accepting_conns", key) == 0 or strcmp("listen_disabled_num", key) == 0 + or strcmp("conn_yields", key) == 0 or strcmp("auth_cmds", key) == 0 + or strcmp("auth_errors", key) == 0 or strcmp("reclaimed", key) == 0) + == 0) { WATCHPOINT_STRING(key); /* return MEMCACHED_UNKNOWN_STAT_KEY; */ @@ -346,207 +239,145 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key return MEMCACHED_SUCCESS; } -char *memcached_stat_get_value(const memcached_st* shell, memcached_stat_st *memc_stat, - const char *key, memcached_return_t *error) -{ +char *memcached_stat_get_value(const memcached_st *shell, memcached_stat_st *memc_stat, + const char *key, memcached_return_t *error) { memcached_return_t not_used; - if (error == NULL) - { - error= ¬_used; + if (error == NULL) { + error = ¬_used; } - if (memc_stat == NULL) - { - *error= MEMCACHED_INVALID_ARGUMENTS; + if (memc_stat == NULL) { + *error = MEMCACHED_INVALID_ARGUMENTS; return NULL; } char buffer[SMALL_STRING_LEN]; int length; - *error= MEMCACHED_SUCCESS; - - if (!strcmp(key, "pid")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lld", (signed long long)memc_stat->pid); - } - else if (!strcmp(key, "uptime")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->uptime); - } - else if (!strcmp(key, "time")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time); - } - else if (!strcmp(key, "version")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version); - } - else if (!strcmp(key, "pointer_size")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->pointer_size); - } - else if (!strcmp(key, "rusage_user")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds); - } - else if (!strcmp(key, "rusage_system")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds); - } - else if (!strcmp(key, "curr_items")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_items); - } - else if (!strcmp(key, "total_items")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_items); - } - else if (!strcmp(key, "curr_connections")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_connections); - } - else if (!strcmp(key, "total_connections")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_connections); - } - else if (!strcmp(key, "connection_structures")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->connection_structures); - } - else if (!strcmp(key, "cmd_get")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get); - } - else if (!strcmp(key, "cmd_set")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set); - } - else if (!strcmp(key, "get_hits")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits); - } - else if (!strcmp(key, "get_misses")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses); - } - else if (!strcmp(key, "evictions")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions); - } - else if (!strcmp(key, "bytes_read")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read); - } - else if (!strcmp(key, "bytes_written")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written); - } - else if (!strcmp(key, "bytes")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes); - } - else if (!strcmp(key, "limit_maxbytes")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes); - } - else if (!strcmp(key, "threads")) - { - length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->threads); - } - else - { - Memcached* memc= (Memcached*)memcached2Memcached(shell); - *error= memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid key provided")); + *error = MEMCACHED_SUCCESS; + + if (!strcmp(key, "pid")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lld", (signed long long) memc_stat->pid); + } else if (!strcmp(key, "uptime")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->uptime); + } else if (!strcmp(key, "time")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->time); + } else if (!strcmp(key, "version")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%s", memc_stat->version); + } else if (!strcmp(key, "pointer_size")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->pointer_size); + } else if (!strcmp(key, "rusage_user")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu.%lu", memc_stat->rusage_user_seconds, + memc_stat->rusage_user_microseconds); + } else if (!strcmp(key, "rusage_system")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu.%lu", memc_stat->rusage_system_seconds, + memc_stat->rusage_system_microseconds); + } else if (!strcmp(key, "curr_items")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->curr_items); + } else if (!strcmp(key, "total_items")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->total_items); + } else if (!strcmp(key, "curr_connections")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->curr_connections); + } else if (!strcmp(key, "total_connections")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->total_connections); + } else if (!strcmp(key, "connection_structures")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->connection_structures); + } else if (!strcmp(key, "cmd_get")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->cmd_get); + } else if (!strcmp(key, "cmd_set")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->cmd_set); + } else if (!strcmp(key, "get_hits")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->get_hits); + } else if (!strcmp(key, "get_misses")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->get_misses); + } else if (!strcmp(key, "evictions")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->evictions); + } else if (!strcmp(key, "bytes_read")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->bytes_read); + } else if (!strcmp(key, "bytes_written")) { + length = + snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->bytes_written); + } else if (!strcmp(key, "bytes")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->bytes); + } else if (!strcmp(key, "limit_maxbytes")) { + length = + snprintf(buffer, SMALL_STRING_LEN, "%llu", (unsigned long long) memc_stat->limit_maxbytes); + } else if (!strcmp(key, "threads")) { + length = snprintf(buffer, SMALL_STRING_LEN, "%lu", memc_stat->threads); + } else { + Memcached *memc = (Memcached *) memcached2Memcached(shell); + *error = memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid key provided")); return NULL; } - if (length >= SMALL_STRING_LEN || length < 0) - { - Memcached* memc= (Memcached*)memcached2Memcached(shell); - *error= memcached_set_error(*memc, MEMCACHED_FAILURE, MEMCACHED_AT, memcached_literal_param("Internal failure occured with buffer, please report this bug.")); + if (length >= SMALL_STRING_LEN || length < 0) { + Memcached *memc = (Memcached *) memcached2Memcached(shell); + *error = memcached_set_error( + *memc, MEMCACHED_FAILURE, MEMCACHED_AT, + memcached_literal_param("Internal failure occured with buffer, please report this bug.")); return NULL; } // User is responsible for free() memory, so use malloc() - char *ret= static_cast(malloc(size_t(length +1))); + char *ret = static_cast(malloc(size_t(length + 1))); memcpy(ret, buffer, (size_t) length); - ret[length]= '\0'; + ret[length] = '\0'; return ret; } -static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, - const char *args, +static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, const char *args, const size_t args_length, - memcached_instance_st* instance, - struct local_context *check) -{ + memcached_instance_st *instance, + struct local_context *check) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - protocol_binary_request_stats request= {}; // = {.bytes= {0}}; + protocol_binary_request_stats request = {}; // = {.bytes= {0}}; memcached_return_t rc; initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_STAT; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; - if (args_length) - { - request.message.header.request.keylen= htons(uint16_t(args_length)); - request.message.header.request.bodylen= htonl(uint32_t( args_length)); + if (args_length) { + request.message.header.request.keylen = htons(uint16_t(args_length)); + request.message.header.request.bodylen = htonl(uint32_t(args_length)); - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) }, - { args, args_length } - }; + libmemcached_io_vector_st vector[] = {{request.bytes, sizeof(request.bytes)}, + {args, args_length}}; - if (memcached_failed(rc = memcached_vdo(instance, vector, 2, true))) - { + if (memcached_failed(rc = memcached_vdo(instance, vector, 2, true))) { return rc; } - } - else - { - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) } - }; + } else { + libmemcached_io_vector_st vector[] = {{request.bytes, sizeof(request.bytes)}}; - if (memcached_failed(rc = memcached_vdo(instance, vector, 1, true))) - { + if (memcached_failed(rc = memcached_vdo(instance, vector, 1, true))) { return rc; } } memcached_server_response_decrement(instance); - while (1) - { - rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + while (1) { + rc = memcached_response(instance, buffer, sizeof(buffer), NULL); - if (rc == MEMCACHED_END) - { + if (rc == MEMCACHED_END) { break; } - if (rc != MEMCACHED_SUCCESS) - { + if (rc != MEMCACHED_SUCCESS) { return rc; } - if (check && check->func) - { - size_t key_length= strlen(buffer); + if (check && check->func) { + size_t key_length = strlen(buffer); - check->func(instance, - buffer, key_length, - buffer+key_length+1, strlen(buffer+key_length+1), - check->context); + check->func(instance, buffer, key_length, buffer + key_length + 1, + strlen(buffer + key_length + 1), check->context); } - if (memc_stat) - { + if (memc_stat) { if ((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY) { WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); @@ -555,64 +386,52 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, } } - /* + /* * memcached_response will decrement the counter, so I need to reset it.. - * todo: look at this and try to find a better solution. + * todo: look at this and try to find a better solution. * */ - instance->cursor_active_= 0; + instance->cursor_active_ = 0; return MEMCACHED_SUCCESS; } -static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, - const char *args, +static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, const char *args, const size_t args_length, - memcached_instance_st* instance, - struct local_context *check) -{ - libmemcached_io_vector_st vector[]= - { - { memcached_literal_param("stats ") }, - { args, args_length }, - { memcached_literal_param("\r\n") } - }; + memcached_instance_st *instance, + struct local_context *check) { + libmemcached_io_vector_st vector[] = { + {memcached_literal_param("stats ")}, {args, args_length}, {memcached_literal_param("\r\n")}}; - memcached_return_t rc= memcached_vdo(instance, vector, 3, true); - if (memcached_success(rc)) - { + memcached_return_t rc = memcached_vdo(instance, vector, 3, true); + if (memcached_success(rc)) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - while ((rc= memcached_response(instance, buffer, sizeof(buffer), NULL)) == MEMCACHED_STAT) - { - char *string_ptr= buffer; - string_ptr+= 5; /* Move past STAT */ + while ((rc = memcached_response(instance, buffer, sizeof(buffer), NULL)) == MEMCACHED_STAT) { + char *string_ptr = buffer; + string_ptr += 5; /* Move past STAT */ char *end_ptr; - for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {}; - char *key= string_ptr; - key[size_t(end_ptr-string_ptr)]= 0; - - string_ptr= end_ptr + 1; - for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++) {}; - char *value= string_ptr; - value[(size_t)(end_ptr -string_ptr)]= 0; + for (end_ptr = string_ptr; isgraph(*end_ptr); end_ptr++) { + }; + char *key = string_ptr; + key[size_t(end_ptr - string_ptr)] = 0; + + string_ptr = end_ptr + 1; + for (end_ptr = string_ptr; !(isspace(*end_ptr)); end_ptr++) { + }; + char *value = string_ptr; + value[(size_t)(end_ptr - string_ptr)] = 0; #if 0 bool check_bool= bool(check); bool check_func_bool= bool(check) ? bool(check->func) : false; fprintf(stderr, "%s:%d %s %s %d:%d\n", __FILE__, __LINE__, key, value, check_bool, check_func_bool); #endif - if (check and check->func) - { - check->func(instance, - key, strlen(key), - value, strlen(value), - check->context); + if (check and check->func) { + check->func(instance, key, strlen(key), value, strlen(value), check->context); } - if (memc_stat) - { - if((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) - { + if (memc_stat) { + if ((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) { WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); WATCHPOINT_ASSERT(0); } @@ -620,144 +439,120 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, } } - if (rc == MEMCACHED_ERROR) - { + if (rc == MEMCACHED_ERROR) { return MEMCACHED_INVALID_ARGUMENTS; } - if (rc == MEMCACHED_END) - { + if (rc == MEMCACHED_END) { return MEMCACHED_SUCCESS; } return rc; } -memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_return_t *error) -{ - Memcached* self= memcached2Memcached(shell); +memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_return_t *error) { + Memcached *self = memcached2Memcached(shell); memcached_return_t unused; - if (error == NULL) - { - error= &unused; + if (error == NULL) { + error = &unused; } - if (memcached_failed(*error= initialize_query(self, true))) - { + if (memcached_failed(*error = initialize_query(self, true))) { return NULL; } - if (memcached_is_udp(self)) - { - *error= memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + if (memcached_is_udp(self)) { + *error = memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); return NULL; } memcached_return_t rc = MEMCACHED_SUCCESS; - size_t args_length= 0; - if (args) - { - args_length= strlen(args); - if (memcached_failed(rc= memcached_key_test(*self, (const char **)&args, &args_length, 1))) - { - *error= memcached_set_error(*self, rc, MEMCACHED_AT); + size_t args_length = 0; + if (args) { + args_length = strlen(args); + if (memcached_failed(rc = memcached_key_test(*self, (const char **) &args, &args_length, 1))) { + *error = memcached_set_error(*self, rc, MEMCACHED_AT); return NULL; } } WATCHPOINT_ASSERT(error); - memcached_stat_st *stats= libmemcached_xcalloc(self, memcached_server_count(self), memcached_stat_st); - if (stats == NULL) - { - *error= memcached_set_error(*self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + memcached_stat_st *stats = + libmemcached_xcalloc(self, memcached_server_count(self), memcached_stat_st); + if (stats == NULL) { + *error = memcached_set_error(*self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); return NULL; } WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS); - rc= MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < memcached_server_count(self); x++) - { - memcached_stat_st* stat_instance= stats +x; + rc = MEMCACHED_SUCCESS; + for (uint32_t x = 0; x < memcached_server_count(self); x++) { + memcached_stat_st *stat_instance = stats + x; - stat_instance->pid= -1; - stat_instance->root= self; + stat_instance->pid = -1; + stat_instance->root = self; - memcached_instance_st* instance= memcached_instance_fetch(self, x); + memcached_instance_st *instance = memcached_instance_fetch(self, x); memcached_return_t temp_return; - if (memcached_is_binary(self)) - { - temp_return= binary_stats_fetch(stat_instance, args, args_length, instance, NULL); - } - else - { - temp_return= ascii_stats_fetch(stat_instance, args, args_length, instance, NULL); + if (memcached_is_binary(self)) { + temp_return = binary_stats_fetch(stat_instance, args, args_length, instance, NULL); + } else { + temp_return = ascii_stats_fetch(stat_instance, args, args_length, instance, NULL); } // Special case where "args" is invalid - if (temp_return == MEMCACHED_INVALID_ARGUMENTS) - { - rc= MEMCACHED_INVALID_ARGUMENTS; + if (temp_return == MEMCACHED_INVALID_ARGUMENTS) { + rc = MEMCACHED_INVALID_ARGUMENTS; break; } - if (memcached_failed(temp_return)) - { - rc= MEMCACHED_SOME_ERRORS; + if (memcached_failed(temp_return)) { + rc = MEMCACHED_SOME_ERRORS; } } - *error= rc; + *error = rc; return stats; } memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args, - const char *hostname, in_port_t port) -{ + const char *hostname, in_port_t port) { memcached_st memc; memcached_stat_st unused_memc_stat; - if (memc_stat == NULL) - { - memc_stat= &unused_memc_stat; + if (memc_stat == NULL) { + memc_stat = &unused_memc_stat; } memset(memc_stat, 0, sizeof(memcached_stat_st)); - memcached_st *memc_ptr= memcached_create(&memc); - if (memc_ptr == NULL) - { + memcached_st *memc_ptr = memcached_create(&memc); + if (memc_ptr == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } memcached_return_t rc; - if (memcached_failed(rc= memcached_server_add(&memc, hostname, port))) - { + if (memcached_failed(rc = memcached_server_add(&memc, hostname, port))) { memcached_free(&memc); return rc; } - if (memcached_success(rc= initialize_query(memc_ptr, true))) - { - size_t args_length= 0; - if (args) - { - args_length= strlen(args); - rc= memcached_key_test(*memc_ptr, (const char **)&args, &args_length, 1); + if (memcached_success(rc = initialize_query(memc_ptr, true))) { + size_t args_length = 0; + if (args) { + args_length = strlen(args); + rc = memcached_key_test(*memc_ptr, (const char **) &args, &args_length, 1); } - if (memcached_success(rc)) - { - memcached_instance_st* instance= memcached_instance_fetch(memc_ptr, 0); - if (memc.flags.binary_protocol) - { - rc= binary_stats_fetch(memc_stat, args, args_length, instance, NULL); - } - else - { - rc= ascii_stats_fetch(memc_stat, args, args_length, instance, NULL); + if (memcached_success(rc)) { + memcached_instance_st *instance = memcached_instance_fetch(memc_ptr, 0); + if (memc.flags.binary_protocol) { + rc = binary_stats_fetch(memc_stat, args, args_length, instance, NULL); + } else { + rc = ascii_stats_fetch(memc_stat, args, args_length, instance, NULL); } } } @@ -771,19 +566,14 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char We make a copy of the keys since at some point in the not so distant future we will add support for "found" keys. */ -char ** memcached_stat_get_keys(memcached_st *shell, - memcached_stat_st *, - memcached_return_t *error) -{ - Memcached* memc= memcached2Memcached(shell); - if (memc) - { - char **list= static_cast(libmemcached_malloc(memc, sizeof(memcached_stat_keys))); - if (list == NULL) - { - if (error) - { - *error= memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); +char **memcached_stat_get_keys(memcached_st *shell, memcached_stat_st *, + memcached_return_t *error) { + Memcached *memc = memcached2Memcached(shell); + if (memc) { + char **list = static_cast(libmemcached_malloc(memc, sizeof(memcached_stat_keys))); + if (list == NULL) { + if (error) { + *error = memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } return NULL; @@ -791,9 +581,8 @@ char ** memcached_stat_get_keys(memcached_st *shell, memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys)); - if (error) - { - *error= MEMCACHED_SUCCESS; + if (error) { + *error = MEMCACHED_SUCCESS; } return list; @@ -802,29 +591,21 @@ char ** memcached_stat_get_keys(memcached_st *shell, return NULL; } -void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat) -{ +void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat) { WATCHPOINT_ASSERT(memc_stat); // Be polite, but when debugging catch this as an error - if (memc_stat) - { + if (memc_stat) { libmemcached_free(memc_stat->root, memc_stat); } } -static memcached_return_t call_stat_fn(memcached_st *memc, - memcached_instance_st* instance, - void *context) -{ - if (memc) - { - local_context *check= (struct local_context *)context; +static memcached_return_t call_stat_fn(memcached_st *memc, memcached_instance_st *instance, + void *context) { + if (memc) { + local_context *check = (struct local_context *) context; - if (memcached_is_binary(memc)) - { + if (memcached_is_binary(memc)) { return binary_stats_fetch(NULL, check->args, check->args_length, instance, check); - } - else - { + } else { return ascii_stats_fetch(NULL, check->args, check->args_length, instance, check); } } @@ -832,15 +613,14 @@ static memcached_return_t call_stat_fn(memcached_st *memc, return MEMCACHED_INVALID_ARGUMENTS; } -memcached_return_t memcached_stat_execute(memcached_st *shell, const char *args, memcached_stat_fn func, void *context) -{ - Memcached* memc= memcached2Memcached(shell); - if (memcached_fatal(memcached_version(memc))) - { +memcached_return_t memcached_stat_execute(memcached_st *shell, const char *args, + memcached_stat_fn func, void *context) { + Memcached *memc = memcached2Memcached(shell); + if (memcached_fatal(memcached_version(memc))) { return memcached_last_error(memc); } - local_context check(func, context, args, args ? strlen(args) : 0); + local_context check(func, context, args, args ? strlen(args) : 0); - return memcached_server_execute(memc, call_stat_fn, (void *)&check); + return memcached_server_execute(memc, call_stat_fn, (void *) &check); } diff --git a/src/libmemcached/storage.cc b/src/libmemcached/storage.cc index 236dd146..0700c055 100644 --- a/src/libmemcached/storage.cc +++ b/src/libmemcached/storage.cc @@ -1,323 +1,237 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -enum memcached_storage_action_t { - SET_OP, - REPLACE_OP, - ADD_OP, - PREPEND_OP, - APPEND_OP, - CAS_OP -}; +enum memcached_storage_action_t { SET_OP, REPLACE_OP, ADD_OP, PREPEND_OP, APPEND_OP, CAS_OP }; /* Inline this */ -static inline const char *storage_op_string(memcached_storage_action_t verb) -{ - switch (verb) - { - case REPLACE_OP: - return "replace "; +static inline const char *storage_op_string(memcached_storage_action_t verb) { + switch (verb) { + case REPLACE_OP: return "replace "; - case ADD_OP: - return "add "; + case ADD_OP: return "add "; - case PREPEND_OP: - return "prepend "; + case PREPEND_OP: return "prepend "; - case APPEND_OP: - return "append "; + case APPEND_OP: return "append "; - case CAS_OP: - return "cas "; + case CAS_OP: return "cas "; - case SET_OP: - break; + case SET_OP: break; } return "set "; } -static inline bool can_by_encrypted(const memcached_storage_action_t verb) -{ - switch (verb) - { +static inline bool can_by_encrypted(const memcached_storage_action_t verb) { + switch (verb) { case SET_OP: case ADD_OP: case CAS_OP: - case REPLACE_OP: - return true; - + case REPLACE_OP: return true; + case APPEND_OP: - case PREPEND_OP: - break; + case PREPEND_OP: break; } return false; } -static inline uint8_t get_com_code(const memcached_storage_action_t verb, const bool reply) -{ - if (reply == false) - { - switch (verb) - { - case SET_OP: - return PROTOCOL_BINARY_CMD_SETQ; +static inline uint8_t get_com_code(const memcached_storage_action_t verb, const bool reply) { + if (reply == false) { + switch (verb) { + case SET_OP: return PROTOCOL_BINARY_CMD_SETQ; - case ADD_OP: - return PROTOCOL_BINARY_CMD_ADDQ; + case ADD_OP: return PROTOCOL_BINARY_CMD_ADDQ; case CAS_OP: /* FALLTHROUGH */ - case REPLACE_OP: - return PROTOCOL_BINARY_CMD_REPLACEQ; + case REPLACE_OP: return PROTOCOL_BINARY_CMD_REPLACEQ; - case APPEND_OP: - return PROTOCOL_BINARY_CMD_APPENDQ; + case APPEND_OP: return PROTOCOL_BINARY_CMD_APPENDQ; - case PREPEND_OP: - return PROTOCOL_BINARY_CMD_PREPENDQ; + case PREPEND_OP: return PROTOCOL_BINARY_CMD_PREPENDQ; } } - switch (verb) - { - case SET_OP: - break; + switch (verb) { + case SET_OP: break; - case ADD_OP: - return PROTOCOL_BINARY_CMD_ADD; + case ADD_OP: return PROTOCOL_BINARY_CMD_ADD; case CAS_OP: /* FALLTHROUGH */ - case REPLACE_OP: - return PROTOCOL_BINARY_CMD_REPLACE; + case REPLACE_OP: return PROTOCOL_BINARY_CMD_REPLACE; - case APPEND_OP: - return PROTOCOL_BINARY_CMD_APPEND; + case APPEND_OP: return PROTOCOL_BINARY_CMD_APPEND; - case PREPEND_OP: - return PROTOCOL_BINARY_CMD_PREPEND; + case PREPEND_OP: return PROTOCOL_BINARY_CMD_PREPEND; } return PROTOCOL_BINARY_CMD_SET; } -static memcached_return_t memcached_send_binary(Memcached *ptr, - memcached_instance_st* server, - uint32_t server_key, - const char *key, - const size_t key_length, - const char *value, - const size_t value_length, - const time_t expiration, - const uint32_t flags, - const uint64_t cas, - const bool flush, - const bool reply, - memcached_storage_action_t verb) -{ - protocol_binary_request_set request= {}; - size_t send_length= sizeof(request.bytes); +static memcached_return_t memcached_send_binary(Memcached *ptr, memcached_instance_st *server, + uint32_t server_key, const char *key, + const size_t key_length, const char *value, + const size_t value_length, const time_t expiration, + const uint32_t flags, const uint64_t cas, + const bool flush, const bool reply, + memcached_storage_action_t verb) { + protocol_binary_request_set request = {}; + size_t send_length = sizeof(request.bytes); initialize_binary_request(server, request.message.header); - request.message.header.request.opcode= get_com_code(verb, reply); - request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - if (verb == APPEND_OP or verb == PREPEND_OP) - { + request.message.header.request.opcode = get_com_code(verb, reply); + request.message.header.request.keylen = + htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + if (verb == APPEND_OP or verb == PREPEND_OP) { send_length -= 8; /* append & prepend does not contain extras! */ - } - else - { - request.message.header.request.extlen= 8; - request.message.body.flags= htonl(flags); - request.message.body.expiration= htonl((uint32_t)expiration); + } else { + request.message.header.request.extlen = 8; + request.message.body.flags = htonl(flags); + request.message.body.expiration = htonl((uint32_t) expiration); } - request.message.header.request.bodylen= htonl((uint32_t) (key_length + memcached_array_size(ptr->_namespace) + value_length + - request.message.header.request.extlen)); + request.message.header.request.bodylen = + htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace) + value_length + + request.message.header.request.extlen)); - if (cas) - { - request.message.header.request.cas= memcached_htonll(cas); + if (cas) { + request.message.header.request.cas = memcached_htonll(cas); } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, send_length }, - { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) }, - { key, key_length }, - { value, value_length } - }; + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {request.bytes, send_length}, + {memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)}, + {key, key_length}, + {value, value_length}}; /* write the header */ memcached_return_t rc; - if ((rc= memcached_vdo(server, vector, 5, flush)) != MEMCACHED_SUCCESS) - { + if ((rc = memcached_vdo(server, vector, 5, flush)) != MEMCACHED_SUCCESS) { assert(memcached_last_error(server->root) != MEMCACHED_SUCCESS); return memcached_last_error(server->root); } - if (verb == SET_OP and ptr->number_of_replicas > 0) - { - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ; + if (verb == SET_OP and ptr->number_of_replicas > 0) { + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_SETQ; WATCHPOINT_STRING("replicating"); - for (uint32_t x= 0; x < ptr->number_of_replicas; x++) - { + for (uint32_t x = 0; x < ptr->number_of_replicas; x++) { ++server_key; - if (server_key == memcached_server_count(ptr)) - { - server_key= 0; + if (server_key == memcached_server_count(ptr)) { + server_key = 0; } - memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key); + memcached_instance_st *instance = memcached_instance_fetch(ptr, server_key); - if (memcached_success(memcached_vdo(instance, vector, 5, false))) - { + if (memcached_success(memcached_vdo(instance, vector, 5, false))) { memcached_server_response_decrement(instance); } } } - if (flush == false) - { + if (flush == false) { return MEMCACHED_BUFFERED; } // No reply always assumes success - if (reply == false) - { + if (reply == false) { return MEMCACHED_SUCCESS; } return memcached_response(server, NULL, 0, NULL); } -static memcached_return_t memcached_send_ascii(Memcached *ptr, - memcached_instance_st* instance, - const char *key, - const size_t key_length, - const char *value, - const size_t value_length, - const time_t expiration, - const uint32_t flags, - const uint64_t cas, - const bool flush, - const bool reply, - const memcached_storage_action_t verb) -{ - char flags_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int flags_buffer_length= snprintf(flags_buffer, sizeof(flags_buffer), " %u", flags); - if (size_t(flags_buffer_length) >= sizeof(flags_buffer) or flags_buffer_length < 0) - { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); +static memcached_return_t +memcached_send_ascii(Memcached *ptr, memcached_instance_st *instance, const char *key, + const size_t key_length, const char *value, const size_t value_length, + const time_t expiration, const uint32_t flags, const uint64_t cas, + const bool flush, const bool reply, const memcached_storage_action_t verb) { + char flags_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int flags_buffer_length = snprintf(flags_buffer, sizeof(flags_buffer), " %u", flags); + if (size_t(flags_buffer_length) >= sizeof(flags_buffer) or flags_buffer_length < 0) { + return memcached_set_error( + *instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); } - char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int expiration_buffer_length= snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", (unsigned long long)expiration); + char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int expiration_buffer_length = snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", + (unsigned long long) expiration); if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer) or expiration_buffer_length < 0) { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); + return memcached_set_error( + *instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); } - char value_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int value_buffer_length= snprintf(value_buffer, sizeof(value_buffer), " %llu", (unsigned long long)value_length); - if (size_t(value_buffer_length) >= sizeof(value_buffer) or value_buffer_length < 0) - { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); + char value_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int value_buffer_length = + snprintf(value_buffer, sizeof(value_buffer), " %llu", (unsigned long long) value_length); + if (size_t(value_buffer_length) >= sizeof(value_buffer) or value_buffer_length < 0) { + return memcached_set_error( + *instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); } - char cas_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int cas_buffer_length= 0; - if (cas) - { - cas_buffer_length= snprintf(cas_buffer, sizeof(cas_buffer), " %llu", (unsigned long long)cas); - if (size_t(cas_buffer_length) >= sizeof(cas_buffer) or cas_buffer_length < 0) - { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); + char cas_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int cas_buffer_length = 0; + if (cas) { + cas_buffer_length = snprintf(cas_buffer, sizeof(cas_buffer), " %llu", (unsigned long long) cas); + if (size_t(cas_buffer_length) >= sizeof(cas_buffer) or cas_buffer_length < 0) { + return memcached_set_error( + *instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); } } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { storage_op_string(verb), strlen(storage_op_string(verb))}, - { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) }, - { key, key_length }, - { flags_buffer, size_t(flags_buffer_length) }, - { expiration_buffer, size_t(expiration_buffer_length) }, - { value_buffer, size_t(value_buffer_length) }, - { cas_buffer, size_t(cas_buffer_length) }, - { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") }, - { memcached_literal_param("\r\n") }, - { value, value_length }, - { memcached_literal_param("\r\n") } - }; + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {storage_op_string(verb), strlen(storage_op_string(verb))}, + {memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace)}, + {key, key_length}, + {flags_buffer, size_t(flags_buffer_length)}, + {expiration_buffer, size_t(expiration_buffer_length)}, + {value_buffer, size_t(value_buffer_length)}, + {cas_buffer, size_t(cas_buffer_length)}, + {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")}, + {memcached_literal_param("\r\n")}, + {value, value_length}, + {memcached_literal_param("\r\n")}}; /* Send command header */ - memcached_return_t rc= memcached_vdo(instance, vector, 12, flush); + memcached_return_t rc = memcached_vdo(instance, vector, 12, flush); // If we should not reply, return with MEMCACHED_SUCCESS, unless error - if (reply == false) - { - return memcached_success(rc) ? MEMCACHED_SUCCESS : rc; + if (reply == false) { + return memcached_success(rc) ? MEMCACHED_SUCCESS : rc; } - if (flush == false) - { - return memcached_success(rc) ? MEMCACHED_BUFFERED : rc; + if (flush == false) { + return memcached_success(rc) ? MEMCACHED_BUFFERED : rc; } - if (rc == MEMCACHED_SUCCESS) - { + if (rc == MEMCACHED_SUCCESS) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + rc = memcached_response(instance, buffer, sizeof(buffer), NULL); - if (rc == MEMCACHED_STORED) - { + if (rc == MEMCACHED_STORED) { return MEMCACHED_SUCCESS; } } @@ -333,248 +247,182 @@ static memcached_return_t memcached_send_ascii(Memcached *ptr, return rc; } -static inline memcached_return_t memcached_send(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - const time_t expiration, - const uint32_t flags, - const uint64_t cas, - memcached_storage_action_t verb) -{ - Memcached* ptr= memcached2Memcached(shell); +static inline memcached_return_t +memcached_send(memcached_st *shell, const char *group_key, size_t group_key_length, const char *key, + size_t key_length, const char *value, size_t value_length, const time_t expiration, + const uint32_t flags, const uint64_t cas, memcached_storage_action_t verb) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, true))) - { + if (memcached_failed(rc = initialize_query(ptr, true))) { return rc; } - if (memcached_failed(memcached_key_test(*ptr, (const char **)&key, &key_length, 1))) - { + if (memcached_failed(memcached_key_test(*ptr, (const char **) &key, &key_length, 1))) { return memcached_last_error(ptr); } - uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key); + uint32_t server_key = + memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(ptr, server_key); - WATCHPOINT_SET(instance->io_wait_count.read= 0); - WATCHPOINT_SET(instance->io_wait_count.write= 0); + WATCHPOINT_SET(instance->io_wait_count.read = 0); + WATCHPOINT_SET(instance->io_wait_count.write = 0); - bool flush= true; - if (memcached_is_buffering(instance->root) and verb == SET_OP) - { - flush= false; + bool flush = true; + if (memcached_is_buffering(instance->root) and verb == SET_OP) { + flush = false; } - bool reply= memcached_is_replying(ptr); + bool reply = memcached_is_replying(ptr); - hashkit_string_st* destination= NULL; + hashkit_string_st *destination = NULL; - if (memcached_is_encrypted(ptr)) - { - if (can_by_encrypted(verb) == false) - { - return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, - memcached_literal_param("Operation not allowed while encyrption is enabled")); + if (memcached_is_encrypted(ptr)) { + if (can_by_encrypted(verb) == false) { + return memcached_set_error( + *ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, + memcached_literal_param("Operation not allowed while encyrption is enabled")); } - if ((destination= hashkit_encrypt(&ptr->hashkit, value, value_length)) == NULL) - { + if ((destination = hashkit_encrypt(&ptr->hashkit, value, value_length)) == NULL) { return rc; } - value= hashkit_string_c_str(destination); - value_length= hashkit_string_length(destination); + value = hashkit_string_c_str(destination); + value_length = hashkit_string_length(destination); } - if (memcached_is_binary(ptr)) - { - rc= memcached_send_binary(ptr, instance, server_key, - key, key_length, - value, value_length, expiration, + if (memcached_is_binary(ptr)) { + rc = memcached_send_binary(ptr, instance, server_key, key, key_length, value, value_length, + expiration, flags, cas, flush, reply, verb); + } else { + rc = memcached_send_ascii(ptr, instance, key, key_length, value, value_length, expiration, flags, cas, flush, reply, verb); } - else - { - rc= memcached_send_ascii(ptr, instance, - key, key_length, - value, value_length, expiration, - flags, cas, flush, reply, verb); - } hashkit_string_free(destination); return rc; } - memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_SET_START(); - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, 0, SET_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + 0, SET_OP); LIBMEMCACHED_MEMCACHED_SET_END(); return rc; } -memcached_return_t memcached_add(memcached_st *ptr, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_add(memcached_st *ptr, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_ADD_START(); - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, 0, ADD_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + 0, ADD_OP); LIBMEMCACHED_MEMCACHED_ADD_END(); return rc; } -memcached_return_t memcached_replace(memcached_st *ptr, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_replace(memcached_st *ptr, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_REPLACE_START(); - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, 0, REPLACE_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + 0, REPLACE_OP); LIBMEMCACHED_MEMCACHED_REPLACE_END(); return rc; } -memcached_return_t memcached_prepend(memcached_st *ptr, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_prepend(memcached_st *ptr, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, 0, PREPEND_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + 0, PREPEND_OP); return rc; } -memcached_return_t memcached_append(memcached_st *ptr, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_append(memcached_st *ptr, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, 0, APPEND_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + 0, APPEND_OP); return rc; } -memcached_return_t memcached_cas(memcached_st *ptr, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags, - uint64_t cas) -{ +memcached_return_t memcached_cas(memcached_st *ptr, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags, uint64_t cas) { memcached_return_t rc; - rc= memcached_send(ptr, key, key_length, - key, key_length, value, value_length, - expiration, flags, cas, CAS_OP); + rc = memcached_send(ptr, key, key_length, key, key_length, value, value_length, expiration, flags, + cas, CAS_OP); return rc; } -memcached_return_t memcached_set_by_key(memcached_st *ptr, - const char *group_key, - size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_set_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_SET_START(); - rc= memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, - expiration, flags, 0, SET_OP); + rc = memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, + expiration, flags, 0, SET_OP); LIBMEMCACHED_MEMCACHED_SET_END(); return rc; } -memcached_return_t memcached_add_by_key(memcached_st *ptr, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_add_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_ADD_START(); - rc= memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, - expiration, flags, 0, ADD_OP); + rc = memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, + expiration, flags, 0, ADD_OP); LIBMEMCACHED_MEMCACHED_ADD_END(); return rc; } -memcached_return_t memcached_replace_by_key(memcached_st *ptr, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ +memcached_return_t memcached_replace_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, const char *value, + size_t value_length, time_t expiration, + uint32_t flags) { memcached_return_t rc; LIBMEMCACHED_MEMCACHED_REPLACE_START(); - rc= memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, - expiration, flags, 0, REPLACE_OP); + rc = memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, + expiration, flags, 0, REPLACE_OP); LIBMEMCACHED_MEMCACHED_REPLACE_END(); return rc; } -memcached_return_t memcached_prepend_by_key(memcached_st *ptr, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ - return memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, +memcached_return_t memcached_prepend_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, const char *value, + size_t value_length, time_t expiration, + uint32_t flags) { + return memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, expiration, flags, 0, PREPEND_OP); } -memcached_return_t memcached_append_by_key(memcached_st *ptr, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags) -{ - return memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, +memcached_return_t memcached_append_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, const char *value, + size_t value_length, time_t expiration, uint32_t flags) { + return memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, expiration, flags, 0, APPEND_OP); } -memcached_return_t memcached_cas_by_key(memcached_st *ptr, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - const char *value, size_t value_length, - time_t expiration, - uint32_t flags, - uint64_t cas) -{ - return memcached_send(ptr, group_key, group_key_length, - key, key_length, value, value_length, - expiration, flags, cas, CAS_OP); +memcached_return_t memcached_cas_by_key(memcached_st *ptr, const char *group_key, + size_t group_key_length, const char *key, size_t key_length, + const char *value, size_t value_length, time_t expiration, + uint32_t flags, uint64_t cas) { + return memcached_send(ptr, group_key, group_key_length, key, key_length, value, value_length, + expiration, flags, cas, CAS_OP); } - diff --git a/src/libmemcached/strerror.cc b/src/libmemcached/strerror.cc index 0c0c9a1b..3dc89811 100644 --- a/src/libmemcached/strerror.cc +++ b/src/libmemcached/strerror.cc @@ -1,195 +1,125 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -const char *memcached_strerror(const memcached_st *, memcached_return_t rc) -{ - switch (rc) - { - case MEMCACHED_SUCCESS: - return "SUCCESS"; +const char *memcached_strerror(const memcached_st *, memcached_return_t rc) { + switch (rc) { + case MEMCACHED_SUCCESS: return "SUCCESS"; - case MEMCACHED_FAILURE: - return "FAILURE"; + case MEMCACHED_FAILURE: return "FAILURE"; case MEMCACHED_HOST_LOOKUP_FAILURE: // getaddrinfo only return "getaddrinfo() or getnameinfo() HOSTNAME LOOKUP FAILURE"; - case MEMCACHED_CONNECTION_FAILURE: - return "CONNECTION FAILURE"; + case MEMCACHED_CONNECTION_FAILURE: return "CONNECTION FAILURE"; case MEMCACHED_CONNECTION_BIND_FAILURE: // DEPRECATED, see MEMCACHED_HOST_LOOKUP_FAILURE return "CONNECTION BIND FAILURE"; - case MEMCACHED_READ_FAILURE: - return "READ FAILURE"; + case MEMCACHED_READ_FAILURE: return "READ FAILURE"; - case MEMCACHED_UNKNOWN_READ_FAILURE: - return "UNKNOWN READ FAILURE"; + case MEMCACHED_UNKNOWN_READ_FAILURE: return "UNKNOWN READ FAILURE"; - case MEMCACHED_PROTOCOL_ERROR: - return "PROTOCOL ERROR"; + case MEMCACHED_PROTOCOL_ERROR: return "PROTOCOL ERROR"; - case MEMCACHED_CLIENT_ERROR: - return "CLIENT ERROR"; + case MEMCACHED_CLIENT_ERROR: return "CLIENT ERROR"; - case MEMCACHED_SERVER_ERROR: - return "SERVER ERROR"; + case MEMCACHED_SERVER_ERROR: return "SERVER ERROR"; - case MEMCACHED_WRITE_FAILURE: - return "WRITE FAILURE"; + case MEMCACHED_WRITE_FAILURE: return "WRITE FAILURE"; - case MEMCACHED_ERROR: - return "ERROR was returned by server"; + case MEMCACHED_ERROR: return "ERROR was returned by server"; - case MEMCACHED_DATA_EXISTS: - return "CONNECTION DATA EXISTS"; + case MEMCACHED_DATA_EXISTS: return "CONNECTION DATA EXISTS"; - case MEMCACHED_DATA_DOES_NOT_EXIST: - return "CONNECTION DATA DOES NOT EXIST"; + case MEMCACHED_DATA_DOES_NOT_EXIST: return "CONNECTION DATA DOES NOT EXIST"; - case MEMCACHED_NOTSTORED: - return "NOT STORED"; + case MEMCACHED_NOTSTORED: return "NOT STORED"; - case MEMCACHED_STORED: - return "STORED"; + case MEMCACHED_STORED: return "STORED"; - case MEMCACHED_NOTFOUND: - return "NOT FOUND"; + case MEMCACHED_NOTFOUND: return "NOT FOUND"; - case MEMCACHED_MEMORY_ALLOCATION_FAILURE: - return "MEMORY ALLOCATION FAILURE"; + case MEMCACHED_MEMORY_ALLOCATION_FAILURE: return "MEMORY ALLOCATION FAILURE"; - case MEMCACHED_PARTIAL_READ: - return "PARTIAL READ"; + case MEMCACHED_PARTIAL_READ: return "PARTIAL READ"; - case MEMCACHED_SOME_ERRORS: - return "SOME ERRORS WERE REPORTED"; + case MEMCACHED_SOME_ERRORS: return "SOME ERRORS WERE REPORTED"; - case MEMCACHED_NO_SERVERS: - return "NO SERVERS DEFINED"; + case MEMCACHED_NO_SERVERS: return "NO SERVERS DEFINED"; - case MEMCACHED_END: - return "SERVER END"; + case MEMCACHED_END: return "SERVER END"; - case MEMCACHED_DELETED: - return "SERVER DELETE"; + case MEMCACHED_DELETED: return "SERVER DELETE"; - case MEMCACHED_VALUE: - return "SERVER VALUE"; + case MEMCACHED_VALUE: return "SERVER VALUE"; - case MEMCACHED_STAT: - return "STAT VALUE"; + case MEMCACHED_STAT: return "STAT VALUE"; - case MEMCACHED_ITEM: - return "ITEM VALUE"; + case MEMCACHED_ITEM: return "ITEM VALUE"; - case MEMCACHED_ERRNO: - return "SYSTEM ERROR"; + case MEMCACHED_ERRNO: return "SYSTEM ERROR"; - case MEMCACHED_FAIL_UNIX_SOCKET: - return "COULD NOT OPEN UNIX SOCKET"; + case MEMCACHED_FAIL_UNIX_SOCKET: return "COULD NOT OPEN UNIX SOCKET"; - case MEMCACHED_NOT_SUPPORTED: - return "ACTION NOT SUPPORTED"; + case MEMCACHED_NOT_SUPPORTED: return "ACTION NOT SUPPORTED"; - case MEMCACHED_FETCH_NOTFINISHED: - return "FETCH WAS NOT COMPLETED"; + case MEMCACHED_FETCH_NOTFINISHED: return "FETCH WAS NOT COMPLETED"; - case MEMCACHED_NO_KEY_PROVIDED: - return "A KEY LENGTH OF ZERO WAS PROVIDED"; + case MEMCACHED_NO_KEY_PROVIDED: return "A KEY LENGTH OF ZERO WAS PROVIDED"; - case MEMCACHED_BUFFERED: - return "ACTION QUEUED"; + case MEMCACHED_BUFFERED: return "ACTION QUEUED"; - case MEMCACHED_TIMEOUT: - return "A TIMEOUT OCCURRED"; + case MEMCACHED_TIMEOUT: return "A TIMEOUT OCCURRED"; - case MEMCACHED_BAD_KEY_PROVIDED: - return "A BAD KEY WAS PROVIDED/CHARACTERS OUT OF RANGE"; + case MEMCACHED_BAD_KEY_PROVIDED: return "A BAD KEY WAS PROVIDED/CHARACTERS OUT OF RANGE"; case MEMCACHED_INVALID_HOST_PROTOCOL: return "THE HOST TRANSPORT PROTOCOL DOES NOT MATCH THAT OF THE CLIENT"; - case MEMCACHED_SERVER_MARKED_DEAD: - return "SERVER IS MARKED DEAD"; + case MEMCACHED_SERVER_MARKED_DEAD: return "SERVER IS MARKED DEAD"; - case MEMCACHED_UNKNOWN_STAT_KEY: - return "ENCOUNTERED AN UNKNOWN STAT KEY"; + case MEMCACHED_UNKNOWN_STAT_KEY: return "ENCOUNTERED AN UNKNOWN STAT KEY"; - case MEMCACHED_E2BIG: - return "ITEM TOO BIG"; + case MEMCACHED_E2BIG: return "ITEM TOO BIG"; - case MEMCACHED_INVALID_ARGUMENTS: - return "INVALID ARGUMENTS"; + case MEMCACHED_INVALID_ARGUMENTS: return "INVALID ARGUMENTS"; - case MEMCACHED_KEY_TOO_BIG: - return "KEY RETURNED FROM SERVER WAS TOO LARGE"; + case MEMCACHED_KEY_TOO_BIG: return "KEY RETURNED FROM SERVER WAS TOO LARGE"; - case MEMCACHED_AUTH_PROBLEM: - return "FAILED TO SEND AUTHENTICATION TO SERVER"; + case MEMCACHED_AUTH_PROBLEM: return "FAILED TO SEND AUTHENTICATION TO SERVER"; - case MEMCACHED_AUTH_FAILURE: - return "AUTHENTICATION FAILURE"; + case MEMCACHED_AUTH_FAILURE: return "AUTHENTICATION FAILURE"; - case MEMCACHED_AUTH_CONTINUE: - return "CONTINUE AUTHENTICATION"; + case MEMCACHED_AUTH_CONTINUE: return "CONTINUE AUTHENTICATION"; - case MEMCACHED_PARSE_ERROR: - return "ERROR OCCURED WHILE PARSING"; + case MEMCACHED_PARSE_ERROR: return "ERROR OCCURED WHILE PARSING"; - case MEMCACHED_PARSE_USER_ERROR: - return "USER INITIATED ERROR OCCURED WHILE PARSING"; + case MEMCACHED_PARSE_USER_ERROR: return "USER INITIATED ERROR OCCURED WHILE PARSING"; - case MEMCACHED_DEPRECATED: - return "DEPRECATED"; + case MEMCACHED_DEPRECATED: return "DEPRECATED"; - case MEMCACHED_IN_PROGRESS: - return "OPERATION IN PROCESS"; + case MEMCACHED_IN_PROGRESS: return "OPERATION IN PROCESS"; case MEMCACHED_SERVER_TEMPORARILY_DISABLED: return "SERVER HAS FAILED AND IS DISABLED UNTIL TIMED RETRY"; - case MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE: - return "SERVER FAILED TO ALLOCATE OBJECT"; + case MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE: return "SERVER FAILED TO ALLOCATE OBJECT"; default: - case MEMCACHED_MAXIMUM_RETURN: - return "INVALID memcached_return_t"; + case MEMCACHED_MAXIMUM_RETURN: return "INVALID memcached_return_t"; } } diff --git a/src/libmemcached/string.cc b/src/libmemcached/string.cc index 6ac24c78..1e6c256a 100644 --- a/src/libmemcached/string.cc +++ b/src/libmemcached/string.cc @@ -1,114 +1,85 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -inline static memcached_return_t _string_check(memcached_string_st *string, size_t need) -{ - if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string))) - { - size_t current_offset= (size_t) (string->end - string->string); +inline static memcached_return_t _string_check(memcached_string_st *string, size_t need) { + if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string))) { + size_t current_offset = (size_t)(string->end - string->string); - /* This is the block multiplier. To keep it larger and surive division errors we must round it up */ - size_t adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / MEMCACHED_BLOCK_SIZE; + /* This is the block multiplier. To keep it larger and surive division errors we must round it + * up */ + size_t adjust = (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) + / MEMCACHED_BLOCK_SIZE; adjust++; - size_t new_size= sizeof(char) * (size_t)((adjust * MEMCACHED_BLOCK_SIZE) + string->current_size); + size_t new_size = + sizeof(char) * (size_t)((adjust * MEMCACHED_BLOCK_SIZE) + string->current_size); /* Test for overflow */ - if (new_size < need) - { + if (new_size < need) { char error_message[1024]; - int error_message_length= snprintf(error_message, sizeof(error_message),"Needed %ld, got %ld", (long)need, (long)new_size); - return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, error_message, error_message_length); + int error_message_length = snprintf(error_message, sizeof(error_message), + "Needed %ld, got %ld", (long) need, (long) new_size); + return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + error_message, error_message_length); } - char *new_value= libmemcached_xrealloc(string->root, string->string, new_size, char); + char *new_value = libmemcached_xrealloc(string->root, string->string, new_size, char); - if (new_value == NULL) - { + if (new_value == NULL) { return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - string->string= new_value; - string->end= string->string + current_offset; + string->string = new_value; + string->end = string->string + current_offset; - string->current_size+= (MEMCACHED_BLOCK_SIZE * adjust); + string->current_size += (MEMCACHED_BLOCK_SIZE * adjust); } return MEMCACHED_SUCCESS; } -static inline void _init_string(memcached_string_st *self) -{ - self->current_size= 0; - self->end= self->string= NULL; +static inline void _init_string(memcached_string_st *self) { + self->current_size = 0; + self->end = self->string = NULL; } -memcached_string_st *memcached_string_create(Memcached *memc, memcached_string_st *self, size_t initial_size) -{ +memcached_string_st *memcached_string_create(Memcached *memc, memcached_string_st *self, + size_t initial_size) { WATCHPOINT_ASSERT(memc); /* Saving malloc calls :) */ - if (self) - { + if (self) { WATCHPOINT_ASSERT(self->options.is_initialized == false); memcached_set_allocated(self, false); - } - else - { - self= libmemcached_xmalloc(memc, memcached_string_st); + } else { + self = libmemcached_xmalloc(memc, memcached_string_st); - if (self == NULL) - { + if (self == NULL) { return NULL; } memcached_set_allocated(self, true); } - self->root= memc; + self->root = memc; _init_string(self); - if (memcached_failed(_string_check(self, initial_size))) - { - if (memcached_is_allocated(self)) - { + if (memcached_failed(_string_check(self, initial_size))) { + if (memcached_is_allocated(self)) { libmemcached_free(memc, self); } @@ -122,49 +93,40 @@ memcached_string_st *memcached_string_create(Memcached *memc, memcached_string_s return self; } -static memcached_return_t memcached_string_append_null(memcached_string_st& string) -{ - if (memcached_failed(_string_check(&string, 1))) - { +static memcached_return_t memcached_string_append_null(memcached_string_st &string) { + if (memcached_failed(_string_check(&string, 1))) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - *string.end= 0; + *string.end = 0; return MEMCACHED_SUCCESS; } -static memcached_return_t memcached_string_append_null(memcached_string_st *string) -{ - if (memcached_failed(_string_check(string, 1))) - { +static memcached_return_t memcached_string_append_null(memcached_string_st *string) { + if (memcached_failed(_string_check(string, 1))) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - *string->end= 0; + *string->end = 0; return MEMCACHED_SUCCESS; } -memcached_return_t memcached_string_append_character(memcached_string_st *string, - char character) -{ - if (memcached_failed(_string_check(string, 1))) - { +memcached_return_t memcached_string_append_character(memcached_string_st *string, char character) { + if (memcached_failed(_string_check(string, 1))) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - *string->end= character; + *string->end = character; string->end++; return MEMCACHED_SUCCESS; } -memcached_return_t memcached_string_append(memcached_string_st *string, - const char *value, size_t length) -{ - if (memcached_failed(_string_check(string, length))) - { +memcached_return_t memcached_string_append(memcached_string_st *string, const char *value, + size_t length) { + if (memcached_failed(_string_check(string, length))) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } @@ -173,36 +135,32 @@ memcached_return_t memcached_string_append(memcached_string_st *string, WATCHPOINT_ASSERT(string->end >= string->string); memcpy(string->end, value, length); - string->end+= length; + string->end += length; return MEMCACHED_SUCCESS; } -char *memcached_string_c_copy(memcached_string_st *string) -{ - if (memcached_string_length(string) == 0) - { +char *memcached_string_c_copy(memcached_string_st *string) { + if (memcached_string_length(string) == 0) { return NULL; } - char *c_ptr= static_cast(libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char))); + char *c_ptr = static_cast( + libmemcached_malloc(string->root, (memcached_string_length(string) + 1) * sizeof(char))); - if (c_ptr == NULL) - { + if (c_ptr == NULL) { return NULL; } memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string)); - c_ptr[memcached_string_length(string)]= 0; + c_ptr[memcached_string_length(string)] = 0; return c_ptr; } -bool memcached_string_set(memcached_string_st& string, const char* value, size_t length) -{ +bool memcached_string_set(memcached_string_st &string, const char *value, size_t length) { memcached_string_reset(&string); - if (memcached_success(memcached_string_append(&string, value, length))) - { + if (memcached_success(memcached_string_append(&string, value, length))) { memcached_string_append_null(string); return true; } @@ -210,89 +168,70 @@ bool memcached_string_set(memcached_string_st& string, const char* value, size_t return false; } -void memcached_string_reset(memcached_string_st *string) -{ - string->end= string->string; +void memcached_string_reset(memcached_string_st *string) { + string->end = string->string; } -void memcached_string_free(memcached_string_st& ptr) -{ +void memcached_string_free(memcached_string_st &ptr) { memcached_string_free(&ptr); } -void memcached_string_free(memcached_string_st *ptr) -{ - if (ptr == NULL) - { +void memcached_string_free(memcached_string_st *ptr) { + if (ptr == NULL) { return; } - if (ptr->string) - { + if (ptr->string) { libmemcached_free(ptr->root, ptr->string); } - if (memcached_is_allocated(ptr)) - { + if (memcached_is_allocated(ptr)) { libmemcached_free(ptr->root, ptr); - } - else - { - ptr->options.is_initialized= false; + } else { + ptr->options.is_initialized = false; } } -memcached_return_t memcached_string_check(memcached_string_st *string, size_t need) -{ +memcached_return_t memcached_string_check(memcached_string_st *string, size_t need) { return _string_check(string, need); } -bool memcached_string_resize(memcached_string_st& string, const size_t need) -{ +bool memcached_string_resize(memcached_string_st &string, const size_t need) { return memcached_success(_string_check(&string, need)); } -size_t memcached_string_length(const memcached_string_st *self) -{ - return size_t(self->end -self->string); +size_t memcached_string_length(const memcached_string_st *self) { + return size_t(self->end - self->string); } -size_t memcached_string_length(const memcached_string_st& self) -{ - return size_t(self.end -self.string); +size_t memcached_string_length(const memcached_string_st &self) { + return size_t(self.end - self.string); } -size_t memcached_string_size(const memcached_string_st *self) -{ +size_t memcached_string_size(const memcached_string_st *self) { return self->current_size; } -const char *memcached_string_value(const memcached_string_st *self) -{ +const char *memcached_string_value(const memcached_string_st *self) { return self->string; } -const char *memcached_string_value(const memcached_string_st& self) -{ +const char *memcached_string_value(const memcached_string_st &self) { return self.string; } -char *memcached_string_take_value(memcached_string_st *self) -{ - char* value= NULL; +char *memcached_string_take_value(memcached_string_st *self) { + char *value = NULL; assert_msg(self, "Invalid memcached_string_st"); - if (self) - { - if (memcached_string_length(self)) - { + if (self) { + if (memcached_string_length(self)) { // If we fail at adding the null, we copy and move on - if (memcached_failed(memcached_string_append_null(self))) - { + if (memcached_failed(memcached_string_append_null(self))) { return NULL; } - value= self->string; + value = self->string; _init_string(self); } } @@ -300,31 +239,25 @@ char *memcached_string_take_value(memcached_string_st *self) return value; } -char *memcached_string_value_mutable(const memcached_string_st *self) -{ +char *memcached_string_value_mutable(const memcached_string_st *self) { return self->string; } -char *memcached_string_c_str(memcached_string_st& self) -{ +char *memcached_string_c_str(memcached_string_st &self) { return self.string; } -void memcached_string_set_length(memcached_string_st *self, size_t length) -{ - self->end= self->string +length; +void memcached_string_set_length(memcached_string_st *self, size_t length) { + self->end = self->string + length; } -void memcached_string_set_length(memcached_string_st& self, const size_t length) -{ +void memcached_string_set_length(memcached_string_st &self, const size_t length) { assert(self.current_size >= length); - size_t set_length= length; - if (self.current_size > length) - { - if (memcached_failed(_string_check(&self, length))) - { - set_length= self.current_size; + size_t set_length = length; + if (self.current_size > length) { + if (memcached_failed(_string_check(&self, length))) { + set_length = self.current_size; } } - self.end= self.string +set_length; + self.end = self.string + set_length; } diff --git a/src/libmemcached/string.hpp b/src/libmemcached/string.hpp index e2978ccd..28aa4b00 100644 --- a/src/libmemcached/string.hpp +++ b/src/libmemcached/string.hpp @@ -1,48 +1,26 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * libmcachedd client library. - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include "util/string.hpp" -#define memcached_literal_param util_literal_param -#define memcached_literal_param_size util_literal_param_size +#define memcached_literal_param util_literal_param +#define memcached_literal_param_size util_literal_param_size #define memcached_string_make_from_cstr util_string_make_from_cstr -#define memcached_array_length util_array_length +#define memcached_array_length util_array_length /** Strings are always under our control so we make some assumptions @@ -54,41 +32,39 @@ 3) A string always has a root. */ -memcached_string_st *memcached_string_create(memcached_st *ptr, - memcached_string_st *string, +memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size); memcached_return_t memcached_string_check(memcached_string_st *string, size_t need); char *memcached_string_c_copy(memcached_string_st *string); -memcached_return_t memcached_string_append_character(memcached_string_st *string, - char character); +memcached_return_t memcached_string_append_character(memcached_string_st *string, char character); -memcached_return_t memcached_string_append(memcached_string_st *string, - const char *value, size_t length); +memcached_return_t memcached_string_append(memcached_string_st *string, const char *value, + size_t length); void memcached_string_reset(memcached_string_st *string); void memcached_string_free(memcached_string_st *string); -void memcached_string_free(memcached_string_st&); +void memcached_string_free(memcached_string_st &); size_t memcached_string_length(const memcached_string_st *self); -size_t memcached_string_length(const memcached_string_st&); +size_t memcached_string_length(const memcached_string_st &); size_t memcached_string_size(const memcached_string_st *self); const char *memcached_string_value(const memcached_string_st *self); -const char *memcached_string_value(const memcached_string_st&); +const char *memcached_string_value(const memcached_string_st &); char *memcached_string_take_value(memcached_string_st *self); char *memcached_string_value_mutable(const memcached_string_st *self); -bool memcached_string_set(memcached_string_st&, const char*, size_t); +bool memcached_string_set(memcached_string_st &, const char *, size_t); void memcached_string_set_length(memcached_string_st *self, size_t length); -void memcached_string_set_length(memcached_string_st&, const size_t length); +void memcached_string_set_length(memcached_string_st &, const size_t length); -bool memcached_string_resize(memcached_string_st&, const size_t); -char *memcached_string_c_str(memcached_string_st&); +bool memcached_string_resize(memcached_string_st &, const size_t); +char *memcached_string_c_str(memcached_string_st &); diff --git a/src/libmemcached/touch.cc b/src/libmemcached/touch.cc index a4bb585a..7213cbbe 100644 --- a/src/libmemcached/touch.cc +++ b/src/libmemcached/touch.cc @@ -1,155 +1,122 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -static memcached_return_t ascii_touch(memcached_instance_st* instance, - const char *key, size_t key_length, - time_t expiration) -{ - char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1]; - int expiration_buffer_length= snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", (unsigned long long)expiration); - if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer)+1 or expiration_buffer_length < 0) +static memcached_return_t ascii_touch(memcached_instance_st *instance, const char *key, + size_t key_length, time_t expiration) { + char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1]; + int expiration_buffer_length = snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", + (unsigned long long) expiration); + if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer) + 1 + or expiration_buffer_length < 0) { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); + return memcached_set_error( + *instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)")); } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { memcached_literal_param("touch ") }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length }, - { expiration_buffer, size_t(expiration_buffer_length) }, - { memcached_literal_param("\r\n") } - }; + libmemcached_io_vector_st vector[] = {{NULL, 0}, + {memcached_literal_param("touch ")}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}, + {expiration_buffer, size_t(expiration_buffer_length)}, + {memcached_literal_param("\r\n")}}; memcached_return_t rc; - if (memcached_failed(rc= memcached_vdo(instance, vector, 6, true))) - { + if (memcached_failed(rc = memcached_vdo(instance, vector, 6, true))) { return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); } return rc; } -static memcached_return_t binary_touch(memcached_instance_st* instance, - const char *key, size_t key_length, - time_t expiration) -{ - protocol_binary_request_touch request= {}; //{.bytes= {0}}; +static memcached_return_t binary_touch(memcached_instance_st *instance, const char *key, + size_t key_length, time_t expiration) { + protocol_binary_request_touch request = {}; //{.bytes= {0}}; initialize_binary_request(instance, request.message.header); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_TOUCH; - request.message.header.request.extlen= 4; - request.message.header.request.keylen= htons((uint16_t)(key_length +memcached_array_size(instance->root->_namespace))); - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - request.message.header.request.bodylen= htonl((uint32_t)(key_length +memcached_array_size(instance->root->_namespace) +request.message.header.request.extlen)); - request.message.body.expiration= htonl((uint32_t) expiration); - - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { request.bytes, sizeof(request.bytes) }, - { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) }, - { key, key_length } - }; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_TOUCH; + request.message.header.request.extlen = 4; + request.message.header.request.keylen = + htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace))); + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.bodylen = + htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace) + + request.message.header.request.extlen)); + request.message.body.expiration = htonl((uint32_t) expiration); + + libmemcached_io_vector_st vector[] = {{NULL, 0}, + {request.bytes, sizeof(request.bytes)}, + {memcached_array_string(instance->root->_namespace), + memcached_array_size(instance->root->_namespace)}, + {key, key_length}}; memcached_return_t rc; - if (memcached_failed(rc= memcached_vdo(instance, vector, 4, true))) - { + if (memcached_failed(rc = memcached_vdo(instance, vector, 4, true))) { return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); } return rc; } -memcached_return_t memcached_touch(memcached_st *ptr, - const char *key, size_t key_length, - time_t expiration) -{ +memcached_return_t memcached_touch(memcached_st *ptr, const char *key, size_t key_length, + time_t expiration) { return memcached_touch_by_key(ptr, key, key_length, key, key_length, expiration); } -memcached_return_t memcached_touch_by_key(memcached_st *shell, - const char *group_key, size_t group_key_length, - const char *key, size_t key_length, - time_t expiration) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_touch_by_key(memcached_st *shell, const char *group_key, + size_t group_key_length, const char *key, + size_t key_length, time_t expiration) { + Memcached *ptr = memcached2Memcached(shell); LIBMEMCACHED_MEMCACHED_TOUCH_START(); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, true))) - { + if (memcached_failed(rc = initialize_query(ptr, true))) { return rc; } - if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1))) - { + if (memcached_failed(rc = memcached_key_test(*ptr, (const char **) &key, &key_length, 1))) { return memcached_set_error(*ptr, rc, MEMCACHED_AT); } - uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); - memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key); + uint32_t server_key = + memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); + memcached_instance_st *instance = memcached_instance_fetch(ptr, server_key); - if (ptr->flags.binary_protocol) - { - rc= binary_touch(instance, key, key_length, expiration); - } - else - { - rc= ascii_touch(instance, key, key_length, expiration); + if (ptr->flags.binary_protocol) { + rc = binary_touch(instance, key, key_length, expiration); + } else { + rc = ascii_touch(instance, key, key_length, expiration); } - if (memcached_failed(rc)) - { - return memcached_set_error(*instance, rc, MEMCACHED_AT, memcached_literal_param("Error occcured while writing touch command to server")); + if (memcached_failed(rc)) { + return memcached_set_error( + *instance, rc, MEMCACHED_AT, + memcached_literal_param("Error occcured while writing touch command to server")); } char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + rc = memcached_response(instance, buffer, sizeof(buffer), NULL); - if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_NOTFOUND) - { + if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_NOTFOUND) { return rc; } - return memcached_set_error(*instance, rc, MEMCACHED_AT, memcached_literal_param("Error occcured while reading response")); + return memcached_set_error(*instance, rc, MEMCACHED_AT, + memcached_literal_param("Error occcured while reading response")); } diff --git a/src/libmemcached/udp.cc b/src/libmemcached/udp.cc index f58b5bb7..0d1eb157 100644 --- a/src/libmemcached/udp.cc +++ b/src/libmemcached/udp.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -48,30 +26,27 @@ * extracts the message number from message_id, increments it and then * writes the new value back into the header */ -void increment_udp_message_id(memcached_instance_st* ptr) -{ - struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer; - uint16_t cur_req= get_udp_datagram_request_id(header); - int msg_num= get_msg_num_from_request_id(cur_req); - int thread_id= get_thread_id_from_request_id(cur_req); +void increment_udp_message_id(memcached_instance_st *ptr) { + struct udp_datagram_header_st *header = (struct udp_datagram_header_st *) ptr->write_buffer; + uint16_t cur_req = get_udp_datagram_request_id(header); + int msg_num = get_msg_num_from_request_id(cur_req); + int thread_id = get_thread_id_from_request_id(cur_req); if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0) - msg_num= 0; + msg_num = 0; - header->request_id= htons((uint16_t) (thread_id | msg_num)); + header->request_id = htons((uint16_t)(thread_id | msg_num)); } -bool memcached_io_init_udp_header(memcached_instance_st* ptr, const uint16_t thread_id) -{ - if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID) - { +bool memcached_io_init_udp_header(memcached_instance_st *ptr, const uint16_t thread_id) { + if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID) { return MEMCACHED_FAILURE; } - struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer; - header->request_id= htons(uint16_t((generate_udp_request_thread_id(thread_id)))); - header->num_datagrams= htons(1); - header->sequence_number= htons(0); + struct udp_datagram_header_st *header = (struct udp_datagram_header_st *) ptr->write_buffer; + header->request_id = htons(uint16_t((generate_udp_request_thread_id(thread_id)))); + header->num_datagrams = htons(1); + header->sequence_number = htons(0); return MEMCACHED_SUCCESS; } diff --git a/src/libmemcached/udp.hpp b/src/libmemcached/udp.hpp index 7442c597..ca564c7a 100644 --- a/src/libmemcached/udp.hpp +++ b/src/libmemcached/udp.hpp @@ -1,59 +1,37 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -#define MAX_UDP_DATAGRAM_LENGTH 1400 -#define UDP_DATAGRAM_HEADER_LENGTH 8 -#define UDP_REQUEST_ID_MSG_SIG_DIGITS 10 -#define UDP_REQUEST_ID_THREAD_MASK 0xFFFF << UDP_REQUEST_ID_MSG_SIG_DIGITS -#define get_udp_datagram_request_id(A) ntohs((A)->request_id) -#define get_udp_datagram_seq_num(A) ntohs((A)->sequence_number) +#define MAX_UDP_DATAGRAM_LENGTH 1400 +#define UDP_DATAGRAM_HEADER_LENGTH 8 +#define UDP_REQUEST_ID_MSG_SIG_DIGITS 10 +#define UDP_REQUEST_ID_THREAD_MASK 0xFFFF << UDP_REQUEST_ID_MSG_SIG_DIGITS +#define get_udp_datagram_request_id(A) ntohs((A)->request_id) +#define get_udp_datagram_seq_num(A) ntohs((A)->sequence_number) #define get_udp_datagram_num_datagrams(A) ntohs((A)->num_datagrams) -#define get_msg_num_from_request_id(A) ( (A) & (~(UDP_REQUEST_ID_THREAD_MASK)) ) -#define get_thread_id_from_request_id(A) ( (A) & (UDP_REQUEST_ID_THREAD_MASK) ) >> UDP_REQUEST_ID_MSG_SIG_DIGITS +#define get_msg_num_from_request_id(A) ((A) & (~(UDP_REQUEST_ID_THREAD_MASK))) +#define get_thread_id_from_request_id(A) \ + ((A) & (UDP_REQUEST_ID_THREAD_MASK)) >> UDP_REQUEST_ID_MSG_SIG_DIGITS #define generate_udp_request_thread_id(A) (A) << UDP_REQUEST_ID_MSG_SIG_DIGITS -#define UDP_REQUEST_ID_MAX_THREAD_ID get_thread_id_from_request_id(0xFFFF) +#define UDP_REQUEST_ID_MAX_THREAD_ID get_thread_id_from_request_id(0xFFFF) -struct udp_datagram_header_st -{ +struct udp_datagram_header_st { uint16_t request_id; uint16_t sequence_number; uint16_t num_datagrams; uint16_t reserved; }; -bool memcached_io_init_udp_header(memcached_instance_st*, const uint16_t thread_id); -void increment_udp_message_id(memcached_instance_st*); +bool memcached_io_init_udp_header(memcached_instance_st *, const uint16_t thread_id); +void increment_udp_message_id(memcached_instance_st *); diff --git a/src/libmemcached/util.h b/src/libmemcached/util.h index 59405b5a..48c7fc49 100644 --- a/src/libmemcached/util.h +++ b/src/libmemcached/util.h @@ -1,40 +1,18 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #include "libmemcachedutil-1.0/util.h" - diff --git a/src/libmemcached/verbosity.cc b/src/libmemcached/verbosity.cc index 315ee346..575a2aee 100644 --- a/src/libmemcached/verbosity.cc +++ b/src/libmemcached/verbosity.cc @@ -1,69 +1,43 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" -struct context_st -{ +struct context_st { size_t length; const char *buffer; }; -static memcached_return_t _set_verbosity(const Memcached *, - const memcached_instance_st * server, - void *context) -{ - libmemcached_io_vector_st *vector= (libmemcached_io_vector_st *)context; +static memcached_return_t _set_verbosity(const Memcached *, const memcached_instance_st *server, + void *context) { + libmemcached_io_vector_st *vector = (libmemcached_io_vector_st *) context; Memcached local_memc; - Memcached *memc_ptr= memcached_create(&local_memc); + Memcached *memc_ptr = memcached_create(&local_memc); - memcached_return_t rc= memcached_server_add(memc_ptr, memcached_server_name(server), memcached_server_port(server)); + memcached_return_t rc = + memcached_server_add(memc_ptr, memcached_server_name(server), memcached_server_port(server)); - if (rc == MEMCACHED_SUCCESS) - { - memcached_instance_st* instance= memcached_instance_fetch(memc_ptr, 0); + if (rc == MEMCACHED_SUCCESS) { + memcached_instance_st *instance = memcached_instance_fetch(memc_ptr, 0); - rc= memcached_vdo(instance, vector, 2, true); + rc = memcached_vdo(instance, vector, 2, true); - if (rc == MEMCACHED_SUCCESS) - { + if (rc == MEMCACHED_SUCCESS) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + rc = memcached_response(instance, buffer, sizeof(buffer), NULL); } } @@ -72,12 +46,10 @@ static memcached_return_t _set_verbosity(const Memcached *, return rc; } -memcached_return_t memcached_verbosity(memcached_st *shell, uint32_t verbosity) -{ - Memcached* ptr= memcached2Memcached(shell); +memcached_return_t memcached_verbosity(memcached_st *shell, uint32_t verbosity) { + Memcached *ptr = memcached2Memcached(shell); memcached_return_t rc; - if (memcached_failed(rc= initialize_query(ptr, false))) - { + if (memcached_failed(rc = initialize_query(ptr, false))) { return rc; } @@ -85,20 +57,18 @@ memcached_return_t memcached_verbosity(memcached_st *shell, uint32_t verbosity) char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - int send_length= snprintf(buffer, sizeof(buffer), "verbosity %u\r\n", verbosity); - if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0) - { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + int send_length = snprintf(buffer, sizeof(buffer), "verbosity %u\r\n", verbosity); + if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0) { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); } - libmemcached_io_vector_st vector[]= - { - { NULL, 0 }, - { buffer, size_t(send_length) }, + libmemcached_io_vector_st vector[] = { + {NULL, 0}, + {buffer, size_t(send_length)}, }; - callbacks[0]= _set_verbosity; + callbacks[0] = _set_verbosity; return memcached_server_cursor(ptr, callbacks, vector, 1); } diff --git a/src/libmemcached/version.cc b/src/libmemcached/version.cc index 6a04b835..557b936d 100644 --- a/src/libmemcached/version.cc +++ b/src/libmemcached/version.cc @@ -1,86 +1,56 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2010 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "libmemcached/common.h" -const char * memcached_lib_version(void) -{ +const char *memcached_lib_version(void) { return LIBMEMCACHED_VERSION_STRING; } -static inline memcached_return_t memcached_version_textual(Memcached *memc) -{ - libmemcached_io_vector_st vector[]= - { - { memcached_literal_param("version\r\n") }, +static inline memcached_return_t memcached_version_textual(Memcached *memc) { + libmemcached_io_vector_st vector[] = { + {memcached_literal_param("version\r\n")}, }; - uint32_t success= 0; - bool errors_happened= false; - for (uint32_t x= 0; x < memcached_server_count(memc); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + uint32_t success = 0; + bool errors_happened = false; + for (uint32_t x = 0; x < memcached_server_count(memc); x++) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); // Optimization, we only fetch version once. - if (instance->major_version != UINT8_MAX) - { + if (instance->major_version != UINT8_MAX) { continue; } memcached_return_t rrc; - if (memcached_failed(rrc= memcached_vdo(instance, vector, 1, true))) - { - errors_happened= true; - (void)memcached_set_error(*instance, rrc, MEMCACHED_AT); + if (memcached_failed(rrc = memcached_vdo(instance, vector, 1, true))) { + errors_happened = true; + (void) memcached_set_error(*instance, rrc, MEMCACHED_AT); continue; } success++; } - if (success) - { + if (success) { // Collect the returned items - memcached_instance_st* instance; + memcached_instance_st *instance; memcached_return_t readable_error; - while ((instance= memcached_io_get_readable_server(memc, readable_error))) - { - memcached_return_t rrc= memcached_response(instance, NULL); - if (memcached_failed(rrc)) - { - errors_happened= true; + while ((instance = memcached_io_get_readable_server(memc, readable_error))) { + memcached_return_t rrc = memcached_response(instance, NULL); + if (memcached_failed(rrc)) { + errors_happened = true; } } } @@ -88,53 +58,43 @@ static inline memcached_return_t memcached_version_textual(Memcached *memc) return errors_happened ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS; } -static inline memcached_return_t memcached_version_binary(Memcached *memc) -{ - protocol_binary_request_version request= {}; +static inline memcached_return_t memcached_version_binary(Memcached *memc) { + protocol_binary_request_version request = {}; - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_VERSION; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) } - }; + libmemcached_io_vector_st vector[] = {{request.bytes, sizeof(request.bytes)}}; - uint32_t success= 0; - bool errors_happened= false; - for (uint32_t x= 0; x < memcached_server_count(memc); x++) - { - memcached_instance_st* instance= memcached_instance_fetch(memc, x); + uint32_t success = 0; + bool errors_happened = false; + for (uint32_t x = 0; x < memcached_server_count(memc); x++) { + memcached_instance_st *instance = memcached_instance_fetch(memc, x); initialize_binary_request(instance, request.message.header); - if (instance->major_version != UINT8_MAX) - { + if (instance->major_version != UINT8_MAX) { continue; } - memcached_return_t rrc= memcached_vdo(instance, vector, 1, true); - if (memcached_failed(rrc)) - { - errors_happened= true; + memcached_return_t rrc = memcached_vdo(instance, vector, 1, true); + if (memcached_failed(rrc)) { + errors_happened = true; continue; } success++; } - if (success) - { + if (success) { // Collect the returned items - memcached_instance_st* instance; + memcached_instance_st *instance; memcached_return_t readable_error; - while ((instance= memcached_io_get_readable_server(memc, readable_error))) - { + while ((instance = memcached_io_get_readable_server(memc, readable_error))) { char buffer[32]; - memcached_return_t rrc= memcached_response(instance, buffer, sizeof(buffer), NULL); - if (memcached_failed(rrc)) - { - errors_happened= true; + memcached_return_t rrc = memcached_response(instance, buffer, sizeof(buffer), NULL); + if (memcached_failed(rrc)) { + errors_happened = true; } } } @@ -142,79 +102,58 @@ static inline memcached_return_t memcached_version_binary(Memcached *memc) return errors_happened ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS; } -static inline void version_ascii_instance(memcached_instance_st* instance) -{ - if (instance->major_version != UINT8_MAX) - { - libmemcached_io_vector_st vector[]= - { - { memcached_literal_param("version\r\n") }, +static inline void version_ascii_instance(memcached_instance_st *instance) { + if (instance->major_version != UINT8_MAX) { + libmemcached_io_vector_st vector[] = { + {memcached_literal_param("version\r\n")}, }; - (void)memcached_vdo(instance, vector, 1, false); + (void) memcached_vdo(instance, vector, 1, false); } } -static inline void version_binary_instance(memcached_instance_st* instance) -{ - if (instance->major_version != UINT8_MAX) - { - protocol_binary_request_version request= {}; +static inline void version_binary_instance(memcached_instance_st *instance) { + if (instance->major_version != UINT8_MAX) { + protocol_binary_request_version request = {}; - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION; - request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; + request.message.header.request.opcode = PROTOCOL_BINARY_CMD_VERSION; + request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES; - libmemcached_io_vector_st vector[]= - { - { request.bytes, sizeof(request.bytes) } - }; + libmemcached_io_vector_st vector[] = {{request.bytes, sizeof(request.bytes)}}; initialize_binary_request(instance, request.message.header); - (void)memcached_vdo(instance, vector, 1, false); + (void) memcached_vdo(instance, vector, 1, false); } } -void memcached_version_instance(memcached_instance_st* instance) -{ - if (instance) - { - if (memcached_has_root(instance)) - { - if (memcached_is_fetching_version(instance->root)) - { - if (memcached_is_udp(instance->root) == false) - { - - if (memcached_is_binary(instance->root)) - { +void memcached_version_instance(memcached_instance_st *instance) { + if (instance) { + if (memcached_has_root(instance)) { + if (memcached_is_fetching_version(instance->root)) { + if (memcached_is_udp(instance->root) == false) { + if (memcached_is_binary(instance->root)) { version_binary_instance(instance); return; } - version_ascii_instance(instance); + version_ascii_instance(instance); } } } } } -int8_t memcached_version_instance_cmp(memcached_instance_st *instance, - uint8_t maj, uint8_t min, uint8_t mic) -{ +int8_t memcached_version_instance_cmp(memcached_instance_st *instance, uint8_t maj, uint8_t min, + uint8_t mic) { if (!instance || memcached_server_major_version(instance) == UINT8_MAX) { return INT8_MIN; } else { uint32_t sv, cv; - sv = memcached_server_micro_version(instance) - |memcached_server_minor_version(instance) << 8 - |memcached_server_major_version(instance) << 16 - ; - cv = mic - |min << 8 - |maj << 16 - ; + sv = memcached_server_micro_version(instance) | memcached_server_minor_version(instance) << 8 + | memcached_server_major_version(instance) << 16; + cv = mic | min << 8 | maj << 16; if (sv < cv) { return -1; } @@ -222,28 +161,23 @@ int8_t memcached_version_instance_cmp(memcached_instance_st *instance, } } -memcached_return_t memcached_version(memcached_st *shell) -{ - Memcached* memc= memcached2Memcached(shell); - if (memc) - { +memcached_return_t memcached_version(memcached_st *shell) { + Memcached *memc = memcached2Memcached(shell); + if (memc) { memcached_return_t rc; - if (memcached_failed(rc= initialize_query(memc, true))) - { + if (memcached_failed(rc = initialize_query(memc, true))) { return rc; } - if (memcached_is_udp(memc)) - { + if (memcached_is_udp(memc)) { return MEMCACHED_NOT_SUPPORTED; } - if (memcached_is_binary(memc)) - { + if (memcached_is_binary(memc)) { return memcached_version_binary(memc); } - return memcached_version_textual(memc); + return memcached_version_textual(memc); } return MEMCACHED_INVALID_ARGUMENTS; diff --git a/src/libmemcached/version.hpp b/src/libmemcached/version.hpp index 0c167433..b73a397b 100644 --- a/src/libmemcached/version.hpp +++ b/src/libmemcached/version.hpp @@ -1,46 +1,20 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * LibMemcached - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ All - * rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - /* - Common include file for libmemached + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #pragma once -void memcached_version_instance(memcached_instance_st*); -int8_t memcached_version_instance_cmp(memcached_instance_st*, - uint8_t maj, uint8_t min, uint8_t mic); +void memcached_version_instance(memcached_instance_st *); +int8_t memcached_version_instance_cmp(memcached_instance_st *, uint8_t maj, uint8_t min, + uint8_t mic); diff --git a/src/libmemcached/virtual_bucket.c b/src/libmemcached/virtual_bucket.c index e68aa789..40c3fad0 100644 --- a/src/libmemcached/virtual_bucket.c +++ b/src/libmemcached/virtual_bucket.c @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcached/common.h" @@ -49,71 +27,57 @@ struct memcached_virtual_bucket_t { struct bucket_t buckets[]; }; -memcached_return_t memcached_virtual_bucket_create(memcached_st *self, - const uint32_t *host_map, +memcached_return_t memcached_virtual_bucket_create(memcached_st *self, const uint32_t *host_map, const uint32_t *forward_map, const uint32_t buckets, - const uint32_t replicas) -{ - if (self == NULL || host_map == NULL || buckets == 0U) - { + const uint32_t replicas) { + if (self == NULL || host_map == NULL || buckets == 0U) { return MEMCACHED_INVALID_ARGUMENTS; } memcached_virtual_bucket_free(self); - struct memcached_virtual_bucket_t *virtual_bucket= (struct memcached_virtual_bucket_t *)malloc(sizeof(struct memcached_virtual_bucket_t) + sizeof(struct bucket_t) *buckets); - - if (virtual_bucket == NULL) - { + struct memcached_virtual_bucket_t *virtual_bucket = (struct memcached_virtual_bucket_t *) malloc( + sizeof(struct memcached_virtual_bucket_t) + sizeof(struct bucket_t) * buckets); + + if (virtual_bucket == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } + virtual_bucket->size = buckets; + virtual_bucket->replicas = replicas; + self->virtual_bucket = virtual_bucket; - virtual_bucket->size= buckets; - virtual_bucket->replicas= replicas; - self->virtual_bucket= virtual_bucket; - - uint32_t x= 0; - for (; x < buckets; x++) - { - virtual_bucket->buckets[x].master= host_map[x]; - if (forward_map) - { - virtual_bucket->buckets[x].forward= forward_map[x]; - } - else - { - virtual_bucket->buckets[x].forward= 0; + uint32_t x = 0; + for (; x < buckets; x++) { + virtual_bucket->buckets[x].master = host_map[x]; + if (forward_map) { + virtual_bucket->buckets[x].forward = forward_map[x]; + } else { + virtual_bucket->buckets[x].forward = 0; } } return MEMCACHED_SUCCESS; } -void memcached_virtual_bucket_free(memcached_st *self) -{ - if (self) - { - if (self->virtual_bucket) - { +void memcached_virtual_bucket_free(memcached_st *self) { + if (self) { + if (self->virtual_bucket) { free(self->virtual_bucket); - self->virtual_bucket= NULL; + self->virtual_bucket = NULL; } } } -uint32_t memcached_virtual_bucket_get(const memcached_st *self, uint32_t digest) -{ - if (self) - { - if (self->virtual_bucket) - { - uint32_t result= (uint32_t) (digest & (self->virtual_bucket->size -1)); +uint32_t memcached_virtual_bucket_get(const memcached_st *self, uint32_t digest) { + if (self) { + if (self->virtual_bucket) { + uint32_t result = (uint32_t)(digest & (self->virtual_bucket->size - 1)); return self->virtual_bucket->buckets[result].master; } - return (uint32_t) (digest & (self->number_of_hosts -1)); + return (uint32_t)(digest & (self->number_of_hosts - 1)); } return 0; diff --git a/src/libmemcached/virtual_bucket.h b/src/libmemcached/virtual_bucket.h index 9a63c38f..a43d3e81 100644 --- a/src/libmemcached/virtual_bucket.h +++ b/src/libmemcached/virtual_bucket.h @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once @@ -41,11 +19,9 @@ extern "C" { #endif -memcached_return_t memcached_virtual_bucket_create(memcached_st *self, - const uint32_t *host_map, +memcached_return_t memcached_virtual_bucket_create(memcached_st *self, const uint32_t *host_map, const uint32_t *forward_map, - const uint32_t buckets, - const uint32_t replicas); + const uint32_t buckets, const uint32_t replicas); uint32_t memcached_virtual_bucket_get(const memcached_st *self, uint32_t digest); diff --git a/src/libmemcached/watchpoint.h b/src/libmemcached/watchpoint.h index 822101e9..2a9a6cb9 100644 --- a/src/libmemcached/watchpoint.h +++ b/src/libmemcached/watchpoint.h @@ -1,51 +1,29 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #define WATCHPOINT #define WATCHPOINT_ERROR(A) -#define WATCHPOINT_IFERROR(__memcached_return_t) (void)(__memcached_return_t) +#define WATCHPOINT_IFERROR(__memcached_return_t) (void) (__memcached_return_t) #define WATCHPOINT_STRING(A) #define WATCHPOINT_NUMBER(A) -#define WATCHPOINT_LABELED_NUMBER(A,B) -#define WATCHPOINT_IF_LABELED_NUMBER(A,B,C) +#define WATCHPOINT_LABELED_NUMBER(A, B) +#define WATCHPOINT_IF_LABELED_NUMBER(A, B, C) #define WATCHPOINT_ERRNO(A) -#define WATCHPOINT_ASSERT_PRINT(A,B,C) -#define WATCHPOINT_ASSERT(A) (void)(A) +#define WATCHPOINT_ASSERT_PRINT(A, B, C) +#define WATCHPOINT_ASSERT(A) (void) (A) #define WATCHPOINT_ASSERT_INITIALIZED(A) #define WATCHPOINT_SET(A) diff --git a/src/libmemcached/windows.hpp b/src/libmemcached/windows.hpp index 40564b1d..afa0be15 100644 --- a/src/libmemcached/windows.hpp +++ b/src/libmemcached/windows.hpp @@ -1,124 +1,82 @@ /* - * Libmemcached library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once #ifdef __cplusplus -# include +# include #else -# include +# include #endif #ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN #endif #ifndef _WIN32_WINNT -# define _WIN32_WINNT 0x0501 +# define _WIN32_WINNT 0x0501 #endif #ifdef __MINGW32__ -# if(_WIN32_WINNT >= 0x0501) -# else -# undef _WIN32_WINNT -# define _WIN32_WINNT 0x0501 -# endif /* _WIN32_WINNT >= 0x0501 */ -#endif /* __MINGW32__ */ +# if (_WIN32_WINNT >= 0x0501) +# else +# undef _WIN32_WINNT +# define _WIN32_WINNT 0x0501 +# endif /* _WIN32_WINNT >= 0x0501 */ +#endif /* __MINGW32__ */ #if defined(HAVE_WINSOCK2_H) && HAVE_WINSOCK2_H -# include +# include #endif #if defined(HAVE_WS2TCPIP_H) && HAVE_WS2TCPIP_H -# include +# include #endif #if defined(HAVE_IO_H) && HAVE_IO_H -# include +# include #endif -struct sockaddr_un -{ +struct sockaddr_un { short int sun_family; char sun_path[108]; }; -static inline int translate_windows_error() -{ - int local_errno= WSAGetLastError(); +static inline int translate_windows_error() { + int local_errno = WSAGetLastError(); - switch(local_errno) { - case WSAEINVAL: - local_errno= EINPROGRESS; - break; + switch (local_errno) { + case WSAEINVAL: local_errno = EINPROGRESS; break; case WSAEALREADY: - case WSAEWOULDBLOCK: - local_errno= EAGAIN; - break; + case WSAEWOULDBLOCK: local_errno = EAGAIN; break; - case WSAECONNREFUSED: - local_errno= ECONNREFUSED; - break; + case WSAECONNREFUSED: local_errno = ECONNREFUSED; break; - case WSAENETUNREACH: - local_errno= ENETUNREACH; - break; + case WSAENETUNREACH: local_errno = ENETUNREACH; break; - case WSAETIMEDOUT: - local_errno= ETIMEDOUT; - break; + case WSAETIMEDOUT: local_errno = ETIMEDOUT; break; - case WSAECONNRESET: - local_errno= ECONNRESET; - break; + case WSAECONNRESET: local_errno = ECONNRESET; break; - case WSAEADDRINUSE: - local_errno= EADDRINUSE; - break; + case WSAEADDRINUSE: local_errno = EADDRINUSE; break; - case WSAEOPNOTSUPP: - local_errno= EOPNOTSUPP; - break; + case WSAEOPNOTSUPP: local_errno = EOPNOTSUPP; break; - case WSAENOPROTOOPT: - local_errno= ENOPROTOOPT; - break; + case WSAENOPROTOOPT: local_errno = ENOPROTOOPT; break; - default: - break; + default: break; } return local_errno;