mmap_shared(3)
π NAME π
okay bestie, gather round β mmap_shared is the one that memory maps a file for reading and writing. that's it, that's the whole vibe. it's from the libowfat crew, sitting pretty in section 3 (Library Functions Manual) β¨
π SYNTAX π
like, this is literally what you type to summon her:
#include <libowfat/mmap.h>
char* mmap_shared(const char* filename,size_t* filesize);
no cap, that's the whole signature π
π¦ DESCRIPTION π¦
okay so here's the tea β β mmap_shared does a whole little routine and it's honestly iconic:
- opens
filenamefor reading and writing - maps the whole file into memory
- closes the file (yes, immediately, she's efficient like that π )
- writes the length of the file into
filesize - returns a pointer to the mapped file π«Ά
the file gets mapped shared. and bestie, that word is doing SO much work here β any changes you make to the mapped region get written to disk, and they're visible to other processes that are reading from the file or mapping that same file into memory. it's giving main character energy, everybody sees your edits β¨π₯
the way that the operating system unmaps the file if the process terminates?? automatic, no cleanup needed, she's self-sufficient π
but if you're the type who likes to be in control (respect), you can manually unmap it by calling munmap from <sys/mman.h>. iykyk πΈ
and if you're impatient and want your changes on disk, like, right now, you can use msync from <sys/mman.h>. go off, force that flush π
now the sad part π β if the file could not be opened or mapped, you get (void*)0 back. so please, for the love of everything, check your return value. not me watching you dereference a null pointer fr fr π
π SEE ALSO π
the besties, the squad, the group chat π:
okay that's the whole page, we ate and left absolutely no crumbs πβ¨ this was mmap_shared(3) from the Library Functions Manual β a <rainbow>libowfat</rainbow> classic, brought to you by section 3, and she says bye bestie, go map something responsibly ππ¦π
π¦ where this page comes from
- alpine 3.24: libowfat-doc 0.34-r0 Β· GPL-2.0-only
- nixos 26.05: libowfat 0.34
- nixpkgs unstable: libowfat 0.34
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- ubuntu resolute: libowfat-dev 0.34-2 Β· GPL-2.0-only
- ubuntu stonking: libowfat-dev 0.34-2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms