os.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
  9. return write(fd, transmute([]byte)str);
  10. }
  11. write_byte :: proc(fd: Handle, b: byte) -> (int, Errno) {
  12. return write(fd, []byte{b});
  13. }
  14. write_rune :: proc(fd: Handle, r: rune) -> (int, Errno) {
  15. if r < utf8.RUNE_SELF {
  16. return write_byte(fd, byte(r));
  17. }
  18. b, n := utf8.encode_rune(r);
  19. return write(fd, b[:n]);
  20. }
  21. write_encoded_rune :: proc(fd: Handle, r: rune) {
  22. write_byte(fd, '\'');
  23. switch r {
  24. case '\a': write_string(fd, "\\a");
  25. case '\b': write_string(fd, "\\b");
  26. case '\e': write_string(fd, "\\e");
  27. case '\f': write_string(fd, "\\f");
  28. case '\n': write_string(fd, "\\n");
  29. case '\r': write_string(fd, "\\r");
  30. case '\t': write_string(fd, "\\t");
  31. case '\v': write_string(fd, "\\v");
  32. case:
  33. if r < 32 {
  34. write_string(fd, "\\x");
  35. b: [2]byte;
  36. s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil);
  37. switch len(s) {
  38. case 0: write_string(fd, "00");
  39. case 1: write_rune(fd, '0');
  40. case 2: write_string(fd, s);
  41. }
  42. } else {
  43. write_rune(fd, r);
  44. }
  45. }
  46. write_byte(fd, '\'');
  47. }
  48. file_size_from_path :: proc(path: string) -> i64 {
  49. fd, err := open(path, O_RDONLY, 0);
  50. if err != 0 {
  51. return -1;
  52. }
  53. defer close(fd);
  54. length: i64;
  55. if length, err = file_size(fd); err != 0 {
  56. return -1;
  57. }
  58. return length;
  59. }
  60. read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
  61. fd, err := open(name, O_RDONLY, 0);
  62. if err != 0 {
  63. return nil, false;
  64. }
  65. defer close(fd);
  66. length: i64;
  67. if length, err = file_size(fd); err != 0 {
  68. return nil, false;
  69. }
  70. if length <= 0 {
  71. return nil, true;
  72. }
  73. data = make([]byte, int(length));
  74. if data == nil {
  75. return nil, false;
  76. }
  77. bytes_read, read_err := read(fd, data);
  78. if read_err != ERROR_NONE {
  79. delete(data);
  80. return nil, false;
  81. }
  82. return data[:bytes_read], true;
  83. }
  84. write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
  85. flags: int = O_WRONLY|O_CREATE;
  86. if truncate {
  87. flags |= O_TRUNC;
  88. }
  89. mode: int = 0;
  90. when OS == "linux" || OS == "darwin" {
  91. // NOTE(justasd): 644 (owner read, write; group read; others read)
  92. mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  93. }
  94. fd, err := open(name, flags, mode);
  95. if err != 0 {
  96. return false;
  97. }
  98. defer close(fd);
  99. _, write_err := write(fd, data);
  100. return write_err == 0;
  101. }
  102. write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  103. s := transmute([]byte)mem.Raw_Slice{data, len};
  104. return write(fd, s);
  105. }
  106. read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  107. s := transmute([]byte)mem.Raw_Slice{data, len};
  108. return read(fd, s);
  109. }
  110. heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
  111. size, alignment: int,
  112. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  113. //
  114. // NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
  115. // Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
  116. // padding. We also store the original pointer returned by heap_alloc right before
  117. // the pointer we return to the user.
  118. //
  119. aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> rawptr {
  120. a := max(alignment, align_of(rawptr));
  121. space := size + a - 1;
  122. allocated_mem: rawptr;
  123. if old_ptr != nil {
  124. original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^;
  125. allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr));
  126. } else {
  127. allocated_mem = heap_alloc(space+size_of(rawptr));
  128. }
  129. aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr)));
  130. ptr := uintptr(aligned_mem);
  131. aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
  132. diff := int(aligned_ptr - ptr);
  133. if (size + diff) > space {
  134. return nil;
  135. }
  136. aligned_mem = rawptr(aligned_ptr);
  137. mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
  138. return aligned_mem;
  139. }
  140. aligned_free :: proc(p: rawptr) {
  141. if p != nil {
  142. heap_free(mem.ptr_offset((^rawptr)(p), -1)^);
  143. }
  144. }
  145. aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> rawptr {
  146. if p == nil do return nil;
  147. return aligned_alloc(new_size, new_alignment, p);
  148. }
  149. switch mode {
  150. case .Alloc:
  151. return aligned_alloc(size, alignment);
  152. case .Free:
  153. aligned_free(old_memory);
  154. case .Free_All:
  155. // NOTE(tetra): Do nothing.
  156. case .Resize:
  157. if old_memory == nil {
  158. return aligned_alloc(size, alignment);
  159. }
  160. return aligned_resize(old_memory, old_size, size, alignment);
  161. case .Query_Features:
  162. set := (^mem.Allocator_Mode_Set)(old_memory);
  163. if set != nil {
  164. set^ = {.Alloc, .Free, .Resize, .Query_Features};
  165. }
  166. return set;
  167. case .Query_Info:
  168. return nil;
  169. }
  170. return nil;
  171. }
  172. heap_allocator :: proc() -> mem.Allocator {
  173. return mem.Allocator{
  174. procedure = heap_allocator_proc,
  175. data = nil,
  176. };
  177. }