From f0b9c972a973d997db6660680938dd5acb2baa46 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 23 May 2011 15:16:28 -0700 Subject: [PATCH] This fixes bug 776354. --- libhashkit/hashkit.h | 4 - libhashkit/types.h | 16 ++-- libmemcached/allocators.cc | 87 +++++++++++++------ libmemcached/array.c | 37 ++++++-- libmemcached/array.h | 3 + libmemcached/auto.cc | 12 ++- libmemcached/basic_string.h | 2 + libmemcached/behavior.cc | 142 +++++++++++++++++++++--------- libmemcached/common.h | 1 + libmemcached/connect.cc | 14 ++- libmemcached/error.cc | 144 +++++++++++++++++++++---------- libmemcached/error.h | 29 ------- libmemcached/error.hpp | 84 ++++++++++++++++++ libmemcached/hosts.cc | 15 ++-- libmemcached/include.am | 2 + libmemcached/initialize_query.cc | 2 +- libmemcached/io.cc | 11 +-- libmemcached/memcached.cc | 22 ++--- libmemcached/options.cc | 81 +++++++++++------ libmemcached/options.h | 13 ++- libmemcached/options/context.h | 2 +- libmemcached/options/parser.cc | 2 +- libmemcached/options/parser.yy | 2 +- libmemcached/prefix_key.cc | 12 +-- libmemcached/response.cc | 38 ++++---- libmemcached/result.cc | 12 ++- libmemcached/return.h | 4 +- libmemcached/server.cc | 12 +-- libmemcached/server.h | 2 +- libmemcached/string.cc | 45 +++------- libmemcached/string.h | 2 +- libtest/test.h | 11 +++ tests/mem_functions.cc | 11 ++- tests/parser.cc | 12 +-- 34 files changed, 574 insertions(+), 314 deletions(-) create mode 100644 libmemcached/error.hpp diff --git a/libhashkit/hashkit.h b/libhashkit/hashkit.h index d4e9d5ea..9b8761f1 100644 --- a/libhashkit/hashkit.h +++ b/libhashkit/hashkit.h @@ -90,10 +90,6 @@ void hashkit_free(hashkit_st *hash); #define hashkit_is_allocated(__object) ((__object)->options.is_allocated) #define hashkit_is_initialized(__object) ((__object)->options.is_initialized) - -#define hashkit_success(X) (X) == HASHKIT_SUCCESS -#define hashkit_failed(X) (X) != HASHKIT_SUCCESS - #ifdef __cplusplus } // extern "C" #endif diff --git a/libhashkit/types.h b/libhashkit/types.h index 23bd9d5c..66e4cd9c 100644 --- a/libhashkit/types.h +++ b/libhashkit/types.h @@ -36,13 +36,7 @@ */ -#ifndef HASHKIT_TYPES_H -#define HASHKIT_TYPES_H - -#ifdef __cplusplus - -extern "C" { -#endif +#pragma once typedef enum { HASHKIT_SUCCESS, @@ -53,6 +47,9 @@ typedef enum { HASHKIT_MAXIMUM_RETURN /* Always add new error code before */ } hashkit_return_t; +#define hashkit_success(X) ((X) == HASHKIT_SUCCESS) +#define hashkit_failed(X) ((X) != HASHKIT_SUCCESS) + typedef enum { HASHKIT_HASH_DEFAULT= 0, // hashkit_one_at_a_time() HASHKIT_HASH_MD5, @@ -79,6 +76,9 @@ typedef enum HASHKIT_DISTRIBUTION_MAX /* Always add new values before this. */ } hashkit_distribution_t; +#ifdef __cplusplus +extern "C" { +#endif typedef struct hashkit_st hashkit_st; @@ -87,5 +87,3 @@ typedef uint32_t (*hashkit_hash_fn)(const char *key, size_t key_length, void *co #ifdef __cplusplus } #endif - -#endif /* HASHKIT_TYPES_H */ diff --git a/libmemcached/allocators.cc b/libmemcached/allocators.cc index e716bee3..d0d9e57e 100644 --- a/libmemcached/allocators.cc +++ b/libmemcached/allocators.cc @@ -1,32 +1,63 @@ -#include "common.h" +/* 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. + * + */ -void _libmemcached_free(const memcached_st *ptr, void *mem, void *context) +#include + +void _libmemcached_free(const memcached_st*, void *mem, void*) { - (void) ptr; - (void) context; free(mem); } -void *_libmemcached_malloc(const memcached_st *ptr, size_t size, void *context) +void *_libmemcached_malloc(const memcached_st *, size_t size, void *) { - (void) ptr; - (void) context; return malloc(size); } -void *_libmemcached_realloc(const memcached_st *ptr, void *mem, size_t size, void *context) +void *_libmemcached_realloc(const memcached_st*, void *mem, size_t size, void *) { - (void) ptr; - (void) context; return realloc(mem, size); } -void *_libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size, void *context) +void *_libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size, void *context) { - if (ptr->allocators.malloc != _libmemcached_malloc) + if (self->allocators.malloc != _libmemcached_malloc) { - void *ret = _libmemcached_malloc(ptr, nelem * size, context); - if (ret != NULL) + void *ret = _libmemcached_malloc(self, nelem * size, context); + if (not ret) memset(ret, 0, nelem * size); return ret; @@ -41,7 +72,7 @@ struct memcached_allocator_t memcached_allocators_return_default(void) return global_default_allocator; } -memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, +memcached_return_t memcached_set_memory_allocators(memcached_st *self, memcached_malloc_fn mem_malloc, memcached_free_fn mem_free, memcached_realloc_fn mem_realloc, @@ -51,7 +82,7 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, /* All should be set, or none should be set */ if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL) { - ptr->allocators= memcached_allocators_return_default(); + self->allocators= memcached_allocators_return_default(); } else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL) { @@ -59,29 +90,29 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, } else { - ptr->allocators.malloc= mem_malloc; - ptr->allocators.free= mem_free; - ptr->allocators.realloc= mem_realloc; - ptr->allocators.calloc= mem_calloc; - ptr->allocators.context= context; + 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 *ptr) +void *memcached_get_memory_allocators_context(const memcached_st *self) { - return ptr->allocators.context; + return self->allocators.context; } -void memcached_get_memory_allocators(const memcached_st *ptr, +void memcached_get_memory_allocators(const memcached_st *self, memcached_malloc_fn *mem_malloc, memcached_free_fn *mem_free, memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc) { - *mem_malloc= ptr->allocators.malloc; - *mem_free= ptr->allocators.free; - *mem_realloc= ptr->allocators.realloc; - *mem_calloc= ptr->allocators.calloc; + *mem_malloc= self->allocators.malloc; + *mem_free= self->allocators.free; + *mem_realloc= self->allocators.realloc; + *mem_calloc= self->allocators.calloc; } diff --git a/libmemcached/array.c b/libmemcached/array.c index d9074e7f..36df81d9 100644 --- a/libmemcached/array.c +++ b/libmemcached/array.c @@ -36,6 +36,8 @@ */ #include +#include +#include struct memcached_array_st { @@ -47,7 +49,7 @@ struct memcached_array_st memcached_array_st *memcached_array_clone(struct memcached_st *memc, const memcached_array_st *original) { - if (! original) + if (not original) return NULL; return memcached_strcpy(memc, original->c_str, original->size); @@ -55,21 +57,44 @@ memcached_array_st *memcached_array_clone(struct memcached_st *memc, const memca memcached_array_st *memcached_strcpy(struct memcached_st *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); - if (! array) + if (not array) return NULL; array->root= memc; - array->size= str_length -1; // We don't count the NULL ending + array->size= str_length; // We don't count the NULL ending memcpy(array->c_str, str, str_length); array->c_str[str_length]= 0; return array; } +bool memcached_array_is_null(memcached_array_st *array) +{ + assert(array); + assert(array->root); + + if (not array) + return false; + + if (array->size and array->c_str) + return false; + + assert(not array->size and not array->c_str); + + return true; +} + 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; @@ -79,7 +104,7 @@ memcached_string_t memcached_array_to_string(memcached_array_st *array) void memcached_array_free(memcached_array_st *array) { - if (! array) + if (not array) return; WATCHPOINT_ASSERT(array->root); @@ -95,7 +120,7 @@ void memcached_array_free(memcached_array_st *array) size_t memcached_array_size(memcached_array_st *array) { - if (! array) + if (not array) return 0; return array->size; @@ -103,7 +128,7 @@ size_t memcached_array_size(memcached_array_st *array) const char *memcached_array_string(memcached_array_st *array) { - if (! array) + if (not array) return NULL; return array->c_str; diff --git a/libmemcached/array.h b/libmemcached/array.h index ddc97d5a..22b61ac3 100644 --- a/libmemcached/array.h +++ b/libmemcached/array.h @@ -59,6 +59,9 @@ const char *memcached_array_string(memcached_array_st *array); LIBMEMCACHED_LOCAL memcached_string_t memcached_array_to_string(memcached_array_st *array); +LIBMEMCACHED_LOCAL +bool memcached_array_is_null(memcached_array_st *array); + #ifdef __cplusplus } // extern "C" #endif diff --git a/libmemcached/auto.cc b/libmemcached/auto.cc index 9da3f138..d7289f06 100644 --- a/libmemcached/auto.cc +++ b/libmemcached/auto.cc @@ -50,7 +50,7 @@ static memcached_return_t text_incr_decr(memcached_st *ptr, bool no_reply= ptr->flags.no_reply; if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) - return memcached_set_error(ptr, MEMCACHED_BAD_KEY_PROVIDED); + return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT); server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); instance= memcached_server_instance_fetch(ptr, server_key); @@ -62,7 +62,10 @@ static memcached_return_t text_incr_decr(memcached_st *ptr, (int)key_length, key, offset, no_reply ? " noreply" : ""); if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0) - return memcached_set_error_string(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + } memcached_return_t rc= memcached_do(instance, buffer, (size_t)send_length, true); if (no_reply or memcached_failed(rc)) @@ -98,7 +101,7 @@ static memcached_return_t text_incr_decr(memcached_st *ptr, rc= MEMCACHED_SUCCESS; } - return memcached_set_error(*instance, rc); + return memcached_set_error(*instance, rc, MEMCACHED_AT); } static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, @@ -113,7 +116,7 @@ static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, bool no_reply= ptr->flags.no_reply; if (memcached_server_count(ptr) == 0) - return memcached_set_error(ptr, MEMCACHED_NO_SERVERS); + return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT); server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); instance= memcached_server_instance_fetch(ptr, server_key); @@ -122,6 +125,7 @@ static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, { if(cmd == PROTOCOL_BINARY_CMD_DECREMENT) cmd= PROTOCOL_BINARY_CMD_DECREMENTQ; + if(cmd == PROTOCOL_BINARY_CMD_INCREMENT) cmd= PROTOCOL_BINARY_CMD_INCREMENTQ; } diff --git a/libmemcached/basic_string.h b/libmemcached/basic_string.h index b47724bb..de8d3d51 100644 --- a/libmemcached/basic_string.h +++ b/libmemcached/basic_string.h @@ -36,6 +36,8 @@ #pragma once +// No assumptions of NULL should be made + struct memcached_string_t { const char *c_str; size_t size; diff --git a/libmemcached/behavior.cc b/libmemcached/behavior.cc index 9e91f820..836edda6 100644 --- a/libmemcached/behavior.cc +++ b/libmemcached/behavior.cc @@ -31,29 +31,38 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, const memcached_behavior_t flag, uint64_t data) { + if (not ptr) + return MEMCACHED_INVALID_ARGUMENTS; + 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_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_SND_TIMEOUT: ptr->snd_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= set_flag(data); + case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: ptr->server_failure_limit= (uint32_t)data; break; @@ -66,17 +75,21 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, } ptr->flags.binary_protocol= set_flag(data); break; + case MEMCACHED_BEHAVIOR_SUPPORT_CAS: ptr->flags.support_cas= set_flag(data); break; + case MEMCACHED_BEHAVIOR_NO_BLOCK: ptr->flags.no_block= set_flag(data); send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: ptr->flags.buffer_requests= set_flag(data); send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_USE_UDP: if (memcached_server_count(ptr)) { @@ -88,16 +101,20 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, ptr->flags.no_reply= set_flag(data); } break; + case MEMCACHED_BEHAVIOR_TCP_NODELAY: ptr->flags.tcp_nodelay= set_flag(data); send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: ptr->flags.tcp_keepalive= set_flag(data); send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_DISTRIBUTION: return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data); + case MEMCACHED_BEHAVIOR_KETAMA: { if (data) // Turn on @@ -105,6 +122,7 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA); } + case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: { (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5); @@ -115,21 +133,24 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, */ return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); } + case MEMCACHED_BEHAVIOR_HASH: return memcached_behavior_set_key_hash(ptr, (memcached_hash_t)(data)); + case MEMCACHED_BEHAVIOR_KETAMA_HASH: return memcached_behavior_set_distribution_hash(ptr, (memcached_hash_t)(data)); case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: - return memcached_set_error_string(ptr, MEMCACHED_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_string(ptr, MEMCACHED_INVALID_ARGUMENTS, + 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= set_flag(data); break; + case MEMCACHED_BEHAVIOR_SORT_HOSTS: { ptr->flags.use_sort_hosts= set_flag(data); @@ -137,58 +158,69 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, 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_RETRY_TIMEOUT: ptr->retry_timeout= (int32_t)data; break; + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: ptr->send_size= (int32_t)data; send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: ptr->recv_size= (int32_t)data; send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: ptr->tcp_keepidle= (uint32_t)data; send_quit(ptr); break; + case MEMCACHED_BEHAVIOR_USER_DATA: - return memcached_set_error_string(ptr, MEMCACHED_DEPRECATED, - memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated.")); + 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_prefix_key= set_flag(data); break; + case MEMCACHED_BEHAVIOR_NOREPLY: ptr->flags.no_reply= set_flag(data); break; + case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: ptr->flags.auto_eject_hosts= set_flag(data); break; + case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: srandom((uint32_t) time(NULL)); ptr->flags.randomize_replica_read= set_flag(data); break; + case MEMCACHED_BEHAVIOR_CORK: - { - return memcached_set_error_string(ptr, MEMCACHED_DEPRECATED, - memcached_literal_param("MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default.")); - } - break; + 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_string(ptr, MEMCACHED_INVALID_ARGUMENTS, - 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 */ WATCHPOINT_ASSERT(0); - return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS, - memcached_literal_param("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; @@ -206,14 +238,19 @@ uint64_t memcached_behavior_get(memcached_st *ptr, { 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_BYTES_WATERMARK: return ptr->io_bytes_watermark; + case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: return ptr->io_key_prefetch; + case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: return ptr->flags.binary_protocol; + case MEMCACHED_BEHAVIOR_SUPPORT_CAS: return ptr->flags.support_cas; @@ -222,85 +259,102 @@ uint64_t memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_NO_BLOCK: return ptr->flags.no_block; + case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: return ptr->flags.buffer_requests; + case MEMCACHED_BEHAVIOR_USE_UDP: return ptr->flags.use_udp; + case MEMCACHED_BEHAVIOR_TCP_NODELAY: return ptr->flags.tcp_nodelay; + case MEMCACHED_BEHAVIOR_VERIFY_KEY: return ptr->flags.verify_key; + case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: return ptr->ketama.weighted; + case MEMCACHED_BEHAVIOR_DISTRIBUTION: return ptr->distribution; + case MEMCACHED_BEHAVIOR_KETAMA: return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0; + case MEMCACHED_BEHAVIOR_HASH: return hashkit_get_function(&ptr->hashkit); + case MEMCACHED_BEHAVIOR_KETAMA_HASH: return hashkit_get_function(&ptr->hashkit); + case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: return ptr->server_failure_limit; + 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_CONNECT_TIMEOUT: return (uint64_t)ptr->connect_timeout; + case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: return (uint64_t)ptr->retry_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_TCP_KEEPIDLE: return (uint64_t)ptr->tcp_keepidle; + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: { int sock_size= 0; socklen_t sock_length= sizeof(int); - memcached_server_write_instance_st instance; if (ptr->send_size != -1) // If value is -1 then we are using the default return (uint64_t) ptr->send_size; - instance= memcached_server_instance_fetch(ptr, 0); + memcached_server_write_instance_st instance= memcached_server_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_connect(instance)) != MEMCACHED_SUCCESS) + if (memcached_failed(memcached_connect(instance))) { return 0; } - if (memcached_io_wait_for_write(instance) != MEMCACHED_SUCCESS) + if (memcached_failed(memcached_io_wait_for_write(instance))) { return 0; } if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, &sock_size, &sock_length) < 0) { - memcached_set_errno(ptr, errno, NULL); + memcached_set_errno(*ptr, 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); - memcached_server_write_instance_st instance; if (ptr->recv_size != -1) // If value is -1 then we are using the default return (uint64_t) ptr->recv_size; - instance= memcached_server_instance_fetch(ptr, 0); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, 0); /** @note REFACTOR @@ -308,19 +362,19 @@ uint64_t memcached_behavior_get(memcached_st *ptr, if (instance) { /* We just try the first host, and if it is down we return zero */ - if ((memcached_connect(instance)) != MEMCACHED_SUCCESS) + if (memcached_failed(memcached_connect(instance))) { return 0; } - if (memcached_io_wait_for_write(instance) != MEMCACHED_SUCCESS) + if (memcached_failed(memcached_io_wait_for_write(instance))) { return 0; } if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, &sock_size, &sock_length) < 0) { - memcached_set_errno(ptr, errno, NULL); + memcached_set_errno(*ptr, errno, MEMCACHED_AT); return 0; /* Zero means error */ } @@ -328,28 +382,37 @@ uint64_t memcached_behavior_get(memcached_st *ptr, return (uint64_t) sock_size; } + case MEMCACHED_BEHAVIOR_USER_DATA: - memcached_set_error_string(ptr, MEMCACHED_DEPRECATED, - memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated.")); + 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_prefix_key; + case MEMCACHED_BEHAVIOR_NOREPLY: return ptr->flags.no_reply; + 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_CORK: #ifdef HAVE_MSG_MORE return true; #else return false; #endif + case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: return ptr->flags.tcp_keepalive; + case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE: - return ptr->configure.filename ? true : false; + return bool(memcached_parse_filename(ptr)); + case MEMCACHED_BEHAVIOR_MAX: default: WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */ @@ -377,8 +440,8 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcac return MEMCACHED_SUCCESS; } - return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS, - 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")); } @@ -389,11 +452,11 @@ memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st memcached_return_t memcached_behavior_set_key_hash(memcached_st *ptr, memcached_hash_t type) { - if (hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type) == HASHKIT_SUCCESS) + if (hashkit_success(hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type))) return MEMCACHED_SUCCESS; - return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS, - memcached_literal_param("Invalid memcached_hash_t()")); + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid memcached_hash_t()")); } memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr) @@ -406,8 +469,8 @@ memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, m if (hashkit_success(hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type))) return MEMCACHED_SUCCESS; - return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS, - memcached_literal_param("Invalid memcached_hash_t()")); + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid memcached_hash_t()")); } memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr) @@ -484,23 +547,20 @@ memcached_return_t memcached_bucket_set(memcached_st *self, { memcached_return_t rc; - if (! self) + if (not self) return MEMCACHED_INVALID_ARGUMENTS; - if (! host_map) + if (not host_map) return MEMCACHED_INVALID_ARGUMENTS; - memcached_server_distribution_t old; - old= memcached_behavior_get_distribution(self); + memcached_server_distribution_t old= memcached_behavior_get_distribution(self); - rc =memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc =memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET))) { return rc; } - rc= memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc= memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas))) { memcached_behavior_set_distribution(self, old); } diff --git a/libmemcached/common.h b/libmemcached/common.h index a90173bc..b8deac8e 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -88,6 +88,7 @@ memcached_return_t memcached_server_execute(memcached_st *ptr, /* These are private not to be installed headers */ +#include #include #include #include diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 0b77c7a8..35053d60 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -152,24 +152,22 @@ static memcached_return_t set_hostinfo(memcached_server_st *server) break; case EAI_AGAIN: - return memcached_set_error_string(*server, MEMCACHED_TIMEOUT, gai_strerror(errcode), strlen(gai_strerror(errcode))); + return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); case EAI_SYSTEM: - { - static memcached_string_t mesg= { memcached_literal_param("getaddrinfo") }; - return memcached_set_errno(*server, errno, &mesg); - } + return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_SYSTEM)")); + case EAI_BADFLAGS: - return memcached_set_error_string(*server, MEMCACHED_INVALID_ARGUMENTS, 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: - return memcached_set_error_string(*server, MEMCACHED_ERRNO, memcached_literal_param("getaddrinfo(EAI_MEMORY)")); + return memcached_set_error(*server, MEMCACHED_ERRNO, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_MEMORY)")); default: { WATCHPOINT_STRING(server->hostname); WATCHPOINT_STRING(gai_strerror(e)); - return memcached_set_error_string(*server, MEMCACHED_HOST_LOOKUP_FAILURE, gai_strerror(errcode), strlen(gai_strerror(errcode))); + return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); } } server->address_info_next= server->address_info; diff --git a/libmemcached/error.cc b/libmemcached/error.cc index 9611ec02..aaf0015d 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -35,7 +35,7 @@ * */ -#include "libmemcached/common.h" +#include #define MAX_ERROR_LENGTH 2048 struct memcached_error_t @@ -49,31 +49,38 @@ struct memcached_error_t char message[MAX_ERROR_LENGTH]; }; -static void _set(memcached_st *memc, memcached_string_t *str, memcached_return_t &rc, int local_errno= 0) +static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t &rc, const char *at, int local_errno= 0) { - WATCHPOINT_ASSERT(memc); - if (not memc) - return; + (void)at; + if (memc.error_messages && memc.error_messages->query_id != memc.query_id) + { + memcached_error_free(&memc); + } + + // For memory allocation we use our error since it is a bit more specific + if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO) + { + local_errno= ENOMEM; + rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } - if (memc->error_messages && memc->error_messages->query_id != memc->query_id) + if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) { - memcached_error_free(memc); + local_errno= ENOMEM; } - if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE or rc == MEMCACHED_ERRNO) + if (rc == MEMCACHED_ERRNO and not local_errno) { local_errno= errno; rc= MEMCACHED_ERRNO; } - memcached_error_t *error; - error= (struct memcached_error_t *)libmemcached_malloc(memc, sizeof(struct memcached_error_t)); - - if (not error) + memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t)); + if (not error) // Bad business if this happens return; - error->root= memc; - error->query_id= memc->query_id; + error->root= &memc; + error->query_id= memc.query_id; error->rc= rc; error->local_errno= local_errno; @@ -89,47 +96,43 @@ static void _set(memcached_st *memc, memcached_string_t *str, memcached_return_t error->size= 0; } - error->next= memc->error_messages; - memc->error_messages= error; + error->next= memc.error_messages; + memc.error_messages= error; } -memcached_return_t memcached_set_error_string(memcached_st *memc, memcached_return_t rc, const char *str, size_t length) +memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, const char *str, size_t length) { - memcached_string_t tmp; - tmp.c_str= str; - tmp.size= length; - return memcached_set_error_message(memc, rc, &tmp); + memcached_string_t tmp= { str, length }; + return memcached_set_error(memc, rc, at, tmp); } -memcached_return_t memcached_set_error_string(memcached_server_st& self, memcached_return_t rc, const char *str, size_t length) +memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, const char *str, size_t length) { - memcached_string_t tmp; - tmp.c_str= str; - tmp.size= length; - return memcached_set_error_message(self, rc, &tmp); + memcached_string_t tmp= { str, length }; + return memcached_set_error(self, rc, at, tmp); } -memcached_return_t memcached_set_error_message(memcached_st *memc, memcached_return_t rc, memcached_string_t *str) +memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, memcached_string_t& str) { if (memcached_success(rc)) return MEMCACHED_SUCCESS; - _set(memc, str, rc); + _set(memc, &str, rc, at); return rc; } -memcached_return_t memcached_set_error_message(memcached_server_st& self, memcached_return_t rc, memcached_string_t *str) +memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, memcached_string_t& str) { if (memcached_success(rc)) return MEMCACHED_SUCCESS; char hostname_port_message[MAX_ERROR_LENGTH]; int size; - if (str and str->size) + if (str.size) { size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d", - memcached_string_printf(*str), + memcached_string_printf(str), self.hostname, int(self.port)); } else @@ -140,12 +143,15 @@ memcached_return_t memcached_set_error_message(memcached_server_st& self, memcac memcached_string_t error_host= { hostname_port_message, size }; - _set((memcached_st*)self.root, &error_host, rc); + if (not self.root) + return rc; + + _set(*self.root, &error_host, rc, at); return rc; } -memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc) +memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at) { if (memcached_success(rc)) return MEMCACHED_SUCCESS; @@ -155,47 +161,69 @@ memcached_return_t memcached_set_error(memcached_server_st& self, memcached_retu memcached_string_t error_host= { hostname_port, size}; - _set((memcached_st*)self.root, &error_host, rc); + if (not self.root) + return rc; + + _set(*self.root, &error_host, rc, at); return rc; } -memcached_return_t memcached_set_error(memcached_st* self, memcached_return_t rc) +memcached_return_t memcached_set_error(memcached_st& self, memcached_return_t rc, const char *at) { if (memcached_success(rc)) return MEMCACHED_SUCCESS; - _set(self, NULL, rc); + _set(self, NULL, rc, at); return rc; } -memcached_return_t memcached_set_error(memcached_st& self, memcached_return_t rc) +memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at, const char *str, size_t length) { - if (memcached_success(rc)) + memcached_string_t tmp= { str, length }; + return memcached_set_errno(self, local_errno, at, tmp); +} + +memcached_return_t memcached_set_errno(memcached_server_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_st& self, int local_errno, const char *at) +{ + if (not local_errno) return MEMCACHED_SUCCESS; - _set(&self, NULL, rc); + memcached_return_t rc= MEMCACHED_ERRNO; + _set(self, NULL, rc, at, local_errno); return rc; } -memcached_return_t memcached_set_errno(memcached_st *memc, int local_errno, memcached_string_t *str) +memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, memcached_string_t& str) { + if (not local_errno) + return MEMCACHED_SUCCESS; + memcached_return_t rc= MEMCACHED_ERRNO; - _set(memc, str, rc, local_errno); + _set(memc, &str, rc, at, local_errno); return rc; } -memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, memcached_string_t *str) +memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at, memcached_string_t& str) { + if (not local_errno) + return MEMCACHED_SUCCESS; + char hostname_port_message[MAX_ERROR_LENGTH]; int size; - if (str and str->size) + if (str.size) { size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d", - memcached_string_printf(*str), + memcached_string_printf(str), self.hostname, int(self.port)); } else @@ -207,8 +235,34 @@ memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errn memcached_string_t error_host= { hostname_port_message, size }; self.cached_errno= local_errno; // Store in the actual server + + memcached_return_t rc= MEMCACHED_ERRNO; + if (not self.root) + return rc; + + _set(*self.root, &error_host, rc, at, local_errno); + + return rc; +} + +memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at) +{ + if (not local_errno) + return MEMCACHED_SUCCESS; + + char hostname_port_message[MAX_ERROR_LENGTH]; + int size = snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d", + self.hostname, int(self.port)); + + memcached_string_t error_host= { hostname_port_message, size }; + + self.cached_errno= local_errno; // Store in the actual server + memcached_return_t rc= MEMCACHED_ERRNO; - _set((memcached_st*)self.root, &error_host, rc, local_errno); + if (not self.root) + return rc; + + _set(*self.root, &error_host, rc, at, local_errno); return rc; } diff --git a/libmemcached/error.h b/libmemcached/error.h index 1dcf9ee0..754cffe8 100644 --- a/libmemcached/error.h +++ b/libmemcached/error.h @@ -38,35 +38,6 @@ #pragma once #ifdef __cplusplus - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error(memcached_st&, memcached_return_t rc); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error(memcached_st*, memcached_return_t rc); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error(memcached_server_st&, memcached_return_t rc); - - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_errno(memcached_st *memc, int local_errno, memcached_string_t *str); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, memcached_string_t *str); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error_message(memcached_st *memc, memcached_return_t rc, memcached_string_t *str); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error_message(memcached_server_st&, memcached_return_t rc, memcached_string_t *str); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error_string(memcached_st *memc, memcached_return_t rc, const char *str, size_t length); - -LIBMEMCACHED_LOCAL - memcached_return_t memcached_set_error_string(memcached_server_st&, memcached_return_t rc, const char *str, size_t length); - extern "C" { #endif diff --git a/libmemcached/error.hpp b/libmemcached/error.hpp new file mode 100644 index 00000000..c3c83fad --- /dev/null +++ b/libmemcached/error.hpp @@ -0,0 +1,84 @@ +/* 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. + * + */ + +#pragma once + +#include + +#ifdef __cplusplus + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#define MEMCACHED_AT __FILE__ ":" TOSTRING(__LINE__) + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_st&, memcached_return_t rc, const char *at); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_server_st&, memcached_return_t rc, const char *at); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_st&, memcached_return_t rc, const char *at, const char *str, size_t length); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_server_st&, memcached_return_t rc, const char *at, const char *str, size_t length); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, memcached_string_t& str); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_error(memcached_server_st&, memcached_return_t rc, const char *at, memcached_string_t& str); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, memcached_string_t& str); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, const char *at, memcached_string_t& str); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, const char *str, size_t length); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, const char *at, const char *str, size_t length); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, const char *at); + +#endif diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index 152ff703..4aee163c 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -140,10 +140,9 @@ static memcached_return_t update_continuum(memcached_st *ptr) uint32_t live_servers= 0; struct timeval now; - if (gettimeofday(&now, NULL) != 0) + if (gettimeofday(&now, NULL)) { - memcached_set_errno(ptr, errno, NULL); - return MEMCACHED_ERRNO; + return memcached_set_errno(*ptr, errno, MEMCACHED_AT); } list= memcached_server_list(ptr); @@ -157,11 +156,15 @@ static memcached_return_t update_continuum(memcached_st *ptr) 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 || list[host_index].next_retry < ptr->ketama.next_distribution_rebuild) + { ptr->ketama.next_distribution_rebuild= list[host_index].next_retry; + } } } } @@ -173,8 +176,10 @@ static memcached_return_t update_continuum(memcached_st *ptr) uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER); - if (live_servers == 0) + if (not live_servers) + { return MEMCACHED_SUCCESS; + } if (live_servers > ptr->ketama.continuum_count) { @@ -373,7 +378,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv list[x].port, list[x].weight, list[x].type); if (not instance) { - return memcached_set_error(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE); + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } if (list[x].weight > 1) diff --git a/libmemcached/include.am b/libmemcached/include.am index a761ce08..5c0e6638 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -15,6 +15,7 @@ noinst_HEADERS+= \ libmemcached/byteorder.h \ libmemcached/common.h \ libmemcached/do.h \ + libmemcached/error.hpp \ libmemcached/initialize_query.h \ libmemcached/internal.h \ libmemcached/io.h \ @@ -78,6 +79,7 @@ nobase_include_HEADERS+= \ # These symbols will not be exposed in the shipped .so noinst_LTLIBRARIES+= libmemcached/libmemcachedinternal.la libmemcached_libmemcachedinternal_la_SOURCES= \ + libmemcached/array.c \ libmemcached/error.cc \ libmemcached/string.cc libmemcached_libmemcachedinternal_la_CFLAGS= \ diff --git a/libmemcached/initialize_query.cc b/libmemcached/initialize_query.cc index 09402ac3..00e8e642 100644 --- a/libmemcached/initialize_query.cc +++ b/libmemcached/initialize_query.cc @@ -50,7 +50,7 @@ memcached_return_t initialize_query(memcached_st *self) if (memcached_server_count(self) == 0) { - return memcached_set_error(self, MEMCACHED_NO_SERVERS); + return memcached_set_error(*self, MEMCACHED_NO_SERVERS, MEMCACHED_AT); } return MEMCACHED_SUCCESS; diff --git a/libmemcached/io.cc b/libmemcached/io.cc index 74aa4a23..51032f00 100644 --- a/libmemcached/io.cc +++ b/libmemcached/io.cc @@ -296,7 +296,7 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, { memcached_quit_server(ptr, true); *nread= -1; - return rc; + return memcached_set_error(*ptr, rc, MEMCACHED_AT); } } } @@ -314,7 +314,7 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, WATCHPOINT_STRING("We had a zero length recv()"); memcached_quit_server(ptr, true); *nread= -1; - return MEMCACHED_UNKNOWN_READ_FAILURE; + return memcached_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } } @@ -524,13 +524,14 @@ memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st return NULL; } - int err= poll(fds, host_index, memc->poll_timeout); - switch (err) { + switch (poll(fds, host_index, memc->poll_timeout)) + { case -1: - memcached_set_errno(memc, get_socket_errno(), NULL); + memcached_set_errno(*memc, get_socket_errno(), MEMCACHED_AT); /* FALLTHROUGH */ case 0: break; + default: for (size_t x= 0; x < host_index; ++x) { diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index a1551cd2..2bba44a1 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -226,12 +226,6 @@ memcached_st *memcached_create(memcached_st *ptr) memcached_st *memcached(const char *string, size_t length) { - if ((not length and string) or (length and not string)) - { - errno= EINVAL; - return NULL; - } - memcached_st *self= memcached_create(NULL); if (not self) { @@ -242,23 +236,20 @@ memcached_st *memcached(const char *string, size_t length) if (not length) return self; - memcached_return_t rc; - rc= memcached_parse_configuration(self, string, length); + memcached_return_t rc= memcached_parse_configuration(self, string, length); - if (rc == MEMCACHED_SUCCESS && memcached_parse_filename(self)) + if (memcached_success(rc) and memcached_parse_filename(self)) { rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self)); } - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { memcached_free(self); errno= EINVAL; return NULL; } - errno= 0; - return self; } @@ -285,7 +276,7 @@ memcached_return_t memcached_reset(memcached_st *ptr) void memcached_servers_reset(memcached_st *ptr) { - if (! ptr) + if (not ptr) return; memcached_server_list_free(memcached_server_list(ptr)); @@ -302,7 +293,7 @@ void memcached_servers_reset(memcached_st *ptr) void memcached_reset_last_disconnected_server(memcached_st *ptr) { - if (! ptr) + if (not ptr) return; if (ptr->last_disconnected_server) @@ -314,7 +305,7 @@ void memcached_reset_last_disconnected_server(memcached_st *ptr) void memcached_free(memcached_st *ptr) { - if (! ptr) + if (not ptr) return; _free(ptr, true); @@ -388,6 +379,7 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key); + new_clone->configure.filename= memcached_array_clone(new_clone, source->prefix_key); #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT if (source->sasl.callbacks) diff --git a/libmemcached/options.cc b/libmemcached/options.cc index cc7fe3f1..d8b44ff7 100644 --- a/libmemcached/options.cc +++ b/libmemcached/options.cc @@ -36,11 +36,13 @@ */ #include +#include #include const char *memcached_parse_filename(memcached_st *memc) { + assert(memc); return memcached_array_string(memc->configure.filename); } @@ -49,22 +51,15 @@ 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_string_t *filename) +static memcached_return_t _parse_file_options(memcached_st *self, memcached_array_st *real_name) { - memcached_array_st *real_name= memcached_strcpy(self, filename->c_str, filename->size); - - if (not real_name) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - 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, &error_message); - memcached_array_free(real_name); + memcached_return_t rc= memcached_set_errno(*self, errno, MEMCACHED_AT, error_message); return rc; } - memcached_array_free(real_name); char buffer[BUFSIZ]; memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS; @@ -75,8 +70,7 @@ static memcached_return_t _parse_file_options(memcached_st *self, memcached_stri if (length == 1 and buffer[0] == '\n') continue; - rc= memcached_parse_configuration(self, buffer, length); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc= memcached_parse_configuration(self, buffer, length))) break; } fclose(fp); @@ -91,21 +85,24 @@ memcached_return_t libmemcached_check_configuration(const char *option_string, s if (error_buffer_size) error_buffer[0]= 0; - if (! (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 (rc != MEMCACHED_SUCCESS && error_buffer && error_buffer_size) + if (memcached_failed(rc) && error_buffer && error_buffer_size) { strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size); } - if (rc== MEMCACHED_SUCCESS && memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE)) + bool has_filename= memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE); + if (memcached_success(rc) and has_filename) { + assert(memcached_parse_filename(memc_ptr)); + assert(memcached_parse_filename_length(memc_ptr)); memcached_string_t filename= memcached_array_to_string(memc_ptr->configure.filename); - rc= _parse_file_options(memc_ptr, &filename); + rc= _parse_file_options(memc_ptr, memc_ptr->configure.filename); - if (rc != MEMCACHED_SUCCESS && error_buffer && error_buffer_size) + if (memcached_failed(rc) and error_buffer && error_buffer_size) { strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size); } @@ -121,7 +118,7 @@ memcached_return_t memcached_parse_configuration(memcached_st *self, char const WATCHPOINT_ASSERT(self); if (not self) { - return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS); + return MEMCACHED_INVALID_ARGUMENTS; } memcached_return_t rc; @@ -134,21 +131,53 @@ memcached_return_t memcached_parse_configuration(memcached_st *self, char const void memcached_set_configuration_file(memcached_st *self, const char *filename, size_t filename_length) { + assert(filename); + assert(filename_length); memcached_array_free(self->configure.filename); self->configure.filename= memcached_strcpy(self, filename, filename_length); } -memcached_return_t memcached_parse_configure_file(memcached_st *self, const char *filename, size_t filename_length) +memcached_return_t memcached_parse_configure_file(memcached_st *self, const char *filename, size_t length) { + WATCHPOINT_ASSERT(self); if (not self) - return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS); + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + WATCHPOINT_ASSERT(self); + if (not filename) + { + return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); + } + + WATCHPOINT_ASSERT(self); + if (not length) + { + return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); + } - if (! filename || filename_length == 0) - return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS); - - memcached_string_t tmp; - tmp.c_str= filename; - tmp.size= filename_length; + memcached_array_st *tmp_array= memcached_strcpy(self, filename, length); + + 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) +{ + WATCHPOINT_ASSERT(self); + if (not self) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + WATCHPOINT_ASSERT(memcached_array_size(filename)); + if (not memcached_array_size(filename)) + { + return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT); + } - return _parse_file_options(self, &tmp); + return _parse_file_options(self, filename); } diff --git a/libmemcached/options.h b/libmemcached/options.h index 2785c122..893da2ac 100644 --- a/libmemcached/options.h +++ b/libmemcached/options.h @@ -50,9 +50,6 @@ LIBMEMCACHED_LOCAL LIBMEMCACHED_LOCAL memcached_return_t memcached_parse_configuration(memcached_st *ptr, const char *option_string, size_t length); -LIBMEMCACHED_LOCAL - memcached_return_t memcached_parse_configure_file(memcached_st *ptr, const char *filename, size_t filename_length); - LIBMEMCACHED_LOCAL const char *memcached_parse_filename(memcached_st *memc); @@ -62,3 +59,13 @@ LIBMEMCACHED_LOCAL #ifdef __cplusplus } #endif + +#ifdef __cplusplus + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_parse_configure_file(memcached_st *self, const char *filename, size_t length); + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_parse_configure_file(memcached_st *self, memcached_array_st *filename); + +#endif diff --git a/libmemcached/options/context.h b/libmemcached/options/context.h index 7660d2d2..2597b196 100644 --- a/libmemcached/options/context.h +++ b/libmemcached/options/context.h @@ -122,7 +122,7 @@ public: } memcached_string_append(error_string, memcached_literal_param(")")); - memcached_set_error_string(memc, rc, memcached_string_value(error_string), memcached_string_length(error_string)); + memcached_set_error(*memc, rc, MEMCACHED_AT, memcached_string_value(error_string), memcached_string_length(error_string)); memcached_string_free(error_string); } diff --git a/libmemcached/options/parser.cc b/libmemcached/options/parser.cc index c0c5cc25..fc08ed81 100644 --- a/libmemcached/options/parser.cc +++ b/libmemcached/options/parser.cc @@ -2066,7 +2066,7 @@ yyreduce: #line 438 "libmemcached/options/parser.yy" { (yyval.string).c_str= (yyvsp[(1) - (1)].string).c_str +1; // +1 to move use passed the initial quote - (yyval.string).length= (yyvsp[(1) - (1)].string).length -1; // -1 removes the end quote + (yyval.string).length= (yyvsp[(1) - (1)].string).length -2; // -2 removes the begin and end quote ;} break; diff --git a/libmemcached/options/parser.yy b/libmemcached/options/parser.yy index 7a8283ec..bd19d183 100644 --- a/libmemcached/options/parser.yy +++ b/libmemcached/options/parser.yy @@ -437,7 +437,7 @@ string: | QUOTED_STRING { $$.c_str= $1.c_str +1; // +1 to move use passed the initial quote - $$.length= $1.length -1; // -1 removes the end quote + $$.length= $1.length -2; // -2 removes the begin and end quote } ; diff --git a/libmemcached/prefix_key.cc b/libmemcached/prefix_key.cc index 6bfc8374..8a24190e 100644 --- a/libmemcached/prefix_key.cc +++ b/libmemcached/prefix_key.cc @@ -39,19 +39,21 @@ memcached_return_t memcached_set_prefix_key(memcached_st *self, const char *key, size_t key_length) { - if (key && key_length) + WATCHPOINT_ASSERT(self); + + if (key and key_length) { if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) - return memcached_set_error(self, MEMCACHED_BAD_KEY_PROVIDED); + return memcached_set_error(*self, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT); if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)) - return memcached_set_error(self, MEMCACHED_KEY_TOO_BIG); + return memcached_set_error(*self, MEMCACHED_KEY_TOO_BIG, MEMCACHED_AT); memcached_array_free(self->prefix_key); self->prefix_key= memcached_strcpy(self, key, key_length); - if (! self->prefix_key) - return memcached_set_error(self, MEMCACHED_MEMORY_ALLOCATION_FAILURE); + if (not self->prefix_key) + return memcached_set_error(*self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } else { diff --git a/libmemcached/response.cc b/libmemcached/response.cc index 3c996f94..6ee62af0 100644 --- a/libmemcached/response.cc +++ b/libmemcached/response.cc @@ -111,18 +111,15 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st char *buffer, memcached_result_st *result) { - memcached_return_t rc= MEMCACHED_SUCCESS; char *string_ptr; char *end_ptr; char *next_ptr; size_t value_length; size_t to_read; - char *value_ptr; ssize_t read_length= 0; - memcached_return_t rrc; if (ptr->root->flags.use_udp) - return memcached_set_error((memcached_st *)ptr->root, MEMCACHED_NOT_SUPPORTED); + return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); WATCHPOINT_ASSERT(ptr->root); end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; @@ -197,25 +194,26 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st goto read_error; /* We add two bytes so that we can walk the \r\n */ - rc= memcached_string_check(&result->value, value_length+2); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(memcached_string_check(&result->value, value_length +2))) { value_length= 0; - return memcached_set_error((memcached_st *)ptr->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE); + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - 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. - - We are null terminating through, which will most likely make - some people lazy about using the return length. - */ - to_read= (value_length) + 2; - rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length); - if (rrc != MEMCACHED_SUCCESS) - return rrc; + { + 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. + + We are null terminating through, which will most likely make + some people lazy about using the return length. + */ + to_read= (value_length) + 2; + memcached_return_t rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length); + if (memcached_failed(rrc)) + return rrc; + } if (read_length != (ssize_t)(value_length + 2)) { @@ -552,7 +550,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan if ((rc= memcached_safe_read(ptr, hole, nr)) != MEMCACHED_SUCCESS) { WATCHPOINT_ERROR(rc); - return memcached_set_error((memcached_st *)ptr->root, MEMCACHED_UNKNOWN_READ_FAILURE); + return memcached_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT); } bodylen-= (uint32_t) nr; } diff --git a/libmemcached/result.cc b/libmemcached/result.cc index 1d0b763d..f9d55a94 100644 --- a/libmemcached/result.cc +++ b/libmemcached/result.cc @@ -80,7 +80,7 @@ memcached_result_st *memcached_result_create(const memcached_st *memc, _result_init(ptr, (memcached_st *)memc); WATCHPOINT_SET(ptr->value.options.is_initialized= false); - memcached_string_create(memc, &ptr->value, 0); + memcached_string_create((memcached_st*)memc, &ptr->value, 0); WATCHPOINT_ASSERT_INITIALIZED(&ptr->value); WATCHPOINT_ASSERT(ptr->value.string == NULL); @@ -98,7 +98,7 @@ void memcached_result_reset(memcached_result_st *ptr) void memcached_result_free(memcached_result_st *ptr) { - if (ptr == NULL) + if (not ptr) return; memcached_string_free(&ptr->value); @@ -118,14 +118,12 @@ memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, size_t length) { - memcached_return_t rc= memcached_string_append(&ptr->value, value, length); - - if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) + if (memcached_failed(memcached_string_append(&ptr->value, value, length))) { - memcached_set_errno(ptr->root, errno, NULL); + return memcached_set_errno(*ptr->root, errno, MEMCACHED_AT); } - return rc; + return MEMCACHED_SUCCESS; } const char *memcached_result_key_value(const memcached_result_st *self) diff --git a/libmemcached/return.h b/libmemcached/return.h index 3b7875c5..634651c8 100644 --- a/libmemcached/return.h +++ b/libmemcached/return.h @@ -91,6 +91,6 @@ typedef enum memcached_return_t memcached_return_t; #endif -#define memcached_success(X) (X) == MEMCACHED_SUCCESS ? true : false -#define memcached_failed(A) (A) != MEMCACHED_SUCCESS ? true : false +#define memcached_success(X) ((X) == MEMCACHED_SUCCESS) +#define memcached_failed(A) ((A) != MEMCACHED_SUCCESS) diff --git a/libmemcached/server.cc b/libmemcached/server.cc index 30cc611f..cb0f0c2f 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -41,7 +41,7 @@ #include #include -static inline void _server_init(memcached_server_st *self, const memcached_st *root, +static inline void _server_init(memcached_server_st *self, memcached_st *root, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type) { @@ -92,11 +92,11 @@ static inline void _server_init(memcached_server_st *self, const memcached_st *r static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc) { - if (self == NULL) + if (not self) { self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st)); - if (! self) + if (not self) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ self->options.is_allocated= true; @@ -118,10 +118,10 @@ memcached_server_st *memcached_server_create_with(const memcached_st *memc, { self= _server_create(self, memc); - if (self == NULL) + if (not self) return NULL; - _server_init(self, memc, hostname, port, weight, type); + _server_init(self, const_cast(memc), hostname, port, weight, type); if (type == MEMCACHED_CONNECTION_UDP) @@ -299,7 +299,7 @@ void memcached_server_list_free(memcached_server_list_st self) } } - const memcached_st *root= self->root; + memcached_st *root= self->root; if (root) { libmemcached_free(root, self); diff --git a/libmemcached/server.h b/libmemcached/server.h index 06142b96..d4005f89 100644 --- a/libmemcached/server.h +++ b/libmemcached/server.h @@ -69,7 +69,7 @@ struct memcached_server_st { struct addrinfo *address_info; struct addrinfo *address_info_next; time_t next_retry; - const memcached_st *root; + memcached_st *root; uint64_t limit_maxbytes; char read_buffer[MEMCACHED_MAX_BUFFER]; char write_buffer[MEMCACHED_MAX_BUFFER]; diff --git a/libmemcached/string.cc b/libmemcached/string.cc index 80d8dbef..9e029627 100644 --- a/libmemcached/string.cc +++ b/libmemcached/string.cc @@ -54,13 +54,13 @@ inline static memcached_return_t _string_check(memcached_string_st *string, size new_size= sizeof(char) * (size_t)((adjust * MEMCACHED_BLOCK_SIZE) + string->current_size); /* Test for overflow */ if (new_size < need) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); new_value= static_cast(libmemcached_realloc(string->root, string->string, new_size)); - if (new_value == NULL) + if (not new_value) { - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } string->string= new_value; @@ -78,10 +78,8 @@ static inline void _init_string(memcached_string_st *self) self->end= self->string= NULL; } -memcached_string_st *memcached_string_create(const memcached_st *memc, memcached_string_st *self, size_t initial_size) +memcached_string_st *memcached_string_create(memcached_st *memc, memcached_string_st *self, size_t initial_size) { - memcached_return_t rc; - WATCHPOINT_ASSERT(memc); /* Saving malloc calls :) */ @@ -102,17 +100,12 @@ memcached_string_st *memcached_string_create(const memcached_st *memc, memcached self->options.is_allocated= true; } - self->root= const_cast(memc); + self->root= memc; _init_string(self); - rc= _string_check(self, initial_size); - if (memcached_failed(rc)) + if (memcached_failed(_string_check(self, initial_size))) { - if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) - { - memcached_set_errno(self->root, errno, NULL); - } libmemcached_free(memc, self); return NULL; @@ -128,13 +121,9 @@ memcached_string_st *memcached_string_create(const memcached_st *memc, memcached memcached_return_t memcached_string_append_character(memcached_string_st *string, char character) { - memcached_return_t rc; - - rc= _string_check(string, 1); - - if (memcached_failed(rc)) + if (memcached_failed(_string_check(string, 1))) { - return rc; + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } *string->end= character; @@ -146,13 +135,9 @@ memcached_return_t memcached_string_append_character(memcached_string_st *string memcached_return_t memcached_string_append(memcached_string_st *string, const char *value, size_t length) { - memcached_return_t rc; - - rc= _string_check(string, length); - - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(_string_check(string, length))) { - return rc; + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } WATCHPOINT_ASSERT(length <= string->current_size); @@ -167,14 +152,12 @@ memcached_return_t memcached_string_append(memcached_string_st *string, char *memcached_string_c_copy(memcached_string_st *string) { - char *c_ptr; - - if (memcached_string_length(string) == 0) + if (not memcached_string_length(string)) return NULL; - 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 (not c_ptr) return NULL; memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string)); @@ -192,7 +175,7 @@ memcached_return_t memcached_string_reset(memcached_string_st *string) void memcached_string_free(memcached_string_st *ptr) { - if (ptr == NULL) + if (not ptr) return; if (ptr->string) diff --git a/libmemcached/string.h b/libmemcached/string.h index cfc98c74..a0b66ed3 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -67,7 +67,7 @@ extern "C" { #endif LIBMEMCACHED_LOCAL -memcached_string_st *memcached_string_create(const memcached_st *ptr, +memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size); LIBMEMCACHED_LOCAL diff --git a/libtest/test.h b/libtest/test.h index 604e661c..0493f310 100644 --- a/libtest/test.h +++ b/libtest/test.h @@ -228,6 +228,17 @@ do \ } \ } while (0) +#define test_compare_got(A,B,C) \ +do \ +{ \ + if ((A) != (B)) \ + { \ + fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \ + create_core(); \ + return TEST_FAILURE; \ + } \ +} while (0) + #define test_false(A) \ do \ { \ diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 3bc38fcd..10f3291a 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -208,11 +208,15 @@ static test_return_t server_sort2_test(memcached_st *ptr) return TEST_SUCCESS; } -static test_return_t memcached_server_remove_test(memcached_st *ptr) +static test_return_t memcached_server_remove_test(memcached_st*) { const char *server_string= "--server=localhost:4444 --server=localhost:4445 --server=localhost:4446 --server=localhost:4447 --server=localhost --server=memcache1.memcache.bk.sapo.pt:11211 --server=memcache1.memcache.bk.sapo.pt:11212 --server=memcache1.memcache.bk.sapo.pt:11213 --server=memcache1.memcache.bk.sapo.pt:11214 --server=memcache2.memcache.bk.sapo.pt:11211 --server=memcache2.memcache.bk.sapo.pt:11212 --server=memcache2.memcache.bk.sapo.pt:11213 --server=memcache2.memcache.bk.sapo.pt:11214"; - (void)ptr; + char buffer[BUFSIZ]; + memcached_return_t rc; + test_compare_got(MEMCACHED_SUCCESS, + rc= libmemcached_check_configuration(server_string, strlen(server_string), buffer, sizeof(buffer)), + memcached_strerror(NULL, rc)); memcached_st *memc= memcached(server_string, strlen(server_string)); test_true(memc); @@ -225,13 +229,12 @@ static test_return_t memcached_server_remove_test(memcached_st *ptr) return TEST_SUCCESS; } -static memcached_return_t server_display_unsort_function(const memcached_st *ptr, +static memcached_return_t server_display_unsort_function(const memcached_st*, const memcached_server_st *server, void *context) { /* Do Nothing */ uint32_t x= *((uint32_t *)(context)); - (void)ptr; if (! (test_ports[x] == server->port)) { diff --git a/tests/parser.cc b/tests/parser.cc index 1c2fc504..68a65a40 100644 --- a/tests/parser.cc +++ b/tests/parser.cc @@ -43,7 +43,8 @@ #include #define BUILDING_LIBMEMCACHED -#include +// !NEVER use common.h, always use memcached.h in your own apps +#include #include "tests/parser.h" #include "tests/print.h" @@ -332,7 +333,8 @@ test_return_t memcached_create_with_options_with_filename(memcached_st*) memcached_st *memc_ptr; memc_ptr= memcached(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\"")); - test_true_got(memc_ptr, memcached_last_error_message(memc_ptr)); + test_true_got(memc_ptr, "memcached() failed"); + test_strcmp(SUPPORT_EXAMPLE_CNF, memcached_array_string(memc_ptr->configure.filename)); memcached_free(memc_ptr); return TEST_SUCCESS; @@ -347,13 +349,13 @@ test_return_t libmemcached_check_configuration_with_filename_test(memcached_st*) char buffer[BUFSIZ]; rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer)); - test_true_got(rc == MEMCACHED_SUCCESS, buffer); + test_true_got(rc == MEMCACHED_SUCCESS, (rc == MEMCACHED_ERRNO) ? strerror(errno) : memcached_strerror(NULL, rc)); rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer)); - test_false_with(rc == MEMCACHED_SUCCESS, buffer); + test_false_with(rc == MEMCACHED_SUCCESS, memcached_strerror(NULL, rc)); rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer)); - test_true_got(rc == MEMCACHED_ERRNO, buffer); + test_true_got(rc == MEMCACHED_ERRNO, memcached_strerror(NULL, rc)); return TEST_SUCCESS; } -- 2.30.2