LC_MMAPFILE(3)

πŸ’– LC_MMAPFILE(3) πŸ’–

okay bestie, welcome to Librecast's little cutie of a function β€” meet lc_mmapfile, the girl who maps (or unmaps!) files or devices straight into memory. she lives in the Librecast Programmer's Manual, and yes she's part of the Librecast library β€” that's liblibrecast, linked with -llibrecast. slay. ✨


🌸 LIBRARY 🌸

Librecast library (liblibrecast, -llibrecast) 🫢


✨ SYNOPSIS ✨

#include <librecast/sync.h>

void *lc_mmapfile(const char *pathname, size_t *len, int prot, int flags,
                  off_t offset, struct stat *sb);

Compile and link with -llibrecast. no cap, that's the whole vibe πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so lc_mmapfile() is literally just a convenience wrapper for mmap(2) β€” it open(2)s and maps a file in a single call, because who has time for two calls when one will do. if you want the full lore, go read mmap(2).

here's the tea, ok:

  • if prot has the PROT_WRITE flag set, then pathname gets open(2)'ed with the O_RDWR and O_CREAT flags set β€” so the file is created and opened read-write πŸ’–
  • otherwise it's opened O_RDONLY 🌸
  • wanna set the mode for the file being created? set st_mode in sb when you call lc_mmapfile(). that's it, that's the trick ✨

and about len:

  • if len points to a value greater than zero, then ftruncate(2) is called to truncate the file to that specified length βœ‚οΈ
  • otherwise, the variable len points to is set to the size of the file πŸ‘‘

when you're done, the file can be unmapped and the memory freed by calling munmap(3). clean up after yourself, bestie 🧹


πŸ”₯ RETURN VALUE πŸ”₯

on success, lc_mmapfile() returns a pointer to the mapped area. iconic. πŸ‘‘

on error, the value MAP_FAILED (that is, (void*)-1) is returned, and errno is set to indicate the error. it's giving sad girl era 😭


πŸ’€ ERRORS πŸ’€

  • ENODATA β€” File is zero length. yeah. nothing there. empty. the void. 🫠

See also open(2), mmap(2), fstat(2), and ftruncate(2).


🌈 SEE ALSO 🌈


πŸ’œ that's the whole page bestie, go forth and map some memory. iykyk.

LIBRECAST Β· 2023-07-24 Β· LC_MMAPFILE(3) β€” mwah, bye, love u, see u in the group chat πŸ’…πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from