+/* LibMemcached
+ * Copyright (C) 2006-2009 Brian Aker
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license. See
+ * the COPYING file in the parent directory for full text.
+ *
+ */
+
#include "common.h"
-memcached_return memcached_string_check(memcached_string_st *string, size_t need)
+inline static memcached_return_t _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;
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;
return MEMCACHED_SUCCESS;
}
-memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size)
+memcached_string_st *memcached_string_create(memcached_st *memc, memcached_string_st *string, size_t initial_size)
{
- memcached_return rc;
+ memcached_return_t rc;
/* Saving malloc calls :) */
if (string)
+ {
+ WATCHPOINT_ASSERT(memc->options.is_safe && string->options.is_initialized == false);
+
memset(string, 0, sizeof(memcached_string_st));
+ }
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= memc->call_calloc(memc, 1, sizeof(memcached_string_st));
if (string == NULL)
+ {
return NULL;
- memset(string, 0, sizeof(memcached_string_st));
- string->is_allocated= true;
+ }
+
+ string->options.is_allocated= true;
}
string->block_size= MEMCACHED_BLOCK_SIZE;
- string->root= ptr;
+ string->root= memc;
- rc= memcached_string_check(string, initial_size);
+ rc= _string_check(string, initial_size);
if (rc != MEMCACHED_SUCCESS)
{
- if (ptr->call_free)
- ptr->call_free(ptr, string);
- else
- free(string);
-
+ memc->call_free(memc, string);
return NULL;
}
+ string->options.is_initialized= true;
+
WATCHPOINT_ASSERT(string->string == string->end);
return string;
}
-memcached_return memcached_string_append_character(memcached_string_st *string,
+memcached_return_t memcached_string_append_character(memcached_string_st *string,
char character)
{
- memcached_return rc;
+ memcached_return_t rc;
- rc= memcached_string_check(string, 1);
+ rc= _string_check(string, 1);
if (rc != MEMCACHED_SUCCESS)
return rc;
return MEMCACHED_SUCCESS;
}
-memcached_return memcached_string_append(memcached_string_st *string,
- char *value, size_t length)
+memcached_return_t memcached_string_append(memcached_string_st *string,
+ const char *value, size_t length)
{
- memcached_return rc;
+ memcached_return_t rc;
- rc= memcached_string_check(string, length);
+ rc= _string_check(string, length);
if (rc != MEMCACHED_SUCCESS)
return rc;
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;
return c_ptr;
}
-memcached_return memcached_string_reset(memcached_string_st *string)
+memcached_return_t memcached_string_reset(memcached_string_st *string)
{
string->end= string->string;
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)
+ if (memcached_is_allocated(ptr))
{
- if (ptr->root->call_free)
- ptr->root->call_free(ptr->root, ptr);
- else
- free(ptr);
+ ptr->root->call_free(ptr->root, ptr);
}
else
+ {
+ ptr->options.is_initialized= false;
memset(ptr, 0, sizeof(memcached_string_st));
+ }
}
+
+memcached_return_t memcached_string_check(memcached_string_st *string, size_t need)
+{
+ return _string_check(string, need);
+}
+