heap_allocator_windows.odin 1.1 KB

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