From: Brian Aker Date: Sat, 3 Nov 2007 19:37:55 +0000 (-0700) Subject: Fixed 64bit issue in hash. X-Git-Tag: 0.8~4 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=174e1d4d3011843e7e909c9b378b888c67d02538;p=m6w6%2Flibmemcached Fixed 64bit issue in hash. Extended string structure for eventual static strings. Added result structure. --- diff --git a/include/memcached.h b/include/memcached.h index cff65513..f5f7c282 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -20,6 +20,7 @@ extern "C" { typedef struct memcached_st memcached_st; typedef struct memcached_stat_st memcached_stat_st; +typedef struct memcached_result_st memcached_result_st; typedef struct memcached_string_st memcached_string_st; typedef struct memcached_server_st memcached_server_st; @@ -133,12 +134,20 @@ struct memcached_stat_st { }; struct memcached_string_st { + memcached_allocated is_allocated; char *string; char *end; size_t current_size; size_t block_size; }; +struct memcached_result_st { + memcached_string_st key; + memcached_string_st result; + uint16_t flags; + uint64_t cas; +}; + struct memcached_st { memcached_allocated is_allocated; memcached_server_st *hosts; @@ -154,6 +163,7 @@ struct memcached_st { unsigned long long flags; int send_size; int recv_size; + memcached_string_st result_buffer; memcached_hash hash; memcached_return warning; /* Future Use */ }; @@ -209,6 +219,7 @@ memcached_return memcached_mget(memcached_st *ptr, char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, size_t *value_length, uint16_t *flags, memcached_return *error); +memcached_result_st *memcached_fetch_object(memcached_st *ptr, memcached_return *error); /* Server Public functions */ #define memcached_server_count(A) A->number_of_hosts diff --git a/lib/common.h b/lib/common.h index ab5f9385..c16db98a 100644 --- a/lib/common.h +++ b/lib/common.h @@ -63,7 +63,9 @@ void memcached_quit_server(memcached_st *ptr, unsigned int server_key); #define memcached_string_size(A, B) B->current_size #define memcached_string_value(A, B) B->string -memcached_string_st *memcached_string_create(memcached_st *ptr, size_t initial_size); +memcached_string_st *memcached_string_create(memcached_st *ptr, + memcached_string_st *string, + size_t initial_size); memcached_return memcached_string_append_character(memcached_st *ptr, memcached_string_st *string, char character); diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 80b9de37..8ec8cb1c 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -313,3 +313,40 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, *value_length= 0; return NULL; } + +#ifdef NOT_YET +char *memcached_fetch_result(memcached_st *ptr, memcached_result_st *result, + memcached_return *error) +{ + char *value_check; + + while (ptr->cursor_server < ptr->number_of_hosts) + { + if (!ptr->hosts[ptr->cursor_server].cursor_active) + { + ptr->cursor_server++; + continue; + } + + value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags, + error, 1, ptr->cursor_server); + + if (*error == MEMCACHED_NOTFOUND) + ptr->cursor_server++; + else if (*error == MEMCACHED_END && *value_length == 0) + return NULL; + else if (*error == MEMCACHED_END) + { + WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */ + } + else if (*error != MEMCACHED_SUCCESS) + return NULL; + else + return value_check; + + } + + *value_length= 0; + return NULL; +} +#endif diff --git a/lib/memcached_hash.c b/lib/memcached_hash.c index 0d83cb75..961c794b 100644 --- a/lib/memcached_hash.c +++ b/lib/memcached_hash.c @@ -8,14 +8,13 @@ static uint32_t FNV_32_INIT= 2166136261L; static uint32_t FNV_32_PRIME= 16777619; /* Prototypes */ -static unsigned int internal_generate_hash(char *key, size_t key_length); -static uint32_t internal_generate_md5(char *key, size_t key_length); -static uint32_t internal_generate_md5(char *key, size_t key_length); -static uint32_t internal_generate_ketama_md5(char *key, size_t key_length); +static uint64_t internal_generate_hash(char *key, size_t key_length); +static uint64_t internal_generate_md5(char *key, size_t key_length); +static uint64_t internal_generate_ketama_md5(char *key, size_t key_length); unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_length) { - uint32_t hash; + uint64_t hash= 1; /* Just here to remove compile warning */ unsigned int x; switch (ptr->hash) @@ -33,7 +32,7 @@ unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_le case MEMCACHED_HASH_FNV1_64: { /* Thanks to pierre@demartines.com for the pointer */ - hash = FNV_64_INIT; + hash= FNV_64_INIT; for (x= 0; x < key_length; x++) { hash *= FNV_64_PRIME; @@ -88,10 +87,10 @@ unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_le return hash % ptr->number_of_hosts; } -static uint32_t internal_generate_hash(char *key, size_t key_length) +static uint64_t internal_generate_hash(char *key, size_t key_length) { char *ptr= key; - unsigned int value= 0; + uint64_t value= 0; while (--key_length) { @@ -106,19 +105,19 @@ static uint32_t internal_generate_hash(char *key, size_t key_length) return value == 0 ? 1 : value; } -static uint32_t internal_generate_md5(char *key, size_t key_length) +static uint64_t internal_generate_md5(char *key, size_t key_length) { unsigned char results[16]; md5_signature((unsigned char*)key, (unsigned int)key_length, results); - return (uint32_t)(( results[3] << 24 ) + return (uint64_t)(( results[3] << 24 ) | ( results[2] << 16 ) | ( results[1] << 8 ) | results[0] ); } -static uint32_t internal_generate_ketama_md5(char *key, size_t key_length) +static uint64_t internal_generate_ketama_md5(char *key, size_t key_length) { unsigned char results[16]; diff --git a/lib/memcached_string.c b/lib/memcached_string.c index 30d8e175..23543885 100644 --- a/lib/memcached_string.c +++ b/lib/memcached_string.c @@ -36,17 +36,25 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need return MEMCACHED_SUCCESS; } -memcached_string_st *memcached_string_create(memcached_st *ptr, size_t initial_size) +memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size) { memcached_return rc; - memcached_string_st *string; /* Saving malloc calls :) */ - string= (memcached_string_st *)malloc(sizeof(memcached_string_st)); - if (!string) - return NULL; + if (string) + { + memset(string, 0, sizeof(memcached_string_st)); + string->is_allocated= MEMCACHED_NOT_ALLOCATED; + } + else + { + string= (memcached_string_st *)malloc(sizeof(memcached_string_st)); + if (!string) + return NULL; + memset(string, 0, sizeof(memcached_string_st)); + string->is_allocated= MEMCACHED_ALLOCATED; + } string->end= string->string; - memset(string, 0, sizeof(memcached_string_st)); string->block_size= initial_size; rc= memcached_string_check(string, initial_size); @@ -124,5 +132,6 @@ memcached_return memcached_string_reset(memcached_st *ptr, memcached_string_st * void memcached_string_free(memcached_st *ptr, memcached_string_st *string) { free(string->string); - free(string); + if (string->is_allocated == MEMCACHED_ALLOCATED) + free(string); } diff --git a/tests/test.c b/tests/test.c index 523f3034..23b1c699 100644 --- a/tests/test.c +++ b/tests/test.c @@ -710,11 +710,22 @@ void user_supplied_bug3(memcached_st *memc) free(keys); } +void string_static_null(memcached_st *memc) +{ + memcached_string_st string; + memcached_string_st *string_ptr; + + string_ptr= memcached_string_create(memc, &string, 0); + assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED); + assert(string_ptr); + memcached_string_free(memc, &string); +} + void string_alloc_null(memcached_st *memc) { memcached_string_st *string; - string= memcached_string_create(memc, 0); + string= memcached_string_create(memc, NULL, 0); assert(string); memcached_string_free(memc, string); } @@ -723,7 +734,7 @@ void string_alloc_with_size(memcached_st *memc) { memcached_string_st *string; - string= memcached_string_create(memc, 1024); + string= memcached_string_create(memc, NULL, 1024); assert(string); memcached_string_free(memc, string); } @@ -732,7 +743,7 @@ void string_alloc_with_size_toobig(memcached_st *memc) { memcached_string_st *string; - string= memcached_string_create(memc, INT64_MAX); + string= memcached_string_create(memc, NULL, INT64_MAX); assert(string == NULL); } @@ -745,7 +756,7 @@ void string_alloc_append(memcached_st *memc) /* Ring the bell! */ memset(buffer, 6, SMALL_STRING_LEN); - string= memcached_string_create(memc, 100); + string= memcached_string_create(memc, NULL, 100); assert(string); for (x= 0; x < 1024; x++) @@ -767,7 +778,7 @@ void string_alloc_append_toobig(memcached_st *memc) /* Ring the bell! */ memset(buffer, 6, SMALL_STRING_LEN); - string= memcached_string_create(memc, 100); + string= memcached_string_create(memc, NULL, 100); assert(string); for (x= 0; x < 1024; x++) @@ -978,6 +989,7 @@ int main(int argc, char *argv[]) }; test_st string_tests[] ={ + {"string static with null", 0, string_static_null }, {"string alloc with null", 0, string_alloc_null }, {"string alloc with 1K", 0, string_alloc_with_size }, {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },