heap_allocator_windows.odin 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package runtime
  2. import "../sanitizer"
  3. foreign import kernel32 "system:Kernel32.lib"
  4. @(private="file")
  5. @(default_calling_convention="system")
  6. foreign kernel32 {
  7. // NOTE(bill): The types are not using the standard names (e.g. DWORD and LPVOID) to just minimizing the dependency
  8. // default_allocator
  9. GetProcessHeap :: proc() -> rawptr ---
  10. HeapAlloc :: proc(hHeap: rawptr, dwFlags: u32, dwBytes: uint) -> rawptr ---
  11. HeapReAlloc :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr, dwBytes: uint) -> rawptr ---
  12. HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 ---
  13. }
  14. _heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
  15. HEAP_ZERO_MEMORY :: 0x00000008
  16. ptr := HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY if zero_memory else 0, uint(size))
  17. // NOTE(lucas): asan not guarunteed to unpoison win32 heap out of the box, do it ourselves
  18. sanitizer.address_unpoison(ptr, size)
  19. return ptr
  20. }
  21. _heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
  22. if new_size == 0 {
  23. _heap_free(ptr)
  24. return nil
  25. }
  26. if ptr == nil {
  27. return _heap_alloc(new_size)
  28. }
  29. HEAP_ZERO_MEMORY :: 0x00000008
  30. new_ptr := HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, uint(new_size))
  31. // NOTE(lucas): asan not guarunteed to unpoison win32 heap out of the box, do it ourselves
  32. sanitizer.address_unpoison(new_ptr, new_size)
  33. return new_ptr
  34. }
  35. _heap_free :: proc "contextless" (ptr: rawptr) {
  36. if ptr == nil {
  37. return
  38. }
  39. HeapFree(GetProcessHeap(), 0, ptr)
  40. }