Fix for Linux system for async protocol.
[awesomized/libmemcached] / lib / memcached.c
index 157dc89f24580af2707d9edbf8ffda26d7d98d84..a5be8e255fab93eba37af6c3de2a7b71930b27f1 100644 (file)
@@ -5,6 +5,7 @@
 
 memcached_st *memcached_create(memcached_st *ptr)
 {
+  memcached_string_st *string_ptr;
   if (!ptr)
   {
     ptr= (memcached_st *)malloc(sizeof(memcached_st));
@@ -19,25 +20,32 @@ memcached_st *memcached_create(memcached_st *ptr)
   {
     memset(ptr, 0, sizeof(memcached_st));
   }
+  string_ptr= memcached_string_create(ptr, &ptr->result_buffer, 0);
+  WATCHPOINT_ASSERT(string_ptr);
+  ptr->poll_timeout= -1;
+  ptr->distribution= MEMCACHED_DISTRIBUTION_MODULO;
 
   return ptr;
 }
 
 void memcached_free(memcached_st *ptr)
 {
-  if (ptr->hosts)
-  {
-    memcached_quit(ptr);
-    memcached_server_list_free(ptr->hosts);
-    ptr->hosts= NULL;
-  }
+  /* If we have anything open, lets close it now */
+  memcached_quit(ptr);
+  memcached_server_list_free(ptr->hosts);
+  memcached_string_free(&ptr->result_buffer);
 
   if (ptr->is_allocated == MEMCACHED_ALLOCATED)
     free(ptr);
   else
-    memset(ptr, 0, sizeof(memcached_st));
+    ptr->is_allocated= MEMCACHED_USED;
 }
 
+/*
+  clone is the destination, while ptr is the structure to clone.
+  If ptr is NULL the call is the same as if a memcached_create() was
+  called.
+*/
 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
 {
   memcached_return rc;
@@ -45,11 +53,20 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
 
   if (ptr == NULL)
     return memcached_create(clone);
+
+  if (ptr->is_allocated == MEMCACHED_USED)
+  {
+    WATCHPOINT_ASSERT(0);
+    return NULL;
+  }
   
   new_clone= memcached_create(clone);
+  
+  if (new_clone == NULL)
+    return NULL;
 
-
-  rc= memcached_server_push(new_clone, ptr->hosts);
+  if (ptr->hosts)
+    rc= memcached_server_push(new_clone, ptr->hosts);
 
   if (rc != MEMCACHED_SUCCESS)
   {
@@ -63,6 +80,8 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
   new_clone->number_of_hosts= ptr->number_of_hosts;
   new_clone->send_size= ptr->send_size;
   new_clone->recv_size= ptr->recv_size;
+  new_clone->poll_timeout= ptr->poll_timeout;
+  new_clone->distribution= ptr->distribution;
 
   return new_clone;
 }