hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

mmap.ha (2119B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use rt;
      6 
      7 // Values for the [[mmap]] prot parameter. Only the EXEC, READ, WRITE, and NONE
      8 // values are portable.
      9 export type prot = enum uint {
     10 	NONE = rt::PROT_NONE,
     11 	READ = rt::PROT_READ,
     12 	WRITE = rt::PROT_WRITE,
     13 	EXEC = rt::PROT_EXEC,
     14 	GROWSDOWN = rt::PROT_GROWSDOWN,
     15 	GROWSUP = rt::PROT_GROWSUP,
     16 };
     17 
     18 // Values for the [[mmap]] flags parameter. Only the SHARED, PRIVATE, and FIXED
     19 // values are portable.
     20 export type mflag = enum uint {
     21 	SHARED = rt::MAP_SHARED,
     22 	PRIVATE = rt::MAP_PRIVATE,
     23 	SHARED_VALIDATE = rt::MAP_SHARED_VALIDATE,
     24 	FIXED = rt::MAP_FIXED,
     25 	ANON = rt::MAP_ANON,
     26 	NORESERVE = rt::MAP_NORESERVE,
     27 	GROWSDOWN = rt::MAP_GROWSDOWN,
     28 	DENYWRITE = rt::MAP_DENYWRITE,
     29 	EXECUTABLE = rt::MAP_EXECUTABLE,
     30 	LOCKED = rt::MAP_LOCKED,
     31 	POPULATE = rt::MAP_POPULATE,
     32 	NONBLOCK = rt::MAP_NONBLOCK,
     33 	STACK = rt::MAP_STACK,
     34 	HUGETLB = rt::MAP_HUGETLB,
     35 	SYNC = rt::MAP_SYNC,
     36 	FIXED_NOREPLACE = rt::MAP_FIXED_NOREPLACE,
     37 	FILE = rt::MAP_FILE,
     38 	HUGE_SHIFT = rt::MAP_HUGE_SHIFT,
     39 	HUGE_MASK = rt::MAP_HUGE_MASK,
     40 	HUGE_64KB = rt::MAP_HUGE_64KB,
     41 	HUGE_512KB = rt::MAP_HUGE_512KB,
     42 	HUGE_1MB = rt::MAP_HUGE_1MB,
     43 	HUGE_2MB = rt::MAP_HUGE_2MB,
     44 	HUGE_8MB = rt::MAP_HUGE_8MB,
     45 	HUGE_16MB = rt::MAP_HUGE_16MB,
     46 	HUGE_32MB = rt::MAP_HUGE_32MB,
     47 	HUGE_256MB = rt::MAP_HUGE_256MB,
     48 	HUGE_512MB = rt::MAP_HUGE_512MB,
     49 	HUGE_1GB = rt::MAP_HUGE_1GB,
     50 	HUGE_2GB = rt::MAP_HUGE_2GB,
     51 	HUGE_16GB = rt::MAP_HUGE_16GB,
     52 };
     53 
     54 // Performs the mmap syscall. Consult your system for documentation on this
     55 // function.
     56 export fn mmap(
     57 	addr: nullable *opaque,
     58 	length: size,
     59 	prot: prot,
     60 	flags: mflag,
     61 	fd: file,
     62 	offs: size
     63 ) (*opaque | errors::error) = {
     64 	match (rt::mmap(addr, length, prot, flags, fd, offs)) {
     65 	case let ptr: *opaque =>
     66 		return ptr;
     67 	case let err: rt::errno =>
     68 		return errors::errno(err);
     69 	};
     70 };
     71 
     72 // Unmaps memory previously mapped with [[mmap]].
     73 export fn munmap(addr: *opaque, length: size) (void | errors::error) = {
     74 	match (rt::munmap(addr, length)) {
     75 	case void =>
     76 		return;
     77 	case let err: rt::errno =>
     78 		return errors::errno(err);
     79 	};
     80 };