aboutsummaryrefslogtreecommitdiff
path: root/core/mem.odin
blob: ee358fef480dffa737e52d6c844bd54eed5823df (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#import "fmt.odin";
#import "os.odin";

set :: proc(data: rawptr, value: i32, len: int) -> rawptr #link_name "__mem_set" {
	llvm_memset_64bit :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) #foreign "llvm.memset.p0i8.i64"
	llvm_memset_64bit(data, value as byte, len, 1, false);
	return data;
}

zero :: proc(data: rawptr, len: int) -> rawptr #link_name "__mem_zero" {
	return set(data, 0, len);
}

copy :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy" {
	// NOTE(bill): This _must_ implemented like C's memmove
	llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memmove.p0i8.p0i8.i64"
	llvm_memmove_64bit(dst, src, len, 1, false);
	return dst;
}

copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy_non_overlapping" {
	// NOTE(bill): This _must_ implemented like C's memcpy
	llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memcpy.p0i8.p0i8.i64"
	llvm_memcpy_64bit(dst, src, len, 1, false);
	return dst;
}

compare :: proc(dst, src: rawptr, n: int) -> int #link_name "__mem_compare" {
	a := slice_ptr(dst as ^byte, n);
	b := slice_ptr(src as ^byte, n);
	for i : 0..<n {
		match {
		case a[i] < b[i]:
			return -1;
		case a[i] > b[i]:
			return +1;
		}
	}
	return 0;
}



kilobytes :: proc(x: int) -> int #inline { return          (x) * 1024; }
megabytes :: proc(x: int) -> int #inline { return kilobytes(x) * 1024; }
gigabytes :: proc(x: int) -> int #inline { return gigabytes(x) * 1024; }
terabytes :: proc(x: int) -> int #inline { return terabytes(x) * 1024; }

is_power_of_two :: proc(x: int) -> bool {
	if x <= 0 {
		return false;
	}
	return (x & (x-1)) == 0;
}

align_forward :: proc(ptr: rawptr, align: int) -> rawptr {
	assert(is_power_of_two(align));

	a := align as uint;
	p := ptr as uint;
	modulo := p & (a-1);
	if modulo != 0 {
		p += a - modulo;
	}
	return p as rawptr;
}



Allocation_Header :: struct {
	size: int;
}

allocation_header_fill :: proc(header: ^Allocation_Header, data: rawptr, size: int) {
	header.size = size;
	ptr := (header+1) as ^int;

	while i := 0; ptr as rawptr < data {
		(ptr+i)^ = -1;
		i += 1;
	}
}
allocation_header :: proc(data: rawptr) -> ^Allocation_Header {
	p := data as ^int;
	while (p-1)^ == -1 {
		p = (p-1);
	}
	return (p as ^Allocation_Header)-1;
}





// Custom allocators
Arena :: struct {
	backing:    Allocator;
	offset:     int;
	memory:     []byte;
	temp_count: int;
}

Arena_Temp_Memory :: struct {
	arena:          ^Arena;
	original_count: int;
}





init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
	backing    = Allocator{};
	memory     = data[:0];
	temp_count = 0;
}

init_arena_from_context :: proc(using a: ^Arena, size: int) {
	backing = context.allocator;
	memory = new_slice(byte, size);
	temp_count = 0;
}

free_arena :: proc(using a: ^Arena) {
	if backing.procedure != nil {
		push_allocator backing {
			free(memory.data);
			memory = memory[0:0];
			offset = 0;
		}
	}
}

arena_allocator :: proc(arena: ^Arena) -> Allocator {
	return Allocator{
		procedure = arena_allocator_proc,
		data = arena,
	};
}

arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
                          size, alignment: int,
                          old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
	using Allocator_Mode;
	arena := allocator_data as ^Arena;

	match mode {
	case ALLOC:
		total_size := size + alignment;

		if arena.offset + total_size > arena.memory.count {
			fmt.fprintln(os.stderr, "Arena out of memory");
			return nil;
		}

		#no_bounds_check end := ^arena.memory[arena.offset];

		ptr := align_forward(end, alignment);
		arena.offset += total_size;
		return zero(ptr, size);

	case FREE:
		// NOTE(bill): Free all at once
		// Use Arena_Temp_Memory if you want to free a block

	case FREE_ALL:
		arena.offset = 0;

	case RESIZE:
		return default_resize_align(old_memory, old_size, size, alignment);
	}

	return nil;
}

begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
	tmp: Arena_Temp_Memory;
	tmp.arena = a;
	tmp.original_count = a.memory.count;
	a.temp_count += 1;
	return tmp;
}

end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
	assert(arena.memory.count >= original_count);
	assert(arena.temp_count > 0);
	arena.memory.count = original_count;
	arena.temp_count -= 1;
}







align_of_type_info :: proc(type_info: ^Type_Info) -> int {
	prev_pow2 :: proc(n: i64) -> i64 {
		if n <= 0 {
			return 0;
		}
		n |= n >> 1;
		n |= n >> 2;
		n |= n >> 4;
		n |= n >> 8;
		n |= n >> 16;
		n |= n >> 32;
		return n - (n >> 1);
	}

	WORD_SIZE :: size_of(int);
	MAX_ALIGN :: size_of([vector 64]f64); // TODO(bill): Should these constants be builtin constants?
	using Type_Info;

	match type info : type_info {
	case Named:
		return align_of_type_info(info.base);
	case Integer:
		return info.size;
	case Float:
		return info.size;
	case String:
		return WORD_SIZE;
	case Boolean:
		return 1;
	case Pointer:
		return WORD_SIZE;
	case Maybe:
		return max(align_of_type_info(info.elem), 1);
	case Procedure:
		return WORD_SIZE;
	case Array:
		return align_of_type_info(info.elem);
	case Slice:
		return WORD_SIZE;
	case Vector:
		size := size_of_type_info(info.elem);
		count := max(prev_pow2(info.count as i64), 1) as int;
		total := size * count;
		return clamp(total, 1, MAX_ALIGN);
	case Struct:
		return info.align;
	case Union:
		return info.align;
	case Raw_Union:
		return info.align;
	}

	return 0;
}

align_formula :: proc(size, align: int) -> int {
	result := size + align-1;
	return result - result%align;
}

size_of_type_info :: proc(type_info: ^Type_Info) -> int {
	WORD_SIZE :: size_of(int);
	using Type_Info;
	match type info : type_info {
	case Named:
		return size_of_type_info(info.base);
	case Integer:
		return info.size;
	case Float:
		return info.size;
	case Any:
		return 2*WORD_SIZE;
	case String:
		return 2*WORD_SIZE;
	case Boolean:
		return 1;
	case Pointer:
		return WORD_SIZE;
	case Maybe:
		return size_of_type_info(info.elem) + 1;
	case Procedure:
		return WORD_SIZE;
	case Array:
		count := info.count;
		if count == 0 {
			return 0;
		}
		size      := size_of_type_info(info.elem);
		align     := align_of_type_info(info.elem);
		alignment := align_formula(size, align);
		return alignment*(count-1) + size;
	case Slice:
		return 3*WORD_SIZE;
	case Vector:
		is_bool :: proc(type_info: ^Type_Info) -> bool {
			match type info : type_info {
			case Named:
				return is_bool(info.base);
			case Boolean:
				return true;
			}
			return false;
		}

		count := info.count;
		if count == 0 {
			return 0;
		}
		bit_size := 8*size_of_type_info(info.elem);
		if is_bool(info.elem) {
			// NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
			// Silly LLVM spec
			bit_size = 1;
		}
		total_size_in_bits := bit_size * count;
		total_size := (total_size_in_bits+7)/8;
		return total_size;

	case Struct:
		return info.size;
	case Union:
		return info.size;
	case Raw_Union:
		return info.size;
	}

	return 0;
}