wrappers.odin 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //+build linux
  2. package linux
  3. /// Low 8 bits of the exit code
  4. /// Only retrieve the exit code if WIFEXITED(s) = true
  5. WEXITSTATUS :: #force_inline proc "contextless" (s: u32) -> u32 {
  6. return (s & 0xff00) >> 8
  7. }
  8. /// Termination signal
  9. /// Only retrieve the code if WIFSIGNALED(s) = true
  10. WTERMSIG :: #force_inline proc "contextless" (s: u32) -> u32 {
  11. return s & 0x7f
  12. }
  13. /// The signal that stopped the child
  14. /// Only retrieve if WIFSTOPPED(s) = true
  15. WSTOPSIG :: #force_inline proc "contextless" (s: u32) -> u32 {
  16. return WEXITSTATUS(s)
  17. }
  18. /// Check if the process terminated normally (via exit.2)
  19. WIFEXITED :: #force_inline proc "contextless" (s: u32) -> bool {
  20. return WTERMSIG(s) == 0
  21. }
  22. /// Check if the process signaled
  23. WIFSIGNALED :: #force_inline proc "contextless" (s: u32) -> bool {
  24. return cast(i8)(((s) & 0x7f) + 1) >> 1 > 0
  25. }
  26. /// Check if the process has stopped
  27. WIFSTOPPED :: #force_inline proc "contextless" (s: u32) -> bool {
  28. return (s & 0xff) == 0x7f
  29. }
  30. /// Check if the process is continued by the tracee
  31. WIFCONTINUED :: #force_inline proc "contextless" (s: u32) -> bool {
  32. return s == 0xffff
  33. }
  34. /// Check if the process dumped core
  35. WCOREDUMP :: #force_inline proc "contextless" (s: u32) -> bool {
  36. return s & 0x80 == 0x80
  37. }
  38. @private _sigmask :: proc "contextless" (sig: Signal) -> (uint) {
  39. return 1 << ((cast(uint)(sig) - 1) % (8*size_of(uint)))
  40. }
  41. @private _sigword :: proc "contextless" (sig: Signal) -> (uint) {
  42. return (cast(uint)sig - 1) / (8*size_of(uint))
  43. }
  44. // TODO: sigaddset etc
  45. /// Iterate the results of getdents
  46. /// Only iterates as much data as loaded in the buffer
  47. /// In case you need to iterate *all* files in a directory
  48. /// consider using dirent_get_iterate
  49. ///
  50. /// Example of using dirent_iterate_buf
  51. /// // Get dirents into a buffer
  52. /// buf: [128]u8
  53. /// sys.getdents(dirfd, buf[:])
  54. /// // Print the names of the files
  55. /// for dir in sys.dirent_iterate_buf(buf[:], &offs) {
  56. /// name := sys.dirent_name(dir)
  57. /// fmt.println(name)
  58. /// }
  59. /// This function doesn't automatically make a request
  60. /// for the buffer to be refilled
  61. dirent_iterate_buf :: proc "contextless" (buf: []u8, offs: ^int) -> (d: ^Dirent, cont: bool) {
  62. // Stopped iterating when there's no space left
  63. if offs^ >= len(buf) {
  64. return nil, false
  65. }
  66. // Retrieve dirent form the current offset
  67. dirent := cast(^Dirent) &buf[offs^]
  68. // Add the stride of dirent struct to the current offset
  69. offs^ += cast(int) dirent.reclen
  70. return dirent, true
  71. }
  72. /// Obtain the name of dirent as a string
  73. /// The lifetime of the string is bound to the lifetime of the provided dirent structure
  74. dirent_name :: proc "contextless" (dirent: ^Dirent) -> string #no_bounds_check {
  75. str := transmute([^]u8) &dirent.name
  76. // Note(flysand): The string size calculated above applies only to the ideal case
  77. // we subtract 1 byte from the string size, because a null terminator is guaranteed
  78. // to be present. But! That said, the dirents are aligned to 8 bytes and the padding
  79. // between the null terminator and the start of the next struct may be not initialized
  80. // which means we also have to scan these garbage bytes.
  81. str_size := (cast(int) dirent.reclen) - 1 - cast(int) offset_of(Dirent, name)
  82. // This skips *only* over the garbage, since if we're not garbage we're at nul terminator,
  83. // which skips this loop
  84. for str[str_size] != 0 {
  85. str_size -= 1
  86. }
  87. for str[str_size-1] == 0 {
  88. str_size -= 1
  89. }
  90. // Oh yeah btw i could also just `repne scasb` this thing, but honestly I started doing
  91. // it the painful way, might as well finish doing it that way
  92. return string(str[:str_size])
  93. }
  94. /// Constructor for the `futex_op` argument of a FUTEX_WAKE_OP call
  95. futex_op :: proc "contextless" (arg_op: Futex_Arg_Op, cmp_op: Futex_Cmp_Op, op_arg: u32, cmp_arg: u32) -> u32 {
  96. arg_op := cast(u32) arg_op
  97. cmp_op := cast(u32) cmp_op
  98. return (arg_op << 28) | (cmp_op << 24) | ((op_arg & 0xfff) << 12) | (cmp_arg & 0xfff)
  99. }
  100. /// Helper function for constructing the config for caches
  101. perf_cache_config :: #force_inline proc "contextless" (id: Perf_Hardware_Cache_Id,
  102. op: Perf_Hardware_Cache_Op_Id,
  103. res: Perf_Hardware_Cache_Result_Id) -> u64
  104. {
  105. return u64(id) | (u64(op) << 8) | (u64(res) << 16)
  106. }