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

This commit is contained in:
Andrew Kelley 2022-01-26 19:58:40 -07:00
parent 50accb757f
commit db55f469c1

View File

@ -23,7 +23,10 @@ pub const VTable = struct {
/// ///
/// `ret_addr` is optionally provided as the first return address of the allocation call stack. /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
/// If the value is `0` it means no return address has been provided. /// If the value is `0` it means no return address has been provided.
alloc: fn (ptr: *anyopaque, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8, alloc: switch (builtin.zig_backend) {
.stage1 => allocProto, // temporary workaround until we replace stage1 with stage2
else => *const allocProto,
},
/// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent /// Attempt to expand or shrink memory in place. `buf.len` must equal the most recent
/// length returned by `alloc` or `resize`. `buf_align` must equal the same value /// length returned by `alloc` or `resize`. `buf_align` must equal the same value
@ -42,16 +45,26 @@ pub const VTable = struct {
/// ///
/// `ret_addr` is optionally provided as the first return address of the allocation call stack. /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
/// If the value is `0` it means no return address has been provided. /// If the value is `0` it means no return address has been provided.
resize: fn (ptr: *anyopaque, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize, resize: switch (builtin.zig_backend) {
.stage1 => resizeProto, // temporary workaround until we replace stage1 with stage2
else => *const resizeProto,
},
/// Free and invalidate a buffer. `buf.len` must equal the most recent length returned by `alloc` or `resize`. /// Free and invalidate a buffer. `buf.len` must equal the most recent length returned by `alloc` or `resize`.
/// `buf_align` must equal the same value that was passed as the `ptr_align` parameter to the original `alloc` call. /// `buf_align` must equal the same value that was passed as the `ptr_align` parameter to the original `alloc` call.
/// ///
/// `ret_addr` is optionally provided as the first return address of the allocation call stack. /// `ret_addr` is optionally provided as the first return address of the allocation call stack.
/// If the value is `0` it means no return address has been provided. /// If the value is `0` it means no return address has been provided.
free: fn (ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void, free: switch (builtin.zig_backend) {
.stage1 => freeProto, // temporary workaround until we replace stage1 with stage2
else => *const freeProto,
},
}; };
const allocProto = fn (ptr: *anyopaque, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8;
const resizeProto = fn (ptr: *anyopaque, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize;
const freeProto = fn (ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void;
pub fn init( pub fn init(
pointer: anytype, pointer: anytype,
comptime allocFn: fn (ptr: @TypeOf(pointer), len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8, comptime allocFn: fn (ptr: @TypeOf(pointer), len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,