#include <libmemcached/byteorder.h>
#include <clients/utilities.h>
+#include <vector>
+
#ifdef linux
/* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
* optimize the conversion functions, but the prototypes generate warnings
"test_ascii_mget4 test_ascii_mget5 "
"test_ascii_mget6\r\n"));
- char *returned[nkeys];
+ std::vector<char *> returned;
+ returned.resize(nkeys);
for (uint32_t x= 0; x < nkeys; ++x)
{
hints.ai_protocol= IPPROTO_TCP;
}
- server->address_info= NULL;
+ assert(server->address_info == NULL);
+ assert(server->address_info_next == NULL);
int errcode;
switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info))
{
break;
case EAI_AGAIN:
+ if (server->address_info)
+ {
+ freeaddrinfo(server->address_info);
+ server->address_info= NULL;
+ server->address_info_next= NULL;
+ }
return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode)));
case EAI_SYSTEM:
+ if (server->address_info)
+ {
+ freeaddrinfo(server->address_info);
+ server->address_info= NULL;
+ server->address_info_next= NULL;
+ }
return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_SYSTEM)"));
case EAI_BADFLAGS:
+ if (server->address_info)
+ {
+ freeaddrinfo(server->address_info);
+ server->address_info= NULL;
+ server->address_info_next= NULL;
+ }
return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_BADFLAGS)"));
case EAI_MEMORY:
+ if (server->address_info)
+ {
+ freeaddrinfo(server->address_info);
+ server->address_info= NULL;
+ server->address_info_next= NULL;
+ }
return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_MEMORY)"));
default:
{
+ if (server->address_info)
+ {
+ freeaddrinfo(server->address_info);
+ server->address_info= NULL;
+ server->address_info_next= NULL;
+ }
return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode)));
}
}
{
WATCHPOINT_ASSERT(server->state == MEMCACHED_SERVER_STATE_NEW);
server->address_info_next= NULL;
- memcached_return_t rc;
- uint32_t counter= 5;
- while (--counter)
- {
- if ((rc= set_hostinfo(server)) != MEMCACHED_TIMEOUT)
- {
- break;
- }
-
-#ifndef WIN32
- struct timespec dream, rem;
-
- dream.tv_nsec= 1000;
- dream.tv_sec= 0;
-
- nanosleep(&dream, &rem);
-#endif
- }
+ memcached_return_t rc= set_hostinfo(server);
if (memcached_failed(rc))
{
{
assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
- org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (ptr->number_of_hosts + 1), org::libmemcached::Instance);
+ if (ptr->number_of_hosts)
+ {
+ assert(memcached_instance_list(ptr));
+ }
+
+ if (memcached_instance_list(ptr))
+ {
+ assert(ptr->number_of_hosts);
+ }
+
+ uint32_t host_list_size= ptr->number_of_hosts +1;
+ org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
if (new_host_list == NULL)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
- memcached_instance_set(ptr, new_host_list);
+ memcached_instance_set(ptr, new_host_list, host_list_size);
+ assert(ptr->number_of_hosts == host_list_size);
/* TODO: Check return type */
- org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
+ org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr) -1);
if (__instance_create_with(ptr, instance, hostname, port, weight, type) == NULL)
{
}
}
- ptr->number_of_hosts++;
-
return run_distribution(ptr);
}
return MEMCACHED_SUCCESS;
}
+ uint32_t original_host_size= memcached_server_count(ptr);
uint32_t count= memcached_server_list_count(list);
+ uint32_t host_list_size= count +original_host_size;
- org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (count + memcached_server_count(ptr)), org::libmemcached::Instance);
+ org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
if (new_host_list == NULL)
{
return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
}
- memcached_instance_set(ptr, new_host_list);
+ memcached_instance_set(ptr, new_host_list, host_list_size);
- for (uint32_t x= 0; x < count; x++)
+ ptr->state.is_parsing= true;
+ for (uint32_t x= 0; x < count; ++x, ++original_host_size)
{
WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
// We have extended the array, and now we will find it, and use it.
- org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
+ org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size);
WATCHPOINT_ASSERT(instance);
memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
hostname,
list[x].port, list[x].weight, list[x].type) == NULL)
{
+ ptr->state.is_parsing= false;
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
{
memcached_set_weighted_ketama(ptr, true);
}
-
- ptr->number_of_hosts++;
}
+ ptr->state.is_parsing= false;
return run_distribution(ptr);
}
return MEMCACHED_SUCCESS;
}
- org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (number_of_hosts +memcached_server_count(ptr)), org::libmemcached::Instance);
+ uint32_t original_host_size= memcached_server_count(ptr);
+ uint32_t host_list_size= number_of_hosts +original_host_size;
+ org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
if (new_host_list == NULL)
{
return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
}
- memcached_instance_set(ptr, new_host_list);
+ memcached_instance_set(ptr, new_host_list, host_list_size);
- for (uint32_t x= 0; x < number_of_hosts; x++)
- {
+ // We don't bother with lookups for this operation
+ ptr->state.is_parsing= true;
+ // We use original_host_size since size will now point to the first new
+ // instance allocated.
+ for (uint32_t x= 0; x < number_of_hosts; ++x, ++original_host_size)
+ {
WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
// We have extended the array, and now we will find it, and use it.
- org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
+ org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size);
WATCHPOINT_ASSERT(instance);
memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
hostname,
list[x].port(), list[x].weight, list[x].type) == NULL)
{
+ ptr->state.is_parsing= false;
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
{
memcached_set_weighted_ketama(ptr, true);
}
-
- ptr->number_of_hosts++;
}
+ ptr->state.is_parsing= false;
return run_distribution(ptr);
}
self->address_info= NULL;
self->address_info_next= NULL;
}
+ assert(self->address_info_next == NULL);
memcached_error_free(*self);
void set_last_disconnected_host(org::libmemcached::Instance* self)
{
assert(self->root);
- if (self->root == NULL)
+ if (self->root)
{
- return;
- }
+ if (memcached_server_get_last_disconnect(self->root) and
+ memcached_server_get_last_disconnect(self->root)->version == self->version)
+ {
+ return;
+ }
- if (memcached_server_get_last_disconnect(self->root) and
- memcached_server_get_last_disconnect(self->root)->version == self->version)
- {
- return;
- }
+ // const_cast
+ memcached_st *root= (memcached_st *)self->root;
- // const_cast
- memcached_st *root= (memcached_st *)self->root;
+ memcached_instance_free((org::libmemcached::Instance*)(root->last_disconnected_server));
- memcached_instance_free((org::libmemcached::Instance*)(root->last_disconnected_server));
- root->last_disconnected_server= memcached_instance_clone(self);
- ((org::libmemcached::Instance*)memcached_server_get_last_disconnect(root))->version= self->version;
+ // We set is_parsing so that no lookup happens
+ root->state.is_parsing= true;
+ root->last_disconnected_server= memcached_instance_clone(self);
+ root->state.is_parsing= false;
+
+ ((org::libmemcached::Instance*)memcached_server_get_last_disconnect(root))->version= self->version;
+ }
}
memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *self)
{
memcached_instance_list_free(memcached_instance_list(self), self->number_of_hosts);
- memcached_instance_set(self, NULL);
+ memcached_instance_set(self, NULL, 0);
self->number_of_hosts= 0;
memcached_instance_free((org::libmemcached::Instance*)self->last_disconnected_server);
self->last_disconnected_server= NULL;
: self->number_of_hosts;
}
-void memcached_instance_set(memcached_st* memc, org::libmemcached::Instance* list)
+void memcached_instance_set(memcached_st* memc, org::libmemcached::Instance* list, const uint32_t host_list_size)
{
memc->servers= list;
+ memc->number_of_hosts= host_list_size;
}
void memcached_server_list_free(memcached_server_list_st self)
{
- if (self == NULL)
+ if (self)
{
- return;
- }
+ for (uint32_t x= 0; x < memcached_server_list_count(self); x++)
+ {
+ assert_msg(not memcached_is_allocated(&self[x]), "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st");
+ __server_free(&self[x]);
+ }
- for (uint32_t x= 0; x < memcached_server_list_count(self); x++)
- {
- assert_msg(not memcached_is_allocated(&self[x]), "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st");
- __server_free(&self[x]);
+ libmemcached_free(self->root, self);
}
-
- libmemcached_free(self->root, self);
}
void memcached_instance_list_free(org::libmemcached::Instance* self, uint32_t instance_count)
{
- if (self == NULL)
+ if (self)
{
- return;
- }
+ for (uint32_t x= 0; x < instance_count; x++)
+ {
+ assert_msg(memcached_is_allocated(&self[x]) == false, "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st");
+ __instance_free(&self[x]);
+ }
- for (uint32_t x= 0; x < instance_count; x++)
- {
- assert_msg(not memcached_is_allocated(&self[x]), "You have called memcached_server_list_free(), but you did not pass it a valid memcached_server_list_st");
- __instance_free(&self[x]);
+ libmemcached_free(self->root, self);
}
-
- libmemcached_free(self->root, self);
}
void memcached_instance_list_free(org::libmemcached::Instance* self, uint32_t count);
-void memcached_instance_set(memcached_st*, org::libmemcached::Instance*);
+void memcached_instance_set(memcached_st*, org::libmemcached::Instance*, const uint32_t host_list_size);
return false;
}
- (void)memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 400000);
-
- memcached_return_t rc= memcached_server_add(memc_ptr, hostname, port);
- if (memcached_success(rc))
- {
- rc= memcached_version(memc_ptr);
- }
-
- if (memcached_failed(rc) and rc == MEMCACHED_SOME_ERRORS)
+ if (memcached_success((*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 400000))))
{
- memcached_server_instance_st instance=
- memcached_server_instance_by_position(memc_ptr, 0);
+ memcached_return_t rc= memcached_server_add(memc_ptr, hostname, port);
+ if (memcached_success(rc))
+ {
+ rc= memcached_version(memc_ptr);
+ }
- assert_msg(instance and memcached_server_error(instance), " ");
- if (instance and memcached_server_error(instance))
+ if (memcached_failed(rc) and rc == MEMCACHED_SOME_ERRORS)
{
- rc= memcached_server_error_return(instance);
+ memcached_server_instance_st instance=
+ memcached_server_instance_by_position(memc_ptr, 0);
+
+ assert_msg(instance and memcached_server_error(instance), " ");
+ if (instance and memcached_server_error(instance))
+ {
+ rc= memcached_server_error_return(instance);
+ }
}
+
+ *ret= rc;
}
memcached_free(memc_ptr);
- *ret= rc;
-
- return memcached_success(rc);
+ return memcached_success(*ret);
}
bool libmemcached_util_ping2(const char *hostname, in_port_t port, const char *username, const char *password, memcached_return_t *ret)
#
LIBTOOL_COMMAND= ${abs_top_builddir}/libtool --mode=execute
-VALGRIND_EXEC_COMMAND= $(LIBTOOL_COMMAND) valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE --xml-file=./tmp_chroot/var/tmp/yatl-\%p.xml --xml=yes
+VALGRIND_EXEC_COMMAND= $(LIBTOOL_COMMAND) valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
VALGRIND_COMMAND= TESTS_ENVIRONMENT="valgrind" $(VALGRIND_EXEC_COMMAND)
HELGRIND_COMMAND= $(LIBTOOL_COMMAND) valgrind --tool=helgrind --read-var-info=yes --error-exitcode=1 --read-var-info=yes
DRD_COMMAND= $(LIBTOOL_COMMAND) valgrind --tool=drd
test-memcapable: tests/memcapable
@tests/memcapable
+valgrind-memcapable: tests/memcapable
+ $(VALGRIND_COMMAND) tests/memcapable
+
pahole-mem: tests/testapp
@$(PAHOLE_COMMAND) tests/testapp