#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
*/
-#ifndef HASHKIT_TYPES_H
-#define HASHKIT_TYPES_H
-
-#ifdef __cplusplus
-
-extern "C" {
-#endif
+#pragma once
typedef enum {
HASHKIT_SUCCESS,
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,
HASHKIT_DISTRIBUTION_MAX /* Always add new values before this. */
} hashkit_distribution_t;
+#ifdef __cplusplus
+extern "C" {
+#endif
typedef struct hashkit_st hashkit_st;
#ifdef __cplusplus
}
#endif
-
-#endif /* HASHKIT_TYPES_H */
-#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 <libmemcached/common.h>
+
+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;
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,
/* 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)
{
}
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;
}
*/
#include <libmemcached/common.h>
+#include <assert.h>
+#include <iso646.h>
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);
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;
void memcached_array_free(memcached_array_st *array)
{
- if (! array)
+ if (not array)
return;
WATCHPOINT_ASSERT(array->root);
size_t memcached_array_size(memcached_array_st *array)
{
- if (! array)
+ if (not array)
return 0;
return array->size;
const char *memcached_array_string(memcached_array_st *array)
{
- if (! array)
+ if (not array)
return NULL;
return array->c_str;
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
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);
(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))
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,
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);
{
if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
+
if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
}
#pragma once
+// No assumptions of NULL should be made
+
struct memcached_string_t {
const char *c_str;
size_t size;
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;
}
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))
{
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
return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA);
}
+
case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
{
(void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5);
*/
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);
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;
{
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;
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
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 */
}
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 */
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"));
}
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)
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)
{
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);
}
/* These are private not to be installed headers */
+#include <libmemcached/error.hpp>
#include <libmemcached/memory.h>
#include <libmemcached/io.h>
#include <libmemcached/do.h>
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;
*
*/
-#include "libmemcached/common.h"
+#include <libmemcached/common.h>
#define MAX_ERROR_LENGTH 2048
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;
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
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;
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
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;
}
#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
--- /dev/null
+/* 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 <libmemcached/error.h>
+
+#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
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);
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;
+ }
}
}
}
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)
{
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)
libmemcached/byteorder.h \
libmemcached/common.h \
libmemcached/do.h \
+ libmemcached/error.hpp \
libmemcached/initialize_query.h \
libmemcached/internal.h \
libmemcached/io.h \
# 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= \
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;
{
memcached_quit_server(ptr, true);
*nread= -1;
- return rc;
+ return memcached_set_error(*ptr, rc, MEMCACHED_AT);
}
}
}
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);
}
}
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)
{
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)
{
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;
}
void memcached_servers_reset(memcached_st *ptr)
{
- if (! ptr)
+ if (not ptr)
return;
memcached_server_list_free(memcached_server_list(ptr));
void memcached_reset_last_disconnected_server(memcached_st *ptr)
{
- if (! ptr)
+ if (not ptr)
return;
if (ptr->last_disconnected_server)
void memcached_free(memcached_st *ptr)
{
- if (! ptr)
+ if (not ptr)
return;
_free(ptr, true);
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)
*/
#include <libmemcached/common.h>
+#include <assert.h>
#include <libmemcached/options/context.h>
const char *memcached_parse_filename(memcached_st *memc)
{
+ assert(memc);
return memcached_array_string(memc->configure.filename);
}
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;
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);
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);
}
WATCHPOINT_ASSERT(self);
if (not self)
{
- return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS);
+ return MEMCACHED_INVALID_ARGUMENTS;
}
memcached_return_t rc;
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);
}
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);
#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
}
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);
}
#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;
| 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
}
;
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
{
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;
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))
{
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;
}
_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);
void memcached_result_free(memcached_result_st *ptr)
{
- if (ptr == NULL)
+ if (not ptr)
return;
memcached_string_free(&ptr->value);
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)
#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)
#include <libmemcached/common.h>
#include <cassert>
-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)
{
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;
{
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<memcached_st *>(memc), hostname, port, weight, type);
if (type == MEMCACHED_CONNECTION_UDP)
}
}
- const memcached_st *root= self->root;
+ memcached_st *root= self->root;
if (root)
{
libmemcached_free(root, self);
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];
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<char *>(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;
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 :) */
self->options.is_allocated= true;
}
- self->root= const_cast<memcached_st *>(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;
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;
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);
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<char *>(libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)));
+ char *c_ptr= static_cast<char *>(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));
void memcached_string_free(memcached_string_st *ptr)
{
- if (ptr == NULL)
+ if (not ptr)
return;
if (ptr->string)
#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
} \
} 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 \
{ \
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);
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))
{
#include <errno.h>
#define BUILDING_LIBMEMCACHED
-#include <libmemcached/memcached.h>
+// !NEVER use common.h, always use memcached.h in your own apps
+#include <libmemcached/common.h>
#include "tests/parser.h"
#include "tests/print.h"
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;
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;
}