authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-10-29 04:17:21+01:00
committergravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-11-30 23:32:48+00:00
log9377f32c089a925d7e6f1c64c1ce7777d108213c
treeb8f42ee42e8a0825b74be70499a2569184c10c52
parent80bbf234e0d31266c72bb6e93db2d2199ac5920d
signaturelock-open Commit is signed but in an unrecognized format.

allocgate: utilize a *const vtable field


8 files changed, 90 insertions(+), 66 deletions(-)

lib/std/heap.zig+24-10
...@@ -154,8 +154,11 @@ const CAllocator = struct {...@@ -154,8 +154,11 @@ const CAllocator = struct {
154/// `malloc`/`free`, see `raw_c_allocator`.154/// `malloc`/`free`, see `raw_c_allocator`.
155pub const c_allocator = Allocator{155pub const c_allocator = Allocator{
156 .ptr = undefined,156 .ptr = undefined,
157 .allocFn = CAllocator.alloc,157 .vtable = &c_allocator_vtable,
158 .resizeFn = CAllocator.resize,158};
159const c_allocator_vtable = Allocator.VTable{
160 .alloc = CAllocator.alloc,
161 .resize = CAllocator.resize,
159};162};
160163
161/// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls164/// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls
...@@ -165,8 +168,11 @@ pub const c_allocator = Allocator{...@@ -165,8 +168,11 @@ pub const c_allocator = Allocator{
165/// than `c_allocator`.168/// than `c_allocator`.
166pub const raw_c_allocator = Allocator{169pub const raw_c_allocator = Allocator{
167 .ptr = undefined,170 .ptr = undefined,
168 .allocFn = rawCAlloc,171 .vtable = &raw_c_allocator_vtable,
169 .resizeFn = rawCResize,172};
173const raw_c_allocator_vtable = Allocator.VTable{
174 .alloc = rawCAlloc,
175 .resize = rawCResize,
170};176};
171177
172fn rawCAlloc(178fn rawCAlloc(
...@@ -208,16 +214,14 @@ fn rawCResize(...@@ -208,16 +214,14 @@ fn rawCResize(
208pub const page_allocator = if (builtin.target.isWasm())214pub const page_allocator = if (builtin.target.isWasm())
209 Allocator{215 Allocator{
210 .ptr = undefined,216 .ptr = undefined,
211 .allocFn = WasmPageAllocator.alloc,217 .vtable = &WasmPageAllocator.vtable,
212 .resizeFn = WasmPageAllocator.resize,
213 }218 }
214else if (builtin.target.os.tag == .freestanding)219else if (builtin.target.os.tag == .freestanding)
215 root.os.heap.page_allocator220 root.os.heap.page_allocator
216else221else
217 Allocator{222 Allocator{
218 .ptr = undefined,223 .ptr = undefined,
219 .allocFn = PageAllocator.alloc,224 .vtable = &PageAllocator.vtable,
220 .resizeFn = PageAllocator.resize,
221 };225 };
222226
223/// Verifies that the adjusted length will still map to the full length227/// Verifies that the adjusted length will still map to the full length
...@@ -231,6 +235,11 @@ pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {...@@ -231,6 +235,11 @@ pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {
231pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;235pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;
232236
233const PageAllocator = struct {237const PageAllocator = struct {
238 const vtable = Allocator.VTable{
239 .alloc = alloc,
240 .resize = resize,
241 };
242
234 fn alloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {243 fn alloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {
235 _ = ra;244 _ = ra;
236 assert(n > 0);245 assert(n > 0);
...@@ -400,6 +409,11 @@ const WasmPageAllocator = struct {...@@ -400,6 +409,11 @@ const WasmPageAllocator = struct {
400 }409 }
401 }410 }
402411
412 const vtable = Allocator.VTable{
413 .alloc = alloc,
414 .resize = resize,
415 };
416
403 const PageStatus = enum(u1) {417 const PageStatus = enum(u1) {
404 used = 0,418 used = 0,
405 free = 1,419 free = 1,
...@@ -807,7 +821,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {...@@ -807,7 +821,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
807 return_address: usize,821 return_address: usize,
808 ) error{OutOfMemory}![]u8 {822 ) error{OutOfMemory}![]u8 {
809 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, ptr_align, len_align, return_address) catch823 return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, ptr_align, len_align, return_address) catch
810 return self.fallback_allocator.allocFn(self.fallback_allocator.ptr, len, ptr_align, len_align, return_address);824 return self.fallback_allocator.vtable.alloc(self.fallback_allocator.ptr, len, ptr_align, len_align, return_address);
811 }825 }
812826
813 fn resize(827 fn resize(
...@@ -821,7 +835,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {...@@ -821,7 +835,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
821 if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {835 if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
822 return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, buf_align, new_len, len_align, return_address);836 return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, buf_align, new_len, len_align, return_address);
823 } else {837 } else {
824 return self.fallback_allocator.resizeFn(self.fallback_allocator.ptr, buf, buf_align, new_len, len_align, return_address);838 return self.fallback_allocator.vtable.resize(self.fallback_allocator.ptr, buf, buf_align, new_len, len_align, return_address);
825 }839 }
826 }840 }
827 };841 };
lib/std/heap/arena_allocator.zig+1-1
...@@ -47,7 +47,7 @@ pub const ArenaAllocator = struct {...@@ -47,7 +47,7 @@ pub const ArenaAllocator = struct {
47 const actual_min_size = minimum_size + (@sizeOf(BufNode) + 16);47 const actual_min_size = minimum_size + (@sizeOf(BufNode) + 16);
48 const big_enough_len = prev_len + actual_min_size;48 const big_enough_len = prev_len + actual_min_size;
49 const len = big_enough_len + big_enough_len / 2;49 const len = big_enough_len + big_enough_len / 2;
50 const buf = try self.child_allocator.allocFn(self.child_allocator.ptr, len, @alignOf(BufNode), 1, @returnAddress());50 const buf = try self.child_allocator.vtable.alloc(self.child_allocator.ptr, len, @alignOf(BufNode), 1, @returnAddress());
51 const buf_node = @ptrCast(*BufNode, @alignCast(@alignOf(BufNode), buf.ptr));51 const buf_node = @ptrCast(*BufNode, @alignCast(@alignOf(BufNode), buf.ptr));
52 buf_node.* = BufNode{52 buf_node.* = BufNode{
53 .data = buf,53 .data = buf,
lib/std/heap/general_purpose_allocator.zig+1-1
...@@ -388,7 +388,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -388,7 +388,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
388 var it = self.large_allocations.iterator();388 var it = self.large_allocations.iterator();
389 while (it.next()) |large| {389 while (it.next()) |large| {
390 if (large.value_ptr.freed) {390 if (large.value_ptr.freed) {
391 _ = self.backing_allocator.resizeFn(self.backing_allocator.ptr, large.value_ptr.bytes, large.value_ptr.ptr_align, 0, 0, @returnAddress()) catch unreachable;391 _ = self.backing_allocator.vtable.resize(self.backing_allocator.ptr, large.value_ptr.bytes, large.value_ptr.ptr_align, 0, 0, @returnAddress()) catch unreachable;
392 }392 }
393 }393 }
394 }394 }
lib/std/heap/log_to_writer_allocator.zig+2-2
...@@ -29,7 +29,7 @@ pub fn LogToWriterAllocator(comptime Writer: type) type {...@@ -29,7 +29,7 @@ pub fn LogToWriterAllocator(comptime Writer: type) type {
29 ra: usize,29 ra: usize,
30 ) error{OutOfMemory}![]u8 {30 ) error{OutOfMemory}![]u8 {
31 self.writer.print("alloc : {}", .{len}) catch {};31 self.writer.print("alloc : {}", .{len}) catch {};
32 const result = self.parent_allocator.allocFn(self.parent_allocator.ptr, len, ptr_align, len_align, ra);32 const result = self.parent_allocator.vtable.alloc(self.parent_allocator.ptr, len, ptr_align, len_align, ra);
33 if (result) |_| {33 if (result) |_| {
34 self.writer.print(" success!\n", .{}) catch {};34 self.writer.print(" success!\n", .{}) catch {};
35 } else |_| {35 } else |_| {
...@@ -53,7 +53,7 @@ pub fn LogToWriterAllocator(comptime Writer: type) type {...@@ -53,7 +53,7 @@ pub fn LogToWriterAllocator(comptime Writer: type) type {
53 } else {53 } else {
54 self.writer.print("expand: {} to {}", .{ buf.len, new_len }) catch {};54 self.writer.print("expand: {} to {}", .{ buf.len, new_len }) catch {};
55 }55 }
56 if (self.parent_allocator.resizeFn(self.parent_allocator.ptr, buf, buf_align, new_len, len_align, ra)) |resized_len| {56 if (self.parent_allocator.vtable.resize(self.parent_allocator.ptr, buf, buf_align, new_len, len_align, ra)) |resized_len| {
57 if (new_len > buf.len) {57 if (new_len > buf.len) {
58 self.writer.print(" success!\n", .{}) catch {};58 self.writer.print(" success!\n", .{}) catch {};
59 }59 }
lib/std/heap/logging_allocator.zig+2-2
...@@ -53,7 +53,7 @@ pub fn ScopedLoggingAllocator(...@@ -53,7 +53,7 @@ pub fn ScopedLoggingAllocator(
53 len_align: u29,53 len_align: u29,
54 ra: usize,54 ra: usize,
55 ) error{OutOfMemory}![]u8 {55 ) error{OutOfMemory}![]u8 {
56 const result = self.parent_allocator.allocFn(self.parent_allocator.ptr, len, ptr_align, len_align, ra);56 const result = self.parent_allocator.vtable.alloc(self.parent_allocator.ptr, len, ptr_align, len_align, ra);
57 if (result) |_| {57 if (result) |_| {
58 logHelper(58 logHelper(
59 success_log_level,59 success_log_level,
...@@ -78,7 +78,7 @@ pub fn ScopedLoggingAllocator(...@@ -78,7 +78,7 @@ pub fn ScopedLoggingAllocator(
78 len_align: u29,78 len_align: u29,
79 ra: usize,79 ra: usize,
80 ) error{OutOfMemory}!usize {80 ) error{OutOfMemory}!usize {
81 if (self.parent_allocator.resizeFn(self.parent_allocator.ptr, buf, buf_align, new_len, len_align, ra)) |resized_len| {81 if (self.parent_allocator.vtable.resize(self.parent_allocator.ptr, buf, buf_align, new_len, len_align, ra)) |resized_len| {
82 if (new_len == 0) {82 if (new_len == 0) {
83 logHelper(success_log_level, "free - success - len: {}", .{buf.len});83 logHelper(success_log_level, "free - success - len: {}", .{buf.len});
84 } else if (new_len <= buf.len) {84 } else if (new_len <= buf.len) {
lib/std/mem.zig+11-7
...@@ -70,7 +70,7 @@ pub fn ValidationAllocator(comptime T: type) type {...@@ -70,7 +70,7 @@ pub fn ValidationAllocator(comptime T: type) type {
70 }70 }
7171
72 const underlying = self.getUnderlyingAllocatorPtr();72 const underlying = self.getUnderlyingAllocatorPtr();
73 const result = try underlying.allocFn(underlying.ptr, n, ptr_align, len_align, ret_addr);73 const result = try underlying.vtable.alloc(underlying.ptr, n, ptr_align, len_align, ret_addr);
74 assert(mem.isAligned(@ptrToInt(result.ptr), ptr_align));74 assert(mem.isAligned(@ptrToInt(result.ptr), ptr_align));
75 if (len_align == 0) {75 if (len_align == 0) {
76 assert(result.len == n);76 assert(result.len == n);
...@@ -95,7 +95,7 @@ pub fn ValidationAllocator(comptime T: type) type {...@@ -95,7 +95,7 @@ pub fn ValidationAllocator(comptime T: type) type {
95 assert(new_len >= len_align);95 assert(new_len >= len_align);
96 }96 }
97 const underlying = self.getUnderlyingAllocatorPtr();97 const underlying = self.getUnderlyingAllocatorPtr();
98 const result = try underlying.resizeFn(underlying.ptr, buf, buf_align, new_len, len_align, ret_addr);98 const result = try underlying.vtable.resize(underlying.ptr, buf, buf_align, new_len, len_align, ret_addr);
99 if (len_align == 0) {99 if (len_align == 0) {
100 assert(result == new_len);100 assert(result == new_len);
101 } else {101 } else {
...@@ -131,10 +131,14 @@ pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {...@@ -131,10 +131,14 @@ pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {
131 return adjusted;131 return adjusted;
132}132}
133133
134const failAllocator = Allocator{134const fail_allocator = Allocator{
135 .ptr = undefined,135 .ptr = undefined,
136 .allocFn = failAllocatorAlloc,136 .vtable = &failAllocator_vtable,
137 .resizeFn = Allocator.NoResize(c_void).noResize,137};
138
139const failAllocator_vtable = Allocator.VTable{
140 .alloc = failAllocatorAlloc,
141 .resize = Allocator.NoResize(c_void).noResize,
138};142};
139143
140fn failAllocatorAlloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) Allocator.Error![]u8 {144fn failAllocatorAlloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) Allocator.Error![]u8 {
...@@ -146,8 +150,8 @@ fn failAllocatorAlloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra:...@@ -146,8 +150,8 @@ fn failAllocatorAlloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra:
146}150}
147151
148test "mem.Allocator basics" {152test "mem.Allocator basics" {
149 try testing.expectError(error.OutOfMemory, failAllocator.alloc(u8, 1));153 try testing.expectError(error.OutOfMemory, fail_allocator.alloc(u8, 1));
150 try testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0));154 try testing.expectError(error.OutOfMemory, fail_allocator.allocSentinel(u8, 1, 0));
151}155}
152156
153test "Allocator.resize" {157test "Allocator.resize" {
lib/std/mem/Allocator.zig+47-41
...@@ -10,39 +10,42 @@ pub const Error = error{OutOfMemory};...@@ -10,39 +10,42 @@ pub const Error = error{OutOfMemory};
1010
11// The type erased pointer to the allocator implementation11// The type erased pointer to the allocator implementation
12ptr: *c_void,12ptr: *c_void,
1313vtable: *const VTable,
14/// Attempt to allocate at least `len` bytes aligned to `ptr_align`.14
15///15pub const VTable = struct {
16/// If `len_align` is `0`, then the length returned MUST be exactly `len` bytes,16 /// Attempt to allocate at least `len` bytes aligned to `ptr_align`.
17/// otherwise, the length must be aligned to `len_align`.17 ///
18///18 /// If `len_align` is `0`, then the length returned MUST be exactly `len` bytes,
19/// `len` must be greater than or equal to `len_align` and must be aligned by `len_align`.19 /// otherwise, the length must be aligned to `len_align`.
20///20 ///
21/// `ret_addr` is optionally provided as the first return address of the allocation call stack.21 /// `len` must be greater than or equal to `len_align` and must be aligned by `len_align`.
22/// If the value is `0` it means no return address has been provided.22 ///
23allocFn: fn (ptr: *c_void, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,23 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
2424 /// If the value is `0` it means no return address has been provided.
25/// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent25 alloc: fn (ptr: *c_void, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,
26/// length returned by `allocFn` or `resizeFn`. `buf_align` must equal the same value26
27/// that was passed as the `ptr_align` parameter to the original `allocFn` call.27 /// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent
28///28 /// length returned by `alloc` or `resize`. `buf_align` must equal the same value
29/// Passing a `new_len` of 0 frees and invalidates the buffer such that it can no29 /// that was passed as the `ptr_align` parameter to the original `alloc` call.
30/// longer be passed to `resizeFn`.30 ///
31///31 /// Passing a `new_len` of 0 frees and invalidates the buffer such that it can no
32/// error.OutOfMemory can only be returned if `new_len` is greater than `buf.len`.32 /// longer be passed to `resize`.
33/// If `buf` cannot be expanded to accomodate `new_len`, then the allocation MUST be33 ///
34/// unmodified and error.OutOfMemory MUST be returned.34 /// error.OutOfMemory can only be returned if `new_len` is greater than `buf.len`.
35///35 /// If `buf` cannot be expanded to accomodate `new_len`, then the allocation MUST be
36/// If `len_align` is `0`, then the length returned MUST be exactly `len` bytes,36 /// unmodified and error.OutOfMemory MUST be returned.
37/// otherwise, the length must be aligned to `len_align`. Note that `len_align` does *not*37 ///
38/// provide a way to modify the alignment of a pointer. Rather it provides an API for38 /// If `len_align` is `0`, then the length returned MUST be exactly `len` bytes,
39/// accepting more bytes of memory from the allocator than requested.39 /// otherwise, the length must be aligned to `len_align`. Note that `len_align` does *not*
40///40 /// provide a way to modify the alignment of a pointer. Rather it provides an API for
41/// `new_len` must be greater than or equal to `len_align` and must be aligned by `len_align`.41 /// accepting more bytes of memory from the allocator than requested.
42///42 ///
43/// `ret_addr` is optionally provided as the first return address of the allocation call stack.43 /// `new_len` must be greater than or equal to `len_align` and must be aligned by `len_align`.
44/// If the value is `0` it means no return address has been provided.44 ///
45resizeFn: fn (ptr: *c_void, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) Error!usize,45 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
46 /// If the value is `0` it means no return address has been provided.
47 resize: fn (ptr: *c_void, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) Error!usize,
48};
4649
47pub fn init(50pub fn init(
48 pointer: anytype,51 pointer: anytype,
...@@ -64,11 +67,14 @@ pub fn init(...@@ -64,11 +67,14 @@ pub fn init(
64 return resizeFn(self, buf, buf_align, new_len, len_align, ret_addr);67 return resizeFn(self, buf, buf_align, new_len, len_align, ret_addr);
65 }68 }
66 };69 };
70 const vtable = VTable{
71 .alloc = gen.alloc,
72 .resize = gen.resize,
73 };
6774
68 return .{75 return .{
69 .ptr = pointer,76 .ptr = pointer,
70 .allocFn = gen.alloc,77 .vtable = &vtable,
71 .resizeFn = gen.resize,
72 };78 };
73}79}
7480
...@@ -141,7 +147,7 @@ fn reallocBytes(...@@ -141,7 +147,7 @@ fn reallocBytes(
141 return_address: usize,147 return_address: usize,
142) Error![]u8 {148) Error![]u8 {
143 if (old_mem.len == 0) {149 if (old_mem.len == 0) {
144 const new_mem = try self.allocFn(self.ptr, new_byte_count, new_alignment, len_align, return_address);150 const new_mem = try self.vtable.alloc(self.ptr, new_byte_count, new_alignment, len_align, return_address);
145 // TODO: https://github.com/ziglang/zig/issues/4298151 // TODO: https://github.com/ziglang/zig/issues/4298
146 @memset(new_mem.ptr, undefined, new_byte_count);152 @memset(new_mem.ptr, undefined, new_byte_count);
147 return new_mem;153 return new_mem;
...@@ -152,7 +158,7 @@ fn reallocBytes(...@@ -152,7 +158,7 @@ fn reallocBytes(
152 const shrunk_len = self.shrinkBytes(old_mem, old_alignment, new_byte_count, len_align, return_address);158 const shrunk_len = self.shrinkBytes(old_mem, old_alignment, new_byte_count, len_align, return_address);
153 return old_mem.ptr[0..shrunk_len];159 return old_mem.ptr[0..shrunk_len];
154 }160 }
155 if (self.resizeFn(self.ptr, old_mem, old_alignment, new_byte_count, len_align, return_address)) |resized_len| {161 if (self.vtable.resize(self.ptr, old_mem, old_alignment, new_byte_count, len_align, return_address)) |resized_len| {
156 assert(resized_len >= new_byte_count);162 assert(resized_len >= new_byte_count);
157 // TODO: https://github.com/ziglang/zig/issues/4298163 // TODO: https://github.com/ziglang/zig/issues/4298
158 @memset(old_mem.ptr + new_byte_count, undefined, resized_len - new_byte_count);164 @memset(old_mem.ptr + new_byte_count, undefined, resized_len - new_byte_count);
...@@ -178,7 +184,7 @@ fn moveBytes(...@@ -178,7 +184,7 @@ fn moveBytes(
178) Error![]u8 {184) Error![]u8 {
179 assert(old_mem.len > 0);185 assert(old_mem.len > 0);
180 assert(new_len > 0);186 assert(new_len > 0);
181 const new_mem = try self.allocFn(self.ptr, new_len, new_alignment, len_align, return_address);187 const new_mem = try self.vtable.alloc(self.ptr, new_len, new_alignment, len_align, return_address);
182 @memcpy(new_mem.ptr, old_mem.ptr, math.min(new_len, old_mem.len));188 @memcpy(new_mem.ptr, old_mem.ptr, math.min(new_len, old_mem.len));
183 // TODO https://github.com/ziglang/zig/issues/4298189 // TODO https://github.com/ziglang/zig/issues/4298
184 @memset(old_mem.ptr, undefined, old_mem.len);190 @memset(old_mem.ptr, undefined, old_mem.len);
...@@ -320,7 +326,7 @@ pub fn allocAdvancedWithRetAddr(...@@ -320,7 +326,7 @@ pub fn allocAdvancedWithRetAddr(
320 .exact => 0,326 .exact => 0,
321 .at_least => size_of_T,327 .at_least => size_of_T,
322 };328 };
323 const byte_slice = try self.allocFn(self.ptr, byte_count, a, len_align, return_address);329 const byte_slice = try self.vtable.alloc(self.ptr, byte_count, a, len_align, return_address);
324 switch (exact) {330 switch (exact) {
325 .exact => assert(byte_slice.len == byte_count),331 .exact => assert(byte_slice.len == byte_count),
326 .at_least => assert(byte_slice.len >= byte_count),332 .at_least => assert(byte_slice.len >= byte_count),
...@@ -345,7 +351,7 @@ pub fn resize(self: Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(old...@@ -345,7 +351,7 @@ pub fn resize(self: Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(old
345 }351 }
346 const old_byte_slice = mem.sliceAsBytes(old_mem);352 const old_byte_slice = mem.sliceAsBytes(old_mem);
347 const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;353 const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
348 const rc = try self.resizeFn(self.ptr, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());354 const rc = try self.vtable.resize(self.ptr, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());
349 assert(rc == new_byte_count);355 assert(rc == new_byte_count);
350 const new_byte_slice = old_byte_slice.ptr[0..new_byte_count];356 const new_byte_slice = old_byte_slice.ptr[0..new_byte_count];
351 return mem.bytesAsSlice(T, new_byte_slice);357 return mem.bytesAsSlice(T, new_byte_slice);
...@@ -514,5 +520,5 @@ pub fn shrinkBytes(...@@ -514,5 +520,5 @@ pub fn shrinkBytes(
514 return_address: usize,520 return_address: usize,
515) usize {521) usize {
516 assert(new_len <= buf.len);522 assert(new_len <= buf.len);
517 return self.resizeFn(self.ptr, buf, buf_align, new_len, len_align, return_address) catch unreachable;523 return self.vtable.resize(self.ptr, buf, buf_align, new_len, len_align, return_address) catch unreachable;
518}524}
lib/std/testing/failing_allocator.zig+2-2
...@@ -54,7 +54,7 @@ pub const FailingAllocator = struct {...@@ -54,7 +54,7 @@ pub const FailingAllocator = struct {
54 if (self.index == self.fail_index) {54 if (self.index == self.fail_index) {
55 return error.OutOfMemory;55 return error.OutOfMemory;
56 }56 }
57 const result = try self.internal_allocator.allocFn(self.internal_allocator.ptr, len, ptr_align, len_align, return_address);57 const result = try self.internal_allocator.vtable.alloc(self.internal_allocator.ptr, len, ptr_align, len_align, return_address);
58 self.allocated_bytes += result.len;58 self.allocated_bytes += result.len;
59 self.allocations += 1;59 self.allocations += 1;
60 self.index += 1;60 self.index += 1;
...@@ -69,7 +69,7 @@ pub const FailingAllocator = struct {...@@ -69,7 +69,7 @@ pub const FailingAllocator = struct {
69 len_align: u29,69 len_align: u29,
70 ra: usize,70 ra: usize,
71 ) error{OutOfMemory}!usize {71 ) error{OutOfMemory}!usize {
72 const r = self.internal_allocator.resizeFn(self.internal_allocator.ptr, old_mem, old_align, new_len, len_align, ra) catch |e| {72 const r = self.internal_allocator.vtable.resize(self.internal_allocator.ptr, old_mem, old_align, new_len, len_align, ra) catch |e| {
73 std.debug.assert(new_len > old_mem.len);73 std.debug.assert(new_len > old_mem.len);
74 return e;74 return e;
75 };75 };