X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fallocators.c;h=fe7b296c63873ab23c593f2fb6b5d3bca8a78199;hb=a8efa12d2d90c3777f3c33347fe002884be1185d;hp=767371cb72a66dabd20627ba77bd49251927e5bb;hpb=7c7750f02368b570353ea109f23a0ea26d226e02;p=awesomized%2Flibmemcached diff --git a/libmemcached/allocators.c b/libmemcached/allocators.c index 767371cb..fe7b296c 100644 --- a/libmemcached/allocators.c +++ b/libmemcached/allocators.c @@ -1,28 +1,31 @@ #include "common.h" -void libmemcached_free(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(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(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(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,41 +35,60 @@ void *libmemcached_calloc(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) + { return MEMCACHED_FAILURE; + } 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(memcached_st *ptr, +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; }