sys_mman.odin 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // Interpret addr exactly.
  90. FIXED = log2(MAP_FIXED),
  91. // Changes are private.
  92. PRIVATE = log2(MAP_PRIVATE),
  93. // Changes are shared.
  94. SHARED = log2(MAP_SHARED),
  95. }
  96. Map_Flags :: bit_set[Map_Flag_Bits; c.int]
  97. Lock_Flag_Bits :: enum c.int {
  98. // Lock all pages currently mapped into the address space of the process.
  99. CURRENT = log2(MCL_CURRENT),
  100. // Lock all pages that become mapped into the address space of the process in the future,
  101. // when those mappings are established.
  102. FUTURE = log2(MCL_FUTURE),
  103. }
  104. Lock_Flags :: bit_set[Lock_Flag_Bits; c.int]
  105. Sync_Flags_Bits :: enum c.int {
  106. // Perform asynchronous writes.
  107. ASYNC = log2(MS_ASYNC),
  108. // Invalidate cached data.
  109. INVALIDATE = log2(MS_INVALIDATE),
  110. // Perform synchronous writes.
  111. // NOTE: use with `posix.MS_SYNC + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
  112. // this bit set enum because it is 0 on some platforms and a value on others.
  113. // LOCAL = RTLD_LOCAL
  114. // SYNC = MS_SYNC,
  115. _MAX = 31,
  116. }
  117. Sync_Flags :: bit_set[Sync_Flags_Bits; c.int]
  118. MAdvice :: enum c.int {
  119. DONTNEED = POSIX_MADV_DONTNEED,
  120. NORMAL = POSIX_MADV_NORMAL,
  121. RANDOM = POSIX_MADV_RANDOM,
  122. SEQUENTIAL = POSIX_MADV_SEQUENTIAL,
  123. WILLNEED = POSIX_MADV_WILLNEED,
  124. }
  125. when ODIN_OS == .NetBSD {
  126. @(private) LMSYNC :: "__msync13"
  127. } else {
  128. @(private) LMSYNC :: "msync"
  129. }
  130. when ODIN_OS == .Darwin || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD || ODIN_OS == .Linux {
  131. PROT_EXEC :: 0x04
  132. _PROT_NONE :: 0x00
  133. PROT_READ :: 0x01
  134. PROT_WRITE :: 0x02
  135. MAP_FIXED :: 0x0010
  136. MAP_PRIVATE :: 0x0002
  137. MAP_SHARED :: 0x0001
  138. when ODIN_OS == .Darwin || ODIN_OS == .Linux {
  139. MS_INVALIDATE :: 0x0002
  140. _MS_SYNC :: 0x0010
  141. } else when ODIN_OS == .NetBSD {
  142. MS_INVALIDATE :: 0x0002
  143. _MS_SYNC :: 0x0004
  144. } else when ODIN_OS == .OpenBSD {
  145. MS_INVALIDATE :: 0x0004
  146. _MS_SYNC :: 0x0002
  147. }
  148. MS_ASYNC :: 0x0001
  149. MS_SYNC :: Sync_Flags{Sync_Flags_Bits(log2(_MS_SYNC))}
  150. MCL_CURRENT :: 0x0001
  151. MCL_FUTURE :: 0x0002
  152. MAP_FAILED :: rawptr(~uintptr(0))
  153. POSIX_MADV_DONTNEED :: 4
  154. POSIX_MADV_NORMAL :: 0
  155. POSIX_MADV_RANDOM :: 1
  156. POSIX_MADV_SEQUENTIAL :: 2
  157. POSIX_MADV_WILLNEED :: 3
  158. } else when ODIN_OS == .FreeBSD {
  159. PROT_EXEC :: 0x04
  160. _PROT_NONE :: 0x00
  161. PROT_READ :: 0x01
  162. PROT_WRITE :: 0x02
  163. MAP_FIXED :: 0x0010
  164. MAP_PRIVATE :: 0x0002
  165. MAP_SHARED :: 0x0001
  166. MS_ASYNC :: 0x0001
  167. MS_INVALIDATE :: 0x0002
  168. MS_SYNC :: Sync_Flags{}
  169. MCL_CURRENT :: 0x0001
  170. MCL_FUTURE :: 0x0002
  171. MAP_FAILED :: rawptr(~uintptr(0))
  172. POSIX_MADV_DONTNEED :: 4
  173. POSIX_MADV_NORMAL :: 0
  174. POSIX_MADV_RANDOM :: 1
  175. POSIX_MADV_SEQUENTIAL :: 2
  176. POSIX_MADV_WILLNEED :: 3
  177. }