Speed up host lookup for cases where we don't need to do it, or where it ends up...
[awesomized/libmemcached] / libmemcached / hosts.cc
index 11b316308b6e6f4b8abb72ce6a47bddf3c19c1d0..61c1b05974383c67064e8ba96028c94cacbae5f2 100644 (file)
 #include <sys/time.h>
 
 /* Protoypes (static) */
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
-                                     in_port_t port,
-                                     uint32_t weight,
-                                     memcached_connection_t type);
-
 static memcached_return_t update_continuum(memcached_st *ptr);
 
 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)
   {
-    return_value= (int) (a->port - b->port);
+    return_value= int(a->port() - b->port());
   }
 
   return return_value;
@@ -68,11 +62,7 @@ static void sort_hosts(memcached_st *ptr)
 {
   if (memcached_server_count(ptr))
   {
-    memcached_server_write_instance_st instance;
-
-    qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers);
-    instance= memcached_server_instance_fetch(ptr, 0);
-    instance->number_of_hosts= memcached_server_count(ptr);
+    qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(org::libmemcached::Instance), compare_servers);
   }
 }
 
@@ -80,7 +70,9 @@ static void sort_hosts(memcached_st *ptr)
 memcached_return_t run_distribution(memcached_st *ptr)
 {
   if (ptr->flags.use_sort_hosts)
+  {
     sort_hosts(ptr);
+  }
 
   switch (ptr->distribution)
   {
@@ -89,15 +81,18 @@ memcached_return_t run_distribution(memcached_st *ptr)
   case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
   case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
     return update_continuum(ptr);
+
   case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
   case MEMCACHED_DISTRIBUTION_MODULA:
     break;
+
   case MEMCACHED_DISTRIBUTION_RANDOM:
     srandom((uint32_t) time(NULL));
     break;
+
   case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
   default:
-    WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
+    assert_msg(0, "Invalid distribution type passed to run_distribution()");
   }
 
   return MEMCACHED_SUCCESS;
@@ -123,17 +118,22 @@ static int continuum_item_cmp(const void *t1, const void *t2)
   /* Why 153? Hmmm... */
   WATCHPOINT_ASSERT(ct1->value != 153);
   if (ct1->value == ct2->value)
+  {
     return 0;
+  }
   else if (ct1->value > ct2->value)
+  {
     return 1;
+  }
   else
+  {
     return -1;
+  }
 }
 
 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;
@@ -145,7 +145,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
     return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
   }
 
-  list= memcached_server_list(ptr);
+  org::libmemcached::Instance* list= memcached_instance_list(ptr);
 
   /* count live servers (those without a retry delay set) */
   bool is_auto_ejecting= _is_auto_eject_host(ptr);
@@ -161,7 +161,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
       }
       else
       {
-        if (ptr->ketama.next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
+        if (ptr->ketama.next_distribution_rebuild == 0 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
         {
           ptr->ketama.next_distribution_rebuild= list[host_index].next_retry;
         }
@@ -173,10 +173,9 @@ static memcached_return_t update_continuum(memcached_st *ptr)
     live_servers= memcached_server_count(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);
+  uint32_t points_per_server= (uint32_t) (memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
 
-  if (not live_servers)
+  if (live_servers == 0)
   {
     return MEMCACHED_SUCCESS;
   }
@@ -185,22 +184,23 @@ static memcached_return_t update_continuum(memcached_st *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)
+    {
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    }
 
     ptr->ketama.continuum= new_ptr;
     ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
   }
 
   uint64_t total_weight= 0;
-  if (is_ketama_weighted)
+  if (memcached_is_weighted_ketama(ptr))
   {
     for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
     {
-      if (! is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
+      if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec)
       {
         total_weight += list[host_index].weight;
       }
@@ -209,21 +209,24 @@ static memcached_return_t update_continuum(memcached_st *ptr)
 
   for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
   {
-    if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
+    if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec)
+    {
       continue;
+    }
 
-    if (is_ketama_weighted)
+    if (memcached_is_weighted_ketama(ptr))
     {
-        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);
+        float pct= (float)list[host_index].weight / (float)total_weight;
+        pointer_per_server= (uint32_t) ((::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
         pointer_per_hash= 4;
-#ifdef DEBUG
-        printf("ketama_weighted:%s|%d|%llu|%u\n",
-               list[host_index].hostname,
-               list[host_index].port,
-               (unsigned long long)list[host_index].weight,
-               pointer_per_server);
-#endif
+        if (DEBUG)
+        {
+          printf("ketama_weighted:%s|%d|%llu|%u\n",
+                 list[host_index].hostname,
+                 list[host_index].port(),
+                 (unsigned long long)list[host_index].weight,
+                 pointer_per_server);
+        }
     }
 
 
@@ -233,28 +236,29 @@ static memcached_return_t update_continuum(memcached_st *ptr)
            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,
+                                   (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_FAILURE;
+          return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                                     memcached_literal_param("snprintf(sizeof(sort_host))"));
         }
-#ifdef DEBUG
-        printf("update_continuum: key is %s\n", sort_host);
-#endif
 
-        WATCHPOINT_ASSERT(sort_host_length);
+        if (DEBUG)
+        {
+          fprintf(stdout, "update_continuum: key is %s\n", sort_host);
+        }
 
-        if (is_ketama_weighted)
+        if (memcached_is_weighted_ketama(ptr))
         {
           for (uint32_t x= 0; x < pointer_per_hash; x++)
           {
@@ -277,33 +281,32 @@ static memcached_return_t update_continuum(memcached_st *ptr)
            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)
+        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,
+                                     (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_FAILURE;
+          return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                                     memcached_literal_param("snprintf(sizeof(sort_host)))"));
         }
 
-        WATCHPOINT_ASSERT(sort_host_length);
-
-        if (is_ketama_weighted)
+        if (memcached_is_weighted_ketama(ptr))
         {
           for (uint32_t x = 0; x < pointer_per_hash; x++)
           {
@@ -330,72 +333,161 @@ static memcached_return_t update_continuum(memcached_st *ptr)
   ptr->ketama.continuum_points_counter= pointer_counter;
   qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
 
-#ifdef DEBUG
-  for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
+  if (DEBUG)
   {
-    WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value);
+    for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
+    {
+      WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value);
+    }
   }
-#endif
 
   return MEMCACHED_SUCCESS;
 }
 
+static memcached_return_t server_add(memcached_st *ptr, 
+                                     const memcached_string_t& hostname,
+                                     in_port_t port,
+                                     uint32_t weight,
+                                     memcached_connection_t type)
+{
+  assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
+
+  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, 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) -1);
+
+  if (__instance_create_with(ptr, instance, hostname, port, weight, type) == NULL)
+  {
+    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+  }
+
+  if (weight > 1)
+  {
+    if (memcached_is_consistent_distribution(ptr))
+    {
+      memcached_set_weighted_ketama(ptr, true);
+    }
+  }
+
+  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 original_host_size= memcached_server_count(ptr);
   uint32_t count= memcached_server_list_count(list);
+  uint32_t host_list_size= count +original_host_size;
 
-  memcached_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))));
+  org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
 
-  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, 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)
   {
-    memcached_server_write_instance_st instance;
-
-    if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
-            || ((list[x].type == MEMCACHED_CONNECTION_UDP)
-            && ! (ptr->flags.use_udp)) )
-    {
-      return MEMCACHED_INVALID_HOST_PROTOCOL;
-    }
-
     WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
 
-    instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
+    // We have extended the array, and now we will find it, and use it.
+    org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size);
     WATCHPOINT_ASSERT(instance);
 
-    /* TODO check return type */
-    instance= memcached_server_create_with(ptr, instance, list[x].hostname,
-                                           list[x].port, list[x].weight, list[x].type);
-    if (not 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)
     {
+      ptr->state.is_parsing= false;
       return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
     }
 
     if (list[x].weight > 1)
     {
-      ptr->ketama.weighted= true;
+      memcached_set_weighted_ketama(ptr, true);
     }
+  }
+  ptr->state.is_parsing= false;
+
+  return run_distribution(ptr);
+}
 
-    ptr->number_of_hosts++;
+memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org::libmemcached::Instance* list, uint32_t number_of_hosts)
+{
+  if (list == NULL)
+  {
+    return MEMCACHED_SUCCESS;
   }
 
-  // Provides backwards compatibility with server list.
+  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)
   {
-    memcached_server_write_instance_st instance;
-    instance= memcached_server_instance_fetch(ptr, 0);
-    instance->number_of_hosts= memcached_server_count(ptr);
+    return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
   }
 
+  memcached_instance_set(ptr, new_host_list, host_list_size);
+
+  // We don't bother with lookups for this operation
+  ptr->state.is_parsing= true;
+
+  // 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, original_host_size);
+    WATCHPOINT_ASSERT(instance);
+
+    memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+    if (__instance_create_with(ptr, instance, 
+                               hostname,
+                               list[x].port(), list[x].weight, list[x].type) == NULL)
+    {
+      ptr->state.is_parsing= false;
+      return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+    }
+
+    if (list[x].weight > 1)
+    {
+      memcached_set_weighted_ketama(ptr, true);
+    }
+  }
+  ptr->state.is_parsing= false;
+
   return run_distribution(ptr);
 }
 
@@ -409,10 +501,18 @@ memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *pt
                                                                 const char *filename,
                                                                 uint32_t weight)
 {
-  if (! filename)
+  if (ptr == NULL)
+  {
     return MEMCACHED_FAILURE;
+  }
+
+  memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
+  if (memcached_is_valid_servername(_filename) == false)
+  {
+    memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
+  }
 
-  return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
+  return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
 }
 
 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
@@ -423,17 +523,16 @@ memcached_return_t memcached_server_add_udp(memcached_st *ptr,
 }
 
 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 (not port)
-    port= MEMCACHED_DEFAULT_PORT;
-
-  if (not hostname)
-    hostname= "localhost";
+  if (ptr == NULL)
+  {
+    return MEMCACHED_INVALID_ARGUMENTS;
+  }
 
-  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,
@@ -448,51 +547,31 @@ memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
                                                     in_port_t port,
                                                     uint32_t weight)
 {
-  if (not port)
-    port= MEMCACHED_DEFAULT_PORT;
-
-  if (not hostname)
-    hostname= "localhost";
-
-  return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
-}
-
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
-                                     in_port_t port,
-                                     uint32_t weight,
-                                     memcached_connection_t type)
-{
-
-  if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
-      or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
+  if (ptr == NULL)
   {
-    return MEMCACHED_INVALID_HOST_PROTOCOL;
+    return MEMCACHED_INVALID_ARGUMENTS;
   }
 
-  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)));
-
-  if (not new_host_list)
-    return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-
-  memcached_server_list_set(ptr, new_host_list);
-
-  /* TODO: Check return type */
-  memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
-
-  (void)memcached_server_create_with(ptr, instance, hostname, port, weight, type);
+  if (port == 0)
+  {
+    port= MEMCACHED_DEFAULT_PORT;
+  }
 
-  if (weight > 1)
+  size_t hostname_length= hostname ? strlen(hostname) : 0;
+  if (hostname_length == 0)
   {
-    ptr->ketama.weighted= true;
+    hostname= "localhost";
+    hostname_length= memcached_literal_param_size("localhost");
   }
 
-  ptr->number_of_hosts++;
+  memcached_string_t _hostname= { hostname, hostname_length };
 
-  instance= memcached_server_instance_fetch(ptr, 0);
-  memcached_servers_set_count(instance, memcached_server_count(ptr));
+  if (memcached_is_valid_servername(_hostname) == false)
+  {
+    return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+  }
 
-  return run_distribution(ptr);
+  return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET  : MEMCACHED_CONNECTION_TCP);
 }
 
 memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
@@ -506,7 +585,9 @@ memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
   memcpy(buffer, hostname, hostname_length);
   buffer[hostname_length]= 0;
 
-  return server_add(ptr, buffer,
+  memcached_string_t _hostname= { buffer, hostname_length };
+
+  return server_add(ptr, _hostname,
                     port,
                     weight,
                     MEMCACHED_CONNECTION_TCP);