aboutsummaryrefslogtreecommitdiff
path: root/core/mem/mutex_allocator.odin
blob: 591703eab3874ebec7335f7a54409aa199548858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//+build !freestanding
package mem

import "core:sync"

Mutex_Allocator :: struct {
	backing: Allocator,
	mutex:   sync.Mutex,
}

mutex_allocator_init :: proc(m: ^Mutex_Allocator, backing_allocator: Allocator) {
	m.backing = backing_allocator
	m.mutex = {}
}


@(require_results)
mutex_allocator :: proc(m: ^Mutex_Allocator) -> Allocator {
	return Allocator{
		procedure = mutex_allocator_proc,
		data = m,
	}
}

mutex_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
                             size, alignment: int,
                             old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) {
	m := (^Mutex_Allocator)(allocator_data)

	sync.mutex_guard(&m.mutex)
	return m.backing.procedure(m.backing.data, mode, size, alignment, old_memory, old_size, loc)
}