MEMCACHED_SET_MEMORY_ALLOCATORS(3)

πŸ’– NAME πŸ’–

memcached_set_memory_allocators β€” libmemcached Documentation, bestie edition ✨

It's giving Manage memory allocator functions. Yes queen, we're doing custom allocators today πŸŽ€

Oh and real quick, this whole page lives in the libmemcached-awesome manual, section 3, under the very dramatic name MEMCACHED_S...Y_ALLOCATORS(3) (the truncation is iconic, we don't question it πŸ’…).


🌸 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 πŸ¦‹

Okay so, the whole vibe here? libmemcached lets you bring your own memory allocators, tuned specifically for your app. Which means libmemcached can live inside applications that already have their own malloc implementation. No cap, it just fits in 🫢

memcached_set_memory_allocators() sets the memory allocators used by the memcached instance specified by ptr. Important heads up though: you can not override just one of the memory allocators β€” you have to hand over a complete new set if you want to override any of them. All of the memory allocation functions should behave as specified in the C99 standard. And if you wanna reset them all back to the default values? Specify NULL as all functions. Boom, factory reset ✨

memcached_get_memory_allocators() grabs the currently used memory allocators from a memcached handle. Peek behind the curtain, slay πŸ’…

memcached_get_memory_allocators_context() returns the void * that you passed in during the call to memcached_set_memory_allocators(). It remembers. It always remembers πŸ₯Ί

The first argument to the memory allocator functions is a pointer to a memcached structure β€” it's passed as const, so if you need to do anything that would modify it, you will need to clone it first. Respect the const, bestie, it's not a suggestion 😀


πŸŽ€ NOTES πŸŽ€

In version 0.38, all functions were modified to have a context void pointer passed to them. Why? So that custom allocators could have their own space for memory. Character development, honestly 🌈


⭐ 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… all of them or none of them. No half-sending.


πŸ‘‘ SEE ALSO πŸ‘‘


And that's the whole page, bestie πŸ§šπŸ’œ This was version 1.1, dated Jul 24, 2025, and yes it lives in the libmemcached-awesome collection, section 3, MEMCACHED_S...Y_ALLOCATORS(3). Go forth and allocate responsibly 🌸🫢

πŸ“¦ where this page comes from