authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-26 19:58:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-26 20:02:01-07:00
logdb55f469c12c01831bd393c6701c26c15ffe726c
tree60a760bef0cf5288188bde6a5b8861e74b49208d
parent50accb757fb8514c3eac824fad5d09d569a6fdc9

std.mem.Allocator: upgrade to new function pointer semantics


1 files changed, 16 insertions(+), 3 deletions(-)

lib/std/mem/Allocator.zig+16-3
...@@ -23,7 +23,10 @@ pub const VTable = struct {...@@ -23,7 +23,10 @@ pub const VTable = struct {
23 ///23 ///
24 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.24 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
25 /// If the value is `0` it means no return address has been provided.25 /// If the value is `0` it means no return address has been provided.
26 alloc: fn (ptr: *anyopaque, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,26 alloc: switch (builtin.zig_backend) {
27 .stage1 => allocProto, // temporary workaround until we replace stage1 with stage2
28 else => *const allocProto,
29 },
2730
28 /// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent31 /// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent
29 /// length returned by `alloc` or `resize`. `buf_align` must equal the same value32 /// length returned by `alloc` or `resize`. `buf_align` must equal the same value
...@@ -42,16 +45,26 @@ pub const VTable = struct {...@@ -42,16 +45,26 @@ pub const VTable = struct {
42 ///45 ///
43 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.46 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
44 /// If the value is `0` it means no return address has been provided.47 /// If the value is `0` it means no return address has been provided.
45 resize: fn (ptr: *anyopaque, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize,48 resize: switch (builtin.zig_backend) {
49 .stage1 => resizeProto, // temporary workaround until we replace stage1 with stage2
50 else => *const resizeProto,
51 },
4652
47 /// Free and invalidate a buffer. `buf.len` must equal the most recent length returned by `alloc` or `resize`. 53 /// Free and invalidate a buffer. `buf.len` must equal the most recent length returned by `alloc` or `resize`.
48 /// `buf_align` must equal the same value that was passed as the `ptr_align` parameter to the original `alloc` call.54 /// `buf_align` must equal the same value that was passed as the `ptr_align` parameter to the original `alloc` call.
49 ///55 ///
50 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.56 /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
51 /// If the value is `0` it means no return address has been provided.57 /// If the value is `0` it means no return address has been provided.
52 free: fn (ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void,58 free: switch (builtin.zig_backend) {
59 .stage1 => freeProto, // temporary workaround until we replace stage1 with stage2
60 else => *const freeProto,
61 },
53};62};
5463
64const allocProto = fn (ptr: *anyopaque, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8;
65const resizeProto = fn (ptr: *anyopaque, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize;
66const freeProto = fn (ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void;
67
55pub fn init(68pub fn init(
56 pointer: anytype,69 pointer: anytype,
57 comptime allocFn: fn (ptr: @TypeOf(pointer), len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,70 comptime allocFn: fn (ptr: @TypeOf(pointer), len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,