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
789789 .fallback_allocator = fallback_allocator,
790790 .fixed_buffer_allocator = undefined,
791791 .allocator = Allocator{
792 .allocFn = StackFallbackAllocator(size).realloc,
792 .allocFn = StackFallbackAllocator(size).alloc,
793793 .resizeFn = StackFallbackAllocator(size).resize,
794794 },
795795 };
......@@ -815,25 +815,25 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
815815 ptr_align: u29,
816816 len_align: u29,
817817 return_address: usize,
818 ) error{OutOfMemory}![*]u8 {
818 ) error{OutOfMemory}![]u8 {
819819 const self = @fieldParentPtr(Self, "allocator", allocator);
820 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, ptr_align) catch
821 return fallback_allocator.alloc(len, ptr_align);
820 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator.allocator, len, ptr_align, len_align, return_address) catch
821 return self.fallback_allocator.allocFn(self.fallback_allocator, len, ptr_align, len_align, return_address);
822822 }
823823
824824 fn resize(
825 self: *Allocator,
825 allocator: *Allocator,
826826 buf: []u8,
827827 buf_align: u29,
828828 new_len: usize,
829829 len_align: u29,
830830 return_address: usize,
831 ) error{OutOfMemory}!void {
831 ) error{OutOfMemory}!usize {
832832 const self = @fieldParentPtr(Self, "allocator", allocator);
833833 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);
835835 } 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);
837837 }
838838 }
839839 };
......@@ -970,6 +970,16 @@ test "FixedBufferAllocator.reset" {
970970 testing.expect(y.* == Y);
971971}
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
973983test "FixedBufferAllocator Reuse memory on realloc" {
974984 var small_fixed_buffer: [10]u8 = undefined;
975985 // check if we re-use the memory