3 void _libmemcached_free(const memcached_st
*ptr
, void *mem
, void *context
)
10 void *_libmemcached_malloc(const memcached_st
*ptr
, size_t size
, void *context
)
17 void *_libmemcached_realloc(const memcached_st
*ptr
, void *mem
, size_t size
, void *context
)
21 return realloc(mem
, size
);
24 void *_libmemcached_calloc(const memcached_st
*ptr
, size_t nelem
, size_t size
, void *context
)
26 if (ptr
->allocators
.malloc
!= _libmemcached_malloc
)
28 void *ret
= _libmemcached_malloc(ptr
, nelem
* size
, context
);
30 memset(ret
, 0, nelem
* size
);
35 return calloc(nelem
, size
);
38 struct memcached_allocator_t
memcached_allocators_return_default(void)
40 static struct memcached_allocator_t global_default_allocator
= { _libmemcached_calloc
, _libmemcached_free
, _libmemcached_malloc
, _libmemcached_realloc
, 0 };
41 return global_default_allocator
;
44 memcached_return_t
memcached_set_memory_allocators(memcached_st
*ptr
,
45 memcached_malloc_fn mem_malloc
,
46 memcached_free_fn mem_free
,
47 memcached_realloc_fn mem_realloc
,
48 memcached_calloc_fn mem_calloc
,
51 /* All should be set, or none should be set */
52 if (mem_malloc
== NULL
&& mem_free
== NULL
&& mem_realloc
== NULL
&& mem_calloc
== NULL
)
54 ptr
->allocators
= memcached_allocators_return_default();
56 else if (mem_malloc
== NULL
|| mem_free
== NULL
|| mem_realloc
== NULL
|| mem_calloc
== NULL
)
58 return MEMCACHED_FAILURE
;
62 ptr
->allocators
.malloc
= mem_malloc
;
63 ptr
->allocators
.free
= mem_free
;
64 ptr
->allocators
.realloc
= mem_realloc
;
65 ptr
->allocators
.calloc
= mem_calloc
;
66 ptr
->allocators
.context
= context
;
69 return MEMCACHED_SUCCESS
;
72 void *memcached_get_memory_allocators_context(const memcached_st
*ptr
)
74 return ptr
->allocators
.context
;
77 void memcached_get_memory_allocators(const memcached_st
*ptr
,
78 memcached_malloc_fn
*mem_malloc
,
79 memcached_free_fn
*mem_free
,
80 memcached_realloc_fn
*mem_realloc
,
81 memcached_calloc_fn
*mem_calloc
)
83 *mem_malloc
= ptr
->allocators
.malloc
;
84 *mem_free
= ptr
->allocators
.free
;
85 *mem_realloc
= ptr
->allocators
.realloc
;
86 *mem_calloc
= ptr
->allocators
.calloc
;