From: Brian Aker Date: Sat, 16 Jan 2010 00:00:43 +0000 (-0800) Subject: Small cleanup in string.h and converted result_st macro to inline. X-Git-Tag: 0.40~96 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=f1563c45b37bba7d51228af44dc184a9d0308e81;p=awesomized%2Flibmemcached Small cleanup in string.h and converted result_st macro to inline. --- diff --git a/docs/memcached_result_st.pod b/docs/memcached_result_st.pod index ab69f266..a81442e2 100644 --- a/docs/memcached_result_st.pod +++ b/docs/memcached_result_st.pod @@ -3,7 +3,7 @@ memcached_result_create, memcached_result_free, memcached_result_key_value, memcached_result_key_length, memcached_result_value, memcached_result_length, -memcached_result_flags, memcached_result_cas - Work with results +memcached_result_flags, memcached_result_cas - Work with memcached_result_st =head1 LIBRARY @@ -18,19 +18,19 @@ C Client Library for memcached (libmemcached, -lmemcached) memcached_result_create (memcached_st *ptr, memcached_result_st *result); - void memcached_result_free(memcached_result_st *result); + void memcached_result_free (memcached_result_st *result); - char * memcached_result_key_value (memcached_result_st *result); + const char * memcached_result_key_value (memcached_result_st *result); - size_t memcached_result_key_length (memcached_result_st *result); + size_t memcached_result_key_length (const memcached_result_st *result); - char *memcached_result_value (memcached_result_st *ptr); + const char *memcached_result_value (memcached_result_st *ptr); - size_t memcached_result_length (memcached_result_st *ptr); + size_t memcached_result_length (const memcached_result_st *ptr); - uint32_t memcached_result_flags (memcached_result_st *result) + uint32_t memcached_result_flags (const memcached_result_st *result) - uint64_t memcached_result_cas (memcached_result_st *result); + uint64_t memcached_result_cas (const memcached_result_st *result); memcached_return_t memcached_result_set_value (memcached_result_st *ptr, diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index c6b898dc..52ed0c22 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -257,6 +257,8 @@ memcached_return_t run_distribution(memcached_st *ptr); #define memcached_is_processing_input(__object) ((__object)->flags.is_processing_input) #define memcached_set_purging(__object, __value) ((__object)->flags.is_purging= (__value)) #define memcached_set_processing_input(__object, __value) ((__object)->flags.is_processing_input= (__value)) +#define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized(= (__value)) +#define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated(= (__value)) #ifdef __cplusplus } diff --git a/libmemcached/result.c b/libmemcached/result.c index 03cd4fc3..6de2cf63 100644 --- a/libmemcached/result.c +++ b/libmemcached/result.c @@ -55,14 +55,6 @@ void memcached_result_reset(memcached_result_st *ptr) ptr->expiration= 0; } -/* - NOTE turn into macro -*/ -memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, size_t length) -{ - return memcached_string_append(&ptr->value, value, length); -} - void memcached_result_free(memcached_result_st *ptr) { if (ptr == NULL) @@ -86,17 +78,3 @@ void memcached_result_free(memcached_result_st *ptr) ptr->options.is_initialized= false; } } - - -char *memcached_result_value(memcached_result_st *ptr) -{ - memcached_string_st *sptr= &ptr->value; - return memcached_string_value(sptr); -} - -size_t memcached_result_length(memcached_result_st *ptr) -{ - memcached_string_st *sptr= &ptr->value; - return memcached_string_length(sptr); -} - diff --git a/libmemcached/result.h b/libmemcached/result.h index c685984a..c45ae202 100644 --- a/libmemcached/result.h +++ b/libmemcached/result.h @@ -39,27 +39,53 @@ void memcached_result_reset(memcached_result_st *ptr); LIBMEMCACHED_API memcached_result_st *memcached_result_create(memcached_st *ptr, memcached_result_st *result); -#define memcached_result_key_value(A) (A)->key -#define memcached_result_key_length(A) (A)->key_length -#define memcached_result_string_st(A) ((A)->value) -#ifdef FIX -#define memcached_result_value(A) memcached_string_value((A)->value) -#define memcached_result_length(A) memcached_string_length((A)->value) -#else -LIBMEMCACHED_API -char *memcached_result_value(memcached_result_st *ptr); +static inline const char *memcached_result_key_value(const memcached_result_st *self) +{ + return self->key; +} -LIBMEMCACHED_API -size_t memcached_result_length(memcached_result_st *ptr); +static inline size_t memcached_result_key_length(const memcached_result_st *self) +{ + return self->key_length; +} -#endif -#define memcached_result_flags(A) (A)->flags -#define memcached_result_cas(A) (A)->cas -LIBMEMCACHED_API -memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, size_t length); -#define memcached_result_set_flags(A,B) (A)->flags=(B) -#define memcached_result_set_expiration(A,B) (A)->expiration=(B) +static inline const char *memcached_result_value(const memcached_result_st *self) +{ + const memcached_string_st *sptr= &self->value; + return memcached_string_value(sptr); +} + +static inline size_t memcached_result_length(const memcached_result_st *self) +{ + const memcached_string_st *sptr= &self->value; + return memcached_string_length(sptr); +} + +static inline uint32_t memcached_result_flags(const memcached_result_st *self) +{ + return self->flags; +} + +static inline uint64_t memcached_result_cas(const memcached_result_st *self) +{ + return self->cas; +} + +static inline memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, size_t length) +{ + return memcached_string_append(&ptr->value, value, length); +} + +static inline void memcached_result_set_flags(memcached_result_st *self, uint32_t flags) +{ + self->flags= flags; +} + +static inline void memcached_result_set_expiration(memcached_result_st *self, uint32_t expiration) +{ + self->expiration= expiration; +} #ifdef __cplusplus } diff --git a/libmemcached/string.c b/libmemcached/string.c index 615d54f7..33b9222a 100644 --- a/libmemcached/string.c +++ b/libmemcached/string.c @@ -21,10 +21,10 @@ 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; @@ -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(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= memc->call_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); + memc->call_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, @@ -160,7 +169,6 @@ void memcached_string_free(memcached_string_st *ptr) else { ptr->options.is_initialized= false; - memset(ptr, 0, sizeof(memcached_string_st)); } } diff --git a/libmemcached/string.h b/libmemcached/string.h index 9a987264..39a86773 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -16,12 +16,21 @@ extern "C" { #endif +/** + Strings are always under our control so we make some assumptions + about them. + + 1) is_initialized is always valid. + 2) A string once intialized will always be, until free where we + unset this flag. + 3) A string always has a root. +*/ + struct memcached_string_st { memcached_st *root; char *end; char *string; size_t current_size; - size_t block_size; struct { bool is_allocated:1; bool is_initialized:1;