sys_shm.odin 4.5 KB

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