aboutsummaryrefslogtreecommitdiff
path: root/core/mem
diff options
context:
space:
mode:
Diffstat (limited to 'core/mem')
-rw-r--r--core/mem/allocators.odin30
-rw-r--r--core/mem/mem.odin8
2 files changed, 22 insertions, 16 deletions
diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin
index af6ed329a..77cc0abf1 100644
--- a/core/mem/allocators.odin
+++ b/core/mem/allocators.odin
@@ -47,12 +47,10 @@ arena_allocator :: proc(arena: ^Arena) -> Allocator {
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
- using Allocator_Mode;
arena := cast(^Arena)allocator_data;
-
switch mode {
- case Alloc:
+ case .Alloc:
total_size := size + alignment;
if arena.offset + total_size > len(arena.data) {
@@ -66,14 +64,14 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
arena.peak_used = max(arena.peak_used, arena.offset);
return zero(ptr, size);
- case Free:
+ case .Free:
// NOTE(bill): Free all at once
// Use Arena_Temp_Memory if you want to free a block
- case Free_All:
+ case .Free_All:
arena.offset = 0;
- case Resize:
+ case .Resize:
return default_resize_align(old_memory, old_size, size, alignment, arena_allocator(arena));
}
@@ -227,7 +225,6 @@ stack_allocator :: proc(stack: ^Stack) -> Allocator {
stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
- using Allocator_Mode;
s := cast(^Stack)allocator_data;
if s.data == nil {
@@ -256,9 +253,9 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
switch mode {
- case Alloc:
+ case .Alloc:
return raw_alloc(s, size, alignment);
- case Free:
+ case .Free:
if old_memory == nil {
return nil;
}
@@ -288,11 +285,11 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
s.prev_offset = int(header.prev_offset);
- case Free_All:
+ case .Free_All:
s.prev_offset = 0;
s.curr_offset = 0;
- case Resize:
+ case .Resize:
if old_memory == nil {
return raw_alloc(s, size, alignment);
}
@@ -374,13 +371,14 @@ small_stack_allocator :: proc(stack: ^Small_Stack) -> Allocator {
small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
- using Allocator_Mode;
s := cast(^Small_Stack)allocator_data;
if s.data == nil {
return nil;
}
+ alignment = clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2);
+
raw_alloc :: proc(s: ^Small_Stack, size, alignment: int) -> rawptr {
curr_addr := uintptr(&s.data[0]) + uintptr(s.offset);
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header));
@@ -401,9 +399,9 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
switch mode {
- case Alloc:
+ case .Alloc:
return raw_alloc(s, size, alignment);
- case Free:
+ case .Free:
if old_memory == nil {
return nil;
}
@@ -426,10 +424,10 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
s.offset = int(old_offset);
- case Free_All:
+ case .Free_All:
s.offset = 0;
- case Resize:
+ case .Resize:
if old_memory == nil {
return raw_alloc(s, size, alignment);
}
diff --git a/core/mem/mem.odin b/core/mem/mem.odin
index 4bb94ba0f..47591437f 100644
--- a/core/mem/mem.odin
+++ b/core/mem/mem.odin
@@ -183,6 +183,14 @@ align_forward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
return uintptr(p);
}
+
+align_forward_int :: inline proc(ptr, align: int) -> int {
+ return int(align_forward_uintptr(uintptr(ptr), uintptr(align)));
+}
+align_forward_uint :: inline proc(ptr, align: uint) -> uint {
+ return uint(align_forward_uintptr(uintptr(ptr), uintptr(align)));
+}
+
context_from_allocator :: proc(a: Allocator) -> type_of(context) {
context.allocator = a;
return context;