]>
git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/mman/mman.c
e0b1021c59cda269474e2910c8dc0c28a5bdbc95
2 * Userspace emulation of mmap and munmap. Restrictions apply.
4 * This is meant to be complete enough to be compatible with code that uses
5 * mmap for simple file I/O. It just allocates memory with malloc and reads
6 * and writes data with pread and pwrite.
9 #define _WASI_EMULATED_MMAN
15 #include <sys/types.h>
25 void *mmap(void *addr
, size_t length
, int prot
, int flags
,
26 int fd
, off_t offset
) {
27 // Check for unsupported flags.
28 if ((flags
& MAP_FIXED
) != 0 ||
29 #ifdef MAP_SHARED_VALIDATE
30 (flags
& MAP_SHARED_VALIDATE
) != 0 ||
33 (flags
& MAP_NORESERVE
) != 0 ||
36 (flags
& MAP_GROWSDOWN
) != 0 ||
39 (flags
& MAP_HUGETLB
) != 0 ||
41 #ifdef MAP_FIXED_NOREPLACE
42 (flags
& MAP_FIXED_NOREPLACE
) != 0 ||
50 // Check for unsupported protection requests.
51 if (prot
== PROT_NONE
||
53 (prot
& PROT_EXEC
) != 0 ||
61 const size_t buf_len
= sizeof(struct map
) + length
;
63 // Check for integer overflow.
64 if(buf_len
< length
) {
69 // Allocate the memory.
70 struct map
*map
= malloc(buf_len
);
76 // Initialize the header.
82 // Initialize the main memory buffer, either with the contents of a file,
84 if ((flags
& MAP_ANON
) == 0) {
85 char *body
= map
->body
;
87 const ssize_t nread
= pread(fd
, body
, length
, offset
);
95 length
-= (size_t)nread
;
96 offset
+= (size_t)nread
;
97 body
+= (size_t)nread
;
100 memset(map
->body
, 0, length
);
106 int munmap(void *addr
, size_t length
) {
107 struct map
*map
= (struct map
*)addr
- 1;
109 // We don't support partial munmapping.
110 if (map
->length
!= length
) {
115 // Release the memory.