Commit db55f469c1

Andrew Kelley <andrew@ziglang.org>
2022-01-27 03:58:40
std.mem.Allocator: upgrade to new function pointer semantics
1 parent 50accb7
Changed files (1)
lib
lib/std/mem/Allocator.zig
@@ -23,7 +23,10 @@ pub const VTable = struct {
     ///
     /// `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.
-    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
     /// 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.
     /// 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`. 
     /// `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.
     /// 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(
     pointer: anytype,
     comptime allocFn: fn (ptr: @TypeOf(pointer), len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8,