mmap_private(3)

πŸ“¦
version 0.34 Β· 3 versions
, in alpine 3.24, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu resolute, ubuntu stonking

πŸ’– mmap_private β€” from the Library Functions Manual πŸ’–

okay bestie, pull up a chair, because mmap_private(3) is literally about to hand you an entire file like it's a gift 🎁


✨ NAME ✨

mmap_private β€” memory map a file for reading and writing πŸ¦‹


✨ SYNTAX ✨

#include <libowfat/mmap.h>

char* mmap_private(const char* filename,size_t* filesize);

✨ DESCRIPTION ✨

so here's the tea, and it is good tea β˜•

mmap_private opens filename for reading, maps the whole file into memory, closes the file, writes the length of the file into filesize, and returns a pointer to that mapped file. that's it. that's the whole vibe. you get a pointer, you get a size, you go be the main character πŸ‘‘

the file is mapped copy-on-write. so like… changes you make to the mapped region will not be written to disk. nope. not happening. your edits live in memory and stay in memory, no cap πŸ™ˆ

when your process terminates, the operating system unmaps the file for you β€” automatic, no cleanup on your part, iconic behaviour honestly πŸ’…

but if you're the type who likes to be in control (respect), you can also manually unmap it by calling munmap from <sys/mman.h>.

and if the file could not be opened or mapped? you get (void*)0 back. that's the "it's giving nothing" response 😭 always check for it, bestie, fr fr.

✨ SEE ALSO ✨


and that's a wrap on mmap_private(3) from the Library Functions Manual πŸŽ€ mwah, go map something pretty, and remember: copy-on-write means the disk never finds out 🌸✨