MEM_OPEN(3debug)
π MEM_OPEN(3debug) β the girlies' guide to not leaking memory π
okay so bestie, welcome to the Debug Library memory routines, aka mem_open, mem_check, mem_alloc, mem_realloc, mem_free and mem_close π
these are the memory allocation girlies but with a whole debugging situation attached. it's giving "malloc but she has a therapist". this page lives in section 3debug of the manual, and yes we're gonna read the whole thing together, no skimming β¨
π«Ά LIBRARIES
Debug Library β link it with -ldebug or she simply will not show up to the party π
β¨ SYNOPSIS
#include <debug/memory.h>
void mem_open(void (*fail)(const char *fmt, ...));
void mem_check(void);
void *mem_alloc(size_t size);
void *mem_realloc(void *ptr, size_t size);
void mem_free(void *ptr);
void mem_close(void);
πΈ DESCRIPTION
okay so let's go one by one, bestie, no cap π₯
mem_open()β this one initializes the whole memory debugging system. she's the main character and she needs to go first: call her before any of the other routines, periodt. you can hand her a callback function that gets called whenever something bad happens, or you can passNULLand she'll just use the default error handler instead. and what does the default error handler do? it logs error messages using the debug logging routines and then exit. yeah. she's dramatic like that πmem_check()β she checks all the allocated memory areas. the system calls her automatically every single time memory is allocated or freed (she's literally everywhere, obsessed). but you can also call her yourself anytime you think memory might be corrupted π΅οΈβοΈ vibe check for your heap, fr fr.mem_alloc()β allocatessizebytes and returns a pointer to the allocated memory. and no, the memory is not cleared. she hands you whatever was chillin' there before πmem_realloc()β changes the size of the memory block pointed to byptrtosizebytes. the contents stay unchanged up to the minimum of the old and new sizes; the newly allocated memory will be uninitialized β¨. special cases, bestie, pay attention:- if
ptrisNULL, the call is equivalent tomem_alloc(size) - if
sizeis equal to zero, the call is equivalent tomem_free(ptr) - and unless
ptrisNULL, it must have been returned by an earlier call tomem_alloc()ormem_realloc(). no random pointers, we don't do that here π
- if
mem_free()β frees the memory space pointed to byptr, which must have been returned by a previous call tomem_alloc()ormem_realloc(). ifptrisNULL, no operation is performed β she just looks at you and moves on, iconic πmem_close()β checks for leaks and possible memory corruptions. the final boss. the closing ceremony. the last episode of the season π
π RETURN VALUE
mem_alloc()β returns a pointer to the allocated memory, which is suitably aligned for any kind of variable, orNULLif the request fails πmem_realloc()β returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different fromptr, orNULLif the request fails or ifsizewas equal to0. and here's the tea: ifmem_realloc()fails, the original block is left untouched β it is not freed or moved. she would never do that to you π«Ά- all the other functions return no value. they simply do the thing and leave. silent. mysterious. slay.
β NOTES
okay this part is important bestie, listen up π§
if the default fail callback is used, or if these routines are combined with the log routines, care should be taken to call the open and close functions in the right order. and the correct order is literally this, exactly this:
mem_open (NULL);
log_open (NULL,LOG_NORMAL,LOG_HAVE_COLORS | LOG_PRINT_FUNCTION);
atexit (mem_close);
atexit (log_close);
and of course, atexit(3) should only be used if the program will not forked π«
now the part nobody wants to hear: none of the libdebug routines are thread-safe. none. zero. and the author is not planning to change this either! for more information, please see http://threading.2038bug.com/ β go read it, i'll wait π
π¦ SEE ALSO
π AUTHOR
written by Abraham vd Merwe <abz@blio.com> β thank you for your service, king π«‘π
and that's the whole page, bestie!! this one's from the Unix manual, August 2004, filed under MEM_OPEN(3debug) β yeah she's vintage, she's been slaying since 2004 and she still hits different βπΈ mwah, go free your memory responsibly ππβ¨
π¦ where this page comes from
- debian bookworm: libdebug0-dev 0.5.3-7 Β· BSD-3-Clause
- debian trixie: libdebug0-dev 0.5.3-8+b1 Β· BSD-3-Clause
- ubuntu jammy: libdebug0-dev 0.5.3-6 Β· BSD-3-Clause
- ubuntu noble: libdebug0-dev 0.5.3-7 Β· BSD-3-Clause
- ubuntu questing: libdebug0-dev 0.5.3-8
- Copyright (c) 2002-2004 Abraham vd Merwe <abz@blio.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms