sys_mman.odin 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package posix
  2. import "core:c"
  3. when ODIN_OS == .Darwin {
  4. foreign import lib "system:System.framework"
  5. } else {
  6. foreign import lib "system:c"
  7. }
  8. // mman.h - memory management declarations
  9. foreign lib {
  10. /*
  11. Establish a mapping between an address space of a process and a memory object.
  12. Returns: MAP_FAILED (setting errno) on failure, the address in memory otherwise
  13. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html ]]
  14. */
  15. mmap :: proc(
  16. addr: rawptr,
  17. len: c.size_t,
  18. prot: Prot_Flags,
  19. flags: Map_Flags,
  20. fd: FD = -1,
  21. off: off_t = 0,
  22. ) -> rawptr ---
  23. /*
  24. Unmaps pages of memory.
  25. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/munmap.html ]]
  26. */
  27. munmap :: proc(addr: rawptr, len: c.size_t) -> result ---
  28. /*
  29. Locks a range of the process address space.
  30. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html ]]
  31. */
  32. mlock :: proc(addr: rawptr, len: c.size_t) -> result ---
  33. /*
  34. Unlocks a range of the process address space.
  35. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html ]]
  36. */
  37. munlock :: proc(addr: rawptr, len: c.size_t) -> result ---
  38. /*
  39. Locks all pages of the process address space.
  40. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlockall.html ]]
  41. */
  42. mlockall :: proc(flags: Lock_Flags) -> result ---
  43. /*
  44. Unlocks all pages of the process address space.
  45. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlockall.html ]]
  46. */
  47. munlockall :: proc() -> result ---
  48. /*
  49. Set protection of a memory mapping.
  50. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html ]]
  51. */
  52. mprotect :: proc(addr: rawptr, len: c.size_t, prot: Prot_Flags) -> result ---
  53. /*
  54. Write all modified data to permanent storage locations.
  55. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/msync.html ]]
  56. */
  57. @(link_name=LMSYNC)
  58. msync :: proc(addr: rawptr, len: c.size_t, flags: Sync_Flags) -> result ---
  59. /*
  60. Advise the implementation of expected behavior of the application.
  61. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_madvise.html ]]
  62. */
  63. posix_madvise :: proc(addr: rawptr, len: c.size_t, advice: MAdvice) -> Errno ---
  64. /*
  65. Open a shared memory object.
  66. Returns: -1 (setting errno) on failure, an open file descriptor otherwise
  67. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html ]]
  68. */
  69. shm_open :: proc(name: cstring, oflag: O_Flags, mode: mode_t) -> FD ---
  70. /*
  71. Removes a shared memory object.
  72. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_unlink.html ]]
  73. */
  74. shm_unlink :: proc(name: cstring) -> result ---
  75. }
  76. #assert(_PROT_NONE == 0)
  77. PROT_NONE :: Prot_Flags{}
  78. Prot_Flag_Bits :: enum c.int {
  79. // Data can be executed.
  80. EXEC = log2(PROT_EXEC),
  81. // Data can be read.
  82. READ = log2(PROT_READ),
  83. // Data can be written.
  84. WRITE = log2(PROT_WRITE),
  85. }
  86. Prot_Flags :: bit_set[Prot_Flag_Bits; c.int]
  87. Map_Flag_Bits :: enum c.int {
  88. // Interpret addr exactly.
  89. FIXED = log2(MAP_FIXED),
  90. // Changes are private.
  91. PRIVATE = log2(MAP_PRIVATE),
  92. // Changes are shared.
  93. SHARED = log2(MAP_SHARED),
  94. }
  95. Map_Flags :: bit_set[Map_Flag_Bits; c.int]
  96. Lock_Flag_Bits :: enum c.int {
  97. // Lock all pages currently mapped into the address space of the process.
  98. CURRENT = log2(MCL_CURRENT),
  99. // Lock all pages that become mapped into the address space of the process in the future,
  100. // when those mappings are established.
  101. FUTURE = log2(MCL_FUTURE),
  102. }
  103. Lock_Flags :: bit_set[Lock_Flag_Bits; c.int]
  104. Sync_Flags_Bits :: enum c.int {
  105. // Perform asynchronous writes.
  106. ASYNC = log2(MS_ASYNC),
  107. // Invalidate cached data.
  108. INVALIDATE = log2(MS_INVALIDATE),
  109. // Perform synchronous writes.
  110. // NOTE: use with `posix.MS_SYNC + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
  111. // this bit set enum because it is 0 on some platforms and a value on others.
  112. // LOCAL = RTLD_LOCAL
  113. // SYNC = MS_SYNC,
  114. _MAX = 31,
  115. }
  116. Sync_Flags :: bit_set[Sync_Flags_Bits; c.int]
  117. MAdvice :: enum c.int {
  118. DONTNEED = POSIX_MADV_DONTNEED,
  119. NORMAL = POSIX_MADV_NORMAL,
  120. RANDOM = POSIX_MADV_RANDOM,
  121. SEQUENTIAL = POSIX_MADV_SEQUENTIAL,
  122. WILLNEED = POSIX_MADV_WILLNEED,
  123. }
  124. when ODIN_OS == .NetBSD {
  125. @(private) LMSYNC :: "__msync13"
  126. } else {
  127. @(private) LMSYNC :: "msync"
  128. }
  129. when ODIN_OS == .Darwin || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  130. PROT_EXEC :: 0x04
  131. _PROT_NONE :: 0x00
  132. PROT_READ :: 0x01
  133. PROT_WRITE :: 0x02
  134. MAP_FIXED :: 0x0010
  135. MAP_PRIVATE :: 0x0002
  136. MAP_SHARED :: 0x0001
  137. when ODIN_OS == .Darwin {
  138. MS_INVALIDATE :: 0x0002
  139. _MS_SYNC :: 0x0010
  140. } else when ODIN_OS == .NetBSD {
  141. MS_INVALIDATE :: 0x0002
  142. _MS_SYNC :: 0x0004
  143. } else when ODIN_OS == .OpenBSD {
  144. MS_INVALIDATE :: 0x0004
  145. _MS_SYNC :: 0x0002
  146. }
  147. MS_ASYNC :: 0x0001
  148. MS_SYNC :: Sync_Flags{Sync_Flags_Bits(log2(_MS_SYNC))}
  149. MCL_CURRENT :: 0x0001
  150. MCL_FUTURE :: 0x0002
  151. MAP_FAILED :: rawptr(~uintptr(0))
  152. POSIX_MADV_DONTNEED :: 4
  153. POSIX_MADV_NORMAL :: 0
  154. POSIX_MADV_RANDOM :: 1
  155. POSIX_MADV_SEQUENTIAL :: 2
  156. POSIX_MADV_WILLNEED :: 3
  157. } else when ODIN_OS == .FreeBSD {
  158. PROT_EXEC :: 0x04
  159. _PROT_NONE :: 0x00
  160. PROT_READ :: 0x01
  161. PROT_WRITE :: 0x02
  162. MAP_FIXED :: 0x0010
  163. MAP_PRIVATE :: 0x0002
  164. MAP_SHARED :: 0x0001
  165. MS_ASYNC :: 0x0001
  166. MS_INVALIDATE :: 0x0002
  167. MS_SYNC :: Sync_Flags{}
  168. MCL_CURRENT :: 0x0001
  169. MCL_FUTURE :: 0x0002
  170. MAP_FAILED :: rawptr(~uintptr(0))
  171. POSIX_MADV_DONTNEED :: 4
  172. POSIX_MADV_NORMAL :: 0
  173. POSIX_MADV_RANDOM :: 1
  174. POSIX_MADV_SEQUENTIAL :: 2
  175. POSIX_MADV_WILLNEED :: 3
  176. } else {
  177. #panic("posix is unimplemented for the current target")
  178. }