From 276c3db8d1dc8e14949df2b408960a803bea5d53 Mon Sep 17 00:00:00 2001 From: Date: Wed, 13 May 2009 21:11:04 -0700 Subject: [PATCH] Flip call (NULL is more common). Also add in helgrind and fix calloc. --- ChangeLog | 3 ++- clients/memslap.c | 3 +-- docs/memcached_callback.pod | 1 + libmemcached/memcached.c | 3 +-- libmemcached/memcached_analyze.c | 4 +--- libmemcached/memcached_fetch.c | 6 ++++-- libmemcached/memcached_result.c | 3 +-- libmemcached/memcached_server.c | 3 +-- libmemcached/memcached_stats.c | 6 ++---- libmemcached/memcached_string.c | 3 +-- tests/Makefile.am | 7 +++++++ tests/function.c | 2 +- 12 files changed, 23 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 63bec518..d5b9b029 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ -0.29 Wed May 13 08:06:05 PDT 2009 +0.29 + * Fixed malloc usage to calloc for spots where we need zero filled memory. * All code warnings now treated as errors. * Fixes for debian packaging. * Added new pooling mechanism. diff --git a/clients/memslap.c b/clients/memslap.c index d85642f8..509204eb 100644 --- a/clients/memslap.c +++ b/clients/memslap.c @@ -178,8 +178,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion) for (x= 0; x < opt_concurrency; x++) { thread_context_st *context; - context= (thread_context_st *)malloc(sizeof(thread_context_st)); - memset(context, 0, sizeof(thread_context_st)); + context= (thread_context_st *)calloc(1, sizeof(thread_context_st)); context->memc= memcached_clone(NULL, memc); context->test= opt_test; diff --git a/docs/memcached_callback.pod b/docs/memcached_callback.pod index 5aa2e3b4..a11f9585 100644 --- a/docs/memcached_callback.pod +++ b/docs/memcached_callback.pod @@ -65,6 +65,7 @@ will copy the pointer to the clone. =item MEMCACHED_CALLBACK_MALLOC_FUNCTION This alllows yout to pass in a customized version of malloc that will be used instead of the builtin malloc(3) call. +Your malloc must zero all memory. The prototype for this is: void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size); diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 52b3834b..a93bef7d 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -9,12 +9,11 @@ memcached_st *memcached_create(memcached_st *ptr) if (ptr == NULL) { - ptr= (memcached_st *)malloc(sizeof(memcached_st)); + ptr= (memcached_st *)calloc(1, sizeof(memcached_st)); if (!ptr) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ - memset(ptr, 0, sizeof(memcached_st)); ptr->is_allocated= true; } else diff --git a/libmemcached/memcached_analyze.c b/libmemcached/memcached_analyze.c index 1063fb27..583d828f 100644 --- a/libmemcached/memcached_analyze.c +++ b/libmemcached/memcached_analyze.c @@ -70,7 +70,7 @@ memcached_analysis_st *memcached_analyze(memcached_st *memc, *error= MEMCACHED_SUCCESS; server_count= memcached_server_count(memc); - result= (memcached_analysis_st*)malloc(sizeof(memcached_analysis_st) + result= (memcached_analysis_st*)calloc(1, sizeof(memcached_analysis_st) * (memc->number_of_hosts)); if (!result) { @@ -78,8 +78,6 @@ memcached_analysis_st *memcached_analyze(memcached_st *memc, return NULL; } - memset(result, 0, sizeof(*result)); - for (x= 0; x < server_count; x++) { calc_largest_consumption(result, x, stat[x].bytes); diff --git a/libmemcached/memcached_fetch.c b/libmemcached/memcached_fetch.c index e9939890..8afdfd04 100644 --- a/libmemcached/memcached_fetch.c +++ b/libmemcached/memcached_fetch.c @@ -16,8 +16,9 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, result_buffer= memcached_fetch_result(ptr, result_buffer, error); - if (*error != MEMCACHED_SUCCESS || result_buffer == NULL) + if (result_buffer == NULL || *error != MEMCACHED_SUCCESS) { + WATCHPOINT_ASSERT(result_buffer == NULL); *value_length= 0; return NULL; } @@ -54,7 +55,8 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr, if ((result= memcached_result_create(ptr, NULL)) == NULL) return NULL; - while ((server = memcached_io_get_readable_server(ptr)) != NULL) { + while ((server = memcached_io_get_readable_server(ptr)) != NULL) + { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; *error= memcached_response(server, buffer, sizeof(buffer), result); diff --git a/libmemcached/memcached_result.c b/libmemcached/memcached_result.c index 8d376f68..fac3b8c5 100644 --- a/libmemcached/memcached_result.c +++ b/libmemcached/memcached_result.c @@ -17,11 +17,10 @@ memcached_result_st *memcached_result_create(memcached_st *memc, if (memc->call_malloc) ptr= (memcached_result_st *)memc->call_malloc(memc, sizeof(memcached_result_st)); else - ptr= (memcached_result_st *)malloc(sizeof(memcached_result_st)); + ptr= (memcached_result_st *)calloc(1, sizeof(memcached_result_st)); if (ptr == NULL) return NULL; - memset(ptr, 0, sizeof(memcached_result_st)); ptr->is_allocated= true; } diff --git a/libmemcached/memcached_server.c b/libmemcached/memcached_server.c index 4aebfd3b..7fff6cdb 100644 --- a/libmemcached/memcached_server.c +++ b/libmemcached/memcached_server.c @@ -7,12 +7,11 @@ memcached_server_st *memcached_server_create(memcached_st *memc, memcached_serve { if (ptr == NULL) { - ptr= (memcached_server_st *)malloc(sizeof(memcached_server_st)); + ptr= (memcached_server_st *)calloc(1, sizeof(memcached_server_st)); if (!ptr) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ - memset(ptr, 0, sizeof(memcached_server_st)); ptr->is_allocated= true; } else diff --git a/libmemcached/memcached_stats.c b/libmemcached/memcached_stats.c index d5ec819f..e6a4e07f 100644 --- a/libmemcached/memcached_stats.c +++ b/libmemcached/memcached_stats.c @@ -360,14 +360,13 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur if (ptr->call_malloc) stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); else - stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts)); + stats= (memcached_stat_st *)calloc(1, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); if (!stats) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } - memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); rc= MEMCACHED_SUCCESS; for (x= 0; x < ptr->number_of_hosts; x++) @@ -420,14 +419,13 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __att if (ptr->call_malloc) list= (char **)ptr->call_malloc(ptr, length); else - list= (char **)malloc(length); + list= (char **)calloc(1, length); if (!list) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } - memset(list, 0, sizeof(memcached_stat_keys)); memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys)); diff --git a/libmemcached/memcached_string.c b/libmemcached/memcached_string.c index 5a6f190e..3bac225a 100644 --- a/libmemcached/memcached_string.c +++ b/libmemcached/memcached_string.c @@ -47,11 +47,10 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string 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= (memcached_string_st *)calloc(1, sizeof(memcached_string_st)); if (string == NULL) return NULL; - memset(string, 0, sizeof(memcached_string_st)); string->is_allocated= true; } string->block_size= MEMCACHED_BLOCK_SIZE; diff --git a/tests/Makefile.am b/tests/Makefile.am index dc242aca..dd7ba749 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -85,6 +85,13 @@ callgrind: libtool --mode=execute valgrind --tool=callgrind testapp callgrind_annotate callgrind.out.* --auto=yes > /tmp/callgrind.out +helgrind: + rm -f helgrind.out.* + libtool --mode=execute valgrind --tool=helgrind testapp + +helgrind-slap: + libtool --mode=execute valgrind --tool=helgrind ../clients/memslap --server=localhost --concurrency=30 + test-no-outputdiff: testapp ./testapp > /dev/null @echo "Test completed" diff --git a/tests/function.c b/tests/function.c index cd7a82ca..86a255ba 100644 --- a/tests/function.c +++ b/tests/function.c @@ -3035,7 +3035,7 @@ static void my_free(memcached_st *ptr __attribute__((unused)), void *mem) static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size) { - return malloc(size); + return calloc(1, size); } static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size) -- 2.30.2