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

Add `Allocator_Mode.Alloc_Non_Zerored`
This commit is contained in:
gingerBill
2022-11-03 12:57:23 +00:00
committed by GitHub
20 changed files with 128 additions and 65 deletions
+7
View File
@@ -43,6 +43,13 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
args = {la.prefix, padding, size, alignment}, args = {la.prefix, padding, size, alignment},
location = location, 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: case .Free:
if old_size != 0 { if old_size != 0 {
logf( logf(
+4
View File
@@ -69,6 +69,10 @@ alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator :=
return runtime.mem_alloc(size, alignment, allocator, loc) 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 { free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
return runtime.mem_free(ptr, allocator, loc) return runtime.mem_free(ptr, allocator, loc)
} }
+37 -20
View File
@@ -59,7 +59,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
arena := cast(^Arena)allocator_data arena := cast(^Arena)allocator_data
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
#no_bounds_check end := &arena.data[arena.offset] #no_bounds_check end := &arena.data[arena.offset]
ptr := align_forward(end, uintptr(alignment)) 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.offset += total_size
arena.peak_used = max(arena.peak_used, arena.offset) arena.peak_used = max(arena.peak_used, arena.offset)
if mode != .Alloc_Non_Zeroed {
zero(ptr, size) zero(ptr, size)
}
return byte_slice(ptr, size), nil return byte_slice(ptr, size), nil
case .Free: case .Free:
@@ -87,7 +89,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Free_All, .Resize, .Query_Features} set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features}
} }
return nil, nil return nil, nil
@@ -162,7 +164,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size := size size := size
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
size = align_forward_int(size, alignment) size = align_forward_int(size, alignment)
switch { switch {
@@ -170,7 +172,9 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
start := uintptr(raw_data(s.data)) start := uintptr(raw_data(s.data))
ptr := start + uintptr(s.curr_offset) ptr := start + uintptr(s.curr_offset)
ptr = align_forward_uintptr(ptr, uintptr(alignment)) ptr = align_forward_uintptr(ptr, uintptr(alignment))
if mode != .Alloc_Non_Zeroed {
zero(rawptr(ptr), size) zero(rawptr(ptr), size)
}
s.prev_allocation = rawptr(ptr) s.prev_allocation = rawptr(ptr)
offset := int(ptr - start) offset := int(ptr - start)
@@ -180,7 +184,9 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case size <= len(s.data): case size <= len(s.data):
start := uintptr(raw_data(s.data)) start := uintptr(raw_data(s.data))
ptr := align_forward_uintptr(start, uintptr(alignment)) ptr := align_forward_uintptr(start, uintptr(alignment))
if mode != .Alloc_Non_Zeroed {
zero(rawptr(ptr), size) zero(rawptr(ptr), size)
}
s.prev_allocation = rawptr(ptr) s.prev_allocation = rawptr(ptr)
offset := int(ptr - start) offset := int(ptr - start)
@@ -266,7 +272,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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 return nil, nil
@@ -333,7 +339,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
return nil, .Invalid_Argument 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) 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)) padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Stack_Allocation_Header))
if s.curr_offset + padding + size > len(s.data) { 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) s.peak_used = max(s.peak_used, s.curr_offset)
if zero_memory {
zero(rawptr(next_addr), size) zero(rawptr(next_addr), size)
}
return byte_slice(rawptr(next_addr), size), nil return byte_slice(rawptr(next_addr), size), nil
} }
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return raw_alloc(s, size, alignment) return raw_alloc(s, size, alignment, mode == .Alloc)
case .Free: case .Free:
if old_memory == nil { if old_memory == nil {
return nil, nil return nil, nil
@@ -392,7 +400,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Resize: case .Resize:
if old_memory == nil { if old_memory == nil {
return raw_alloc(s, size, alignment) return raw_alloc(s, size, alignment, true)
} }
if size == 0 { if size == 0 {
return nil, nil 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))) old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)))
if old_offset != header.prev_offset { if old_offset != header.prev_offset {
data, err := raw_alloc(s, size, alignment) data, err := raw_alloc(s, size, alignment, true)
if err == nil { if err == nil {
runtime.copy(data, byte_slice(old_memory, old_size)) 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: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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 return nil, nil
case .Query_Info: 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) 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) 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)) padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header))
if s.offset + padding + size > len(s.data) { 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) s.peak_used = max(s.peak_used, s.offset)
if zero_memory {
zero(rawptr(next_addr), size) zero(rawptr(next_addr), size)
}
return byte_slice(rawptr(next_addr), size), nil return byte_slice(rawptr(next_addr), size), nil
} }
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return raw_alloc(s, size, align) return raw_alloc(s, size, align, mode == .Alloc)
case .Free: case .Free:
if old_memory == nil { if old_memory == nil {
return nil, nil return nil, nil
@@ -548,7 +558,7 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Resize: case .Resize:
if old_memory == nil { if old_memory == nil {
return raw_alloc(s, size, align) return raw_alloc(s, size, align, true)
} }
if size == 0 { if size == 0 {
return nil, nil 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 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 { if err == nil {
runtime.copy(data, byte_slice(old_memory, old_size)) 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: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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 return nil, nil
@@ -623,7 +633,7 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
pool := (^Dynamic_Pool)(allocator_data) pool := (^Dynamic_Pool)(allocator_data)
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return dynamic_pool_alloc_bytes(pool, size) return dynamic_pool_alloc_bytes(pool, size)
case .Free: case .Free:
return nil, .Mode_Not_Implemented return nil, .Mode_Not_Implemented
@@ -643,7 +653,7 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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 return nil, nil
@@ -794,6 +804,10 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
if size > 0 { if size > 0 {
panic("mem: panic allocator, .Alloc called") panic("mem: panic allocator, .Alloc called")
} }
case .Alloc_Non_Zeroed:
if size > 0 {
panic("mem: panic allocator, .Alloc_Non_Zeroed called")
}
case .Resize: case .Resize:
if size > 0 { if size > 0 {
panic("mem: panic allocator, .Resize called") panic("mem: panic allocator, .Resize called")
@@ -831,6 +845,7 @@ Tracking_Allocator_Entry :: struct {
memory: rawptr, memory: rawptr,
size: int, size: int,
alignment: int, alignment: int,
mode: Allocator_Mode,
err: Allocator_Error, err: Allocator_Error,
location: runtime.Source_Code_Location, location: runtime.Source_Code_Location,
} }
@@ -900,10 +915,11 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
} }
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
data.allocation_map[result_ptr] = Tracking_Allocator_Entry{ data.allocation_map[result_ptr] = Tracking_Allocator_Entry{
memory = result_ptr, memory = result_ptr,
size = size, size = size,
mode = mode,
alignment = alignment, alignment = alignment,
err = err, err = err,
location = loc, location = loc,
@@ -921,6 +937,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
data.allocation_map[result_ptr] = Tracking_Allocator_Entry{ data.allocation_map[result_ptr] = Tracking_Allocator_Entry{
memory = result_ptr, memory = result_ptr,
size = size, size = size,
mode = mode,
alignment = alignment, alignment = alignment,
err = err, err = err,
location = loc, location = loc,
@@ -929,7 +946,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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 return nil, nil
+2 -2
View File
@@ -232,7 +232,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
old_size := uint(old_size) old_size := uint(old_size)
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return arena_alloc(arena, size, alignment) return arena_alloc(arena, size, alignment)
case .Free: case .Free:
err = .Mode_Not_Implemented err = .Mode_Not_Implemented
@@ -266,7 +266,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^mem.Allocator_Mode_Set)(old_memory) set := (^mem.Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Free_All, .Resize, .Query_Features} set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features}
} }
case .Query_Info: case .Query_Info:
err = .Mode_Not_Implemented err = .Mode_Not_Implemented
+5 -5
View File
@@ -178,7 +178,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
// the pointer we return to the user. // 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)) a := max(alignment, align_of(rawptr))
space := size + a - 1 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)^ original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)) allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
} else { } 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))) 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 { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return aligned_alloc(size, alignment) return aligned_alloc(size, alignment, nil, mode == .Alloc)
case .Free: case .Free:
aligned_free(old_memory) aligned_free(old_memory)
@@ -244,7 +244,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^mem.Allocator_Mode_Set)(old_memory) set := (^mem.Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features} set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Query_Features}
} }
return nil, nil return nil, nil
+8 -2
View File
@@ -646,9 +646,15 @@ access :: proc(path: string, mask: int) -> bool {
return _unix_access(cstr, mask) == 0 return _unix_access(cstr, mask) == 0
} }
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
assert(size > 0) if size <= 0 {
return nil
}
if zero_memory {
return _unix_calloc(1, size) return _unix_calloc(1, size)
} else {
return _unix_malloc(size)
}
} }
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
// NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
+2 -2
View File
@@ -18,8 +18,8 @@ current_thread_id :: proc "contextless" () -> int {
return (int) (es.ThreadGetID(es.CURRENT_THREAD)); return (int) (es.ThreadGetID(es.CURRENT_THREAD));
} }
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
return es.HeapAllocate(size, false); return es.HeapAllocate(size, zero_memory);
} }
heap_free :: proc(ptr: rawptr) { heap_free :: proc(ptr: rawptr) {
+8 -2
View File
@@ -603,9 +603,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) {
return true, ERROR_NONE return true, ERROR_NONE
} }
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
assert(size >= 0) if size <= 0 {
return nil
}
if zero_memory {
return _unix_calloc(1, c.size_t(size)) 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 { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+8 -2
View File
@@ -755,9 +755,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) {
return true, ERROR_NONE return true, ERROR_NONE
} }
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
assert(size >= 0) if size <= 0 {
return nil
}
if zero_memory {
return _unix_calloc(1, c.size_t(size)) 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 { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+8 -2
View File
@@ -605,9 +605,15 @@ access :: proc(path: string, mask: int) -> (bool, Errno) {
return true, ERROR_NONE return true, ERROR_NONE
} }
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
assert(size >= 0) if size <= 0 {
return nil
}
if zero_memory {
return _unix_calloc(1, c.size_t(size)) 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 { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+1 -1
View File
@@ -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 return nil
} }
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+2 -2
View File
@@ -91,8 +91,8 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size)) 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 { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
if new_size == 0 { if new_size == 0 {
+1
View File
@@ -303,6 +303,7 @@ Allocator_Mode :: enum byte {
Resize, Resize,
Query_Features, Query_Features,
Query_Info, Query_Info,
Alloc_Non_Zeroed,
} }
Allocator_Mode_Set :: distinct bit_set[Allocator_Mode] Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
+1 -1
View File
@@ -4,7 +4,7 @@ nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
return nil, .Out_Of_Memory return nil, .Out_Of_Memory
case .Free: case .Free:
return nil, .None return nil, .None
+3 -3
View File
@@ -10,8 +10,8 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR {
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) { old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
data, err = _windows_default_alloc(size, alignment) data, err = _windows_default_alloc(size, alignment, mode == .Alloc)
case .Free: case .Free:
_windows_default_free(old_memory) _windows_default_free(old_memory)
@@ -25,7 +25,7 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR {
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features} set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Query_Features}
} }
case .Query_Info: case .Query_Info:
@@ -167,7 +167,7 @@ when ODIN_OS == .Freestanding || ODIN_OS == .JS || ODIN_DEFAULT_TO_NIL_ALLOCATOR
} }
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
data, err = default_temp_allocator_alloc(s, size, alignment, loc) data, err = default_temp_allocator_alloc(s, size, alignment, loc)
case .Free: case .Free:
err = default_temp_allocator_free(s, old_memory, loc) 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: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { 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: case .Query_Info:
+7
View File
@@ -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) 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 { mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
if ptr == nil || allocator.procedure == nil { if ptr == nil || allocator.procedure == nil {
return nil return nil
+6 -6
View File
@@ -58,9 +58,9 @@ _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_b
return return
} }
heap_alloc :: proc "contextless" (size: int) -> rawptr { heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
HEAP_ZERO_MEMORY :: 0x00000008 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 { heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
if new_size == 0 { 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 { if size == 0 {
_windows_default_free(old_ptr) _windows_default_free(old_ptr)
return nil, nil 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)^ original_old_ptr := intrinsics.ptr_offset((^rawptr)(old_ptr), -1)^
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)) allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
} else { } 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))) 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 return byte_slice(aligned_mem, size), nil
} }
_windows_default_alloc :: proc "contextless" (size, alignment: int) -> ([]byte, Allocator_Error) { _windows_default_alloc :: proc "contextless" (size, alignment: int, zero_memory := true) -> ([]byte, Allocator_Error) {
return _windows_default_alloc_or_resize(size, alignment, nil) return _windows_default_alloc_or_resize(size, alignment, nil, zero_memory)
} }
+6 -3
View File
@@ -489,10 +489,13 @@ cmark_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mod
cmark_alloc := cast(^Allocator)allocator_data cmark_alloc := cast(^Allocator)allocator_data
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
ptr := cmark_alloc.calloc(1, c.size_t(size)) ptr := cmark_alloc.calloc(1, c.size_t(size))
res = transmute([]byte)runtime.Raw_Slice{ptr, size} res = ([^]byte)(ptr)[:size]
return res, nil if ptr == nil {
err = .Out_Of_Memory
}
return
case .Free: case .Free:
cmark_alloc.free(old_memory) cmark_alloc.free(old_memory)
+1 -1
View File
@@ -1564,7 +1564,7 @@ MemAllocatorProc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, location := #caller_location) -> (data: []byte, err: mem.Allocator_Error) { old_memory: rawptr, old_size: int, location := #caller_location) -> (data: []byte, err: mem.Allocator_Error) {
switch mode { switch mode {
case .Alloc: case .Alloc, .Alloc_Non_Zeroed:
ptr := MemAlloc(c.int(size)) ptr := MemAlloc(c.int(size))
if ptr == nil { if ptr == nil {
err = .Out_Of_Memory err = .Out_Of_Memory