Merge from trunk.
[m6w6/libmemcached] / libmemcached / memcached_string.c
index 1595ec6108b9f00b69ea3293de0cabeeb847e32f..614343c8c087d7703d6273b2e774fbc29b8c0eaa 100644 (file)
@@ -4,7 +4,7 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need
 {
   if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
   {
-    size_t current_offset= string->end - string->string;
+    size_t current_offset= (size_t) (string->end - string->string);
     char *new_value;
     size_t adjust;
     size_t new_size;
@@ -18,10 +18,7 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need
     if (new_size < need)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
 
-    if (string->root->call_realloc)
-      new_value= (char *)string->root->call_realloc(string->root, string->string, new_size);
-    else
-      new_value= (char *)realloc(string->string, new_size);
+    new_value= string->root->call_realloc(string->root, string->string, new_size);
 
     if (new_value == NULL)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
@@ -41,21 +38,14 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string
 
   /* Saving malloc calls :) */
   if (string)
-  {
     memset(string, 0, sizeof(memcached_string_st));
-    string->is_allocated= MEMCACHED_NOT_ALLOCATED;
-  }
   else
   {
-    if (ptr->call_malloc)
-      string= (memcached_string_st *)ptr->call_malloc(ptr, sizeof(memcached_string_st));
-    else
-      string= (memcached_string_st *)malloc(sizeof(memcached_string_st));
+    string= ptr->call_calloc(ptr, 1, sizeof(memcached_string_st));
 
     if (string == NULL)
       return NULL;
-    memset(string, 0, sizeof(memcached_string_st));
-    string->is_allocated= MEMCACHED_ALLOCATED;
+    string->is_allocated= true;
   }
   string->block_size= MEMCACHED_BLOCK_SIZE;
   string->root= ptr;
@@ -63,11 +53,7 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string
   rc=  memcached_string_check(string, initial_size);
   if (rc != MEMCACHED_SUCCESS)
   {
-    if (ptr->call_free)
-      ptr->call_free(ptr, string);
-    else
-      free(string);
-
+    ptr->call_free(ptr, string);
     return NULL;
   }
 
@@ -81,26 +67,22 @@ memcached_return memcached_string_append_character(memcached_string_st *string,
 {
   memcached_return rc;
 
-  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
-
   rc=  memcached_string_check(string, 1);
 
   if (rc != MEMCACHED_SUCCESS)
     return rc;
 
-  *string->end= ' ';
+  *string->end= character;
   string->end++;
 
   return MEMCACHED_SUCCESS;
 }
 
 memcached_return memcached_string_append(memcached_string_st *string,
-                                         char *value, size_t length)
+                                         const char *value, size_t length)
 {
   memcached_return rc;
 
-  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
-
   rc= memcached_string_check(string, length);
 
   if (rc != MEMCACHED_SUCCESS)
@@ -116,37 +98,14 @@ memcached_return memcached_string_append(memcached_string_st *string,
   return MEMCACHED_SUCCESS;
 }
 
-size_t memcached_string_backspace(memcached_string_st *string, size_t remove)
-{
-  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
-
-  if (string->end - string->string  > remove)
-  {
-    size_t difference;
-
-    difference= string->end - string->string;
-    string->end= string->string;
-
-    return difference;
-  }
-  string->end-= remove;
-
-  return remove;
-}
-
 char *memcached_string_c_copy(memcached_string_st *string)
 {
   char *c_ptr;
 
-  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
-
   if (memcached_string_length(string) == 0)
     return NULL;
 
-  if (string->root->call_malloc)
-    c_ptr= (char *)string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
-  else
-    c_ptr= (char *)malloc((memcached_string_length(string)+1) * sizeof(char));
+  c_ptr= string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
 
   if (c_ptr == NULL)
     return NULL;
@@ -159,7 +118,6 @@ char *memcached_string_c_copy(memcached_string_st *string)
 
 memcached_return memcached_string_reset(memcached_string_st *string)
 {
-  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
   string->end= string->string;
   
   return MEMCACHED_SUCCESS;
@@ -171,20 +129,10 @@ void memcached_string_free(memcached_string_st *ptr)
     return;
 
   if (ptr->string)
-  {
-    if (ptr->root->call_free)
-      ptr->root->call_free(ptr->root, ptr->string);
-    else
-      free(ptr->string);
-  }
+    ptr->root->call_free(ptr->root, ptr->string);
 
-  if (ptr->is_allocated == MEMCACHED_ALLOCATED)
-  {
-    if (ptr->root->call_free)
-      ptr->root->call_free(ptr->root, ptr);
-    else
-      free(ptr);
-  }
+  if (ptr->is_allocated)
+    ptr->root->call_free(ptr->root, ptr);
   else
-    ptr->is_allocated= MEMCACHED_USED;
+    memset(ptr, 0, sizeof(memcached_string_st));
 }