From fbe4a2e21817d0c106db22ab83aab053ccc89a05 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 23 May 2011 16:24:07 -0700 Subject: [PATCH] Simply free so that we can not have a bunch of if/else around using custom/not custom allocators. --- ChangeLog | 3 ++- libmemcached/allocators.cc | 3 ++- libmemcached/array.c | 9 +-------- libmemcached/memcached.cc | 5 ++--- libmemcached/memory.h | 30 ++++++++++++++++++++++++++---- libmemcached/server.cc | 18 ++---------------- libmemcached/stats.cc | 11 +++-------- 7 files changed, 38 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2cf2aea1..afbfce1a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ - * Updates to C++ interface + * Updates to C++ interface + * Custom free allocators need to now check for value before calling free. 0.49 Thu Apr 14 08:43:37 PDT 2011 * Fix calls to auto methods so that if value is not passed in nothing bad happens. diff --git a/libmemcached/allocators.cc b/libmemcached/allocators.cc index d0d9e57e..ec491ddb 100644 --- a/libmemcached/allocators.cc +++ b/libmemcached/allocators.cc @@ -39,7 +39,8 @@ void _libmemcached_free(const memcached_st*, void *mem, void*) { - free(mem); + if (mem) + free(mem); } void *_libmemcached_malloc(const memcached_st *, size_t size, void *) diff --git a/libmemcached/array.c b/libmemcached/array.c index 36df81d9..5096b187 100644 --- a/libmemcached/array.c +++ b/libmemcached/array.c @@ -108,14 +108,7 @@ void memcached_array_free(memcached_array_st *array) return; WATCHPOINT_ASSERT(array->root); - if (array && array->root) - { - libmemcached_free(array->root, array); - } - else if (array) - { - free(array); - } + libmemcached_free(array->root, array); } size_t memcached_array_size(memcached_array_st *array) diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index 2bba44a1..21306f2a 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -157,8 +157,7 @@ static void _free(memcached_st *ptr, bool release_st) if (ptr->on_cleanup) ptr->on_cleanup(ptr); - if (ptr->ketama.continuum) - libmemcached_free(ptr, ptr->ketama.continuum); + libmemcached_free(ptr, ptr->ketama.continuum); memcached_array_free(ptr->prefix_key); ptr->prefix_key= NULL; @@ -190,7 +189,7 @@ memcached_st *memcached_create(memcached_st *ptr) { ptr= (memcached_st *)malloc(sizeof(memcached_st)); - if (! ptr) + if (not ptr) { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ } diff --git a/libmemcached/memory.h b/libmemcached/memory.h index 7b30e371..65d06b8c 100644 --- a/libmemcached/memory.h +++ b/libmemcached/memory.h @@ -38,20 +38,42 @@ static inline void libmemcached_free(const memcached_st *self, void *mem) { - self->allocators.free(self, mem, self->allocators.context); + if (self) + { + self->allocators.free(self, mem, self->allocators.context); + } + else if (mem) + { + free(mem); + } } static inline void *libmemcached_malloc(const memcached_st *self, const size_t size) { - return self->allocators.malloc(self, size, self->allocators.context); + if (self) + { + return self->allocators.malloc(self, size, self->allocators.context); + } + + return malloc(size); } static inline void *libmemcached_realloc(const memcached_st *self, void *mem, const size_t size) { - return self->allocators.realloc(self, mem, size, self->allocators.context); + if (self) + { + return self->allocators.realloc(self, mem, size, self->allocators.context); + } + + return realloc(mem, size); } static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size) { - return self->allocators.calloc(self, nelem, size, self->allocators.context); + if (self) + { + return self->allocators.calloc(self, nelem, size, self->allocators.context); + } + + return calloc(nelem, size); } diff --git a/libmemcached/server.cc b/libmemcached/server.cc index cb0f0c2f..72f1b822 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -146,14 +146,7 @@ void memcached_server_free(memcached_server_st *self) if (memcached_is_allocated(self)) { - if (self->root) - { - libmemcached_free(self->root, self); - } - else - { - free(self); - } + libmemcached_free(self->root, self); } else { @@ -300,14 +293,7 @@ void memcached_server_list_free(memcached_server_list_st self) } memcached_st *root= self->root; - if (root) - { - libmemcached_free(root, self); - } - else - { - free(self); - } + libmemcached_free(root, self); } uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count) diff --git a/libmemcached/stats.cc b/libmemcached/stats.cc index 3c269be1..eb290443 100644 --- a/libmemcached/stats.cc +++ b/libmemcached/stats.cc @@ -552,15 +552,10 @@ void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat) if (memc_stat->root) { libmemcached_free(memc_stat->root, memc_stat); + return; } - else if (ptr) - { - libmemcached_free(ptr, memc_stat); - } - else - { - free(memc_stat); - } + + libmemcached_free(ptr, memc_stat); } static memcached_return_t call_stat_fn(memcached_st *ptr, -- 2.30.2