# Use custom allocators for embedded usage

Manage memory allocator functions

## SYNOPSIS

#include <libmemcached/memcached.h>

  Compile and link with -lmemcached

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, void *context)
  Parameters:

    - ptr -- pointer to an initialized `memcached_st` struct

    - mem_malloc -- pointer to a `memcached_malloc_fn` callback

    - mem_free -- pointer to a `memcached_free_fn` callback

    - mem_realloc -- pointer to a `memcached_realloc_fn` callback

    - mem_calloc -- pointer to a `memcached_calloc_fn` callback

    - context -- pointer to a user supplied context

  Returns:

    `memcached_return_t` indicating success

void memcached_get_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)
  Parameters:

    - ptr -- pointer to an initialized `memcached_st` struct

    - mem_malloc -- pointer to store the address of the `memcached_malloc_fn` callback

    - mem_free -- pointer to store the address of the `memcached_free_fn` callback

    - mem_realloc -- pointer to store the address of the `memcached_realloc_fn` callback

    - mem_calloc -- pointer to store the address of the `memcached_calloc_fn` callback

void *memcached_get_memory_allocators_context(const memcached_st *ptr)
  Parameters:

    ptr -- pointer to an initialized `memcached_st` struct

  Returns:

    pointer to the user supplied context

typedef void *(*memcached_malloc_fn)(memcached_st *ptr, const size_t size, void *context)
  Param ptr:

    pointer to an initialized `memcached_st` struct

  Param size:

    the number of bytes to allocate

  Param context:

    pointer to the user supplied context

  Returns:

    pointer to at least `size` bytes of allocated memory

typedef void *(*memcached_realloc_fn)(memcached_st *ptr, void *mem, const size_t size, void *context)
  Param ptr:

    pointer to an initialized `memcached_st` struct

  Param mem:

    pointer to previously allocated memory

  Param size:

    the number of bytes to allocate

  Param context:

    pointer to the user supplied context

  Returns:

    pointer to at least `size` bytes of allocated memory

typedef void (*memcached_free_fn)(memcached_st *ptr, void *mem, void *context)
  Param ptr:

    pointer to an initialized `memcached_st` struct

  Param mem:

    pointer to previously allocated memory

  Param context:

    pointer to the user supplied context

typedef void *(*memcached_calloc_fn)(memcached_st *ptr, size_t nelem, const size_t elsize, void *context)
  Param ptr:

    pointer to an initialized `memcached_st` struct

  Param nelem:

    number of elements to allocate

  Param elsize:

    the number of bytes to allocate per element

  Param context:

    pointer to the user supplied context

  Returns:

    pointer to at least `elsize` * `nelem` bytes of allocated and zeroed memory

## DESCRIPTION

`libmemcached` allows you to specify your own memory allocators, optimized for your application. This enables libmemcached to be used inside of applications that have their own malloc implementation.

`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 memcached 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, the is passed as const and you will need to clone it in order to make use of any operation which would modify it.

## NOTES

In version 0.38 all functions were modified to have a context void pointer passed to them. This was so that custom allocators could have their own space for memory.

## RETURN VALUE

`memcached_set_memory_allocators()` returns `MEMCACHED_SUCCESS` upon success, and `MEMCACHED_FAILURE` if you don't pass a complete set of function pointers.

## SEE ALSO

- memcached(1)

- C/C++ Client Library for memcached

- Converting Error Codes to Messages