sys_shm.odin 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // sys/shm.h = XSI shared memory facility
  10. foreign lib {
  11. /*
  12. Attaches the shared memory segment associated with the identifier
  13. into the address space of the calling process.
  14. Returns: nil (setting errno) on failure, the address otherwise
  15. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shmat.html ]]
  16. */
  17. shmat :: proc(shmid: FD, shmaddr: rawptr, shmflag: SHM_Flags) -> rawptr ---
  18. /*
  19. Provides various shared memory operation as specified by the given cmd.
  20. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shmctl.html ]]
  21. */
  22. @(link_name=LSHMCTL)
  23. shmctl :: proc(shmid: FD, cmd: IPC_Cmd, buf: ^shmid_ds) -> result ---
  24. /*
  25. Detaches the shared memory segment located at the address specified.
  26. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shmdt.html ]]
  27. */
  28. shmdt :: proc(shmaddr: rawptr) -> result ---
  29. /*
  30. Returns the shared memory identifier associated with key.
  31. Returns: -1 (setting errno) on failure, the shared memory ID otherwise
  32. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/shmget.html ]]
  33. */
  34. shmget :: proc(key: key_t, size: c.size_t, shmflag: SHM_Flags) -> FD ---
  35. }
  36. SHM_Flag_Bits :: enum c.int {
  37. RDONLY = log2(SHM_RDONLY),
  38. RND = log2(SHM_RND),
  39. }
  40. SHM_Flags :: bit_set[SHM_Flag_Bits; c.int]
  41. when ODIN_OS == .NetBSD {
  42. @(private) LSHMCTL :: "__shmctl50"
  43. } else {
  44. @(private) LSHMCTL :: "shmctl"
  45. }
  46. when ODIN_OS == .Darwin {
  47. SHM_RDONLY :: 0o10000
  48. SHM_RND :: 0o20000
  49. SHMLBA :: 16 * 1024 when ODIN_ARCH == .arm64 else 4096
  50. shmatt_t :: distinct c.ushort
  51. shmid_ds :: struct #max_field_align(4) {
  52. shm_perm: ipc_perm, /* [PSX] operation permission structure */
  53. shm_segsz: c.size_t, /* [PSX] size of segment in bytes */
  54. shm_lpid: pid_t, /* [PSX] process ID of last shared memory operation */
  55. shm_cpid: pid_t, /* [PSX] process ID of creator */
  56. shm_nattch: shmatt_t, /* [PSX] number of current attaches */
  57. shm_atime: time_t, /* [PSX] time of last shmat() */
  58. shm_dtime: time_t, /* [PSX] time of last shmdt() */
  59. shm_ctime: time_t, /* [PSX] time of last change by shmctl() */
  60. shm_internal: rawptr,
  61. }
  62. } else when ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD {
  63. SHM_RDONLY :: 0o10000
  64. SHM_RND :: 0o20000
  65. SHMLBA :: PAGESIZE
  66. shmatt_t :: distinct c.uint
  67. when ODIN_OS == .FreeBSD {
  68. shmid_ds :: struct {
  69. shm_perm: ipc_perm, /* [PSX] operation permission structure */
  70. shm_segsz: c.size_t, /* [PSX] size of segment in bytes */
  71. shm_lpid: pid_t, /* [PSX] process ID of last shared memory operation */
  72. shm_cpid: pid_t, /* [PSX] process ID of creator */
  73. shm_nattch: shmatt_t, /* [PSX] number of current attaches */
  74. shm_atime: time_t, /* [PSX] time of last shmat() */
  75. shm_dtime: time_t, /* [PSX] time of last shmdt() */
  76. shm_ctime: time_t, /* [PSX] time of last change by shmctl() */
  77. }
  78. } else {
  79. shmid_ds :: struct {
  80. shm_perm: ipc_perm, /* [PSX] operation permission structure */
  81. shm_segsz: c.size_t, /* [PSX] size of segment in bytes */
  82. shm_lpid: pid_t, /* [PSX] process ID of last shared memory operation */
  83. shm_cpid: pid_t, /* [PSX] process ID of creator */
  84. shm_nattch: shmatt_t, /* [PSX] number of current attaches */
  85. shm_atime: time_t, /* [PSX] time of last shmat() */
  86. shm_dtime: time_t, /* [PSX] time of last shmdt() */
  87. shm_ctime: time_t, /* [PSX] time of last change by shmctl() */
  88. _shm_internal: rawptr,
  89. }
  90. }
  91. } else when ODIN_OS == .OpenBSD {
  92. SHM_RDONLY :: 0o10000
  93. SHM_RND :: 0o20000
  94. SHMLBA :: 1 << 12
  95. shmatt_t :: distinct c.short
  96. shmid_ds :: struct {
  97. shm_perm: ipc_perm, /* [PSX] operation permission structure */
  98. shm_segsz: c.int, /* [PSX] size of segment in bytes */
  99. shm_lpid: pid_t, /* [PSX] process ID of last shared memory operation */
  100. shm_cpid: pid_t, /* [PSX] process ID of creator */
  101. shm_nattch: shmatt_t, /* [PSX] number of current attaches */
  102. shm_atime: time_t, /* [PSX] time of last shmat() */
  103. __shm_atimensec: c.long,
  104. shm_dtime: time_t, /* [PSX] time of last shmdt() */
  105. __shm_dtimensec: c.long,
  106. shm_ctime: time_t, /* [PSX] time of last change by shmctl() */
  107. __shm_ctimensec: c.long,
  108. _shm_internal: rawptr,
  109. }
  110. } else when ODIN_OS == .Linux {
  111. SHM_RDONLY :: 0o10000
  112. SHM_RND :: 0o20000
  113. SHMLBA :: 4096
  114. shmatt_t :: distinct c.ulong
  115. shmid_ds :: struct {
  116. shm_perm: ipc_perm, /* [PSX] operation permission structure */
  117. shm_segsz: c.size_t, /* [PSX] size of segment in bytes */
  118. shm_atime: time_t, /* [PSX] time of last shmat() */
  119. shm_dtime: time_t, /* [PSX] time of last shmdt() */
  120. shm_ctime: time_t, /* [PSX] time of last change by shmctl() */
  121. shm_cpid: pid_t, /* [PSX] process ID of creator */
  122. shm_lpid: pid_t, /* [PSX] process ID of last shared memory operation */
  123. shm_nattch: shmatt_t, /* [PSX] number of current attaches */
  124. _: [2]c.ulong,
  125. }
  126. }