MEMCACHED_GET_MEMORY_ALLOCATORS(3)

πŸ’– NAME πŸ’–

memcached_get_memory_allocators β€” the libmemcached documentation, bestie πŸ’…

Managing memory allocator functions ✨ (this whole page is a libmemcached manual page, so like, yes, it's from that family πŸ‘‘)


πŸŽ€ 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 here's the tea, bestie: libmemcached lets you bring your own memory allocators, tuned specifically for your application. That's the whole vibe β€” it means libmemcached can live happily inside apps that already have their own malloc implementation. Lowkey iconic. πŸ‘‘

memcached_set_memory_allocators() is the one you call to set the memory allocators used by the memcached instance that ptr points at. But heads up, and this part matters: you can't override just one of the memory allocators β€” nope β€” you have to hand over a complete new set if you want to override even one of them. No mix-and-match, periodt. πŸ’…

Every one of those memory allocation functions should behave exactly as specified in the C99 standard. And if you want to reset them back to the default values, pass NULL as all the functions. 🌈

memcached_get_memory_allocators() is how you get the currently used memory allocators out of a memcached handle. πŸ™ˆ

memcached_get_memory_allocators_context() returns the void * that got passed in during the call to memcached_set_memory_allocators(). 🫢

And the first argument to the memory allocator functions? That's a pointer to a memcached structure, and it's passed as const, so you'll need to clone it if you want to make use of any operation that would modify it. No cap. 😭


⭐ NOTES ⭐

In version 0.38, all functions were modified to have a context void pointer passed to them. Why, you ask? So that custom allocators could have their own space for memory. Main character energy fr fr. ✨


πŸ”₯ RETURN VALUE πŸ”₯

memcached_set_memory_allocators() returns MEMCACHED_SUCCESS when it goes well, and MEMCACHED_FAILURE if you don't pass a complete set of function pointers. That's it, that's the rule. πŸ’€


🌈 SEE ALSO 🌈


Okay that's the whole page, mwah πŸ’‹ β€” this was libmemcached-awesome, version 1.1, dated Jul 24, 2025, and the original title up top was MEMCACHED_G...Y_ALLOCATORS(3). Slay, go write some allocators bestie πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from