virtual_platform.odin 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #+private
  2. package mem_virtual
  3. Platform_Memory_Block :: struct {
  4. block: Memory_Block,
  5. committed: uint,
  6. reserved: uint,
  7. }
  8. @(no_sanitize_address)
  9. platform_memory_alloc :: proc "contextless" (to_commit, to_reserve: uint) -> (block: ^Platform_Memory_Block, err: Allocator_Error) {
  10. to_commit, to_reserve := to_commit, to_reserve
  11. to_reserve = max(to_commit, to_reserve)
  12. total_to_reserved := max(to_reserve, size_of(Platform_Memory_Block))
  13. to_commit = clamp(to_commit, size_of(Platform_Memory_Block), total_to_reserved)
  14. data := reserve(total_to_reserved) or_return
  15. commit_err := commit(raw_data(data), to_commit)
  16. assert_contextless(commit_err == nil)
  17. block = (^Platform_Memory_Block)(raw_data(data))
  18. block.committed = to_commit
  19. block.reserved = to_reserve
  20. return
  21. }
  22. @(no_sanitize_address)
  23. platform_memory_free :: proc "contextless" (block: ^Platform_Memory_Block) {
  24. if block != nil {
  25. release(block, block.reserved)
  26. }
  27. }
  28. @(no_sanitize_address)
  29. platform_memory_commit :: proc "contextless" (block: ^Platform_Memory_Block, to_commit: uint) -> (err: Allocator_Error) {
  30. if to_commit < block.committed {
  31. return nil
  32. }
  33. if to_commit > block.reserved {
  34. return .Out_Of_Memory
  35. }
  36. commit(block, to_commit) or_return
  37. block.committed = to_commit
  38. return nil
  39. }