virtual_platform.odin 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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(raw_data(data), to_commit)
  15. block = (^Platform_Memory_Block)(raw_data(data))
  16. block.committed = to_commit
  17. block.reserved = to_reserve
  18. return
  19. }
  20. platform_memory_free :: proc "contextless" (block: ^Platform_Memory_Block) {
  21. if block != nil {
  22. release(block, block.reserved)
  23. }
  24. }
  25. platform_memory_commit :: proc "contextless" (block: ^Platform_Memory_Block, to_commit: uint) -> (err: Allocator_Error) {
  26. if to_commit < block.committed {
  27. return nil
  28. }
  29. if to_commit > block.reserved {
  30. return .Out_Of_Memory
  31. }
  32. commit(block, to_commit) or_return
  33. block.committed = to_commit
  34. return nil
  35. }