commit dafc12de705058dffcbf7c1e44460ca6f4d91541
parent 0bf113ada0bd707abf6b2cdd216498b0a230c42c
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 8 Nov 2021 13:13:09 +0100
io: add io::mmap
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
4 files changed, 120 insertions(+), 0 deletions(-)
diff --git a/io/+freebsd/mmap.ha b/io/+freebsd/mmap.ha
@@ -0,0 +1,47 @@
+use errors;
+use rt;
+
+// Values for the [[mmap]] prot parameter. Only the EXEC, READ, WRITE, and NONE
+// values are portable.
+export type prot = enum uint {
+ NONE = rt::PROT_NONE,
+ READ = rt::PROT_READ,
+ WRITE = rt::PROT_WRITE,
+ EXEC = rt::PROT_EXEC,
+};
+
+// Values for the [[mmap]] flags parameter. Only the SHARED, PRIVATE, and FIXED
+// values are portable.
+export type mflags = enum uint {
+ SHARED = rt::MAP_SHARED,
+ PRIVATE = rt::MAP_PRIVATE,
+ FIXED = rt::MAP_FIXED,
+ HASSEMAPHORE = rt::MAP_HASSEMAPHORE ,
+ STACK = rt::MAP_STACK,
+ NOSYNC = rt::MAP_NOSYNC,
+ FILE = rt::MAP_FILE,
+ ANON = rt::MAP_ANON,
+ GUARD = rt::MAP_GUARD,
+ EXCL = rt::MAP_EXCL,
+ NOCORE = rt::MAP_NOCORE,
+ PREFAULT_READ = rt::MAP_PREFAULT_READ,
+ _32BIT = rt::MAP_32BIT,
+};
+
+// Performs the mmap syscall. Consult your system for documentation on this
+// function.
+export fn mmap(
+ addr: nullable *void,
+ length: size,
+ prot: prot,
+ flags: mflags,
+ fd: file,
+ offs: size
+) (*void | errors::error) = {
+ match (rt::mmap(addr, length, prot, flags, fd, offs)) {
+ case ptr: *void =>
+ return ptr;
+ case err: rt::errno =>
+ return errors::errno(err);
+ };
+};
diff --git a/io/+linux/mmap.ha b/io/+linux/mmap.ha
@@ -0,0 +1,67 @@
+use errors;
+use rt;
+
+// Values for the [[mmap]] prot parameter. Only the EXEC, READ, WRITE, and NONE
+// values are portable.
+export type prot = enum uint {
+ NONE = rt::PROT_NONE,
+ READ = rt::PROT_READ,
+ WRITE = rt::PROT_WRITE,
+ EXEC = rt::PROT_EXEC,
+ GROWSDOWN = rt::PROT_GROWSDOWN,
+ GROWSUP = rt::PROT_GROWSUP,
+};
+
+// Values for the [[mmap]] flags parameter. Only the SHARED, PRIVATE, and FIXED
+// values are portable.
+export type mflags = enum uint {
+ SHARED = rt::MAP_SHARED,
+ PRIVATE = rt::MAP_PRIVATE,
+ SHARED_VALIDATE = rt::MAP_SHARED_VALIDATE,
+ FIXED = rt::MAP_FIXED,
+ ANON = rt::MAP_ANON,
+ NORESERVE = rt::MAP_NORESERVE,
+ GROWSDOWN = rt::MAP_GROWSDOWN,
+ DENYWRITE = rt::MAP_DENYWRITE,
+ EXECUTABLE = rt::MAP_EXECUTABLE,
+ LOCKED = rt::MAP_LOCKED,
+ POPULATE = rt::MAP_POPULATE,
+ NONBLOCK = rt::MAP_NONBLOCK,
+ STACK = rt::MAP_STACK,
+ HUGETLB = rt::MAP_HUGETLB,
+ SYNC = rt::MAP_SYNC,
+ FIXED_NOREPLACE = rt::MAP_FIXED_NOREPLACE,
+ FILE = rt::MAP_FILE,
+ HUGE_SHIFT = rt::MAP_HUGE_SHIFT,
+ HUGE_MASK = rt::MAP_HUGE_MASK,
+ HUGE_64KB = rt::MAP_HUGE_64KB,
+ HUGE_512KB = rt::MAP_HUGE_512KB,
+ HUGE_1MB = rt::MAP_HUGE_1MB,
+ HUGE_2MB = rt::MAP_HUGE_2MB,
+ HUGE_8MB = rt::MAP_HUGE_8MB,
+ HUGE_16MB = rt::MAP_HUGE_16MB,
+ HUGE_32MB = rt::MAP_HUGE_32MB,
+ HUGE_256MB = rt::MAP_HUGE_256MB,
+ HUGE_512MB = rt::MAP_HUGE_512MB,
+ HUGE_1GB = rt::MAP_HUGE_1GB,
+ HUGE_2GB = rt::MAP_HUGE_2GB,
+ HUGE_16GB = rt::MAP_HUGE_16GB,
+};
+
+// Performs the mmap syscall. Consult your system for documentation on this
+// function.
+export fn mmap(
+ addr: nullable *void,
+ length: size,
+ prot: prot,
+ flags: mflags,
+ fd: file,
+ offs: size
+) (*void | errors::error) = {
+ match (rt::mmap(addr, length, prot, flags, fd, offs)) {
+ case ptr: *void =>
+ return ptr;
+ case err: rt::errno =>
+ return errors::errno(err);
+ };
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -596,6 +596,7 @@ gensrcs_io() {
'arch+$(ARCH).ha' \
println+linux.ha \
+linux/file.ha \
+ +linux/mmap.ha \
copy.ha \
drain.ha \
empty.ha \
@@ -610,6 +611,7 @@ gensrcs_io() {
'arch+$(ARCH).ha' \
println+freebsd.ha \
+freebsd/file.ha \
+ +freebsd/mmap.ha \
copy.ha \
drain.ha \
empty.ha \
diff --git a/stdlib.mk b/stdlib.mk
@@ -1058,6 +1058,7 @@ stdlib_io_linux_srcs= \
$(STDLIB)/io/arch+$(ARCH).ha \
$(STDLIB)/io/println+linux.ha \
$(STDLIB)/io/+linux/file.ha \
+ $(STDLIB)/io/+linux/mmap.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
$(STDLIB)/io/empty.ha \
@@ -1073,6 +1074,7 @@ stdlib_io_freebsd_srcs= \
$(STDLIB)/io/arch+$(ARCH).ha \
$(STDLIB)/io/println+freebsd.ha \
$(STDLIB)/io/+freebsd/file.ha \
+ $(STDLIB)/io/+freebsd/mmap.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
$(STDLIB)/io/empty.ha \
@@ -2741,6 +2743,7 @@ testlib_io_linux_srcs= \
$(STDLIB)/io/arch+$(ARCH).ha \
$(STDLIB)/io/println+linux.ha \
$(STDLIB)/io/+linux/file.ha \
+ $(STDLIB)/io/+linux/mmap.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
$(STDLIB)/io/empty.ha \
@@ -2759,6 +2762,7 @@ testlib_io_freebsd_srcs= \
$(STDLIB)/io/arch+$(ARCH).ha \
$(STDLIB)/io/println+freebsd.ha \
$(STDLIB)/io/+freebsd/file.ha \
+ $(STDLIB)/io/+freebsd/mmap.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
$(STDLIB)/io/empty.ha \