From 7f601c953537041467c1f5c3678a6bfd4ea6413f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Sep 2022 12:12:57 +0100 Subject: [PATCH 1/3] Add `Allocator_Mode.Alloc_Non_Zerored` --- core/log/log_allocator.odin | 7 ++ core/mem/alloc.odin | 4 ++ core/mem/allocators.odin | 67 ++++++++++++------- core/mem/virtual/growing_arena.odin | 2 +- core/mem/virtual/static_arena.odin | 2 +- core/os/os.odin | 10 +-- core/os/os_darwin.odin | 12 +++- core/os/os_essence.odin | 4 +- core/os/os_freebsd.odin | 12 +++- core/os/os_linux.odin | 12 +++- core/os/os_openbsd.odin | 12 +++- core/os/os_wasi.odin | 2 +- core/os/os_windows.odin | 4 +- core/runtime/core.odin | 1 + core/runtime/default_allocators_nil.odin | 2 +- core/runtime/default_allocators_windows.odin | 6 +- core/runtime/default_temporary_allocator.odin | 4 +- core/runtime/internal.odin | 7 ++ core/runtime/os_specific_windows.odin | 12 ++-- vendor/commonmark/cmark.odin | 9 ++- vendor/raylib/raylib.odin | 2 +- 21 files changed, 128 insertions(+), 65 deletions(-) diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin index 997bd1a68..f4d1841db 100644 --- a/core/log/log_allocator.odin +++ b/core/log/log_allocator.odin @@ -43,6 +43,13 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, args = {la.prefix, padding, size, alignment}, location = location, ) + case .Alloc_Non_Zeroed: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)", + args = {la.prefix, padding, size, alignment}, + location = location, + ) case .Free: if old_size != 0 { logf( diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 54004d333..76971bf45 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -69,6 +69,10 @@ alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := return runtime.mem_alloc(size, alignment, allocator, loc) } +alloc_bytes_non_zerored :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + return runtime.mem_alloc_non_zeroed(size, alignment, allocator, loc) +} + free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { return runtime.mem_free(ptr, allocator, loc) } diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index 0d52d2599..ae248e1f3 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -59,7 +59,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, arena := cast(^Arena)allocator_data switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: #no_bounds_check end := &arena.data[arena.offset] ptr := align_forward(end, uintptr(alignment)) @@ -72,7 +72,9 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, arena.offset += total_size arena.peak_used = max(arena.peak_used, arena.offset) - zero(ptr, size) + if mode != .Alloc_Non_Zeroed { + zero(ptr, size) + } return byte_slice(ptr, size), nil case .Free: @@ -87,7 +89,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features} } return nil, nil @@ -162,7 +164,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size := size switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: size = align_forward_int(size, alignment) switch { @@ -170,7 +172,9 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, start := uintptr(raw_data(s.data)) ptr := start + uintptr(s.curr_offset) ptr = align_forward_uintptr(ptr, uintptr(alignment)) - zero(rawptr(ptr), size) + if mode != .Alloc_Non_Zeroed { + zero(rawptr(ptr), size) + } s.prev_allocation = rawptr(ptr) offset := int(ptr - start) @@ -180,7 +184,9 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case size <= len(s.data): start := uintptr(raw_data(s.data)) ptr := align_forward_uintptr(start, uintptr(alignment)) - zero(rawptr(ptr), size) + if mode != .Alloc_Non_Zeroed { + zero(rawptr(ptr), size) + } s.prev_allocation = rawptr(ptr) offset := int(ptr - start) @@ -266,7 +272,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, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} } return nil, nil @@ -333,7 +339,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, return nil, .Invalid_Argument } - raw_alloc :: proc(s: ^Stack, size, alignment: int) -> ([]byte, Allocator_Error) { + raw_alloc :: proc(s: ^Stack, size, alignment: int, zero_memory: bool) -> ([]byte, Allocator_Error) { 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) { @@ -351,13 +357,15 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, s.peak_used = max(s.peak_used, s.curr_offset) - zero(rawptr(next_addr), size) + if zero_memory { + zero(rawptr(next_addr), size) + } return byte_slice(rawptr(next_addr), size), nil } switch mode { - case .Alloc: - return raw_alloc(s, size, alignment) + case .Alloc, .Alloc_Non_Zeroed: + return raw_alloc(s, size, alignment, mode == .Alloc) case .Free: if old_memory == nil { return nil, nil @@ -392,7 +400,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Resize: if old_memory == nil { - return raw_alloc(s, size, alignment) + return raw_alloc(s, size, alignment, true) } if size == 0 { return nil, nil @@ -418,7 +426,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) + data, err := raw_alloc(s, size, alignment, true) if err == nil { runtime.copy(data, byte_slice(old_memory, old_size)) } @@ -439,7 +447,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, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} } return nil, nil case .Query_Info: @@ -497,7 +505,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) -> ([]byte, Allocator_Error) { + raw_alloc :: proc(s: ^Small_Stack, size, alignment: int, zero_memory: bool) -> ([]byte, Allocator_Error) { 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) { @@ -513,13 +521,15 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, s.peak_used = max(s.peak_used, s.offset) - zero(rawptr(next_addr), size) + if zero_memory { + zero(rawptr(next_addr), size) + } return byte_slice(rawptr(next_addr), size), nil } switch mode { - case .Alloc: - return raw_alloc(s, size, align) + case .Alloc, .Alloc_Non_Zeroed: + return raw_alloc(s, size, align, mode == .Alloc) case .Free: if old_memory == nil { return nil, nil @@ -548,7 +558,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Resize: if old_memory == nil { - return raw_alloc(s, size, align) + return raw_alloc(s, size, align, true) } if size == 0 { return nil, nil @@ -571,7 +581,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) + data, err := raw_alloc(s, size, align, true) if err == nil { runtime.copy(data, byte_slice(old_memory, old_size)) } @@ -580,7 +590,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, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} } return nil, nil @@ -623,7 +633,7 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode pool := (^Dynamic_Pool)(allocator_data) switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: return dynamic_pool_alloc_bytes(pool, size) case .Free: return nil, .Mode_Not_Implemented @@ -643,7 +653,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, .Free_All, .Resize, .Query_Features, .Query_Info} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features, .Query_Info} } return nil, nil @@ -794,6 +804,10 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, if size > 0 { panic("mem: panic allocator, .Alloc called") } + case .Alloc_Non_Zeroed: + if size > 0 { + panic("mem: panic allocator, .Alloc_Non_Zeroed called") + } case .Resize: if size > 0 { panic("mem: panic allocator, .Resize called") @@ -831,6 +845,7 @@ Tracking_Allocator_Entry :: struct { memory: rawptr, size: int, alignment: int, + mode: Allocator_Mode, err: Allocator_Error, location: runtime.Source_Code_Location, } @@ -900,10 +915,11 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, } switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: data.allocation_map[result_ptr] = Tracking_Allocator_Entry{ memory = result_ptr, size = size, + mode = mode, alignment = alignment, err = err, location = loc, @@ -921,6 +937,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, data.allocation_map[result_ptr] = Tracking_Allocator_Entry{ memory = result_ptr, size = size, + mode = mode, alignment = alignment, err = err, location = loc, @@ -929,7 +946,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features, .Query_Info} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features, .Query_Info} } return nil, nil diff --git a/core/mem/virtual/growing_arena.odin b/core/mem/virtual/growing_arena.odin index 0fb826c07..5beeaa14c 100644 --- a/core/mem/virtual/growing_arena.odin +++ b/core/mem/virtual/growing_arena.odin @@ -108,7 +108,7 @@ growing_arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator arena := (^Growing_Arena)(allocator_data) switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: return growing_arena_alloc(arena, size, alignment) case .Free: err = .Mode_Not_Implemented diff --git a/core/mem/virtual/static_arena.odin b/core/mem/virtual/static_arena.odin index 7e8a62f4a..06747523a 100644 --- a/core/mem/virtual/static_arena.odin +++ b/core/mem/virtual/static_arena.odin @@ -99,7 +99,7 @@ static_arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_ arena := (^Static_Arena)(allocator_data) switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: return static_arena_alloc(arena, size, alignment) case .Free: err = .Mode_Not_Implemented diff --git a/core/os/os.odin b/core/os/os.odin index 1d8845da7..dd20de17c 100644 --- a/core/os/os.odin +++ b/core/os/os.odin @@ -178,7 +178,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, // the pointer we return to the user. // - aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) { + aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil, zero_memory := true) -> ([]byte, mem.Allocator_Error) { a := max(alignment, align_of(rawptr)) space := size + a - 1 @@ -187,7 +187,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^ allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)) } else { - allocated_mem = heap_alloc(space+size_of(rawptr)) + allocated_mem = heap_alloc(space+size_of(rawptr), zero_memory) } aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr))) @@ -226,8 +226,8 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, } switch mode { - case .Alloc: - return aligned_alloc(size, alignment) + case .Alloc, .Alloc_Non_Zeroed: + return aligned_alloc(size, alignment, nil, mode == .Alloc) case .Free: aligned_free(old_memory) @@ -244,7 +244,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, case .Query_Features: set := (^mem.Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Free, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Query_Features} } return nil, nil diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index 5c2cf8ec2..8c9e26c00 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -625,9 +625,15 @@ access :: proc(path: string, mask: int) -> bool { return _unix_access(cstr, mask) == 0 } -heap_alloc :: proc(size: int) -> rawptr { - assert(size > 0) - return _unix_calloc(1, size) +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + if size <= 0 { + return nil + } + if zero_memory { + return _unix_calloc(1, size) + } else { + return _unix_malloc(size) + } } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on diff --git a/core/os/os_essence.odin b/core/os/os_essence.odin index 4858ab2e9..e4281f6c9 100644 --- a/core/os/os_essence.odin +++ b/core/os/os_essence.odin @@ -18,8 +18,8 @@ current_thread_id :: proc "contextless" () -> int { return (int) (es.ThreadGetID(es.CURRENT_THREAD)); } -heap_alloc :: proc(size: int) -> rawptr { - return es.HeapAllocate(size, false); +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + return es.HeapAllocate(size, zero_memory); } heap_free :: proc(ptr: rawptr) { diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index adf4f246f..2e695cc33 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -603,9 +603,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) { return true, ERROR_NONE } -heap_alloc :: proc(size: int) -> rawptr { - assert(size >= 0) - return _unix_calloc(1, c.size_t(size)) +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + if zero <= 0 { + return nil + } + if zero_memory { + return _unix_calloc(1, c.size_t(size)) + } else { + return _unix_malloc(c.size_t(size)) + } } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index 1064f4aa8..e299734ee 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -755,9 +755,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) { return true, ERROR_NONE } -heap_alloc :: proc(size: int) -> rawptr { - assert(size >= 0) - return _unix_calloc(1, c.size_t(size)) +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + if size <= 0 { + return nil + } + if zero_memory { + return _unix_calloc(1, c.size_t(size)) + } else { + return _unix_malloc(c.size_t(size)) + } } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { diff --git a/core/os/os_openbsd.odin b/core/os/os_openbsd.odin index fba079f15..2fb8e4065 100644 --- a/core/os/os_openbsd.odin +++ b/core/os/os_openbsd.odin @@ -605,9 +605,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) { return true, ERROR_NONE } -heap_alloc :: proc(size: int) -> rawptr { - assert(size >= 0) - return _unix_calloc(1, c.size_t(size)) +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + if size <= 0 { + return nil + } + if zero_memory { + return _unix_calloc(1, c.size_t(size)) + } else { + return _unix_malloc(c.size_t(size)) + } } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { diff --git a/core/os/os_wasi.odin b/core/os/os_wasi.odin index 7bab1b949..81d4a4d4e 100644 --- a/core/os/os_wasi.odin +++ b/core/os/os_wasi.odin @@ -72,7 +72,7 @@ file_size :: proc(fd: Handle) -> (i64, Errno) { -heap_alloc :: proc(size: int) -> rawptr { +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { return nil } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { diff --git a/core/os/os_windows.odin b/core/os/os_windows.odin index fe9496e4c..f48f46fa3 100644 --- a/core/os/os_windows.odin +++ b/core/os/os_windows.odin @@ -91,8 +91,8 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) { -heap_alloc :: proc(size: int) -> rawptr { - return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size)) +heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { + return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY if zero_memory else 0, uint(size)) } heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { if new_size == 0 { diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 81cce8caf..385a03b71 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -303,6 +303,7 @@ Allocator_Mode :: enum byte { Resize, Query_Features, Query_Info, + Alloc_Non_Zeroed, } Allocator_Mode_Set :: distinct bit_set[Allocator_Mode] diff --git a/core/runtime/default_allocators_nil.odin b/core/runtime/default_allocators_nil.odin index 04dea0e19..f86990581 100644 --- a/core/runtime/default_allocators_nil.odin +++ b/core/runtime/default_allocators_nil.odin @@ -4,7 +4,7 @@ nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: return nil, .Out_Of_Memory case .Free: return nil, .None diff --git a/core/runtime/default_allocators_windows.odin b/core/runtime/default_allocators_windows.odin index 45d4d3e6a..a78a4d04e 100644 --- a/core/runtime/default_allocators_windows.odin +++ b/core/runtime/default_allocators_windows.odin @@ -10,8 +10,8 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR { size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { switch mode { - case .Alloc: - data, err = _windows_default_alloc(size, alignment) + case .Alloc, .Alloc_Non_Zeroed: + data, err = _windows_default_alloc(size, alignment, mode == .Alloc) case .Free: _windows_default_free(old_memory) @@ -25,7 +25,7 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR { case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Free, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Query_Features} } case .Query_Info: diff --git a/core/runtime/default_temporary_allocator.odin b/core/runtime/default_temporary_allocator.odin index 52f781980..176634ff9 100644 --- a/core/runtime/default_temporary_allocator.odin +++ b/core/runtime/default_temporary_allocator.odin @@ -167,7 +167,7 @@ when ODIN_OS == .Freestanding || ODIN_OS == .JS || ODIN_DEFAULT_TO_NIL_ALLOCATOR } switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: data, err = default_temp_allocator_alloc(s, size, alignment, loc) case .Free: err = default_temp_allocator_free(s, old_memory, loc) @@ -181,7 +181,7 @@ when ODIN_OS == .Freestanding || ODIN_OS == .JS || ODIN_DEFAULT_TO_NIL_ALLOCATOR case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) if set != nil { - set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features} + set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Query_Features} } case .Query_Info: diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 048bff7ca..cb04ff0aa 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -145,6 +145,13 @@ mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, a return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc) } +mem_alloc_non_zeroed :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + if size == 0 || allocator.procedure == nil { + return nil, nil + } + return allocator.procedure(allocator.data, .Alloc_Non_Zeroed, size, alignment, nil, 0, loc) +} + mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { if ptr == nil || allocator.procedure == nil { return nil diff --git a/core/runtime/os_specific_windows.odin b/core/runtime/os_specific_windows.odin index 425071f85..6e7474257 100644 --- a/core/runtime/os_specific_windows.odin +++ b/core/runtime/os_specific_windows.odin @@ -58,9 +58,9 @@ _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_b return } -heap_alloc :: proc "contextless" (size: int) -> rawptr { +heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr { HEAP_ZERO_MEMORY :: 0x00000008 - return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size)) + return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY if zero_memory else 0, uint(size)) } heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr { if new_size == 0 { @@ -91,7 +91,7 @@ heap_free :: proc "contextless" (ptr: rawptr) { -_windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, Allocator_Error) { +_windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil, zero_memory := true) -> ([]byte, Allocator_Error) { if size == 0 { _windows_default_free(old_ptr) return nil, nil @@ -105,7 +105,7 @@ _windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, ol original_old_ptr := intrinsics.ptr_offset((^rawptr)(old_ptr), -1)^ allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)) } else { - allocated_mem = heap_alloc(space+size_of(rawptr)) + allocated_mem = heap_alloc(space+size_of(rawptr), zero_memory) } aligned_mem := rawptr(intrinsics.ptr_offset((^u8)(allocated_mem), size_of(rawptr))) @@ -122,8 +122,8 @@ _windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, ol return byte_slice(aligned_mem, size), nil } -_windows_default_alloc :: proc "contextless" (size, alignment: int) -> ([]byte, Allocator_Error) { - return _windows_default_alloc_or_resize(size, alignment, nil) +_windows_default_alloc :: proc "contextless" (size, alignment: int, zero_memory := true) -> ([]byte, Allocator_Error) { + return _windows_default_alloc_or_resize(size, alignment, nil, zero_memory) } diff --git a/vendor/commonmark/cmark.odin b/vendor/commonmark/cmark.odin index fba12436d..1aced1efd 100644 --- a/vendor/commonmark/cmark.odin +++ b/vendor/commonmark/cmark.odin @@ -489,10 +489,13 @@ cmark_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mod cmark_alloc := cast(^Allocator)allocator_data switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: ptr := cmark_alloc.calloc(1, c.size_t(size)) - res = transmute([]byte)runtime.Raw_Slice{ptr, size} - return res, nil + res = ([^]byte)(ptr)[:size] + if ptr == nil { + err = .Out_Of_Memory + } + return case .Free: cmark_alloc.free(old_memory) diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index 1a7f81577..97e800392 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -1564,7 +1564,7 @@ MemAllocatorProc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, location := #caller_location) -> (data: []byte, err: mem.Allocator_Error) { switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: ptr := MemAlloc(c.int(size)) if ptr == nil { err = .Out_Of_Memory From c767d55e9abf866938ece2fa4d05cd34fffec0a3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Sep 2022 12:21:43 +0100 Subject: [PATCH 2/3] Fix typo --- core/os/os_freebsd.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index 2e695cc33..b0978ad6e 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -604,7 +604,7 @@ access :: proc(path: string, mask: int) -> (bool, Errno) { } heap_alloc :: proc(size: int, zero_memory := true) -> rawptr { - if zero <= 0 { + if size <= 0 { return nil } if zero_memory { From 5cf473b31cc1a405e11ac37e135251760b0814d1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Sep 2022 15:19:24 +0100 Subject: [PATCH 3/3] Fix typo --- core/mem/alloc.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 76971bf45..551906bed 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -69,7 +69,7 @@ alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := return runtime.mem_alloc(size, alignment, allocator, loc) } -alloc_bytes_non_zerored :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { +alloc_bytes_non_zeroed :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { return runtime.mem_alloc_non_zeroed(size, alignment, allocator, loc) }