Add Allocator_Mode.Alloc_Non_Zerored

This commit is contained in:
gingerBill
2022-09-22 12:12:57 +01:00
parent eb0d7465e2
commit 7f601c9535
21 changed files with 128 additions and 65 deletions
+42 -25
View File
@@ -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