49fb4454ca49a38a0b7a103c20c643da1ca3c1a5
[pcsx_rearmed.git] / frontend / switch / sys / mman.h
1 #ifndef MMAN_H
2 #define MMAN_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <switch.h>
11
12 #define PROT_READ       0b001
13 #define PROT_WRITE      0b010
14 #define PROT_EXEC       0b100
15 #define MAP_PRIVATE     2
16 #define MAP_FIXED       0x10
17 #define MAP_ANONYMOUS   0x20
18
19 #define MAP_FAILED      ((void *)-1)
20
21 #define ALIGNMENT       0x1000
22
23 static inline void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
24 {
25     (void)fd;
26     (void)offset;
27
28     // match Linux behavior
29     len = (len + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
30
31     Result rc = svcMapPhysicalMemory(addr, len);
32     if (R_FAILED(rc))
33     {
34         //printf("mmap failed\n");
35         addr = aligned_alloc(ALIGNMENT, len);
36     }
37
38     if (addr)
39         memset(addr, 0, len);
40     return addr;
41 }
42
43 static inline int munmap(void *addr, size_t len)
44 {
45     len = (len + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
46     Result rc = svcUnmapPhysicalMemory(addr, len);
47     if (R_FAILED(rc))
48     {
49         //printf("munmap failed\n");
50         free(addr);
51     }
52     return 0;
53 }
54
55 #ifdef __cplusplus
56 };
57 #endif
58
59 #endif // MMAN_H
60