X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fstring.c;h=464d55c0c9878bd2fea2eb5252a55f00ae2e9530;hb=a4456cc10079f2e6f648befc91657f2723c825e5;hp=615d54f73da5cc8b5934de51704b0839ee588e34;hpb=98c99981e0ebe7f716bea2e4d95434d159103fcd;p=awesomized%2Flibmemcached diff --git a/libmemcached/string.c b/libmemcached/string.c index 615d54f7..464d55c0 100644 --- a/libmemcached/string.c +++ b/libmemcached/string.c @@ -21,15 +21,15 @@ 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; @@ -37,49 +37,58 @@ inline static memcached_return_t _string_check(memcached_string_st *string, size 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); + 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, @@ -125,7 +134,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; @@ -150,17 +159,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)); } }