From: Trond Norbye Date: Sun, 14 Jun 2009 18:52:39 +0000 (+0200) Subject: merge upstream X-Git-Tag: 0.31~15^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=921abbe1906e77cc18cbe8859bc5afe9494d780e;hp=9124a34c62a7e7c99fa7280596cace7d0bcb131e;p=m6w6%2Flibmemcached merge upstream --- diff --git a/.bzrignore b/.bzrignore index 7cb02a8e..4b8966e1 100644 --- a/.bzrignore +++ b/.bzrignore @@ -95,6 +95,10 @@ docs/memflush.1 docs/memrm.1 docs/memslap.1 docs/memstat.1 +docs/memcached_set_memory_allocators.3 +docs/memcached_get_memory_allocators.3 +docs/memcached_set_user_data.3 +docs/memcached_get_user_data.3 libmemcached/.deps libmemcached/.libs libmemcached/Makefile diff --git a/ChangeLog b/ChangeLog index 70cddaff..a972319d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ + * Added support for memcached_set_memory_allocators(). + * Fixed bug in configure.ac for have_htoll. + 0.30 Sun May 31 17:31:52 PDT 2009 * Added memcachd_dump command (and framework for memdump tool). * Realigned all structures to remove padding (and line up important bits for 64bit caches. diff --git a/docs/Makefile.am b/docs/Makefile.am index 1ac9b2fa..fa2d3383 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -31,7 +31,9 @@ EXTRA_DIST = libmemcached.pod\ memdump.pod\ memcached_flush_buffers.pod\ memcached_analyze.pod\ - memcached_generate_hash_value.pod + memcached_generate_hash_value.pod\ + memcached_memory_allocators.pod\ + memory_user_data.pod man_MANS = libmemcached.3\ libmemcached_examples.3\ @@ -95,7 +97,11 @@ man_MANS = libmemcached.3\ memcached_version.3\ memcached_flush_buffers.3\ memcached_dump.3\ - memcached_generate_hash_value.3 + memcached_generate_hash_value.3\ + memcached_set_memory_allocators.3\ + memcached_get_memory_allocators.3\ + memcached_set_user_data.3\ + memcached_get_user_data.3 if BUILD_LIBMEMCACHEDUTIL man_MANS+= libmemcachedutil.3 \ @@ -307,6 +313,18 @@ memcached_analyze.3: memcached_analyze.pod memcached_generate_hash_value.3: memcached_generate_hash_value.pod @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_generate_hash_value.pod > memcached_generate_hash_value.3 +memcached_get_memory_allocators.3: memcached_memory_allocators.pod + @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_memory_allocators.pod > memcached_get_memory_allocators.3 + +memcached_set_memory_allocators.3: memcached_memory_allocators.pod + @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_memory_allocators.pod > memcached_set_memory_allocators.3 + +memcached_get_user_data.3: memcached_user_data.pod + @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_user_data.pod > memcached_get_user_data.3 + +memcached_set_user_data.3: memcached_user_data.pod + @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_user_data.pod > memcached_set_user_data.3 + memcached_pool_create.3: memcached_pool.pod @POD2MAN@ -c "libmemcachedutil" -r "" -s 3 ${top_srcdir}/docs/memcached_pool.pod > memcached_pool_create.3 @@ -370,6 +388,8 @@ test: podchecker $(top_srcdir)/docs/memflush.pod podchecker $(top_srcdir)/docs/memcached_flush_buffers.pod podchecker $(top_srcdir)/docs/memcached_pool.pod + podchecker $(top_srcdir)/docs/memcached_memory_allocators.pod + podchecker $(top_srcdir)/docs/memcached_user_data.pod html-local: pod2htmltree "/libmemcached" . diff --git a/docs/libmemcached.pod b/docs/libmemcached.pod index 2ee593a8..e1a95a73 100644 --- a/docs/libmemcached.pod +++ b/docs/libmemcached.pod @@ -121,7 +121,7 @@ Brian Aker, Ebrian@tangent.orgE =head1 SEE ALSO -memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcached_result_create(3) memcached_result_free(3) memcached_result_key_value(3) memcached_result_key_length(3) memcached_result_value(3) memcached_result_length(3) memcached_result_flags(3) memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) memcached_callback_get(3) memcached_callback_set(3) memcached_version(3) memcached_lib_version(3) memcached_result_set_value(3) memcached_dump(3) memdump(1) +memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcached_result_create(3) memcached_result_free(3) memcached_result_key_value(3) memcached_result_key_length(3) memcached_result_value(3) memcached_result_length(3) memcached_result_flags(3) memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) memcached_callback_get(3) memcached_callback_set(3) memcached_version(3) memcached_lib_version(3) memcached_result_set_value(3) memcached_dump(3) memdump(1) memcached_set_memory_allocators(3) memcached_get_memory_allocators(3) memcached_get_user_data(3) memcached_set_user_data(3) =cut diff --git a/docs/memcached_callback.pod b/docs/memcached_callback.pod index 100aff18..6eba4f32 100644 --- a/docs/memcached_callback.pod +++ b/docs/memcached_callback.pod @@ -64,25 +64,15 @@ 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); +DEPRECATED: use memcached_set_memory_allocators instead. =item MEMCACHED_CALLBACK_REALLOC_FUNCTION -This alllows yout to pass in a customized version of realloc that will be used instead of the builtin realloc(3) call. -The prototype for this is: - -void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, const size_t size); +DEPRECATED: use memcached_set_memory_allocators instead. =item MEMCACHED_CALLBACK_FREE_FUNCTION -This alllows yout to pass in a customized version of realloc that will be used instead of the builtin free(3) call. -The prototype for this is: - -typedef void (*memcached_free_function)(memcached_st *ptr, void *mem); +DEPRECATED: use memcached_set_memory_allocators instead. =item MEMCACHED_CALLBACK_GET_FAILURE diff --git a/docs/memcached_memory_allocators.pod b/docs/memcached_memory_allocators.pod new file mode 100644 index 00000000..5d6d00ae --- /dev/null +++ b/docs/memcached_memory_allocators.pod @@ -0,0 +1,73 @@ +=head1 NAME + +memcached_set_memory_allocators, memcached_get_memory_allocators + +=head1 LIBRARY + +C Client Library for memcached (libmemcached, -lmemcached) + +=head1 SYNOPSIS + + #include + + memcached_return + memcached_set_memory_allocators(memcached_st *ptr, + memcached_malloc_function mem_malloc, + memcached_free_function mem_free, + memcached_realloc_function mem_realloc, + memcached_calloc_function mem_calloc); + + void memcached_get_memory_allocators(memcached_st *ptr, + memcached_malloc_function *mem_malloc, + memcached_free_function *mem_free, + memcached_realloc_function *mem_realloc, + memcached_calloc_function *mem_calloc); + + void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size); + void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, + const size_t size); + void (*memcached_free_function)(memcached_st *ptr, void *mem); + void *(*memcached_calloc_function)(memcached_st *ptr, size_t nelem, + const size_t elsize); + + +=head1 DESCRIPTION + +libmemcached(3) allows you to specify your own memory allocators optimized +for your application. + +memcached_set_memory_allocators() is used to set the memory allocators used +by the memcached instance specified by ptr. Please note that you cannot +override only one of the memory allocators, you have to specify a complete +new set if you want to override one of them. All of the memory allocation +functions should behave as specified in the C99 standard. Specify NULL as +all functions to reset them to the default values. + +memcached_get_memory_allocators() is used to get the currently used memory +allocators by a mamcached handle. + +The first argument to the memory allocator functions is a pointer to a +memcached structure, and you may use the memcached_set_user_data() and +memcached_get_user_data() to store a user-specific value to each memcached +structure. + +=head1 RETURN + +memcached_set_memory_allocators() return MEMCACHED_SUCCESS upon success, +and MEMCACHED_FAILURE if you don't pass a complete set of function pointers. + +=head1 HOME + +To find out more information please check: +L + +=head1 AUTHOR + +Trond Norbye, Etrond.norbye@sun.comE + +=head1 SEE ALSO + +memcached(1) libmemcached(3) memcached_get_user_data(3) memcached_set_user_data(3) + +=cut + diff --git a/docs/memcached_user_data.pod b/docs/memcached_user_data.pod new file mode 100644 index 00000000..d5161a1d --- /dev/null +++ b/docs/memcached_user_data.pod @@ -0,0 +1,49 @@ +=head1 NAME + +memcached_set_user_data, memcached_get_user_data + +=head1 LIBRARY + +C Client Library for memcached (libmemcached, -lmemcached) + +=head1 SYNOPSIS + + #include + + void *memcached_get_user_data(memcached_st *ptr); + void *memcached_set_user_data(memcached_st *ptr, void *data); + +=head1 DESCRIPTION + +libmemcached(3) allows you to store a pointer to a user specific data inside +the memcached_st structure. + +memcached_set_user_data() is used to set the user specific data in the +memcached_st structure. + +memcached_get_user_data() is used to retrieve the user specific data in +the memcached_st structure. + +=head1 RETURN + +memcached_set_user_data() returns the previous value of the user specific +data. + +memcached_get_user_data() returns the current value uf the user specific +data. + +=head1 HOME + +To find out more information please check: +L + +=head1 AUTHOR + +Trond Norbye, Etrond.norbye@sun.comE + +=head1 SEE ALSO + +memcached(1) libmemcached(3) + +=cut + diff --git a/libmemcached/Makefile.am b/libmemcached/Makefile.am index be314700..1e3224c0 100644 --- a/libmemcached/Makefile.am +++ b/libmemcached/Makefile.am @@ -9,6 +9,7 @@ DTRACEFLAGS = @DTRACEFLAGS@ noinst_HEADERS = libmemcached_probes.h \ libmemcached_config.h \ memcached_io.h \ + memcached_internal.h \ common.h \ memcached/protocol_binary.h @DTRACE_HEADER@ @@ -61,7 +62,8 @@ libmemcached_la_SOURCES = crc.c \ memcached_verbosity.c \ memcached_version.c \ murmur_hash.c \ - jenkins_hash.c + jenkins_hash.c \ + memcached_allocators.c if INCLUDE_HSIEH_SRC libmemcached_la_SOURCES += hsieh_hash.c diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index b880d833..4e8ec062 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -20,6 +20,9 @@ memcached_st *memcached_create(memcached_st *ptr) { memset(ptr, 0, sizeof(memcached_st)); } + + memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL); + result_ptr= memcached_result_create(ptr, &ptr->result); WATCHPOINT_ASSERT(result_ptr); ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT; @@ -45,20 +48,10 @@ void memcached_free(memcached_st *ptr) ptr->on_cleanup(ptr); if (ptr->continuum) - { - if (ptr->call_free) - ptr->call_free(ptr, ptr->continuum); - else - free(ptr->continuum); - } + ptr->call_free(ptr, ptr->continuum); if (ptr->is_allocated) - { - if (ptr->call_free) - ptr->call_free(ptr, ptr); - else - free(ptr); - } + ptr->call_free(ptr, ptr); else memset(ptr, 0, sizeof(memcached_st)); } @@ -105,6 +98,7 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) new_clone->call_free= source->call_free; new_clone->call_malloc= source->call_malloc; new_clone->call_realloc= source->call_realloc; + new_clone->call_calloc= source->call_calloc; new_clone->get_key_failure= source->get_key_failure; new_clone->delete_trigger= source->delete_trigger; new_clone->server_failure_limit= source->server_failure_limit; @@ -143,3 +137,14 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) return new_clone; } +void *memcached_get_user_data(memcached_st *ptr) +{ + return ptr->user_data; +} + +void *memcached_set_user_data(memcached_st *ptr, void *data) +{ + void *ret= ptr->user_data; + ptr->user_data= data; + return ret; +} diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 47cc4c56..23f5d99a 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -116,6 +116,7 @@ struct memcached_st { memcached_free_function call_free; memcached_malloc_function call_malloc; memcached_realloc_function call_realloc; + memcached_calloc_function call_calloc; memcached_trigger_key get_key_failure; memcached_trigger_delete_key delete_trigger; char prefix_key[MEMCACHED_PREFIX_KEY_MAX_SIZE]; @@ -231,10 +232,29 @@ void *memcached_callback_get(memcached_st *ptr, memcached_return memcached_dump(memcached_st *ptr, memcached_dump_func *function, void *context, uint32_t number_of_callbacks); +memcached_return memcached_set_memory_allocators(memcached_st *ptr, + memcached_malloc_function mem_malloc, + memcached_free_function mem_free, + memcached_realloc_function mem_realloc, + memcached_calloc_function mem_calloc); + +void memcached_get_memory_allocators(memcached_st *ptr, + memcached_malloc_function *mem_malloc, + memcached_free_function *mem_free, + memcached_realloc_function *mem_realloc, + memcached_calloc_function *mem_calloc); + +void *memcached_get_user_data(memcached_st *ptr); +void *memcached_set_user_data(memcached_st *ptr, void *data); + #ifdef __cplusplus } #endif #include +#ifdef MEMCACHED_INTERNAL +#include +#endif + #endif /* __MEMCACHED_H__ */ diff --git a/libmemcached/memcached_allocators.c b/libmemcached/memcached_allocators.c new file mode 100644 index 00000000..3192f9e9 --- /dev/null +++ b/libmemcached/memcached_allocators.c @@ -0,0 +1,72 @@ +#include "common.h" + +void libmemcached_free(memcached_st *ptr __attribute__((unused)), void *mem) +{ + free(mem); +} + +void *libmemcached_malloc(memcached_st *ptr __attribute__((unused)), + size_t size) +{ + return malloc(size); +} + +void *libmemcached_realloc(memcached_st *ptr __attribute__((unused)), + void *mem, size_t size) +{ + return realloc(mem, size); +} + +void *libmemcached_calloc(memcached_st *ptr __attribute__((unused)), + size_t nelem, size_t size) +{ + if (ptr->call_malloc != libmemcached_malloc) + { + void *ret = libmemcached_malloc(ptr, nelem * size); + if (ret != NULL) + memset(ret, 0, nelem * size); + + return ret; + } + + return calloc(nelem, size); +} + +memcached_return memcached_set_memory_allocators(memcached_st *ptr, + memcached_malloc_function mem_malloc, + memcached_free_function mem_free, + memcached_realloc_function mem_realloc, + memcached_calloc_function mem_calloc) +{ + /* All should be set, or none should be set */ + if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL) + { + ptr->call_malloc= libmemcached_malloc; + ptr->call_free= libmemcached_free; + ptr->call_realloc= libmemcached_realloc; + ptr->call_calloc= libmemcached_calloc; + } + else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL) + return MEMCACHED_FAILURE; + else + { + ptr->call_malloc= mem_malloc; + ptr->call_free= mem_free; + ptr->call_realloc= mem_realloc; + ptr->call_calloc= mem_calloc; + } + + return MEMCACHED_SUCCESS; +} + +void memcached_get_memory_allocators(memcached_st *ptr, + memcached_malloc_function *mem_malloc, + memcached_free_function *mem_free, + memcached_realloc_function *mem_realloc, + memcached_calloc_function *mem_calloc) +{ + *mem_malloc= ptr->call_malloc; + *mem_free= ptr->call_free; + *mem_realloc= ptr->call_realloc; + *mem_calloc= ptr->call_calloc; +} diff --git a/libmemcached/memcached_analyze.c b/libmemcached/memcached_analyze.c index 583d828f..e2c17fda 100644 --- a/libmemcached/memcached_analyze.c +++ b/libmemcached/memcached_analyze.c @@ -70,8 +70,9 @@ memcached_analysis_st *memcached_analyze(memcached_st *memc, *error= MEMCACHED_SUCCESS; server_count= memcached_server_count(memc); - result= (memcached_analysis_st*)calloc(1, sizeof(memcached_analysis_st) - * (memc->number_of_hosts)); + result= (memcached_analysis_st*)calloc(memc->number_of_hosts, + sizeof(memcached_analysis_st)); + if (!result) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index 88256aed..3e8b122b 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -68,7 +68,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers) servers[x].address_info= NULL; } - if (ptr && ptr->call_free) + if (ptr) ptr->call_free(ptr, servers); else free(servers); @@ -155,12 +155,8 @@ memcached_return update_continuum(memcached_st *ptr) { memcached_continuum_item_st *new_ptr; - if (ptr->call_realloc) - new_ptr= (memcached_continuum_item_st *)ptr->call_realloc(ptr, ptr->continuum, - sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server); - else - new_ptr= (memcached_continuum_item_st *)realloc(ptr->continuum, - sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server); + new_ptr= ptr->call_realloc(ptr, ptr->continuum, + sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server); if (new_ptr == 0) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -265,14 +261,8 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l return MEMCACHED_SUCCESS; count= list[0].count; - if (ptr->call_realloc) - new_host_list= - (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts, - sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); - else - new_host_list= - (memcached_server_st *)realloc(ptr->hosts, - sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); + new_host_list= ptr->call_realloc(ptr, ptr->hosts, + sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); if (!new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -367,12 +357,9 @@ static memcached_return server_add(memcached_st *ptr, const char *hostname, || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) ) return MEMCACHED_INVALID_HOST_PROTOCOL; - if (ptr->call_realloc) - new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts, - sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); - else - new_host_list= (memcached_server_st *)realloc(ptr->hosts, - sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); + new_host_list= ptr->call_realloc(ptr, ptr->hosts, + sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); + if (new_host_list == NULL) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; diff --git a/libmemcached/memcached_internal.h b/libmemcached/memcached_internal.h new file mode 100644 index 00000000..cda67a20 --- /dev/null +++ b/libmemcached/memcached_internal.h @@ -0,0 +1,25 @@ +/* + * Summary: Internal functions used by the library. Not for public use! + * Copy: See Copyright for the status of this software. + * + * Author: Trond Norbye + */ + +#if !defined(MEMCACHED_INTERNAL_H) && defined(MEMCACHED_INTERNAL) +#define MEMCACHED_INTERNAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +void libmemcached_free(memcached_st *ptr __attribute__((unused)), void *mem); +void *libmemcached_malloc(memcached_st *ptr __attribute__((unused)), + const size_t size); +void *libmemcached_realloc(memcached_st *ptr __attribute__((unused)), + void *mem, const size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* MEMCACHED_INTERNAL_H */ diff --git a/libmemcached/memcached_result.c b/libmemcached/memcached_result.c index fac3b8c5..1462b89c 100644 --- a/libmemcached/memcached_result.c +++ b/libmemcached/memcached_result.c @@ -14,10 +14,7 @@ memcached_result_st *memcached_result_create(memcached_st *memc, memset(ptr, 0, sizeof(memcached_result_st)); else { - if (memc->call_malloc) - ptr= (memcached_result_st *)memc->call_malloc(memc, sizeof(memcached_result_st)); - else - ptr= (memcached_result_st *)calloc(1, sizeof(memcached_result_st)); + ptr= memc->call_malloc(memc, sizeof(memcached_result_st)); if (ptr == NULL) return NULL; diff --git a/libmemcached/memcached_server.c b/libmemcached/memcached_server.c index 7fff6cdb..04364f3a 100644 --- a/libmemcached/memcached_server.c +++ b/libmemcached/memcached_server.c @@ -60,12 +60,7 @@ void memcached_server_free(memcached_server_st *ptr) } if (ptr->is_allocated) - { - if (ptr->root && ptr->root->call_free) - ptr->root->call_free(ptr->root, ptr); - else - free(ptr); - } + ptr->root->call_free(ptr->root, ptr); else memset(ptr, 0, sizeof(memcached_server_st)); } diff --git a/libmemcached/memcached_stats.c b/libmemcached/memcached_stats.c index d7e3b8dd..d52f781a 100644 --- a/libmemcached/memcached_stats.c +++ b/libmemcached/memcached_stats.c @@ -216,10 +216,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, return NULL; } - if (ptr->call_malloc) - ret= ptr->call_malloc(ptr, length + 1); - else - ret= malloc(length + 1); + ret= ptr->call_malloc(ptr, length + 1); memcpy(ret, buffer, length); ret[length]= '\0'; @@ -369,10 +366,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur return NULL; } - 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 *)calloc(1, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); + stats= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(memcached_stat_st)); if (!stats) { @@ -428,10 +422,7 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __att char **list; size_t length= sizeof(memcached_stat_keys); - if (ptr->call_malloc) - list= (char **)ptr->call_malloc(ptr, length); - else - list= (char **)calloc(1, length); + list= ptr->call_malloc(ptr, length); if (!list) { @@ -454,7 +445,7 @@ void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat) return; } - if (ptr && ptr->call_free) + if (ptr) ptr->call_free(ptr, stat); else free(stat); diff --git a/libmemcached/memcached_string.c b/libmemcached/memcached_string.c index 3bac225a..c190997b 100644 --- a/libmemcached/memcached_string.c +++ b/libmemcached/memcached_string.c @@ -18,10 +18,7 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need if (new_size < need) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - if (string->root->call_realloc) - new_value= (char *)string->root->call_realloc(string->root, string->string, new_size); - else - new_value= (char *)realloc(string->string, new_size); + new_value= string->root->call_realloc(string->root, string->string, new_size); if (new_value == NULL) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -44,10 +41,7 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string memset(string, 0, sizeof(memcached_string_st)); else { - if (ptr->call_malloc) - string= (memcached_string_st *)ptr->call_malloc(ptr, sizeof(memcached_string_st)); - else - string= (memcached_string_st *)calloc(1, sizeof(memcached_string_st)); + string= ptr->call_calloc(ptr, 1, sizeof(memcached_string_st)); if (string == NULL) return NULL; @@ -59,11 +53,7 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string rc= memcached_string_check(string, initial_size); if (rc != MEMCACHED_SUCCESS) { - if (ptr->call_free) - ptr->call_free(ptr, string); - else - free(string); - + ptr->call_free(ptr, string); return NULL; } @@ -115,10 +105,7 @@ char *memcached_string_c_copy(memcached_string_st *string) if (memcached_string_length(string) == 0) return NULL; - if (string->root->call_malloc) - c_ptr= (char *)string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)); - else - c_ptr= (char *)malloc((memcached_string_length(string)+1) * sizeof(char)); + c_ptr= string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)); if (c_ptr == NULL) return NULL; @@ -142,20 +129,10 @@ void memcached_string_free(memcached_string_st *ptr) return; if (ptr->string) - { - if (ptr->root->call_free) - ptr->root->call_free(ptr->root, ptr->string); - else - free(ptr->string); - } + ptr->root->call_free(ptr->root, ptr->string); if (ptr->is_allocated) - { - if (ptr->root->call_free) - ptr->root->call_free(ptr->root, ptr); - else - free(ptr); - } + ptr->root->call_free(ptr->root, ptr); else memset(ptr, 0, sizeof(memcached_string_st)); } diff --git a/libmemcached/memcached_types.h b/libmemcached/memcached_types.h index 538ab4bf..d7bb470a 100644 --- a/libmemcached/memcached_types.h +++ b/libmemcached/memcached_types.h @@ -25,6 +25,7 @@ typedef memcached_return (*memcached_cleanup_func)(memcached_st *ptr); typedef void (*memcached_free_function)(memcached_st *ptr, void *mem); typedef void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size); typedef void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, const size_t size); +typedef void *(*memcached_calloc_function)(memcached_st *ptr, size_t nelem, const size_t elsize); typedef memcached_return (*memcached_execute_function)(memcached_st *ptr, memcached_result_st *result, void *context); typedef memcached_return (*memcached_server_function)(memcached_st *ptr, memcached_server_st *server, void *context); typedef memcached_return (*memcached_trigger_key)(memcached_st *ptr, diff --git a/m4/byteorder.m4 b/m4/byteorder.m4 index b6bd84a8..7dd1a41d 100644 --- a/m4/byteorder.m4 +++ b/m4/byteorder.m4 @@ -16,7 +16,7 @@ AC_DEFUN([DETECT_BYTEORDER], ]) AC_MSG_RESULT([$have_htoll]) - AM_CONDITIONAL([BUILD_BYTEORDER],[test "x$have_htoll" == "xno"]) + AM_CONDITIONAL([BUILD_BYTEORDER],[test "x$have_htoll" = "xno"]) AC_MSG_CHECKING([byteorder]) have_htoll="no" AC_RUN_IFELSE([ diff --git a/m4/memcached.m4 b/m4/memcached.m4 index c0e457e7..0916bb69 100644 --- a/m4/memcached.m4 +++ b/m4/memcached.m4 @@ -7,14 +7,14 @@ AC_ARG_WITH(memcached, MEMC_BINARY="$withval" fi - if test x$withval == xyes + if test x$withval = xyes then MEMC_BINARY=memcached fi # just ignore the user if --without-memcached is passed.. it is # only used by make test - if test x$withval == xno + if test x$withval = xno then MEMC_BINARY=memcached fi @@ -23,7 +23,7 @@ AC_ARG_WITH(memcached, AC_PATH_PROG([MEMC_BINARY], [memcached], "no", [$PATH]) ]) -if test x$MEMC_BINARY == "xno" +if test x$MEMC_BINARY = "xno" then AC_MSG_ERROR(["could not find memcached binary"]) fi diff --git a/m4/pod2man.m4 b/m4/pod2man.m4 index 991942e8..8dde96e7 100644 --- a/m4/pod2man.m4 +++ b/m4/pod2man.m4 @@ -1,5 +1,5 @@ AC_PATH_PROG([POD2MAN], [pod2man], "no", [$PATH:/usr/bin:/usr/local/bin:/usr/perl5/bin]) -if test "x$POD2MAN" == "xno"; then +if test "x$POD2MAN" = "xno"; then AC_MSG_ERROR(["Could not find pod2man anywhere in path"]) fi AC_SUBST(POD2MAN) diff --git a/tests/function.c b/tests/function.c index 3113fb1f..7b13b1b6 100644 --- a/tests/function.c +++ b/tests/function.c @@ -215,6 +215,7 @@ static test_return clone_test(memcached_st *memc) assert(clone->call_free == memc->call_free); assert(clone->call_malloc == memc->call_malloc); assert(clone->call_realloc == memc->call_realloc); + assert(clone->call_calloc == memc->call_calloc); assert(clone->connect_timeout == memc->connect_timeout); assert(clone->delete_trigger == memc->delete_trigger); assert(clone->distribution == memc->distribution); @@ -263,6 +264,16 @@ static test_return clone_test(memcached_st *memc) return 0; } +static test_return userdata_test(memcached_st *memc) +{ + void* foo; + assert(memcached_set_user_data(memc, foo) == NULL); + assert(memcached_get_user_data(memc) == foo); + assert(memcached_set_user_data(memc, NULL) == foo); + + return TEST_SUCCESS; +} + static test_return connection_test(memcached_st *memc) { memcached_return rc; @@ -3065,7 +3076,11 @@ 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 calloc(1, size); + void *ret= malloc(size); + if (ret != NULL) + memset(ret, 0xff, size); + + return ret; } static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size) @@ -3073,6 +3088,11 @@ static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, co return realloc(mem, size); } +static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size) +{ + return calloc(nelem, size); +} + static memcached_return set_prefix(memcached_st *memc) { memcached_return rc; @@ -3140,7 +3160,7 @@ static memcached_return set_prefix(memcached_st *memc) return MEMCACHED_SUCCESS; } -static memcached_return set_memory_alloc(memcached_st *memc) +static memcached_return deprecated_set_memory_alloc(memcached_st *memc) { void *test_ptr= NULL; void *cb_ptr= NULL; @@ -3182,6 +3202,30 @@ static memcached_return set_memory_alloc(memcached_st *memc) assert(rc == MEMCACHED_SUCCESS); assert(test_ptr == cb_ptr); } + return MEMCACHED_SUCCESS; +} + +static memcached_return set_memory_alloc(memcached_st *memc) +{ + memcached_return rc; + rc= memcached_set_memory_allocators(memc, NULL, my_free, + my_realloc, my_calloc); + assert(rc == MEMCACHED_FAILURE); + + rc= memcached_set_memory_allocators(memc, my_malloc, my_free, + my_realloc, my_calloc); + + memcached_malloc_function mem_malloc; + memcached_free_function mem_free; + memcached_realloc_function mem_realloc; + memcached_calloc_function mem_calloc; + memcached_get_memory_allocators(memc, &mem_malloc, &mem_free, + &mem_realloc, &mem_calloc); + + assert(mem_malloc == my_malloc); + assert(mem_realloc == my_realloc); + assert(mem_calloc == my_calloc); + assert(mem_free == my_free); return MEMCACHED_SUCCESS; } @@ -4288,6 +4332,7 @@ test_st tests[] ={ {"connection_test", 0, connection_test}, {"callback_test", 0, callback_test}, {"behavior_test", 0, behavior_test}, + {"userdata_test", 0, userdata_test}, {"error", 0, error_test }, {"set", 0, set_test }, {"set2", 0, set_test2 }, @@ -4484,6 +4529,7 @@ collection_st collection[] ={ {"poll_timeout", poll_timeout, 0, tests}, {"gets", enable_cas, 0, tests}, {"consistent", enable_consistent, 0, tests}, + {"deprecated_memory_allocators", deprecated_set_memory_alloc, 0, tests}, {"memory_allocators", set_memory_alloc, 0, tests}, {"prefix", set_prefix, 0, tests}, {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},