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 pass NULL and 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() β€” allocates size bytes 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 by ptr to size bytes. 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 ptr is NULL, the call is equivalent to mem_alloc(size)
    • if size is equal to zero, the call is equivalent to mem_free(ptr)
    • and unless ptr is NULL, it must have been returned by an earlier call to mem_alloc() or mem_realloc(). no random pointers, we don't do that here πŸ’€
  • mem_free() β€” frees the memory space pointed to by ptr, which must have been returned by a previous call to mem_alloc() or mem_realloc(). if ptr is NULL, 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, or NULL if 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 from ptr, or NULL if the request fails or if size was equal to 0. and here's the tea: if mem_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