MEMCACHED_SET_MEMORY_ALLOCATORS_CONTEXT(3)

πŸ’– NAME πŸ’–

memcached_set_memory_allocators_context β€” it's from the libmemcached Documentation bestie, and it's all about managing memory allocator functions. yes queen, we're doing malloc now πŸ’…


✨ SYNOPSIS ✨

okay so first things first, you gotta include the header and link the lib:

#include <libmemcached/memcached.h>

Compile and link with -lmemcached


πŸŽ€ Setting your own allocators

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


πŸ¦‹ Getting them back out

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

🧚 And the context specifically

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


πŸ”₯ The callback typedefs, iconic behavior

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 🌈

ngl this is the good part. libmemcached lets you specify your own memory allocators, optimized for your application. the whole point is that libmemcached can live inside apps that already have their own malloc implementation β€” like, it just vibes with your setup instead of fighting it πŸ’–

memcached_set_memory_allocators() is what you use to set the memory allocators used by the memcached instance specified by ptr.

⚠️ okay bestie, important: 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. no picking and choosing, that's the rule 😭

All of the memory allocation functions should behave as specified in the C99 standard. And if you want to reset them back to the defaults? Specify NULL as all functions. poof, defaults restored ✨

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. It is passed as const and you will need to clone it in order to make use of any operation which would modify it. don't fight the const, it's giving boundaries πŸ’…


πŸ“ 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. slay for custom allocators having room to breathe 🫢


⭐ 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. so like… pass the whole set, bestie. all four. every time πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page, no cap πŸ’–βœ¨

this was libmemcached-awesome, page MEMCACHED_S...TORS_CONTEXT(3), version 1.1, dated Jul 24, 2025 β€” ily, go allocate responsibly πŸŽ€πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from