os_specific_windows.odin 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //+private
  2. //+build windows
  3. package runtime
  4. import "core:intrinsics"
  5. foreign import kernel32 "system:Kernel32.lib"
  6. @(private="file")
  7. @(default_calling_convention="stdcall")
  8. foreign kernel32 {
  9. // NOTE(bill): The types are not using the standard names (e.g. DWORD and LPVOID) to just minimizing the dependency
  10. // os_write
  11. GetStdHandle :: proc(which: u32) -> rawptr ---
  12. SetHandleInformation :: proc(hObject: rawptr, dwMask: u32, dwFlags: u32) -> b32 ---
  13. WriteFile :: proc(hFile: rawptr, lpBuffer: rawptr, nNumberOfBytesToWrite: u32, lpNumberOfBytesWritten: ^u32, lpOverlapped: rawptr) -> b32 ---
  14. GetLastError :: proc() -> u32 ---
  15. // default_allocator
  16. GetProcessHeap :: proc() -> rawptr ---
  17. HeapAlloc :: proc(hHeap: rawptr, dwFlags: u32, dwBytes: uint) -> rawptr ---
  18. HeapReAlloc :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr, dwBytes: uint) -> rawptr ---
  19. HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 ---
  20. }
  21. _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_bounds_check {
  22. if len(data) == 0 {
  23. return 0, 0
  24. }
  25. STD_ERROR_HANDLE :: ~u32(0) -12 + 1
  26. HANDLE_FLAG_INHERIT :: 0x00000001
  27. MAX_RW :: 1<<30
  28. h := GetStdHandle(STD_ERROR_HANDLE)
  29. when size_of(uintptr) == 8 {
  30. SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0)
  31. }
  32. single_write_length: u32
  33. total_write: i64
  34. length := i64(len(data))
  35. for total_write < length {
  36. remaining := length - total_write
  37. to_write := u32(min(i32(remaining), MAX_RW))
  38. e := WriteFile(h, &data[total_write], to_write, &single_write_length, nil)
  39. if single_write_length <= 0 || !e {
  40. err = _OS_Errno(GetLastError())
  41. n = int(total_write)
  42. return
  43. }
  44. total_write += i64(single_write_length)
  45. }
  46. n = int(total_write)
  47. return
  48. }
  49. heap_alloc :: proc "contextless" (size: int) -> rawptr {
  50. HEAP_ZERO_MEMORY :: 0x00000008
  51. return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size))
  52. }
  53. heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
  54. if new_size == 0 {
  55. heap_free(ptr)
  56. return nil
  57. }
  58. if ptr == nil {
  59. return heap_alloc(new_size)
  60. }
  61. HEAP_ZERO_MEMORY :: 0x00000008
  62. return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, uint(new_size))
  63. }
  64. heap_free :: proc "contextless" (ptr: rawptr) {
  65. if ptr == nil {
  66. return
  67. }
  68. HeapFree(GetProcessHeap(), 0, ptr)
  69. }
  70. //
  71. // NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
  72. // Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
  73. // padding. We also store the original pointer returned by heap_alloc right before
  74. // the pointer we return to the user.
  75. //
  76. _windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, Allocator_Error) {
  77. if size == 0 {
  78. _windows_default_free(old_ptr)
  79. return nil, nil
  80. }
  81. a := max(alignment, align_of(rawptr))
  82. space := size + a - 1
  83. allocated_mem: rawptr
  84. if old_ptr != nil {
  85. original_old_ptr := intrinsics.ptr_offset((^rawptr)(old_ptr), -1)^
  86. allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
  87. } else {
  88. allocated_mem = heap_alloc(space+size_of(rawptr))
  89. }
  90. aligned_mem := rawptr(intrinsics.ptr_offset((^u8)(allocated_mem), size_of(rawptr)))
  91. ptr := uintptr(aligned_mem)
  92. aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a)
  93. diff := int(aligned_ptr - ptr)
  94. if (size + diff) > space {
  95. return nil, .Out_Of_Memory
  96. }
  97. aligned_mem = rawptr(aligned_ptr)
  98. intrinsics.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem
  99. return byte_slice(aligned_mem, size), nil
  100. }
  101. _windows_default_alloc :: proc "contextless" (size, alignment: int) -> ([]byte, Allocator_Error) {
  102. return _windows_default_alloc_or_resize(size, alignment, nil)
  103. }
  104. _windows_default_free :: proc "contextless" (ptr: rawptr) {
  105. if ptr != nil {
  106. heap_free(intrinsics.ptr_offset((^rawptr)(ptr), -1)^)
  107. }
  108. }
  109. _windows_default_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, Allocator_Error) {
  110. return _windows_default_alloc_or_resize(new_size, new_alignment, p)
  111. }