aboutsummaryrefslogtreecommitdiff
path: root/core/os/os2/allocators.odin
blob: 8645328509ffc4ae404accb18827dfad0ab903a9 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#+private
package os2

import "base:runtime"

@(require_results)
file_allocator :: proc() -> runtime.Allocator {
	return heap_allocator()
}

temp_allocator_proc :: runtime.arena_allocator_proc

@(private="file")
MAX_TEMP_ARENA_COUNT :: 2

@(private="file", thread_local)
global_default_temp_allocator_arenas: [MAX_TEMP_ARENA_COUNT]runtime.Arena

@(private="file", thread_local)
global_default_temp_allocator_index: uint


@(require_results)
temp_allocator :: proc() -> runtime.Allocator {
	arena := &global_default_temp_allocator_arenas[global_default_temp_allocator_index]
	if arena.backing_allocator.procedure == nil {
		arena.backing_allocator = heap_allocator()
	}

	return runtime.Allocator{
		procedure = temp_allocator_proc,
		data      = arena,
	}
}



@(require_results)
temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
	temp = runtime.arena_temp_begin(&global_default_temp_allocator_arenas[global_default_temp_allocator_index], loc)
	return
}

temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
	runtime.arena_temp_end(temp, loc)
}

@(fini, private)
temp_allocator_fini :: proc() {
	for &arena in global_default_temp_allocator_arenas {
		runtime.arena_destroy(&arena)
	}
	global_default_temp_allocator_arenas = {}
}

TEMP_ALLOCATOR_GUARD_END :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
	runtime.arena_temp_end(temp, loc)
	if temp.arena != nil {
		global_default_temp_allocator_index = (global_default_temp_allocator_index-1)%MAX_TEMP_ARENA_COUNT
	}
}

@(deferred_out=TEMP_ALLOCATOR_GUARD_END)
TEMP_ALLOCATOR_GUARD :: #force_inline proc(loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
	global_default_temp_allocator_index = (global_default_temp_allocator_index+1)%MAX_TEMP_ARENA_COUNT
	tmp := temp_allocator_temp_begin(loc)
	return tmp, loc
}

@(init, private)
init_thread_local_cleaner :: proc() {
	runtime.add_thread_local_cleaner(temp_allocator_fini)
}