os.odin 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package os
  2. import "core:mem"
  3. import "core:strconv"
  4. import "core:unicode/utf8"
  5. OS :: ODIN_OS
  6. ARCH :: ODIN_ARCH
  7. ENDIAN :: ODIN_ENDIAN
  8. SEEK_SET :: 0
  9. SEEK_CUR :: 1
  10. SEEK_END :: 2
  11. write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
  12. return write(fd, transmute([]byte)str)
  13. }
  14. write_byte :: proc(fd: Handle, b: byte) -> (int, Errno) {
  15. return write(fd, []byte{b})
  16. }
  17. write_rune :: proc(fd: Handle, r: rune) -> (int, Errno) {
  18. if r < utf8.RUNE_SELF {
  19. return write_byte(fd, byte(r))
  20. }
  21. b, n := utf8.encode_rune(r)
  22. return write(fd, b[:n])
  23. }
  24. write_encoded_rune :: proc(fd: Handle, r: rune) {
  25. write_byte(fd, '\'')
  26. switch r {
  27. case '\a': write_string(fd, "\\a")
  28. case '\b': write_string(fd, "\\b")
  29. case '\e': write_string(fd, "\\e")
  30. case '\f': write_string(fd, "\\f")
  31. case '\n': write_string(fd, "\\n")
  32. case '\r': write_string(fd, "\\r")
  33. case '\t': write_string(fd, "\\t")
  34. case '\v': write_string(fd, "\\v")
  35. case:
  36. if r < 32 {
  37. write_string(fd, "\\x")
  38. b: [2]byte
  39. s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
  40. switch len(s) {
  41. case 0: write_string(fd, "00")
  42. case 1: write_rune(fd, '0')
  43. case 2: write_string(fd, s)
  44. }
  45. } else {
  46. write_rune(fd, r)
  47. }
  48. }
  49. write_byte(fd, '\'')
  50. }
  51. read_at_least :: proc(fd: Handle, buf: []byte, min: int) -> (n: int, err: Errno) {
  52. if len(buf) < min {
  53. return 0, -1
  54. }
  55. for n < min && err == 0 {
  56. nn: int
  57. nn, err = read(fd, buf[n:])
  58. n += nn
  59. }
  60. if n >= min {
  61. err = 0
  62. }
  63. return
  64. }
  65. read_full :: proc(fd: Handle, buf: []byte) -> (n: int, err: Errno) {
  66. return read_at_least(fd, buf, len(buf))
  67. }
  68. file_size_from_path :: proc(path: string) -> i64 {
  69. fd, err := open(path, O_RDONLY, 0)
  70. if err != 0 {
  71. return -1
  72. }
  73. defer close(fd)
  74. length: i64
  75. if length, err = file_size(fd); err != 0 {
  76. return -1
  77. }
  78. return length
  79. }
  80. read_entire_file_from_filename :: proc(name: string, allocator := context.allocator) -> (data: []byte, success: bool) {
  81. context.allocator = allocator
  82. fd, err := open(name, O_RDONLY, 0)
  83. if err != 0 {
  84. return nil, false
  85. }
  86. defer close(fd)
  87. return read_entire_file_from_handle(fd, allocator)
  88. }
  89. read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator) -> (data: []byte, success: bool) {
  90. context.allocator = allocator
  91. length: i64
  92. err: Errno
  93. if length, err = file_size(fd); err != 0 {
  94. return nil, false
  95. }
  96. if length <= 0 {
  97. return nil, true
  98. }
  99. data = make([]byte, int(length), allocator)
  100. if data == nil {
  101. return nil, false
  102. }
  103. bytes_read, read_err := read_full(fd, data)
  104. if read_err != ERROR_NONE {
  105. delete(data)
  106. return nil, false
  107. }
  108. return data[:bytes_read], true
  109. }
  110. read_entire_file :: proc {
  111. read_entire_file_from_filename,
  112. read_entire_file_from_handle,
  113. }
  114. write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
  115. flags: int = O_WRONLY|O_CREATE
  116. if truncate {
  117. flags |= O_TRUNC
  118. }
  119. mode: int = 0
  120. when OS == .Linux || OS == .Darwin {
  121. // NOTE(justasd): 644 (owner read, write; group read; others read)
  122. mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
  123. }
  124. fd, err := open(name, flags, mode)
  125. if err != 0 {
  126. return false
  127. }
  128. defer close(fd)
  129. _, write_err := write(fd, data)
  130. return write_err == 0
  131. }
  132. write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  133. s := transmute([]byte)mem.Raw_Slice{data, len}
  134. return write(fd, s)
  135. }
  136. read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  137. s := transmute([]byte)mem.Raw_Slice{data, len}
  138. return read(fd, s)
  139. }
  140. heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
  141. size, alignment: int,
  142. old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, mem.Allocator_Error) {
  143. //
  144. // NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
  145. // Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
  146. // padding. We also store the original pointer returned by heap_alloc right before
  147. // the pointer we return to the user.
  148. //
  149. aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) {
  150. a := max(alignment, align_of(rawptr))
  151. space := size + a - 1
  152. allocated_mem: rawptr
  153. if old_ptr != nil {
  154. original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^
  155. allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
  156. } else {
  157. allocated_mem = heap_alloc(space+size_of(rawptr))
  158. }
  159. aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr)))
  160. ptr := uintptr(aligned_mem)
  161. aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a)
  162. diff := int(aligned_ptr - ptr)
  163. if (size + diff) > space {
  164. return nil, .Out_Of_Memory
  165. }
  166. aligned_mem = rawptr(aligned_ptr)
  167. mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem
  168. return mem.byte_slice(aligned_mem, size), nil
  169. }
  170. aligned_free :: proc(p: rawptr) {
  171. if p != nil {
  172. heap_free(mem.ptr_offset((^rawptr)(p), -1)^)
  173. }
  174. }
  175. aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> (new_memory: []byte, err: mem.Allocator_Error) {
  176. if p == nil {
  177. return nil, nil
  178. }
  179. new_memory = aligned_alloc(new_size, new_alignment, p) or_return
  180. // NOTE: heap_resize does not zero the new memory, so we do it
  181. if new_size > old_size {
  182. new_region := mem.raw_data(new_memory[old_size:])
  183. mem.zero(new_region, new_size - old_size)
  184. }
  185. return
  186. }
  187. switch mode {
  188. case .Alloc:
  189. return aligned_alloc(size, alignment)
  190. case .Free:
  191. aligned_free(old_memory)
  192. case .Free_All:
  193. return nil, .Mode_Not_Implemented
  194. case .Resize:
  195. if old_memory == nil {
  196. return aligned_alloc(size, alignment)
  197. }
  198. return aligned_resize(old_memory, old_size, size, alignment)
  199. case .Query_Features:
  200. set := (^mem.Allocator_Mode_Set)(old_memory)
  201. if set != nil {
  202. set^ = {.Alloc, .Free, .Resize, .Query_Features}
  203. }
  204. return nil, nil
  205. case .Query_Info:
  206. return nil, .Mode_Not_Implemented
  207. }
  208. return nil, nil
  209. }
  210. heap_allocator :: proc() -> mem.Allocator {
  211. return mem.Allocator{
  212. procedure = heap_allocator_proc,
  213. data = nil,
  214. }
  215. }