| ... | ... | @@ -789,7 +789,7 @@ pub fn stackFallback(comptime size: usize, fallback_allocator: *Allocator) Stack |
| 789 | 789 | .fallback_allocator = fallback_allocator, |
| 790 | 790 | .fixed_buffer_allocator = undefined, |
| 791 | 791 | .allocator = Allocator{ |
| 792 | | .allocFn = StackFallbackAllocator(size).realloc, |
| 792 | .allocFn = StackFallbackAllocator(size).alloc, |
| 793 | 793 | .resizeFn = StackFallbackAllocator(size).resize, |
| 794 | 794 | }, |
| 795 | 795 | }; |
| ... | ... | @@ -815,25 +815,25 @@ pub fn StackFallbackAllocator(comptime size: usize) type { |
| 815 | 815 | ptr_align: u29, |
| 816 | 816 | len_align: u29, |
| 817 | 817 | return_address: usize, |
| 818 | | ) error{OutOfMemory}![*]u8 { |
| 818 | ) error{OutOfMemory}![]u8 { |
| 819 | 819 | 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); |
| 822 | 822 | } |
| 823 | 823 | |
| 824 | 824 | fn resize( |
| 825 | | self: *Allocator, |
| 825 | allocator: *Allocator, |
| 826 | 826 | buf: []u8, |
| 827 | 827 | buf_align: u29, |
| 828 | 828 | new_len: usize, |
| 829 | 829 | len_align: u29, |
| 830 | 830 | return_address: usize, |
| 831 | | ) error{OutOfMemory}!void { |
| 831 | ) error{OutOfMemory}!usize { |
| 832 | 832 | const self = @fieldParentPtr(Self, "allocator", allocator); |
| 833 | 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 | 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 | 970 | testing.expect(y.* == Y); |
| 971 | 971 | } |
| 972 | 972 | |
| 973 | test "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 | |
| 973 | 983 | test "FixedBufferAllocator Reuse memory on realloc" { |
| 974 | 984 | var small_fixed_buffer: [10]u8 = undefined; |
| 975 | 985 | // check if we re-use the memory |