From 6bd950c979c31a5cf84c7bdaaad9bccc789a0eb1 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 18 Jan 2010 14:58:20 -0800 Subject: [PATCH] New allocator interface. --- ChangeLog | 3 ++ docs/Makefile.am | 3 +- docs/memcached_memory_allocators.pod | 38 +++++++++++++----- libmemcached/allocators.c | 58 +++++++++++++++++++--------- libmemcached/allocators.h | 17 +++++--- libmemcached/common.h | 21 ++++++++++ libmemcached/get.c | 12 +++--- libmemcached/hosts.c | 12 +++--- libmemcached/memcached.c | 16 ++++---- libmemcached/memcached.h | 11 ++++-- libmemcached/response.c | 6 +-- libmemcached/result.c | 12 ++---- libmemcached/server.c | 4 +- libmemcached/stats.c | 18 +++++---- libmemcached/string.c | 12 +++--- libmemcached/types.h | 8 ++-- tests/mem_functions.c | 27 ++++++++----- 17 files changed, 178 insertions(+), 100 deletions(-) diff --git a/ChangeLog b/ChangeLog index e4c731b9..1f65d5c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 0.38 + * Modified memcached_set_memory_allocators() so that it requires a context + pointer. + * memcached_clone() now runs 5 times faster. * Functions used for callbacks are now given const memcached_st. diff --git a/docs/Makefile.am b/docs/Makefile.am index 03ba2eb6..b1d3e6ee 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -79,7 +79,8 @@ BUILT_SOURCES += ${GET_PAGES} MEMORY_ALLOCATORS_PAGES= \ memcached_get_memory_allocators.pop \ - memcached_set_memory_allocators.pop + memcached_set_memory_allocators.pop \ + memcached_set_memory_allocators_context.pop BUILT_SOURCES += ${MEMORY_ALLOCATORS_PAGES} POOL_PAGES= \ diff --git a/docs/memcached_memory_allocators.pod b/docs/memcached_memory_allocators.pod index cafa70c2..6b9ddf9f 100644 --- a/docs/memcached_memory_allocators.pod +++ b/docs/memcached_memory_allocators.pod @@ -1,6 +1,6 @@ =head1 NAME -memcached_set_memory_allocators, memcached_get_memory_allocators - Manage memory allocator functions +memcached_set_memory_allocators, memcached_get_memory_allocators, memcached_set_memory_allocators_context - Manage memory allocator functions =head1 LIBRARY @@ -15,7 +15,8 @@ C Client Library for memcached (libmemcached, -lmemcached) memcached_malloc_fn mem_malloc, memcached_free_fn mem_free, memcached_realloc_fn mem_realloc, - memcached_calloc_fn mem_calloc); + memcached_calloc_fn mem_calloc, + void *context); void memcached_get_memory_allocators (memcached_st *ptr, @@ -24,19 +25,27 @@ C Client Library for memcached (libmemcached, -lmemcached) memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc); + void * + memcached_get_memory_allocators_context(const memcached_st *ptr); + void * - (*memcached_malloc_fn) (memcached_st *ptr, const size_t size); + (*memcached_malloc_fn) (memcached_st *ptr, const size_t size, + void *context); void * (*memcached_realloc_fn) (memcached_st *ptr, void *mem, - const size_t size); + const size_t size, + void *context); void - (*memcached_free_fn) (memcached_st *ptr, void *mem); + (*memcached_free_fn) (memcached_st *ptr, void *mem, + void *context); void * - (*memcached_calloc_fn) (memcached_st *ptr, size_t nelem, - const size_t elsize); + (*memcached_calloc_fn) (memcached_st *ptr, + size_t nelem, + const size_t elsize, + void *context); =head1 DESCRIPTION @@ -54,10 +63,18 @@ 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. +memcached_get_memory_allocators_context() returns the void * that was +passed in during the call to memcached_set_memory_allocators(). + 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. +memcached structure, the is passed as const and you will need to clone +it in order to make use of any operation which would modify it. + +=head1 NOTES + +In version 0.38 all functions were modified to have a context void pointer +passed to them. This was so that customer allocators could have their +own space for memory. =head1 RETURN @@ -72,6 +89,7 @@ L =head1 AUTHOR Trond Norbye, Etrond.norbye@gmail.comE +Brian Aker, Ebrian@tangent.orf =head1 SEE ALSO diff --git a/libmemcached/allocators.c b/libmemcached/allocators.c index 14502867..fe7b296c 100644 --- a/libmemcached/allocators.c +++ b/libmemcached/allocators.c @@ -1,28 +1,31 @@ #include "common.h" -void libmemcached_free(const memcached_st *ptr, void *mem) +void _libmemcached_free(const memcached_st *ptr, void *mem, void *context) { (void) ptr; + (void) context; free(mem); } -void *libmemcached_malloc(const memcached_st *ptr, size_t size) +void *_libmemcached_malloc(const memcached_st *ptr, size_t size, void *context) { (void) ptr; + (void) context; return malloc(size); } -void *libmemcached_realloc(const memcached_st *ptr, void *mem, size_t size) +void *_libmemcached_realloc(const memcached_st *ptr, void *mem, size_t size, void *context) { (void) ptr; + (void) context; return realloc(mem, size); } -void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size) +void *_libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size, void *context) { - if (ptr->call_malloc != libmemcached_malloc) + if (ptr->allocators.malloc != _libmemcached_malloc) { - void *ret = libmemcached_malloc(ptr, nelem * size); + void *ret = _libmemcached_malloc(ptr, nelem * size, context); if (ret != NULL) memset(ret, 0, nelem * size); @@ -32,19 +35,30 @@ void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size) return calloc(nelem, size); } +static const struct _allocators_st global_default_allocator= { + .calloc= _libmemcached_calloc, + .context= NULL, + .free= _libmemcached_free, + .malloc= _libmemcached_malloc, + .realloc= _libmemcached_realloc +}; + +struct _allocators_st memcached_allocators_return_default(void) +{ + return global_default_allocator; +} + memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, memcached_malloc_fn mem_malloc, memcached_free_fn mem_free, memcached_realloc_fn mem_realloc, - memcached_calloc_fn mem_calloc) + memcached_calloc_fn mem_calloc, + void *context) { /* 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; + ptr->allocators= memcached_allocators_return_default(); } else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL) { @@ -52,23 +66,29 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, } else { - ptr->call_malloc= mem_malloc; - ptr->call_free= mem_free; - ptr->call_realloc= mem_realloc; - ptr->call_calloc= mem_calloc; + ptr->allocators.malloc= mem_malloc; + ptr->allocators.free= mem_free; + ptr->allocators.realloc= mem_realloc; + ptr->allocators.calloc= mem_calloc; + ptr->allocators.context= context; } return MEMCACHED_SUCCESS; } +void *memcached_get_memory_allocators_context(const memcached_st *ptr) +{ + return ptr->allocators.context; +} + void memcached_get_memory_allocators(const memcached_st *ptr, memcached_malloc_fn *mem_malloc, memcached_free_fn *mem_free, memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc) { - *mem_malloc= ptr->call_malloc; - *mem_free= ptr->call_free; - *mem_realloc= ptr->call_realloc; - *mem_calloc= ptr->call_calloc; + *mem_malloc= ptr->allocators.malloc; + *mem_free= ptr->allocators.free; + *mem_realloc= ptr->allocators.realloc; + *mem_calloc= ptr->allocators.calloc; } diff --git a/libmemcached/allocators.h b/libmemcached/allocators.h index 30ed3821..c87cd2ec 100644 --- a/libmemcached/allocators.h +++ b/libmemcached/allocators.h @@ -21,7 +21,8 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *ptr, memcached_malloc_fn mem_malloc, memcached_free_fn mem_free, memcached_realloc_fn mem_realloc, - memcached_calloc_fn mem_calloc); + memcached_calloc_fn mem_calloc, + void *context); LIBMEMCACHED_API void memcached_get_memory_allocators(const memcached_st *ptr, @@ -30,17 +31,23 @@ void memcached_get_memory_allocators(const memcached_st *ptr, memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc); +LIBMEMCACHED_API +void *memcached_get_memory_allocators_context(const memcached_st *ptr); + +LIBMEMCACHED_LOCAL +void _libmemcached_free(const memcached_st *ptr, void *mem, void *context); + LIBMEMCACHED_LOCAL -void libmemcached_free(const memcached_st *ptr, void *mem); +void *_libmemcached_malloc(const memcached_st *ptr, const size_t size, void *context); LIBMEMCACHED_LOCAL -void *libmemcached_malloc(const memcached_st *ptr, const size_t size); +void *_libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size, void *context); LIBMEMCACHED_LOCAL -void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size); +void *_libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size, void *context); LIBMEMCACHED_LOCAL -void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size); +struct _allocators_st memcached_allocators_return_default(void); #ifdef __cplusplus diff --git a/libmemcached/common.h b/libmemcached/common.h index b4a9e8b4..66dd43bc 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -213,4 +213,25 @@ static inline memcached_ternary_t test_cork(memcached_server_st *ptr, int enable #endif } +static inline void libmemcached_free(const memcached_st *ptr, void *mem) +{ + ptr->allocators.free(ptr, mem, ptr->allocators.context); +} + +static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size) +{ + return ptr->allocators.malloc(ptr, size, ptr->allocators.context); +} + +static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size) +{ + return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context); +} + +static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size) +{ + return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context); +} + + #endif /* LIBMEMCACHED_COMMON_H */ diff --git a/libmemcached/get.c b/libmemcached/get.c index 1ccd2046..a7d0ebc6 100644 --- a/libmemcached/get.c +++ b/libmemcached/get.c @@ -577,13 +577,13 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, uint32_t* hash; bool* dead_servers; - hash= ptr->call_malloc(ptr, sizeof(uint32_t) * number_of_keys); - dead_servers= ptr->call_calloc(ptr, memcached_server_count(ptr), sizeof(bool)); + hash= libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys); + dead_servers= libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool)); if (hash == NULL || dead_servers == NULL) { - ptr->call_free(ptr, hash); - ptr->call_free(ptr, dead_servers); + libmemcached_free(ptr, hash); + libmemcached_free(ptr, dead_servers); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } @@ -597,8 +597,8 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, rc= replication_binary_mget(ptr, hash, dead_servers, keys, key_length, number_of_keys); - ptr->call_free(ptr, hash); - ptr->call_free(ptr, dead_servers); + libmemcached_free(ptr, hash); + libmemcached_free(ptr, dead_servers); return MEMCACHED_SUCCESS; } diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index 06083741..c6c9eb8e 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -146,8 +146,8 @@ static memcached_return_t update_continuum(memcached_st *ptr) { memcached_continuum_item_st *new_ptr; - new_ptr= ptr->call_realloc(ptr, ptr->continuum, - sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server); + new_ptr= libmemcached_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; @@ -304,8 +304,8 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st return MEMCACHED_SUCCESS; count= memcached_servers_count(list); - new_host_list= ptr->call_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_instance_st) * (count + memcached_server_count(ptr))); + new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr), + sizeof(memcached_server_instance_st) * (count + memcached_server_count(ptr))); if (! new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -411,8 +411,8 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) ) return MEMCACHED_INVALID_HOST_PROTOCOL; - new_host_list= ptr->call_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_instance_st) * (ptr->number_of_hosts + 1)); + new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr), + sizeof(memcached_server_instance_st) * (ptr->number_of_hosts + 1)); if (new_host_list == NULL) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 4547c6ff..c53d958a 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -67,7 +67,8 @@ static inline void _memcached_init(memcached_st *self) self->continuum= NULL; - memcached_set_memory_allocators(self, NULL, NULL, NULL, NULL); + memcached_set_memory_allocators(self, NULL, NULL, NULL, NULL, NULL); + self->allocators= memcached_allocators_return_default(); self->on_clone= NULL; self->on_cleanup= NULL; @@ -130,7 +131,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers) if (ptr) { - ptr->call_free(ptr, servers); + libmemcached_free(ptr, servers); } else { @@ -159,11 +160,11 @@ void memcached_free(memcached_st *ptr) ptr->on_cleanup(ptr); if (ptr->continuum) - ptr->call_free(ptr, ptr->continuum); + libmemcached_free(ptr, ptr->continuum); if (memcached_is_allocated(ptr)) { - ptr->call_free(ptr, ptr); + libmemcached_free(ptr, ptr); } } @@ -206,10 +207,9 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) new_clone->on_clone= source->on_clone; new_clone->on_cleanup= source->on_cleanup; - 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->allocators= source->allocators; + 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; diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 50421441..044b6f0b 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -109,10 +109,13 @@ struct memcached_st { memcached_result_st result; memcached_continuum_item_st *continuum; // Ketama - memcached_free_fn call_free; - memcached_malloc_fn call_malloc; - memcached_realloc_fn call_realloc; - memcached_calloc_fn call_calloc; + struct _allocators_st { + memcached_calloc_fn calloc; + memcached_free_fn free; + memcached_malloc_fn malloc; + memcached_realloc_fn realloc; + void *context; + } allocators; memcached_clone_fn on_clone; memcached_cleanup_fn on_cleanup; diff --git a/libmemcached/response.c b/libmemcached/response.c index 26418ebd..afaed844 100644 --- a/libmemcached/response.c +++ b/libmemcached/response.c @@ -259,9 +259,9 @@ static memcached_return_t textual_read_one_response(memcached_server_instance_st memory in the struct, which is important, for something that rarely should happen? */ - rel_ptr= (char *)ptr->root->call_realloc(ptr->root, - ptr->cached_server_error, - (size_t) (endptr - startptr + 1)); + rel_ptr= (char *)libmemcached_realloc(ptr->root, + ptr->cached_server_error, + (size_t) (endptr - startptr + 1)); if (rel_ptr == NULL) { diff --git a/libmemcached/result.c b/libmemcached/result.c index bca92740..f151160e 100644 --- a/libmemcached/result.c +++ b/libmemcached/result.c @@ -30,7 +30,7 @@ memcached_result_st *memcached_result_create(memcached_st *memc, } else { - ptr= memc->call_calloc(memc, 1, sizeof(memcached_result_st)); + ptr= libmemcached_calloc(memc, 1, sizeof(memcached_result_st)); if (ptr == NULL) return NULL; @@ -65,14 +65,8 @@ void memcached_result_free(memcached_result_st *ptr) if (memcached_is_allocated(ptr)) { - if (ptr->root != NULL) - { - ptr->root->call_free(ptr->root, ptr); - } - else - { - free(ptr); - } + WATCHPOINT_ASSERT(ptr->root); // Without a root, that means that result was not properly initialized. + libmemcached_free(ptr->root, ptr); } else { diff --git a/libmemcached/server.c b/libmemcached/server.c index 516cd2c4..bcc211f6 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -60,7 +60,7 @@ static memcached_server_st *_server_create(memcached_server_st *self, const memc { if (self == NULL) { - self= (memcached_server_st *)memc->call_malloc(memc, sizeof(memcached_server_st)); + self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st)); if (! self) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ @@ -110,7 +110,7 @@ void memcached_server_free(memcached_server_st *self) if (memcached_is_allocated(self)) { - self->root->call_free(self->root, self); + libmemcached_free(self->root, self); } else { diff --git a/libmemcached/stats.c b/libmemcached/stats.c index cf7f4d68..d162277a 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -219,7 +219,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *memc_stat, return NULL; } - ret= ptr->call_malloc(ptr, (size_t) (length + 1)); + ret= libmemcached_malloc(ptr, (size_t) (length + 1)); memcpy(ret, buffer, (size_t) length); ret[length]= '\0'; @@ -367,7 +367,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur return NULL; } - stats= ptr->call_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st)); + stats= libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st)); stats->root= ptr; @@ -435,14 +435,18 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char We make a copy of the keys since at some point in the not so distant future we will add support for "found" keys. */ -char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *memc_stat, +char ** memcached_stat_get_keys(memcached_st *ptr, + memcached_stat_st *memc_stat, memcached_return_t *error) { - (void) memc_stat; char **list; size_t length= sizeof(memcached_stat_keys); - list= ptr->call_malloc(ptr, length); + (void)memc_stat; +#if 0 + list= libmemcached_malloc(memc_stat ? memc_stat->root : ptr, length); +#endif + list= libmemcached_malloc(ptr, length); if (!list) { @@ -467,11 +471,11 @@ void memcached_stat_free(memcached_st *ptr, memcached_stat_st *memc_stat) if (memc_stat->root) { - memc_stat->root->call_free(ptr, memc_stat); + libmemcached_free(memc_stat->root, memc_stat); } else if (ptr) { - ptr->call_free(ptr, memc_stat); + libmemcached_free(ptr, memc_stat); } else { diff --git a/libmemcached/string.c b/libmemcached/string.c index 33b9222a..2b6f0820 100644 --- a/libmemcached/string.c +++ b/libmemcached/string.c @@ -29,7 +29,7 @@ inline static memcached_return_t _string_check(memcached_string_st *string, size if (new_size < need) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - new_value= string->root->call_realloc(string->root, string->string, new_size); + new_value= libmemcached_realloc(string->root, string->string, new_size); if (new_value == NULL) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -64,7 +64,7 @@ memcached_string_st *memcached_string_create(memcached_st *memc, memcached_strin } else { - self= memc->call_malloc(memc, sizeof(memcached_string_st)); + self= libmemcached_malloc(memc, sizeof(memcached_string_st)); if (self == NULL) { @@ -80,7 +80,7 @@ memcached_string_st *memcached_string_create(memcached_st *memc, memcached_strin rc= _string_check(self, initial_size); if (rc != MEMCACHED_SUCCESS) { - memc->call_free(memc, self); + libmemcached_free(memc, self); return NULL; } @@ -134,7 +134,7 @@ char *memcached_string_c_copy(memcached_string_st *string) if (memcached_string_length(string) == 0) return NULL; - c_ptr= string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)); + c_ptr= libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char)); if (c_ptr == NULL) return NULL; @@ -159,12 +159,12 @@ void memcached_string_free(memcached_string_st *ptr) if (ptr->string) { - ptr->root->call_free(ptr->root, ptr->string); + libmemcached_free(ptr->root, ptr->string); } if (memcached_is_allocated(ptr)) { - ptr->root->call_free(ptr->root, ptr); + libmemcached_free(ptr->root, ptr); } else { diff --git a/libmemcached/types.h b/libmemcached/types.h index 710fd28b..28182cd9 100644 --- a/libmemcached/types.h +++ b/libmemcached/types.h @@ -30,10 +30,10 @@ typedef memcached_return_t (*memcached_cleanup_fn)(const memcached_st *ptr); /** Memory allocation functions. */ -typedef void (*memcached_free_fn)(const memcached_st *ptr, void *mem); -typedef void *(*memcached_malloc_fn)(const memcached_st *ptr, const size_t size); -typedef void *(*memcached_realloc_fn)(const memcached_st *ptr, void *mem, const size_t size); -typedef void *(*memcached_calloc_fn)(const memcached_st *ptr, size_t nelem, const size_t elsize); +typedef void (*memcached_free_fn)(const memcached_st *ptr, void *mem, void *context); +typedef void *(*memcached_malloc_fn)(const memcached_st *ptr, const size_t size, void *context); +typedef void *(*memcached_realloc_fn)(const memcached_st *ptr, void *mem, const size_t size, void *context); +typedef void *(*memcached_calloc_fn)(const memcached_st *ptr, size_t nelem, const size_t elsize, void *context); typedef memcached_return_t (*memcached_execute_fn)(const memcached_st *ptr, memcached_result_st *result, void *context); typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, memcached_server_st *server, void *context); diff --git a/tests/mem_functions.c b/tests/mem_functions.c index dbf50797..5460078c 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -230,10 +230,13 @@ static test_return_t clone_test(memcached_st *memc) memc_clone= memcached_clone(NULL, memc); test_true(memc_clone); - test_true(memc_clone->call_free == memc->call_free); - test_true(memc_clone->call_malloc == memc->call_malloc); - test_true(memc_clone->call_realloc == memc->call_realloc); - test_true(memc_clone->call_calloc == memc->call_calloc); + { // Test allocators + test_true(memc_clone->allocators.free == memc->allocators.free); + test_true(memc_clone->allocators.malloc == memc->allocators.malloc); + test_true(memc_clone->allocators.realloc == memc->allocators.realloc); + test_true(memc_clone->allocators.calloc == memc->allocators.calloc); + } + test_true(memc_clone->connect_timeout == memc->connect_timeout); test_true(memc_clone->delete_trigger == memc->delete_trigger); test_true(memc_clone->distribution == memc->distribution); @@ -3616,8 +3619,9 @@ static test_return_t pre_replication_noblock(memcached_st *memc) } -static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem) +static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem, void *context) { + (void) context; #ifdef HARD_MALLOC_TESTS void *real_ptr= (mem == NULL) ? mem : (void*)((caddr_t)mem - 8); free(real_ptr); @@ -3627,8 +3631,9 @@ static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem) } -static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const size_t size) +static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const size_t size, void *context) { + (void)context; #ifdef HARD_MALLOC_TESTS void *ret= malloc(size + 8); if (ret != NULL) @@ -3648,8 +3653,9 @@ static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const si } -static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *mem, const size_t size) +static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *mem, const size_t size, void *context) { + (void)context; #ifdef HARD_MALLOC_TESTS void *real_ptr= (mem == NULL) ? NULL : (void*)((caddr_t)mem - 8); void *nmem= realloc(real_ptr, size + 8); @@ -3667,8 +3673,9 @@ static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *m } -static void *my_calloc(const memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size) +static void *my_calloc(const memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size, void *context) { + (void)context; #ifdef HARD_MALLOC_TESTS void *mem= my_malloc(ptr, nelem * size); if (mem) @@ -3806,11 +3813,11 @@ static test_return_t set_memory_alloc(memcached_st *memc) { memcached_return_t rc; rc= memcached_set_memory_allocators(memc, NULL, my_free, - my_realloc, my_calloc); + my_realloc, my_calloc, NULL); test_true(rc == MEMCACHED_FAILURE); rc= memcached_set_memory_allocators(memc, my_malloc, my_free, - my_realloc, my_calloc); + my_realloc, my_calloc, NULL); memcached_malloc_fn mem_malloc; memcached_free_fn mem_free; -- 2.30.2