virtual_platform.odin 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //+private
  2. package mem_virtual
  3. import sync "core:sync/sync2"
  4. Platform_Memory_Block :: struct {
  5. block: Memory_Block,
  6. reserved: uint,
  7. prev, next: ^Platform_Memory_Block,
  8. }
  9. platform_memory_alloc :: proc(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(raw_data(data), to_commit)
  16. block = (^Platform_Memory_Block)(raw_data(data))
  17. block.reserved = to_reserve
  18. return
  19. }
  20. platform_memory_free :: proc(block: ^Platform_Memory_Block) {
  21. if block != nil {
  22. release(block, block.reserved)
  23. }
  24. }
  25. platform_mutex_lock :: proc() {
  26. sync.mutex_lock(&global_memory_block_mutex)
  27. }
  28. platform_mutex_unlock :: proc() {
  29. sync.mutex_unlock(&global_memory_block_mutex)
  30. }
  31. global_memory_block_mutex: sync.Mutex
  32. global_platform_memory_block_sentinel: Platform_Memory_Block
  33. global_platform_memory_block_sentinel_set: bool
  34. @(init)
  35. platform_memory_init :: proc() {
  36. if !global_platform_memory_block_sentinel_set {
  37. _platform_memory_init()
  38. global_platform_memory_block_sentinel.prev = &global_platform_memory_block_sentinel
  39. global_platform_memory_block_sentinel.next = &global_platform_memory_block_sentinel
  40. global_platform_memory_block_sentinel_set = true
  41. }
  42. }