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(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= memc;
+
+ _init_string(self);
- rc= _string_check(string, initial_size);
+ rc= _string_check(self, initial_size);
if (rc != MEMCACHED_SUCCESS)
{
- memc->call_free(memc, string);
+ if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
+ {
+ ((memcached_st *)memc)->cached_errno= errno;
+ }
+ 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)
+ char character)
{
memcached_return_t rc;
rc= _string_check(string, 1);
if (rc != MEMCACHED_SUCCESS)
+ {
return rc;
+ }
*string->end= character;
string->end++;
}
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);
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;
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));
}
}
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;
+}