From: Brian Aker Date: Wed, 16 Dec 2009 17:34:28 +0000 (-0800) Subject: Code change to use struct bitsets. X-Git-Tag: 0.37~78 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=1a92818ec19dd3e9ca4b6765a5765ca1a97dcfdc;p=awesomized%2Flibmemcached Code change to use struct bitsets. --- diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 4e8ec062..c5aff374 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -11,16 +11,20 @@ memcached_st *memcached_create(memcached_st *ptr) { ptr= (memcached_st *)calloc(1, sizeof(memcached_st)); - if (!ptr) + if (! ptr) + { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ + } - ptr->is_allocated= true; + ptr->options.is_allocated= true; } else { memset(ptr, 0, sizeof(memcached_st)); } + ptr->options.is_initialized= true; + memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL); result_ptr= memcached_result_create(ptr, &ptr->result); @@ -34,6 +38,9 @@ memcached_st *memcached_create(memcached_st *ptr) ptr->io_msg_watermark= 500; ptr->io_bytes_watermark= 65 * 1024; + WATCHPOINT_ASSERT_INITIALIZED(&ptr->result); + WATCHPOINT_ASSERT_INITIALIZED(&ptr->hashkit); + return ptr; } @@ -50,10 +57,14 @@ void memcached_free(memcached_st *ptr) if (ptr->continuum) ptr->call_free(ptr, ptr->continuum); - if (ptr->is_allocated) + if (memcached_is_allocated(ptr)) + { ptr->call_free(ptr, ptr); + } else - memset(ptr, 0, sizeof(memcached_st)); + { + ptr->options.is_initialized= false; + } } /* @@ -69,7 +80,7 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) if (source == NULL) return memcached_create(clone); - if (clone && clone->is_allocated) + if (clone && memcached_is_allocated(clone)) { return NULL; } diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 6ca40a5c..512e768e 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -73,8 +73,11 @@ struct memcached_stat_st { }; struct memcached_st { - uint8_t purging; - bool is_allocated; + struct { + bool is_allocated:1; + bool is_initialized:1; + bool is_purging:1; + } options; uint8_t distribution; uint8_t hash; uint32_t continuum_points_counter; @@ -323,6 +326,10 @@ void *memcached_set_user_data(memcached_st *ptr, void *data); LIBMEMCACHED_LOCAL memcached_return run_distribution(memcached_st *ptr); + +#define memcached_is_allocated(__object) ((__object)->options.is_allocated) +#define memcached_is_initialized(__object) ((__object)->options.is_initialized) + #ifdef __cplusplus } #endif diff --git a/libmemcached/memcached_fetch.c b/libmemcached/memcached_fetch.c index 9c31e2b0..dc253ca2 100644 --- a/libmemcached/memcached_fetch.c +++ b/libmemcached/memcached_fetch.c @@ -69,10 +69,14 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr, } /* We have completed reading data */ - if (result->is_allocated) + if (memcached_is_allocated(result)) + { memcached_result_free(result); + } else + { memcached_string_reset(&result->value); + } return NULL; } diff --git a/libmemcached/memcached_purge.c b/libmemcached/memcached_purge.c index 9e5e31ab..1f47e59e 100644 --- a/libmemcached/memcached_purge.c +++ b/libmemcached/memcached_purge.c @@ -7,7 +7,7 @@ memcached_return memcached_purge(memcached_server_st *ptr) uint32_t x; memcached_return ret= MEMCACHED_SUCCESS; - if (ptr->root->purging || /* already purging */ + if (ptr->root->options.is_purging || /* already purging */ (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark && ptr->io_bytes_sent < ptr->root->io_bytes_watermark) || (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark && @@ -18,14 +18,14 @@ memcached_return memcached_purge(memcached_server_st *ptr) /* memcached_io_write and memcached_response may call memcached_purge so we need to be able stop any recursion.. */ - ptr->root->purging= 1; + ptr->root->options.is_purging= true; WATCHPOINT_ASSERT(ptr->fd != -1); /* Force a flush of the buffer to ensure that we don't have the n-1 pending requests buffered up.. */ if (memcached_io_write(ptr, NULL, 0, 1) == -1) { - ptr->root->purging= 0; + ptr->root->options.is_purging= true; return MEMCACHED_WRITE_FAILURE; } WATCHPOINT_ASSERT(ptr->fd != -1); @@ -70,7 +70,7 @@ memcached_return memcached_purge(memcached_server_st *ptr) memcached_result_free(result_ptr); ptr->root->poll_timeout= timeo; } - ptr->root->purging= 0; + ptr->root->options.is_purging= false; return ret; } diff --git a/libmemcached/memcached_result.c b/libmemcached/memcached_result.c index 0d77130d..6fdad670 100644 --- a/libmemcached/memcached_result.c +++ b/libmemcached/memcached_result.c @@ -9,20 +9,27 @@ memcached_result_st *memcached_result_create(memcached_st *memc, memcached_result_st *ptr) { + WATCHPOINT_ASSERT(memc && memc->options.is_initialized); + /* Saving malloc calls :) */ if (ptr) + { memset(ptr, 0, sizeof(memcached_result_st)); + } else { ptr= memc->call_malloc(memc, sizeof(memcached_result_st)); if (ptr == NULL) return NULL; - ptr->is_allocated= true; + ptr->options.is_allocated= true; } + ptr->options.is_initialized= true; + ptr->root= memc; memcached_string_create(memc, &ptr->value, 0); + WATCHPOINT_ASSERT_INITIALIZED(&ptr->value); WATCHPOINT_ASSERT(ptr->value.string == NULL); return ptr; @@ -52,6 +59,12 @@ void memcached_result_free(memcached_result_st *ptr) memcached_string_free(&ptr->value); - if (ptr->is_allocated) + if (memcached_is_allocated(ptr)) + { free(ptr); + } + else + { + ptr->options.is_initialized= false; + } } diff --git a/libmemcached/memcached_result.h b/libmemcached/memcached_result.h index e7ce012b..88212824 100644 --- a/libmemcached/memcached_result.h +++ b/libmemcached/memcached_result.h @@ -14,8 +14,11 @@ extern "C" { #endif struct memcached_result_st { + struct { + bool is_allocated:1; + bool is_initialized:1; + } options; uint32_t flags; - bool is_allocated; time_t expiration; memcached_st *root; size_t key_length; diff --git a/libmemcached/memcached_server.c b/libmemcached/memcached_server.c index 2ecbe214..ca0f4d04 100644 --- a/libmemcached/memcached_server.c +++ b/libmemcached/memcached_server.c @@ -12,10 +12,12 @@ memcached_server_st *memcached_server_create(memcached_st *memc, memcached_serve if (!ptr) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ - ptr->is_allocated= true; + ptr->options.is_allocated= true; } else + { memset(ptr, 0, sizeof(memcached_server_st)); + } ptr->root= memc; @@ -59,10 +61,15 @@ void memcached_server_free(memcached_server_st *ptr) if (ptr->address_info) freeaddrinfo(ptr->address_info); - if (ptr->is_allocated) + + if (memcached_is_allocated(ptr)) + { ptr->root->call_free(ptr->root, ptr); + } else + { memset(ptr, 0, sizeof(memcached_server_st)); + } } /* diff --git a/libmemcached/memcached_server.h b/libmemcached/memcached_server.h index de57eaec..389f241d 100644 --- a/libmemcached/memcached_server.h +++ b/libmemcached/memcached_server.h @@ -14,7 +14,9 @@ extern "C" { #endif struct memcached_server_st { - bool is_allocated; + struct { + bool is_allocated:1; + } options; bool sockaddr_inited; uint16_t count; unsigned int cursor_active; diff --git a/libmemcached/memcached_stats.c b/libmemcached/memcached_stats.c index 04928f04..f1defc5b 100644 --- a/libmemcached/memcached_stats.c +++ b/libmemcached/memcached_stats.c @@ -33,7 +33,7 @@ static const char *memcached_stat_keys[] = { static memcached_return set_data(memcached_stat_st *memc_stat, char *key, char *value) { - if(strlen(key) < 1) + if (strlen(key) < 1) { WATCHPOINT_STRING(key); return MEMCACHED_UNKNOWN_STAT_KEY; @@ -398,8 +398,10 @@ memcached_return memcached_stat_servername(memcached_stat_st *memc_stat, char *a { memcached_return rc; memcached_st memc; + memcached_st *memc_ptr; - memcached_create(&memc); + memc_ptr= memcached_create(&memc); + WATCHPOINT_ASSERT(memc_ptr); memcached_server_add(&memc, hostname, port); diff --git a/libmemcached/memcached_string.c b/libmemcached/memcached_string.c index 614343c8..0911b409 100644 --- a/libmemcached/memcached_string.c +++ b/libmemcached/memcached_string.c @@ -1,6 +1,6 @@ #include "common.h" -memcached_return memcached_string_check(memcached_string_st *string, size_t need) +inline static memcached_return _string_check(memcached_string_st *string, size_t need) { if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string))) { @@ -32,31 +32,40 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need 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; /* Saving malloc calls :) */ if (string) + { + WATCHPOINT_ASSERT(memc->options.is_safe && string->options.is_initialized == false); + memset(string, 0, sizeof(memcached_string_st)); + } else { - string= ptr->call_calloc(ptr, 1, sizeof(memcached_string_st)); + string= memc->call_calloc(memc, 1, sizeof(memcached_string_st)); if (string == NULL) + { return NULL; - 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) { - ptr->call_free(ptr, string); + memc->call_free(memc, string); return NULL; } + string->options.is_initialized= true; + WATCHPOINT_ASSERT(string->string == string->end); return string; @@ -67,7 +76,7 @@ memcached_return memcached_string_append_character(memcached_string_st *string, { memcached_return rc; - rc= memcached_string_check(string, 1); + rc= _string_check(string, 1); if (rc != MEMCACHED_SUCCESS) return rc; @@ -83,7 +92,7 @@ memcached_return memcached_string_append(memcached_string_st *string, { memcached_return rc; - rc= memcached_string_check(string, length); + rc= _string_check(string, length); if (rc != MEMCACHED_SUCCESS) return rc; @@ -129,10 +138,23 @@ void memcached_string_free(memcached_string_st *ptr) return; if (ptr->string) + { ptr->root->call_free(ptr->root, ptr->string); + } - if (ptr->is_allocated) + if (memcached_is_allocated(ptr)) + { ptr->root->call_free(ptr->root, ptr); + } else + { + ptr->options.is_initialized= false; memset(ptr, 0, sizeof(memcached_string_st)); + } +} + +memcached_return memcached_string_check(memcached_string_st *string, size_t need) +{ + return _string_check(string, need); } + diff --git a/libmemcached/memcached_string.h b/libmemcached/memcached_string.h index bed14281..6ecdbe94 100644 --- a/libmemcached/memcached_string.h +++ b/libmemcached/memcached_string.h @@ -19,7 +19,10 @@ struct memcached_string_st { char *string; size_t current_size; size_t block_size; - bool is_allocated; + struct { + bool is_allocated:1; + bool is_initialized:1; + } options; }; #define memcached_string_length(A) (size_t)((A)->end - (A)->string) @@ -33,8 +36,10 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, size_t initial_size); LIBMEMCACHED_API memcached_return memcached_string_check(memcached_string_st *string, size_t need); + LIBMEMCACHED_API char *memcached_string_c_copy(memcached_string_st *string); + LIBMEMCACHED_API memcached_return memcached_string_append_character(memcached_string_st *string, char character); @@ -43,6 +48,7 @@ memcached_return memcached_string_append(memcached_string_st *string, const char *value, size_t length); LIBMEMCACHED_API memcached_return memcached_string_reset(memcached_string_st *string); + LIBMEMCACHED_API void memcached_string_free(memcached_string_st *string); diff --git a/libmemcached/memcached_watchpoint.h b/libmemcached/memcached_watchpoint.h index dc8045aa..d7f759fd 100644 --- a/libmemcached/memcached_watchpoint.h +++ b/libmemcached/memcached_watchpoint.h @@ -23,7 +23,10 @@ #define WATCHPOINT_ERRNO(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__, strerror(A));fflush(stdout); #define WATCHPOINT_ASSERT_PRINT(A,B,C) if(!(A)){fprintf(stderr, "\nWATCHPOINT ASSERT %s:%d (%s) ", __FILE__, __LINE__,__func__);fprintf(stderr, (B),(C));fprintf(stderr,"\n");fflush(stdout);}assert((A)); #define WATCHPOINT_ASSERT(A) assert((A)); +#define WATCHPOINT_ASSERT_INITIALIZED(A) (memcached_is_initialized((A)); + #else + #define WATCHPOINT #define WATCHPOINT_ERROR(A) #define WATCHPOINT_IFERROR(A) @@ -32,6 +35,7 @@ #define WATCHPOINT_ERRNO(A) #define WATCHPOINT_ASSERT_PRINT(A,B,C) #define WATCHPOINT_ASSERT(A) +#define WATCHPOINT_ASSERT_INITIALIZED(A) #endif /* DEBUG */ diff --git a/tests/function.c b/tests/function.c index c95d3348..70a75557 100644 --- a/tests/function.c +++ b/tests/function.c @@ -2933,20 +2933,28 @@ static test_return_t result_static(memcached_st *memc) memcached_result_st *result_ptr; result_ptr= memcached_result_create(memc, &result); - test_truth(result.is_allocated == false); + test_truth(result.options.is_allocated == false); + test_truth(memcached_is_initialized(&result) == true); test_truth(result_ptr); + test_truth(result_ptr == &result); + memcached_result_free(&result); + test_truth(result.options.is_allocated == false); + test_truth(memcached_is_initialized(&result) == false); + return TEST_SUCCESS; } static test_return_t result_alloc(memcached_st *memc) { - memcached_result_st *result; + memcached_result_st *result_ptr; - result= memcached_result_create(memc, NULL); - test_truth(result); - memcached_result_free(result); + result_ptr= memcached_result_create(memc, NULL); + test_truth(result_ptr); + test_truth(result_ptr->options.is_allocated == true); + test_truth(memcached_is_initialized(result_ptr) == true); + memcached_result_free(result_ptr); return TEST_SUCCESS; } @@ -2957,9 +2965,18 @@ static test_return_t string_static_null(memcached_st *memc) memcached_string_st *string_ptr; string_ptr= memcached_string_create(memc, &string, 0); - test_truth(string.is_allocated == false); + test_truth(string.options.is_initialized == true); test_truth(string_ptr); + + /* The following two better be the same! */ + test_truth(memcached_is_allocated(string_ptr) == false); + test_truth(memcached_is_allocated(&string) == false); + test_truth(&string == string_ptr); + + test_truth(string.options.is_initialized == true); + test_truth(memcached_is_initialized(&string) == true); memcached_string_free(&string); + test_truth(memcached_is_initialized(&string) == false); return TEST_SUCCESS; } @@ -2970,6 +2987,8 @@ static test_return_t string_alloc_null(memcached_st *memc) string= memcached_string_create(memc, NULL, 0); test_truth(string); + test_truth(memcached_is_allocated(string) == true); + test_truth(memcached_is_initialized(string) == true); memcached_string_free(string); return TEST_SUCCESS; @@ -2981,6 +3000,8 @@ static test_return_t string_alloc_with_size(memcached_st *memc) string= memcached_string_create(memc, NULL, 1024); test_truth(string); + test_truth(memcached_is_allocated(string) == true); + test_truth(memcached_is_initialized(string) == true); memcached_string_free(string); return TEST_SUCCESS; @@ -3007,6 +3028,8 @@ static test_return_t string_alloc_append(memcached_st *memc) string= memcached_string_create(memc, NULL, 100); test_truth(string); + test_truth(memcached_is_allocated(string) == true); + test_truth(memcached_is_initialized(string) == true); for (x= 0; x < 1024; x++) { @@ -3014,6 +3037,7 @@ static test_return_t string_alloc_append(memcached_st *memc) rc= memcached_string_append(string, buffer, SMALL_STRING_LEN); test_truth(rc == MEMCACHED_SUCCESS); } + test_truth(memcached_is_allocated(string) == true); memcached_string_free(string); return TEST_SUCCESS; @@ -3031,6 +3055,8 @@ static test_return_t string_alloc_append_toobig(memcached_st *memc) string= memcached_string_create(memc, NULL, 100); test_truth(string); + test_truth(memcached_is_allocated(string) == true); + test_truth(memcached_is_initialized(string) == true); for (x= 0; x < 1024; x++) { @@ -3039,6 +3065,7 @@ static test_return_t string_alloc_append_toobig(memcached_st *memc) } rc= memcached_string_append(string, buffer, SIZE_MAX); test_truth(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE); + test_truth(memcached_is_allocated(string) == true); memcached_string_free(string); return TEST_SUCCESS;