aboutsummaryrefslogtreecommitdiff
path: root/core/runtime/default_allocators.odin
blob: a4ba9f33b9adbc29a73ae5c68131236d7bb5b741 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package runtime

import "core:os"

default_allocator_proc :: os.heap_allocator_proc;

default_allocator :: proc() -> Allocator {
	return os.heap_allocator();
}


Default_Temp_Allocator :: struct {
	data:     []byte,
	curr_offset: int,
	prev_offset: int,
	backup_allocator: Allocator,
	leaked_allocations: [dynamic]rawptr,
}

default_temp_allocator_init :: proc(allocator: ^Default_Temp_Allocator, data: []byte, backup_allocator := context.allocator) {
	allocator.data = data;
	allocator.curr_offset = 0;
	allocator.prev_offset = 0;
	allocator.backup_allocator = backup_allocator;
	allocator.leaked_allocations.allocator = backup_allocator;
}

default_temp_allocator_destroy :: proc(using allocator: ^Default_Temp_Allocator) {
	if allocator == nil {
		return;
	}
	for ptr in leaked_allocations {
		free(ptr, backup_allocator);
	}
	delete(leaked_allocations);
	delete(data, backup_allocator);
	allocator^ = {};
}

default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
                                    size, alignment: int,
                                    old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {

	allocator := (^Default_Temp_Allocator)(allocator_data);

	if allocator.data == nil {
		DEFAULT_SCRATCH_BACKING_SIZE :: 1<<22;
		a := context.allocator;
		if !(context.allocator.procedure != default_temp_allocator_proc &&
		     context.allocator.data != allocator_data) {
			a = default_allocator();
		}
		default_temp_allocator_init(allocator, make([]byte, 1<<22, a), a);
	}

	switch mode {
	case .Alloc:
		switch {
		case allocator.curr_offset+size <= len(allocator.data):
			offset := align_forward_uintptr(uintptr(allocator.curr_offset), uintptr(alignment));
			ptr := &allocator.data[offset];
			mem_zero(ptr, size);
			allocator.prev_offset = int(offset);
			allocator.curr_offset = int(offset) + size;
			return ptr;
		case size <= len(allocator.data):
			offset := align_forward_uintptr(uintptr(0), uintptr(alignment));
			ptr := &allocator.data[offset];
			mem_zero(ptr, size);
			allocator.prev_offset = int(offset);
			allocator.curr_offset = int(offset) + size;
			return ptr;
		}
		// TODO(bill): Should leaks be notified about? Should probably use a logging system that is built into the context system
		a := allocator.backup_allocator;
		if a.procedure == nil {
			a = context.allocator;
			allocator.backup_allocator = a;
		}

		ptr := mem_alloc(size, alignment, a, loc);
		if allocator.leaked_allocations == nil {
			allocator.leaked_allocations = make([dynamic]rawptr, a);
		}
		append(&allocator.leaked_allocations, ptr);

		return ptr;

	case .Free:
		if len(allocator.data) == 0 {
			return nil;
		}
		last_ptr := rawptr(&allocator.data[allocator.prev_offset]);
		if old_memory == last_ptr {
			full_size := allocator.curr_offset - allocator.prev_offset;
			allocator.curr_offset = allocator.prev_offset;
			mem_zero(last_ptr, full_size);
			return nil;
		} else {
			#no_bounds_check start, end := &allocator.data[0], &allocator.data[allocator.curr_offset];
			if start <= old_memory && old_memory < end {
				// NOTE(bill): Cannot free this pointer
				return nil;
			}

			if len(allocator.leaked_allocations) != 0 {
				for ptr, i in allocator.leaked_allocations {
					if ptr == old_memory {
						free(ptr, allocator.backup_allocator);
						ordered_remove(&allocator.leaked_allocations, i);
						return nil;
					}
				}
			}
		}
		// NOTE(bill): It's a temporary memory, don't worry about freeing

	case .Free_All:
		allocator.curr_offset = 0;
		allocator.prev_offset = 0;
		for ptr in allocator.leaked_allocations {
			free(ptr, allocator.backup_allocator);
		}
		clear(&allocator.leaked_allocations);

	case .Resize:
		last_ptr := rawptr(&allocator.data[allocator.prev_offset]);
		if old_memory == last_ptr && len(allocator.data)-allocator.prev_offset >= size {
			allocator.curr_offset = allocator.prev_offset+size;
			return old_memory;
		}
		return default_temp_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc);
	}

	return nil;
}

default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
	return Allocator{
		procedure = default_temp_allocator_proc,
		data = allocator,
	};
}