Explorar o código

Merge pull request #2072 from odin-lang/allocator-mode-alloc-non-zeroed

Add `Allocator_Mode.Alloc_Non_Zerored`
gingerBill %!s(int64=2) %!d(string=hai) anos
pai
achega
8c01e952f3

+ 7 - 0
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(

+ 4 - 0
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_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)
+}
+
 free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
 	return runtime.mem_free(ptr, allocator, loc)
 }

+ 42 - 25
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
 

+ 2 - 2
core/mem/virtual/arena.odin

@@ -232,7 +232,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
 	old_size := uint(old_size)
 
 	switch mode {
-	case .Alloc:
+	case .Alloc, .Alloc_Non_Zeroed:
 		return arena_alloc(arena, size, alignment)
 	case .Free:
 		err = .Mode_Not_Implemented
@@ -266,7 +266,7 @@ arena_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_All, .Resize, .Query_Features}
+			set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features}
 		}
 	case .Query_Info:
 		err = .Mode_Not_Implemented

+ 5 - 5
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
 

+ 9 - 3
core/os/os_darwin.odin

@@ -646,9 +646,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

+ 2 - 2
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) {

+ 9 - 3
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 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 {

+ 9 - 3
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 {

+ 9 - 3
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 {

+ 1 - 1
core/os/os_wasi.odin

@@ -101,7 +101,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 {

+ 2 - 2
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 {

+ 1 - 0
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]

+ 1 - 1
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

+ 3 - 3
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:

+ 2 - 2
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:

+ 7 - 0
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

+ 6 - 6
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)
 }
 
 

+ 6 - 3
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)

+ 1 - 1
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