Merge in more updates for docs (we are just going to check them all in so that users...
[awesomized/libmemcached] / libmemcached / string.c
index 838ca416681ffbff66b2658fb5abe3be03672174..b5badc5eff8bb23f970fdd9d5f262cd9ecb60324 100644 (file)
@@ -21,76 +21,94 @@ inline static memcached_return_t _string_check(memcached_string_st *string, size
     size_t new_size;
 
     /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
-    adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / string->block_size;
+    adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / MEMCACHED_BLOCK_SIZE;
     adjust++;
 
-    new_size= sizeof(char) * (size_t)((adjust * string->block_size) + string->current_size);
+    new_size= sizeof(char) * (size_t)((adjust * MEMCACHED_BLOCK_SIZE) + string->current_size);
     /* Test for overflow */
     if (new_size < need)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
 
-    new_value= string->root->call_realloc(string->root, string->string, new_size);
+    new_value= libmemcached_realloc(string->root, string->string, new_size);
 
     if (new_value == NULL)
+    {
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    }
 
     string->string= new_value;
     string->end= string->string + current_offset;
 
-    string->current_size+= (string->block_size * adjust);
+    string->current_size+= (MEMCACHED_BLOCK_SIZE * adjust);
   }
 
   return MEMCACHED_SUCCESS;
 }
 
-memcached_string_st *memcached_string_create(memcached_st *memc, memcached_string_st *string, size_t initial_size)
+static inline void _init_string(memcached_string_st *self)
+{
+  self->current_size= 0;
+  self->end= self->string= NULL;
+}
+
+memcached_string_st *memcached_string_create(const memcached_st *memc, memcached_string_st *self, size_t initial_size)
 {
   memcached_return_t rc;
 
+  WATCHPOINT_ASSERT(memc);
+
   /* Saving malloc calls :) */
-  if (string)
+  if (self)
   {
-    WATCHPOINT_ASSERT(memc->options.is_safe && string->options.is_initialized == false);
+    WATCHPOINT_ASSERT(self->options.is_initialized == false);
 
-    memset(string, 0, sizeof(memcached_string_st));
+    self->options.is_allocated= false;
   }
   else
   {
-    string= memc->call_calloc(memc, 1, sizeof(memcached_string_st));
+    self= libmemcached_malloc(memc, sizeof(memcached_string_st));
 
-    if (string == NULL)
+    if (self == NULL)
     {
       return NULL;
     }
 
-    string->options.is_allocated= true;
+    self->options.is_allocated= true;
   }
-  string->block_size= MEMCACHED_BLOCK_SIZE;
-  string->root= memc;
+  self->root= (memcached_st *)memc;
 
-  rc=  _string_check(string, initial_size);
+  _init_string(self);
+
+  rc=  _string_check(self, initial_size);
   if (rc != MEMCACHED_SUCCESS)
   {
-    memc->call_free(memc, string);
+    if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
+    {
+      memcached_set_errno(self->root, errno, NULL);
+    }
+    libmemcached_free(memc, self);
+
     return NULL;
   }
 
-  string->options.is_initialized= true;
+  self->options.is_initialized= true;
 
-  WATCHPOINT_ASSERT(string->string == string->end);
+  WATCHPOINT_ASSERT(self->string == self->end);
 
-  return string;
+  return self;
 }
 
-memcached_return_t memcached_string_append_character(memcached_string_st *string, 
-                                                   char character)
+memcached_return_t memcached_string_append_character(memcached_string_st *string,
+                                                     char character)
 {
   memcached_return_t rc;
 
   rc=  _string_check(string, 1);
 
   if (rc != MEMCACHED_SUCCESS)
+  {
     return rc;
+  }
 
   *string->end= character;
   string->end++;
@@ -99,14 +117,16 @@ memcached_return_t memcached_string_append_character(memcached_string_st *string
 }
 
 memcached_return_t memcached_string_append(memcached_string_st *string,
-                                         const char *value, size_t length)
+                                           const char *value, size_t length)
 {
   memcached_return_t rc;
 
   rc= _string_check(string, length);
 
   if (rc != MEMCACHED_SUCCESS)
+  {
     return rc;
+  }
 
   WATCHPOINT_ASSERT(length <= string->current_size);
   WATCHPOINT_ASSERT(string->string);
@@ -125,7 +145,7 @@ char *memcached_string_c_copy(memcached_string_st *string)
   if (memcached_string_length(string) == 0)
     return NULL;
 
-  c_ptr= string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
+  c_ptr= libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
 
   if (c_ptr == NULL)
     return NULL;
@@ -139,7 +159,7 @@ char *memcached_string_c_copy(memcached_string_st *string)
 memcached_return_t memcached_string_reset(memcached_string_st *string)
 {
   string->end= string->string;
-  
+
   return MEMCACHED_SUCCESS;
 }
 
@@ -150,17 +170,16 @@ void memcached_string_free(memcached_string_st *ptr)
 
   if (ptr->string)
   {
-    ptr->root->call_free(ptr->root, ptr->string);
+    libmemcached_free(ptr->root, ptr->string);
   }
 
   if (memcached_is_allocated(ptr))
   {
-    ptr->root->call_free(ptr->root, ptr);
+    libmemcached_free(ptr->root, ptr);
   }
   else
   {
     ptr->options.is_initialized= false;
-    memset(ptr, 0, sizeof(memcached_string_st));
   }
 }
 
@@ -169,3 +188,27 @@ memcached_return_t memcached_string_check(memcached_string_st *string, size_t ne
   return _string_check(string, need);
 }
 
+size_t memcached_string_length(const memcached_string_st *self)
+{
+  return (size_t)(self->end - self->string);
+}
+
+size_t memcached_string_size(const memcached_string_st *self)
+{
+  return self->current_size;
+}
+
+const char *memcached_string_value(const memcached_string_st *self)
+{
+  return self->string;
+}
+
+char *memcached_string_value_mutable(const memcached_string_st *self)
+{
+  return self->string;
+}
+
+void memcached_string_set_length(memcached_string_st *self, size_t length)
+{
+  self->end= self->string + length;
+}