mutex_allocator.odin 839 B

123456789101112131415161718192021222324252627282930313233
  1. //+build !freestanding
  2. package mem
  3. import "core:sync"
  4. Mutex_Allocator :: struct {
  5. backing: Allocator,
  6. mutex: sync.Mutex,
  7. }
  8. mutex_allocator_init :: proc(m: ^Mutex_Allocator, backing_allocator: Allocator) {
  9. m.backing = backing_allocator
  10. m.mutex = {}
  11. }
  12. @(require_results)
  13. mutex_allocator :: proc(m: ^Mutex_Allocator) -> Allocator {
  14. return Allocator{
  15. procedure = mutex_allocator_proc,
  16. data = m,
  17. }
  18. }
  19. mutex_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  20. size, alignment: int,
  21. old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) {
  22. m := (^Mutex_Allocator)(allocator_data)
  23. sync.mutex_guard(&m.mutex)
  24. return m.backing.procedure(m.backing.data, mode, size, alignment, old_memory, old_size, loc)
  25. }