#include <stdint.h> #include <stddef.h> #include <sys/stat.h> #include <sys/mman.h> #include "common.h" #include "scaffold.h" #include "types.h" #include "mbuf.h" // https://nullprogram.com/blog/2023/09/27/ // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1428r0.pdf // mmap() int mkmbuf(int fd, struct mbuf *ch) { struct stat sb = { 0 }; u8 *addr; if (fstat(fd, &sb) == -1) return -1; addr = mmap(NULL, (usize)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) return -1; ch->buf = addr; ch->len = sb.st_size; return 0; } void freembuf(struct mbuf *b) { /* FIXME check return code of munmap() */ munmap(b->buf, (usize)b->len); }