virtual_platform.odin 1.2 KB

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