authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2020-12-13 18:58:17+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-17 19:09:29+02:00
logd73f46b57c1c407bacd0daed8c69c4f14d14a06a
treef55f8df918f2954c8f4a8b80cb5800c6e6aebb18
parente93cb2254171f38317f97e685b79dec397f47bb7

Fix StackFallbackAllocator


1 files changed, 18 insertions(+), 8 deletions(-)

lib/std/heap.zig+18-8
...@@ -789,7 +789,7 @@ pub fn stackFallback(comptime size: usize, fallback_allocator: *Allocator) Stack...@@ -789,7 +789,7 @@ pub fn stackFallback(comptime size: usize, fallback_allocator: *Allocator) Stack
789 .fallback_allocator = fallback_allocator,789 .fallback_allocator = fallback_allocator,
790 .fixed_buffer_allocator = undefined,790 .fixed_buffer_allocator = undefined,
791 .allocator = Allocator{791 .allocator = Allocator{
792 .allocFn = StackFallbackAllocator(size).realloc,792 .allocFn = StackFallbackAllocator(size).alloc,
793 .resizeFn = StackFallbackAllocator(size).resize,793 .resizeFn = StackFallbackAllocator(size).resize,
794 },794 },
795 };795 };
...@@ -815,25 +815,25 @@ pub fn StackFallbackAllocator(comptime size: usize) type {...@@ -815,25 +815,25 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
815 ptr_align: u29,815 ptr_align: u29,
816 len_align: u29,816 len_align: u29,
817 return_address: usize,817 return_address: usize,
818 ) error{OutOfMemory}![*]u8 {818 ) error{OutOfMemory}![]u8 {
819 const self = @fieldParentPtr(Self, "allocator", allocator);819 const self = @fieldParentPtr(Self, "allocator", allocator);
820 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, ptr_align) catch820 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator.allocator, len, ptr_align, len_align, return_address) catch
821 return fallback_allocator.alloc(len, ptr_align);821 return self.fallback_allocator.allocFn(self.fallback_allocator, len, ptr_align, len_align, return_address);
822 }822 }
823823
824 fn resize(824 fn resize(
825 self: *Allocator,825 allocator: *Allocator,
826 buf: []u8,826 buf: []u8,
827 buf_align: u29,827 buf_align: u29,
828 new_len: usize,828 new_len: usize,
829 len_align: u29,829 len_align: u29,
830 return_address: usize,830 return_address: usize,
831 ) error{OutOfMemory}!void {831 ) error{OutOfMemory}!usize {
832 const self = @fieldParentPtr(Self, "allocator", allocator);832 const self = @fieldParentPtr(Self, "allocator", allocator);
833 if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {833 if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
834 try self.fixed_buffer_allocator.resize(buf, new_len);834 return FixedBufferAllocator.resize(&self.fixed_buffer_allocator.allocator, buf, buf_align, new_len, len_align, return_address);
835 } else {835 } else {
836 try self.fallback_allocator.resize(buf, new_len);836 return self.fallback_allocator.resizeFn(self.fallback_allocator, buf, buf_align, new_len, len_align, return_address);
837 }837 }
838 }838 }
839 };839 };
...@@ -970,6 +970,16 @@ test "FixedBufferAllocator.reset" {...@@ -970,6 +970,16 @@ test "FixedBufferAllocator.reset" {
970 testing.expect(y.* == Y);970 testing.expect(y.* == Y);
971}971}
972972
973test "StackFallbackAllocator" {
974 const fallback_allocator = page_allocator;
975 var stack_allocator = stackFallback(4096, fallback_allocator);
976
977 try testAllocator(stack_allocator.get());
978 try testAllocatorAligned(stack_allocator.get());
979 try testAllocatorLargeAlignment(stack_allocator.get());
980 try testAllocatorAlignedShrink(stack_allocator.get());
981}
982
973test "FixedBufferAllocator Reuse memory on realloc" {983test "FixedBufferAllocator Reuse memory on realloc" {
974 var small_fixed_buffer: [10]u8 = undefined;984 var small_fixed_buffer: [10]u8 = undefined;
975 // check if we re-use the memory985 // check if we re-use the memory