From 58e4a011c72d268ca35d46d2288d5858677e9cac Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Mon, 4 Dec 2023 00:08:13 -0800 Subject: [PATCH 1/9] add non-zeroing append and resize --- core/mem/alloc.odin | 2 + core/mem/allocators.odin | 32 ++++--- core/os/os.odin | 14 +-- core/runtime/core.odin | 1 + core/runtime/core_builtin.odin | 105 ++++++++++++++++++--- core/runtime/default_allocators_arena.odin | 2 +- core/runtime/default_allocators_nil.odin | 6 +- core/runtime/internal.odin | 27 +++++- 8 files changed, 147 insertions(+), 42 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 57e82a5f8..507b13d6f 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -11,6 +11,8 @@ Allocator_Mode :: enum byte { Free_All, Resize, Query_Features, + Alloc_Non_Zeroed, + Resize_Non_Zeroed, } */ diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index 4f5dd8eef..362b897b4 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -85,7 +85,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Free_All: arena.offset = 0 - case .Resize: + case .Resize, .Resize_Non_Zeroed: return default_resize_bytes_align(byte_slice(old_memory, old_size), size, alignment, arena_allocator(arena)) case .Query_Features: @@ -259,7 +259,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, } clear(&s.leaked_allocations) - case .Resize: + case .Resize, .Resize_Non_Zeroed: begin := uintptr(raw_data(s.data)) end := begin + uintptr(len(s.data)) old_ptr := uintptr(old_memory) @@ -278,7 +278,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features} } return nil, nil @@ -406,9 +406,9 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, s.prev_offset = 0 s.curr_offset = 0 - case .Resize: + case .Resize, .Resize_Non_Zeroed: if old_memory == nil { - return raw_alloc(s, size, alignment, true) + return raw_alloc(s, size, alignment, mode == .Resize) } if size == 0 { return nil, nil @@ -434,7 +434,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data))) if old_offset != header.prev_offset { - data, err := raw_alloc(s, size, alignment, true) + data, err := raw_alloc(s, size, alignment, mode == .Resize) if err == nil { runtime.copy(data, byte_slice(old_memory, old_size)) } @@ -455,7 +455,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features} } return nil, nil case .Query_Info: @@ -565,9 +565,9 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Free_All: s.offset = 0 - case .Resize: + case .Resize, .Resize_Non_Zeroed: if old_memory == nil { - return raw_alloc(s, size, align, true) + return raw_alloc(s, size, align, mode == .Resize) } if size == 0 { return nil, nil @@ -590,7 +590,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, return byte_slice(old_memory, size), nil } - data, err := raw_alloc(s, size, align, true) + data, err := raw_alloc(s, size, align, mode == .Resize) if err == nil { runtime.copy(data, byte_slice(old_memory, old_size)) } @@ -599,7 +599,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features} } return nil, nil @@ -649,7 +649,7 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode case .Free_All: dynamic_pool_free_all(pool) return nil, nil - case .Resize: + case .Resize, .Resize_Non_Zeroed: if old_size >= size { return byte_slice(old_memory, size), nil } @@ -662,7 +662,7 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features, .Query_Info} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features, .Query_Info} } return nil, nil @@ -826,6 +826,10 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, if size > 0 { panic("mem: panic allocator, .Resize called", loc=loc) } + case .Resize_Non_Zeroed: + if size > 0 { + panic("mem: panic allocator, .Resize_Non_Zeroed called", loc=loc) + } case .Free: if old_memory != nil { panic("mem: panic allocator, .Free called", loc=loc) @@ -958,7 +962,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, if data.clear_on_free_all { clear_map(&data.allocation_map) } - case .Resize: + case .Resize, .Resize_Non_Zeroed: if old_memory != result_ptr { delete_key(&data.allocation_map, old_memory) } diff --git a/core/os/os.odin b/core/os/os.odin index 15864e47e..3210a39d0 100644 --- a/core/os/os.odin +++ b/core/os/os.odin @@ -210,15 +210,15 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, } } - aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> (new_memory: []byte, err: mem.Allocator_Error) { + aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int, zero_memory := true) -> (new_memory: []byte, err: mem.Allocator_Error) { if p == nil { return nil, nil } - new_memory = aligned_alloc(new_size, new_alignment, p) or_return + new_memory = aligned_alloc(new_size, new_alignment, p, zero_memory) or_return // NOTE: heap_resize does not zero the new memory, so we do it - if new_size > old_size { + if zero_memory && new_size > old_size { new_region := mem.raw_data(new_memory[old_size:]) mem.zero(new_region, new_size - old_size) } @@ -235,16 +235,16 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, case .Free_All: return nil, .Mode_Not_Implemented - case .Resize: + case .Resize, .Resize_Non_Zeroed: if old_memory == nil { - return aligned_alloc(size, alignment) + return aligned_alloc(size, alignment, nil, mode == .Resize) } - return aligned_resize(old_memory, old_size, size, alignment) + return aligned_resize(old_memory, old_size, size, alignment, mode == .Resize) case .Query_Features: set := (^mem.Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Resize_Non_Zeroed, .Query_Features} } return nil, nil diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 2d176f909..0832ddd13 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -306,6 +306,7 @@ Allocator_Mode :: enum byte { Query_Features, Query_Info, Alloc_Non_Zeroed, + Resize_Non_Zeroed, } Allocator_Mode_Set :: distinct bit_set[Allocator_Mode] diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index 0348a93df..b597c2486 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -169,10 +169,16 @@ clear :: proc{clear_dynamic_array, clear_map} @builtin reserve :: proc{reserve_dynamic_array, reserve_map} +@builtin +non_zero_reserve :: proc{non_zero_reserve_dynamic_array} + // `resize` will try to resize memory of a passed dynamic array or map to the requested element count (setting the `len`, and possibly `cap`). @builtin resize :: proc{resize_dynamic_array} +@builtin +non_zero_resize :: proc{non_zero_resize_dynamic_array} + // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity. @builtin shrink :: proc{shrink_dynamic_array, shrink_map} @@ -404,10 +410,7 @@ delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: return } - - -@builtin -append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { +_append_elem :: #force_inline proc(array: ^$T/[dynamic]$E, arg: E, should_zero: bool, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { if array == nil { return 0, nil } @@ -418,7 +421,13 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> } else { if cap(array) < len(array)+1 { cap := 2 * cap(array) + max(8, 1) - err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success + + // do not 'or_return' here as it could be a partial success + if should_zero { + err = reserve(array, cap, loc) + } else { + err = non_zero_reserve(array, cap, loc) + } } if cap(array)-len(array) > 0 { a := (^Raw_Dynamic_Array)(array) @@ -435,7 +444,16 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> } @builtin -append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { +append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + return _append_elem(array, arg, true, loc=loc) +} + +@builtin +non_zero_append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + return _append_elem(array, arg, false, loc=loc) +} + +_append_elems :: #force_inline proc(array: ^$T/[dynamic]$E, should_zero: bool, loc := #caller_location, args: ..E) -> (n: int, err: Allocator_Error) #optional_allocator_error { if array == nil { return 0, nil } @@ -452,7 +470,13 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) } else { if cap(array) < len(array)+arg_len { cap := 2 * cap(array) + max(8, arg_len) - err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success + + // do not 'or_return' here as it could be a partial success + if should_zero { + err = reserve(array, cap, loc) + } else { + err = non_zero_reserve(array, cap, loc) + } } arg_len = min(cap(array)-len(array), arg_len) if arg_len > 0 { @@ -468,11 +492,33 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) } } +@builtin +append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + return _append_elems(array, true, loc, ..args) +} + +@builtin +non_zero_append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + return _append_elems(array, false, loc, ..args) +} + // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type +_append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, should_zero: bool, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + args := transmute([]E)arg + if should_zero { + return append_elems(array, ..args, loc=loc) + } else { + return non_zero_append_elems(array, ..args, loc=loc) + } +} + @builtin append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { - args := transmute([]E)arg - return append_elems(array, ..args, loc=loc) + return _append_elem_string(array, arg, true, loc) +} +@builtin +non_zero_append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error { + return _append_elem_string(array, arg, false, loc) } @@ -492,6 +538,7 @@ append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_ // The append built-in procedure appends elements to the end of a dynamic array @builtin append :: proc{append_elem, append_elems, append_elem_string} +@builtin non_zero_append :: proc{non_zero_append_elem, non_zero_append_elems, non_zero_append_elem_string} @builtin @@ -633,8 +680,7 @@ clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) { // `reserve_dynamic_array` will try to reserve memory of a passed dynamic array or map to the requested element count (setting the `cap`). // // Note: Prefer the procedure group `reserve`. -@builtin -reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error { +_reserve_dynamic_array :: #force_inline proc(array: ^$T/[dynamic]$E, capacity: int, should_zero: bool, loc := #caller_location) -> Allocator_Error { if array == nil { return nil } @@ -653,7 +699,12 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal new_size := capacity * size_of(E) allocator := a.allocator - new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + new_data: []byte + if should_zero { + new_data = mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + } else { + new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + } if new_data == nil && new_size > 0 { return .Out_Of_Memory } @@ -663,11 +714,20 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal return nil } +@builtin +reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error { + return _reserve_dynamic_array(array, capacity, true, loc) +} + +@builtin +non_zero_reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error { + return _reserve_dynamic_array(array, capacity, false, loc) +} + // `resize_dynamic_array` will try to resize memory of a passed dynamic array or map to the requested element count (setting the `len`, and possibly `cap`). // // Note: Prefer the procedure group `resize` -@builtin -resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error { +_resize_dynamic_array :: #force_inline proc(array: ^$T/[dynamic]$E, length: int, should_zero: bool, loc := #caller_location) -> Allocator_Error { if array == nil { return nil } @@ -687,7 +747,12 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller new_size := length * size_of(E) allocator := a.allocator - new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + new_data : []byte + if should_zero { + new_data = mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + } else { + new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return + } if new_data == nil && new_size > 0 { return .Out_Of_Memory } @@ -698,6 +763,16 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller return nil } +@builtin +resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error { + return _resize_dynamic_array(array, length, true, loc=loc) +} + +@builtin +non_zero_resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error { + return _resize_dynamic_array(array, length, false, loc=loc) +} + /* Shrinks the capacity of a dynamic array down to the current length, or the given capacity. diff --git a/core/runtime/default_allocators_arena.odin b/core/runtime/default_allocators_arena.odin index 1c36c4f7c..3d62ba4eb 100644 --- a/core/runtime/default_allocators_arena.odin +++ b/core/runtime/default_allocators_arena.odin @@ -195,7 +195,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, err = .Mode_Not_Implemented case .Free_All: arena_free_all(arena, location) - case .Resize: + case .Resize, .Resize_Non_Zeroed: old_data := ([^]byte)(old_memory) switch { diff --git a/core/runtime/default_allocators_nil.odin b/core/runtime/default_allocators_nil.odin index eee6a7998..c882f5196 100644 --- a/core/runtime/default_allocators_nil.odin +++ b/core/runtime/default_allocators_nil.odin @@ -10,7 +10,7 @@ nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, return nil, .None case .Free_All: return nil, .Mode_Not_Implemented - case .Resize: + case .Resize, .Resize_Non_Zeroed: if size == 0 { return nil, .None } @@ -55,6 +55,10 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, if size > 0 { panic("panic allocator, .Resize called", loc=loc) } + case .Resize_Non_Zeroed: + if size > 0 { + panic("panic allocator, .Alloc_Non_Zeroed called", loc=loc) + } case .Free: if old_memory != nil { panic("panic allocator, .Free called", loc=loc) diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index d0e550743..7c91a5ce6 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -187,7 +187,7 @@ mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #calle return } -mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { +_mem_resize :: #force_inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, should_zero: bool, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { if allocator.procedure == nil { return nil, nil } @@ -198,15 +198,27 @@ mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAUL } return } else if ptr == nil { - return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + if should_zero { + return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + } else { + return allocator.procedure(allocator.data, .Alloc_Non_Zeroed, new_size, alignment, nil, 0, loc) + } } else if old_size == new_size && uintptr(ptr) % uintptr(alignment) == 0 { data = ([^]byte)(ptr)[:old_size] return } - data, err = allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc) + if should_zero { + data, err = allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc) + } else { + data, err = allocator.procedure(allocator.data, .Resize_Non_Zeroed, new_size, alignment, ptr, old_size, loc) + } if err == .Mode_Not_Implemented { - data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + if should_zero { + data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + } else { + data, err = allocator.procedure(allocator.data, .Alloc_Non_Zeroed, new_size, alignment, nil, 0, loc) + } if err != nil { return } @@ -216,6 +228,13 @@ mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAUL return } +mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { + return _mem_resize(ptr, old_size, new_size, alignment, allocator, true, loc) +} +non_zero_mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { + return _mem_resize(ptr, old_size, new_size, alignment, allocator, false, loc) +} + memory_equal :: proc "contextless" (x, y: rawptr, n: int) -> bool { switch { case n == 0: return true From bfbeb23f5479af375ceea6d97d2add64a6bc6519 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Mon, 4 Dec 2023 03:09:13 -0800 Subject: [PATCH 2/9] add resize non zeroed in more places --- core/log/log_allocator.odin | 9 ++++++++- core/mem/virtual/arena.odin | 2 +- core/runtime/default_allocators_windows.odin | 4 ++-- vendor/commonmark/cmark.odin | 4 ++-- vendor/raylib/raylib.odin | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin index 934f0d643..846a84a62 100644 --- a/core/log/log_allocator.odin +++ b/core/log/log_allocator.odin @@ -80,6 +80,13 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, la.prefix, padding, old_memory, old_size, size, alignment, location = location, ) + case .Resize_Non_Zeroed: + logf( + la.level, + "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%d, size=%d, alignment=%d)", + la.prefix, padding, old_memory, old_size, size, alignment, + location = location, + ) case .Query_Features: logf( la.level, @@ -111,4 +118,4 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, } } return data, err -} \ No newline at end of file +} diff --git a/core/mem/virtual/arena.odin b/core/mem/virtual/arena.odin index d15df46ad..f6fbe237f 100644 --- a/core/mem/virtual/arena.odin +++ b/core/mem/virtual/arena.odin @@ -288,7 +288,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, err = .Mode_Not_Implemented case .Free_All: arena_free_all(arena, location) - case .Resize: + case .Resize, .Resize_Non_Zeroed: old_data := ([^]byte)(old_memory) switch { diff --git a/core/runtime/default_allocators_windows.odin b/core/runtime/default_allocators_windows.odin index a78a4d04e..1b0f78428 100644 --- a/core/runtime/default_allocators_windows.odin +++ b/core/runtime/default_allocators_windows.odin @@ -19,7 +19,7 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR { case .Free_All: return nil, .Mode_Not_Implemented - case .Resize: + case .Resize, .Resize_Non_Zeroed: data, err = _windows_default_resize(old_memory, old_size, size, alignment) case .Query_Features: @@ -41,4 +41,4 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR { data = nil, } } -} \ No newline at end of file +} diff --git a/vendor/commonmark/cmark.odin b/vendor/commonmark/cmark.odin index 80e33cec4..05f639371 100644 --- a/vendor/commonmark/cmark.odin +++ b/vendor/commonmark/cmark.odin @@ -507,7 +507,7 @@ cmark_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mod case .Free_All: return nil, .Mode_Not_Implemented - case .Resize: + case .Resize, .Resize_Non_Zeroed: new_ptr := cmark_alloc.realloc(old_memory, c.size_t(size)) res = transmute([]byte)runtime.Raw_Slice{new_ptr, size} if size > old_size { @@ -529,4 +529,4 @@ get_default_mem_allocator_as_odin :: proc() -> runtime.Allocator { procedure = cmark_allocator_proc, data = rawptr(get_default_mem_allocator()), } -} \ No newline at end of file +} diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index 21d044145..63c663228 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -1683,7 +1683,7 @@ MemAllocatorProc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, MemFree(old_memory) return nil, nil - case .Resize: + case .Resize, .Resize_Non_Zeroed: ptr := MemRealloc(old_memory, c.uint(size)) if ptr == nil { err = .Out_Of_Memory From 7f6f97128492092b82af6298e1c3d863dea07518 Mon Sep 17 00:00:00 2001 From: Ed Yu Date: Mon, 8 Jan 2024 15:08:09 -0800 Subject: [PATCH 3/9] Fix math/fixed floor/ceil/round --- core/math/fixed/fixed.odin | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/math/fixed/fixed.odin b/core/math/fixed/fixed.odin index 5d63bf7be..d347e9c11 100644 --- a/core/math/fixed/fixed.odin +++ b/core/math/fixed/fixed.odin @@ -88,17 +88,19 @@ div_sat :: proc(x, y: $T/Fixed($Backing, $Fraction_Width)) -> (z: T) { @(require_results) floor :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing { - return x.i >> Fraction_Width + if x.i >= 0 { + return x.i >> Fraction_Width + } else { + return (x.i - (1 << (Fraction_Width - 1)) + (1 << (Fraction_Width - 2))) >> Fraction_Width + } } @(require_results) ceil :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing { - Integer :: 8*size_of(Backing) - Fraction_Width - return (x.i + (1 << Integer-1)) >> Fraction_Width + return (x.i + (1 << Fraction_Width - 1)) >> Fraction_Width } @(require_results) round :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing { - Integer :: 8*size_of(Backing) - Fraction_Width - return (x.i + (1 << (Integer - 1))) >> Fraction_Width + return (x.i + (1 << (Fraction_Width - 1))) >> Fraction_Width } From 5476d434410882383862d1d17a63d79502565bf4 Mon Sep 17 00:00:00 2001 From: Kostas Tsiligkiris Date: Wed, 17 Jan 2024 07:00:25 +0200 Subject: [PATCH 4/9] Move package xml before copyright Having the same copyright on all files made the documentation script to include it multiple times in the package information. --- core/encoding/xml/debug_print.odin | 6 ++++-- core/encoding/xml/helpers.odin | 6 ++++-- core/encoding/xml/tokenizer.odin | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/encoding/xml/debug_print.odin b/core/encoding/xml/debug_print.odin index b97617a8a..2607bec23 100644 --- a/core/encoding/xml/debug_print.odin +++ b/core/encoding/xml/debug_print.odin @@ -1,3 +1,5 @@ +package xml + /* An XML 1.0 / 1.1 parser @@ -9,7 +11,7 @@ List of contributors: Jeroen van Rijn: Initial implementation. */ -package xml + import "core:io" import "core:fmt" @@ -81,4 +83,4 @@ print_element :: proc(writer: io.Writer, doc: ^Document, element_id: Element_ID, } return written, .None -} \ No newline at end of file +} diff --git a/core/encoding/xml/helpers.odin b/core/encoding/xml/helpers.odin index e0b5ecc32..42a5258b3 100644 --- a/core/encoding/xml/helpers.odin +++ b/core/encoding/xml/helpers.odin @@ -1,3 +1,5 @@ +package xml + /* An XML 1.0 / 1.1 parser @@ -6,7 +8,7 @@ This file contains helper functions. */ -package xml + // Find parent's nth child with a given ident. find_child_by_ident :: proc(doc: ^Document, parent_id: Element_ID, ident: string, nth := 0) -> (res: Element_ID, found: bool) { @@ -47,4 +49,4 @@ find_attribute_val_by_key :: proc(doc: ^Document, parent_id: Element_ID, key: st if attr.key == key { return attr.val, true } } return "", false -} \ No newline at end of file +} diff --git a/core/encoding/xml/tokenizer.odin b/core/encoding/xml/tokenizer.odin index cd055475c..a223a75d6 100644 --- a/core/encoding/xml/tokenizer.odin +++ b/core/encoding/xml/tokenizer.odin @@ -1,3 +1,5 @@ +package xml + /* An XML 1.0 / 1.1 parser @@ -9,7 +11,7 @@ List of contributors: Jeroen van Rijn: Initial implementation. */ -package xml + import "core:fmt" import "core:unicode" @@ -433,4 +435,4 @@ scan :: proc(t: ^Tokenizer) -> Token { lit = string(t.src[offset : t.offset]) } return Token{kind, lit, pos} -} \ No newline at end of file +} From 02c2aff41b92fa8b8a19c0b6599f02549178ca1a Mon Sep 17 00:00:00 2001 From: Kostas Tsiligkiris Date: Wed, 17 Jan 2024 07:04:00 +0200 Subject: [PATCH 5/9] Fix formatting of package documentation string --- core/encoding/xml/xml_reader.odin | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/core/encoding/xml/xml_reader.odin b/core/encoding/xml/xml_reader.odin index f4f8a4b05..8538febb5 100644 --- a/core/encoding/xml/xml_reader.odin +++ b/core/encoding/xml/xml_reader.odin @@ -1,28 +1,28 @@ /* - An XML 1.0 / 1.1 parser + XML 1.0 / 1.1 parser - Copyright 2021-2022 Jeroen van Rijn . - Made available under Odin's BSD-3 license. + 2021-2022 Jeroen van Rijn . + available under Odin's BSD-3 license. - A from-scratch XML implementation, loosely modelled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816). + from-scratch XML implementation, loosely modelled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816). - Features: - - Supports enough of the XML 1.0/1.1 spec to handle the 99.9% of XML documents in common current usage. - - Simple to understand and use. Small. +Features: +- Supports enough of the XML 1.0/1.1 spec to handle the 99.9% of XML documents in common current usage. +- Simple to understand and use. Small. - Caveats: - - We do NOT support HTML in this package, as that may or may not be valid XML. - If it works, great. If it doesn't, that's not considered a bug. +Caveats: +- We do NOT support HTML in this package, as that may or may not be valid XML. + If it works, great. If it doesn't, that's not considered a bug. - - We do NOT support UTF-16. If you have a UTF-16 XML file, please convert it to UTF-8 first. Also, our condolences. - - <[!ELEMENT and <[!ATTLIST are not supported, and will be either ignored or return an error depending on the parser options. +- We do NOT support UTF-16. If you have a UTF-16 XML file, please convert it to UTF-8 first. Also, our condolences. +- <[!ELEMENT and <[!ATTLIST are not supported, and will be either ignored or return an error depending on the parser options. - MAYBE: - - XML writer? - - Serialize/deserialize Odin types? +MAYBE: +- XML writer? +- Serialize/deserialize Odin types? - List of contributors: - Jeroen van Rijn: Initial implementation. +List of contributors: +- Jeroen van Rijn: Initial implementation. */ package xml // An XML 1.0 / 1.1 parser @@ -700,4 +700,4 @@ new_element :: proc(doc: ^Document) -> (id: Element_ID) { cur := doc.element_count doc.element_count += 1 return cur -} \ No newline at end of file +} From a86cfa6e979f7b8d672561d3c177086f85ea2478 Mon Sep 17 00:00:00 2001 From: Kostas Tsiligkiris Date: Wed, 17 Jan 2024 21:38:33 +0200 Subject: [PATCH 6/9] Fix filename in example --- core/encoding/xml/example/xml_example.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/encoding/xml/example/xml_example.odin b/core/encoding/xml/example/xml_example.odin index aebb8d0ea..9648143db 100644 --- a/core/encoding/xml/example/xml_example.odin +++ b/core/encoding/xml/example/xml_example.odin @@ -20,7 +20,7 @@ example :: proc() { xml.destroy(docs[round]) } - DOC :: #load("../../../../tests/core/assets/XML/unicode.xml") + DOC :: #load("../../../../tests/core/assets/XML/utf8.xml") input := DOC for round in 0.. Date: Wed, 17 Jan 2024 21:58:38 +0200 Subject: [PATCH 7/9] Fix comments in xml_reader.odin for better rendering in documentation --- core/encoding/xml/xml_reader.odin | 183 +++++++++--------------------- 1 file changed, 53 insertions(+), 130 deletions(-) diff --git a/core/encoding/xml/xml_reader.odin b/core/encoding/xml/xml_reader.odin index 8538febb5..562d519d5 100644 --- a/core/encoding/xml/xml_reader.odin +++ b/core/encoding/xml/xml_reader.odin @@ -43,48 +43,32 @@ DEFAULT_OPTIONS :: Options{ } Option_Flag :: enum { - /* - If the caller says that input may be modified, we can perform in-situ parsing. - If this flag isn't provided, the XML parser first duplicates the input so that it can. - */ + // If the caller says that input may be modified, we can perform in-situ parsing. + // If this flag isn't provided, the XML parser first duplicates the input so that it can. Input_May_Be_Modified, - /* - Document MUST start with ` (doc: ^Document, err: Error) { data := data context.allocator = allocator opts := validate_options(options) or_return - /* - If `.Input_May_Be_Modified` is not specified, we duplicate the input so that we can modify it in-place. - */ + // If `.Input_May_Be_Modified` is not specified, we duplicate the input so that we can modify it in-place. if .Input_May_Be_Modified not_in opts.flags { data = bytes.clone(data) } @@ -252,10 +209,8 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha element, parent: Element_ID open: Token - /* - If a DOCTYPE is present, the root tag has to match. - If an expected DOCTYPE is given in options (i.e. it's non-empty), the DOCTYPE (if present) and root tag have to match. - */ + // If a DOCTYPE is present, the root tag has to match. + // If an expected DOCTYPE is given in options (i.e. it's non-empty), the DOCTYPE (if present) and root tag have to match. expected_doctype := options.expected_doctype loop: for { @@ -263,17 +218,13 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha // NOTE(Jeroen): This is faster as a switch. switch t.ch { case '<': - /* - Consume peeked `<` - */ + // Consume peeked `<` advance_rune(t) open = scan(t) // NOTE(Jeroen): We're not using a switch because this if-else chain ordered by likelihood is 2.5% faster at -o:size and -o:speed. if likely(open.kind, Token_Kind.Ident) == .Ident { - /* - e.g. 0 && expected_doctype != open.text { error(t, t.offset, "Root Tag doesn't match DOCTYPE. Expected: %v, got: %v\n", expected_doctype, open.text) @@ -298,23 +247,17 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha } } - /* - One of these should follow: - - `>`, which means we've just opened this tag and expect a later element to close it. - - `/>`, which means this is an 'empty' or self-closing tag. - */ + // One of these should follow: + // - `>`, which means we've just opened this tag and expect a later element to close it. + // - `/>`, which means this is an 'empty' or self-closing tag. end_token := scan(t) #partial switch end_token.kind { case .Gt: - /* - We're now the new parent. - */ + // We're now the new parent. parent = element case .Slash: - /* - Empty tag. Close it. - */ + // Empty tag. Close it. expect(t, .Gt) or_return parent = doc.elements[element].parent element = parent @@ -325,9 +268,7 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha } } else if open.kind == .Slash { - /* - Close tag. - */ + // Close tag. ident := expect(t, .Ident) or_return _ = expect(t, .Gt) or_return @@ -339,9 +280,7 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha element = parent } else if open.kind == .Exclaim { - /* - . - The grammar does not allow a comment to end in ---> - */ + // Comment: . + // The grammar does not allow a comment to end in ---> expect(t, .Dash) comment := scan_comment(t) or_return @@ -395,23 +332,17 @@ parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_ha } } else if open.kind == .Question { - /* - 0 { - /* - We've already seen a prologue. - */ + // We've already seen a prologue. return doc, .Too_Many_Prologs } else { - /* - Could be ` (err: Error) { doc.encoding = .LATIN_1 case: - /* - Unrecognized encoding, assume UTF-8. - */ + // Unrecognized encoding, assume UTF-8. error(t, offset, "[parse_prologue] Warning: Unrecognized encoding: %v\n", attr.val) } @@ -658,11 +583,11 @@ skip_element :: proc(t: ^Tokenizer) -> (err: Error) { parse_doctype :: proc(doc: ^Document) -> (err: Error) { /* - + - - ]> + + ]> */ assert(doc != nil) context.allocator = doc.allocator @@ -675,9 +600,7 @@ parse_doctype :: proc(doc: ^Document) -> (err: Error) { offset := t.offset skip_element(t) or_return - /* - -1 because the current offset is that of the closing tag, so the rest of the DOCTYPE tag ends just before it. - */ + // -1 because the current offset is that of the closing tag, so the rest of the DOCTYPE tag ends just before it. doc.doctype.rest = string(t.src[offset : t.offset - 1]) return .None } From 991c1d4446ab121eb25c32c710f89398d02d6471 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Wed, 17 Jan 2024 13:27:19 -0800 Subject: [PATCH 8/9] add resize_non_zeroed to query features --- core/mem/alloc.odin | 24 ++++++++++++++++++++++-- core/mem/allocators.odin | 9 ++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 507b13d6f..af652a575 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -245,12 +245,26 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: res = raw_data(data) return } + +@(require_results) +default_resize_bytes_align_non_zeroed :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + return _default_resize_bytes_align(old_data, new_size, alignment, false, allocator, loc) +} @(require_results) default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + return _default_resize_bytes_align(old_data, new_size, alignment, true, allocator, loc) +} + +@(require_results) +_default_resize_bytes_align :: #force_inline proc(old_data: []byte, new_size, alignment: int, should_zero: bool, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { old_memory := raw_data(old_data) old_size := len(old_data) if old_memory == nil { - return alloc_bytes(new_size, alignment, allocator, loc) + if should_zero { + return alloc_bytes(new_size, alignment, allocator, loc) + } else { + return alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) + } } if new_size == 0 { @@ -262,7 +276,13 @@ default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, a return old_data, .None } - new_memory, err := alloc_bytes(new_size, alignment, allocator, loc) + new_memory : []byte + err : Allocator_Error + if should_zero { + new_memory, err = alloc_bytes(new_size, alignment, allocator, loc) + } else { + new_memory, err = alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) + } if new_memory == nil || err != nil { return nil, err } diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index 362b897b4..76f87a450 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -85,13 +85,16 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Free_All: arena.offset = 0 - case .Resize, .Resize_Non_Zeroed: - return default_resize_bytes_align(byte_slice(old_memory, old_size), size, alignment, arena_allocator(arena)) + case .Resize: + return default_resize_bytes_align(byte_slice(old_memory, old_size), size, alignment, arena_allocator(arena)) + + case .Resize_Non_Zeroed: + return default_resize_bytes_align_non_zeroed(byte_slice(old_memory, old_size), size, alignment, arena_allocator(arena)) case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features} } return nil, nil From b6838731f5073ce9f1311d64dfba8b01e7636a6a Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Wed, 17 Jan 2024 13:30:03 -0800 Subject: [PATCH 9/9] oops, indentation --- core/mem/alloc.odin | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index af652a575..4cea20f30 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -248,11 +248,11 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: @(require_results) default_resize_bytes_align_non_zeroed :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { - return _default_resize_bytes_align(old_data, new_size, alignment, false, allocator, loc) + return _default_resize_bytes_align(old_data, new_size, alignment, false, allocator, loc) } @(require_results) default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { - return _default_resize_bytes_align(old_data, new_size, alignment, true, allocator, loc) + return _default_resize_bytes_align(old_data, new_size, alignment, true, allocator, loc) } @(require_results) @@ -260,11 +260,11 @@ _default_resize_bytes_align :: #force_inline proc(old_data: []byte, new_size, al old_memory := raw_data(old_data) old_size := len(old_data) if old_memory == nil { - if should_zero { - return alloc_bytes(new_size, alignment, allocator, loc) - } else { - return alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) - } + if should_zero { + return alloc_bytes(new_size, alignment, allocator, loc) + } else { + return alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) + } } if new_size == 0 { @@ -276,13 +276,13 @@ _default_resize_bytes_align :: #force_inline proc(old_data: []byte, new_size, al return old_data, .None } - new_memory : []byte - err : Allocator_Error - if should_zero { - new_memory, err = alloc_bytes(new_size, alignment, allocator, loc) - } else { - new_memory, err = alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) - } + new_memory : []byte + err : Allocator_Error + if should_zero { + new_memory, err = alloc_bytes(new_size, alignment, allocator, loc) + } else { + new_memory, err = alloc_bytes_non_zeroed(new_size, alignment, allocator, loc) + } if new_memory == nil || err != nil { return nil, err }