Add raw_data to replace cases in which &x[0] was used

This commit is contained in:
gingerBill
2020-06-29 15:58:24 +01:00
parent 56a52a1d06
commit 86448ee044
15 changed files with 129 additions and 38 deletions
+9 -9
View File
@@ -247,7 +247,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
raw_alloc :: proc(s: ^Stack, size, alignment: int) -> rawptr {
curr_addr := uintptr(&s.data[0]) + uintptr(s.curr_offset);
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.curr_offset);
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Stack_Allocation_Header));
if s.curr_offset + padding + size > len(s.data) {
return nil;
@@ -274,7 +274,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
if old_memory == nil {
return nil;
}
start := uintptr(&s.data[0]);
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
curr_addr := uintptr(old_memory);
@@ -288,7 +288,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
if old_offset != int(header.prev_offset) {
panic("Out of order stack allocator free");
@@ -310,7 +310,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
return nil;
}
start := uintptr(&s.data[0]);
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
curr_addr := uintptr(old_memory);
if !(start <= curr_addr && curr_addr < end) {
@@ -327,7 +327,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
if old_offset != int(header.prev_offset) {
ptr := raw_alloc(s, size, alignment);
@@ -392,7 +392,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
align := 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);
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.offset);
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header));
if s.offset + padding + size > len(s.data) {
return nil;
@@ -417,7 +417,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
if old_memory == nil {
return nil;
}
start := uintptr(&s.data[0]);
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
curr_addr := uintptr(old_memory);
@@ -431,7 +431,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
}
header := (^Small_Stack_Allocation_Header)(curr_addr - size_of(Small_Stack_Allocation_Header));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
s.offset = int(old_offset);
@@ -446,7 +446,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
return nil;
}
start := uintptr(&s.data[0]);
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
curr_addr := uintptr(old_memory);
if !(start <= curr_addr && curr_addr < end) {
+15 -9
View File
@@ -13,9 +13,7 @@ zero_item :: inline proc(item: $P/^$T) {
set(item, 0, size_of(T));
}
zero_slice :: proc(data: $T/[]$E) {
if n := len(data); n > 0 {
zero(&data[0], size_of(E)*n);
}
zero(raw_data(data), size_of(E)*n);
}
@@ -26,9 +24,7 @@ copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr {
return runtime.mem_copy_non_overlapping(dst, src, len);
}
compare :: inline proc(a, b: []byte) -> int {
// NOTE(tetra): no-abc is okay here because if the slices are empty, `&a[0]` is just nil+0 == nil, which
// compare_byte_ptrs handles fine when the passed length is also zero.
res := #no_bounds_check compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b)));
res := compare_byte_ptrs(raw_data(a), raw_data(b), min(len(a), len(b)));
if res == 0 && len(a) != len(b) {
return len(a) <= len(b) ? -1 : +1;
} else if len(a) == 0 && len(b) == 0 {
@@ -38,6 +34,17 @@ compare :: inline proc(a, b: []byte) -> int {
}
compare_byte_ptrs :: proc(a, b: ^byte, n: int) -> int #no_bounds_check {
switch {
case a == b:
return 0;
case a == nil:
return -1;
case b == nil:
return -1;
case n == 0:
return 0;
}
x := slice_ptr(a, n);
y := slice_ptr(b, n);
@@ -121,11 +128,10 @@ slice_to_components :: proc(slice: $E/[]$T) -> (data: ^T, len: int) {
}
buffer_from_slice :: inline proc(backing: $T/[]$E) -> [dynamic]E {
s := transmute(Raw_Slice)backing;
return transmute([dynamic]E)Raw_Dynamic_Array{
data = s.data,
data = raw_data(s),
len = 0,
cap = s.len,
cap = len(s),
allocator = nil_allocator(),
};
}
+4 -1
View File
@@ -42,6 +42,9 @@ make_any :: inline proc(data: rawptr, id: typeid) -> any {
return transmute(any)Raw_Any{data, id};
}
raw_array_data :: proc(a: $P/^($T/[$N]$E)) -> ^E {
return (^E)(a);
}
raw_string_data :: inline proc(s: $T/string) -> ^byte {
return (transmute(Raw_String)s).data;
}
@@ -52,6 +55,6 @@ raw_dynamic_array_data :: inline proc(a: $T/[dynamic]$E) -> ^E {
return cast(^E)(transmute(Raw_Dynamic_Array)a).data;
}
raw_data :: proc{raw_string_data, raw_slice_data, raw_dynamic_array_data};
raw_data :: proc{raw_array_data, raw_string_data, raw_slice_data, raw_dynamic_array_data};