sys_mman.odin 5.8 KB

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