static int compare_servers(const void *p1, const void *p2)
{
- int return_value;
memcached_server_instance_st a= (memcached_server_instance_st)p1;
memcached_server_instance_st b= (memcached_server_instance_st)p2;
- return_value= strcmp(a->hostname, b->hostname);
+ int return_value= strcmp(a->hostname, b->hostname);
if (return_value == 0)
{
{
memcached_server_write_instance_st instance;
- qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers);
+ qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(memcached_instance_st), compare_servers);
instance= memcached_server_instance_fetch(ptr, 0);
instance->number_of_hosts= memcached_server_count(ptr);
}
static memcached_return_t update_continuum(memcached_st *ptr)
{
uint32_t continuum_index= 0;
- memcached_server_st *list;
uint32_t pointer_counter= 0;
uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
uint32_t pointer_per_hash= 1;
return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
}
- list= memcached_server_list(ptr);
+ memcached_instance_st *list= memcached_instance_list(ptr);
/* count live servers (those without a retry delay set) */
bool is_auto_ejecting= _is_auto_eject_host(ptr);
{
memcached_continuum_item_st *new_ptr;
- new_ptr= static_cast<memcached_continuum_item_st*>(libmemcached_realloc(ptr, ptr->ketama.continuum,
- sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
+ new_ptr= libmemcached_xrealloc(ptr, ptr->ketama.continuum, (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server, memcached_continuum_item_st);
if (new_ptr == 0)
{
if (is_ketama_weighted)
{
float pct= (float)list[host_index].weight / (float)total_weight;
- pointer_per_server= (uint32_t) ((floorf((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
+ pointer_per_server= (uint32_t) ((::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
pointer_per_hash= 4;
if (DEBUG)
{
pointer_index < pointer_per_server / pointer_per_hash;
pointer_index++)
{
- char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
+ char sort_host[1 +MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 + MEMCACHED_NI_MAXSERV ]= "";
int sort_host_length;
// Spymemcached ketema key format is: hostname/ip:port-index
// If hostname is not available then: /ip:port-index
- sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
+ sort_host_length= snprintf(sort_host, sizeof(sort_host),
"/%s:%u-%u",
list[host_index].hostname,
(uint32_t)list[host_index].port,
pointer_index);
- if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
+ if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
- memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+ memcached_literal_param("snprintf(sizeof(sort_host))"));
}
if (DEBUG)
pointer_index <= pointer_per_server / pointer_per_hash;
pointer_index++)
{
- char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
+ char sort_host[MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 +MEMCACHED_NI_MAXSERV]= "";
int sort_host_length;
if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
{
- sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
+ sort_host_length= snprintf(sort_host, sizeof(sort_host),
"%s-%u",
list[host_index].hostname,
pointer_index - 1);
}
else
{
- sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
+ sort_host_length= snprintf(sort_host, sizeof(sort_host),
"%s:%u-%u",
list[host_index].hostname,
(uint32_t)list[host_index].port,
pointer_index - 1);
}
- if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
+ if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
- memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+ memcached_literal_param("snprintf(sizeof(sort_host)))"));
}
if (is_ketama_weighted)
memcached_connection_t type)
{
assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
- if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
- or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
- {
- return memcached_set_error(*ptr, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT);
- }
- memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
+ memcached_instance_st *new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (ptr->number_of_hosts + 1), memcached_instance_st);
- if (not new_host_list)
+ if (new_host_list == NULL)
{
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
- memcached_server_list_set(ptr, new_host_list);
+ memcached_instance_set(ptr, new_host_list);
/* TODO: Check return type */
memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
- if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+ if (__instance_create_with(ptr, instance, hostname, port, weight, type) == NULL)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
// @note we place the count in the bottom of the server list
instance= memcached_server_instance_fetch(ptr, 0);
- memcached_servers_set_count(instance, memcached_server_count(ptr));
+ memcached_instance_set_count(instance, memcached_server_count(ptr));
return run_distribution(ptr);
}
memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
{
- if (not list)
+ if (list == NULL)
{
return MEMCACHED_SUCCESS;
}
uint32_t count= memcached_server_list_count(list);
- memcached_server_st *new_host_list;
- new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (count + memcached_server_count(ptr))));
+ memcached_instance_st *new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (count + memcached_server_count(ptr)), memcached_instance_st);
- if (not new_host_list)
+ if (new_host_list == NULL)
+ {
return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
- memcached_server_list_set(ptr, new_host_list);
+ memcached_instance_set(ptr, new_host_list);
for (uint32_t x= 0; x < count; x++)
{
memcached_server_write_instance_st instance;
- if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
- or ((list[x].type == MEMCACHED_CONNECTION_UDP) and not (ptr->flags.use_udp)) )
+ WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
+
+ // We have extended the array, and now we will find it, and use it.
+ instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
+ WATCHPOINT_ASSERT(instance);
+
+ memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+ if (__instance_create_with(ptr, instance,
+ hostname,
+ list[x].port, list[x].weight, list[x].type) == NULL)
{
- return MEMCACHED_INVALID_HOST_PROTOCOL;
+ return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
+ if (list[x].weight > 1)
+ {
+ ptr->ketama.weighted= true;
+ }
+
+ ptr->number_of_hosts++;
+ }
+
+ // Provides backwards compatibility with server list.
+ {
+ memcached_server_write_instance_st instance;
+ instance= memcached_server_instance_fetch(ptr, 0);
+ instance->number_of_hosts= memcached_server_count(ptr);
+ }
+
+ return run_distribution(ptr);
+}
+
+memcached_return_t memcached_instance_push(memcached_st *ptr, const struct memcached_instance_st* list, uint32_t number_of_hosts)
+{
+ if (list == NULL)
+ {
+ return MEMCACHED_SUCCESS;
+ }
+
+ memcached_instance_st* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (number_of_hosts +memcached_server_count(ptr)), memcached_instance_st);
+
+ if (new_host_list == NULL)
+ {
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
+
+ memcached_instance_set(ptr, new_host_list);
+
+ for (uint32_t x= 0; x < number_of_hosts; x++)
+ {
+ memcached_server_write_instance_st instance;
+
WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
// We have extended the array, and now we will find it, and use it.
WATCHPOINT_ASSERT(instance);
memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
- if (__server_create_with(ptr, instance,
- hostname,
- list[x].port, list[x].weight, list[x].type) == NULL)
+ if (__instance_create_with(ptr, instance,
+ hostname,
+ list[x].port, list[x].weight, list[x].type) == NULL)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
}
memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
- const char *hostname,
- in_port_t port,
- uint32_t weight)
+ const char *,
+ in_port_t,
+ uint32_t)
{
if (ptr == NULL)
{
return MEMCACHED_INVALID_ARGUMENTS;
}
- if (not port)
- {
- port= MEMCACHED_DEFAULT_PORT;
- }
-
- if (not hostname)
- {
- hostname= "localhost";
- }
-
- memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) };
- if (memcached_is_valid_servername(_hostname) == false)
- {
- memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
- }
-
- return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_UDP);
+ return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT);
}
memcached_return_t memcached_server_add(memcached_st *ptr,
if (hostname_length == 0)
{
hostname= "localhost";
- hostname_length= sizeof("localhost") -1;
+ hostname_length= memcached_literal_param_size("localhost");
}
memcached_string_t _hostname= { hostname, hostname_length };