12367ad0 |
1 | #ifndef MMAN_H |
2 | #define MMAN_H |
3 | |
4 | #ifdef __cplusplus |
5 | extern "C" { |
6 | #endif |
7 | |
8 | #include <stdlib.h> |
1534f70a |
9 | #include <string.h> |
12367ad0 |
10 | #include <switch.h> |
12367ad0 |
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 | |
1534f70a |
21 | #define ALIGNMENT 0x1000 |
12367ad0 |
22 | |
1534f70a |
23 | static inline void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) |
12367ad0 |
24 | { |
1534f70a |
25 | (void)fd; |
26 | (void)offset; |
27 | |
28 | // match Linux behavior |
29 | len = (len + ALIGNMENT - 1) & ~(ALIGNMENT - 1); |
12367ad0 |
30 | |
12367ad0 |
31 | Result rc = svcMapPhysicalMemory(addr, len); |
32 | if (R_FAILED(rc)) |
33 | { |
1534f70a |
34 | //printf("mmap failed\n"); |
35 | addr = aligned_alloc(ALIGNMENT, len); |
12367ad0 |
36 | } |
5dbff29e |
37 | if (!addr) |
38 | return MAP_FAILED; |
39 | memset(addr, 0, len); |
12367ad0 |
40 | return addr; |
12367ad0 |
41 | } |
42 | |
43 | static inline int munmap(void *addr, size_t len) |
44 | { |
1534f70a |
45 | len = (len + ALIGNMENT - 1) & ~(ALIGNMENT - 1); |
12367ad0 |
46 | Result rc = svcUnmapPhysicalMemory(addr, len); |
47 | if (R_FAILED(rc)) |
48 | { |
1534f70a |
49 | //printf("munmap failed\n"); |
12367ad0 |
50 | free(addr); |
51 | } |
52 | return 0; |
12367ad0 |
53 | } |
54 | |
55 | #ifdef __cplusplus |
56 | }; |
57 | #endif |
58 | |
59 | #endif // MMAN_H |
60 | |