authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-04-11 17:55:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-04-13 02:20:32-04:00
logf32a5d349d2c359a2a1f627aa70e1a7a6f6330ea
tree05a0fe7de04e6aa91775e954f78dd1a478d7e675
parentec2888858102035f296c01df5aacbd255c35d06f

std: eradicate u29 and embrace std.mem.Alignment


32 files changed, 153 insertions(+), 156 deletions(-)

lib/compiler/aro/aro/Parser.zig+2-2
......@@ -101,7 +101,7 @@ value_map: Tree.ValueMap,
101101
102102// buffers used during compilation
103103syms: SymbolStack = .{},
104strings: std.ArrayListAligned(u8, 4),
104strings: std.ArrayListAligned(u8, .@"4"),
105105labels: std.ArrayList(Label),
106106list_buf: NodeList,
107107decl_buf: NodeList,
......@@ -693,7 +693,7 @@ pub fn parse(pp: *Preprocessor) Compilation.Error!Tree {
693693 .gpa = pp.comp.gpa,
694694 .arena = arena.allocator(),
695695 .tok_ids = pp.tokens.items(.id),
696 .strings = std.ArrayListAligned(u8, 4).init(pp.comp.gpa),
696 .strings = std.ArrayListAligned(u8, .@"4").init(pp.comp.gpa),
697697 .value_map = Tree.ValueMap.init(pp.comp.gpa),
698698 .data = NodeList.init(pp.comp.gpa),
699699 .labels = std.ArrayList(Label).init(pp.comp.gpa),
lib/compiler/aro/aro/Preprocessor.zig+1-1
......@@ -983,7 +983,7 @@ fn expr(pp: *Preprocessor, tokenizer: *Tokenizer) MacroError!bool {
983983 .tok_i = @intCast(token_state.tokens_len),
984984 .arena = pp.arena.allocator(),
985985 .in_macro = true,
986 .strings = std.ArrayListAligned(u8, 4).init(pp.comp.gpa),
986 .strings = std.ArrayListAligned(u8, .@"4").init(pp.comp.gpa),
987987
988988 .data = undefined,
989989 .value_map = undefined,
lib/compiler/objcopy.zig+5-5
......@@ -841,12 +841,12 @@ fn ElfFile(comptime is_64: bool) type {
841841 arena: std.heap.ArenaAllocator,
842842
843843 const SectionCategory = ElfFileHelper.SectionCategory;
844 const section_memory_align = @alignOf(Elf_Sym); // most restrictive of what we may load in memory
844 const section_memory_align: std.mem.Alignment = .of(Elf_Sym); // most restrictive of what we may load in memory
845845 const Section = struct {
846846 section: Elf_Shdr,
847847 name: []const u8 = "",
848848 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)
849 payload: ?[]align(section_memory_align) const u8 = null, // if we need the data in memory
849 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null, // if we need the data in memory
850850 category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both.
851851 };
852852
......@@ -999,7 +999,7 @@ fn ElfFile(comptime is_64: bool) type {
999999 remap_idx: u16,
10001000
10011001 // optionally overrides the payload from the source file
1002 payload: ?[]align(section_memory_align) const u8 = null,
1002 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null,
10031003 section: ?Elf_Shdr = null,
10041004 };
10051005 const sections_update = try allocator.alloc(Update, self.sections.len);
......@@ -1219,7 +1219,7 @@ fn ElfFile(comptime is_64: bool) type {
12191219 if (options.debuglink) |link| {
12201220 const payload = payload: {
12211221 const crc_offset = std.mem.alignForward(usize, link.name.len + 1, 4);
1222 const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4);
1222 const buf = try allocator.alignedAlloc(u8, .@"4", crc_offset + 4);
12231223 @memcpy(buf[0..link.name.len], link.name);
12241224 @memset(buf[link.name.len..crc_offset], 0);
12251225 @memcpy(buf[crc_offset..], std.mem.asBytes(&link.crc32));
......@@ -1498,7 +1498,7 @@ const ElfFileHelper = struct {
14981498 var section_reader = std.io.limitedReader(in_file.reader(), size);
14991499
15001500 // allocate as large as decompressed data. if the compression doesn't fit, keep the data uncompressed.
1501 const compressed_data = try allocator.alignedAlloc(u8, 8, @intCast(size));
1501 const compressed_data = try allocator.alignedAlloc(u8, .@"8", @intCast(size));
15021502 var compressed_stream = std.io.fixedBufferStream(compressed_data);
15031503
15041504 try compressed_stream.writer().writeAll(prefix);
lib/std/Build/Step/CheckObject.zig+1-1
......@@ -563,7 +563,7 @@ fn make(step: *Step, make_options: Step.MakeOptions) !void {
563563 src_path.sub_path,
564564 check_object.max_bytes,
565565 null,
566 @alignOf(u64),
566 .of(u64),
567567 null,
568568 ) catch |err| return step.fail("unable to read '{'}': {s}", .{ src_path, @errorName(err) });
569569
lib/std/Build/Watch.zig+1-1
......@@ -56,7 +56,7 @@ const Os = switch (builtin.os.tag) {
5656 const bytes = lfh.slice();
5757 const new_ptr = try gpa.alignedAlloc(
5858 u8,
59 @alignOf(std.os.linux.file_handle),
59 .of(std.os.linux.file_handle),
6060 @sizeOf(std.os.linux.file_handle) + bytes.len,
6161 );
6262 const new_header: *std.os.linux.file_handle = @ptrCast(new_ptr);
lib/std/array_hash_map.zig+1-1
......@@ -2129,7 +2129,7 @@ const IndexHeader = struct {
21292129 const len = @as(usize, 1) << @as(math.Log2Int(usize), @intCast(new_bit_index));
21302130 const index_size = hash_map.capacityIndexSize(new_bit_index);
21312131 const nbytes = @sizeOf(IndexHeader) + index_size * len;
2132 const bytes = try gpa.alignedAlloc(u8, @alignOf(IndexHeader), nbytes);
2132 const bytes = try gpa.alignedAlloc(u8, .of(IndexHeader), nbytes);
21332133 @memset(bytes[@sizeOf(IndexHeader)..], 0xff);
21342134 const result: *IndexHeader = @alignCast(@ptrCast(bytes.ptr));
21352135 result.* = .{
lib/std/array_list.zig+12-12
......@@ -22,9 +22,9 @@ pub fn ArrayList(comptime T: type) type {
2222///
2323/// This struct internally stores a `std.mem.Allocator` for memory management.
2424/// To manually specify an allocator with each function call see `ArrayListAlignedUnmanaged`.
25pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
25pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
2626 if (alignment) |a| {
27 if (a == @alignOf(T)) {
27 if (a.toByteUnits() == @alignOf(T)) {
2828 return ArrayListAligned(T, null);
2929 }
3030 }
......@@ -43,10 +43,10 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
4343 capacity: usize,
4444 allocator: Allocator,
4545
46 pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
46 pub const Slice = if (alignment) |a| ([]align(a.toByteUnits()) T) else []T;
4747
4848 pub fn SentinelSlice(comptime s: T) type {
49 return if (alignment) |a| ([:s]align(a) T) else [:s]T;
49 return if (alignment) |a| ([:s]align(a.toByteUnits()) T) else [:s]T;
5050 }
5151
5252 /// Deinitialize with `deinit` or use `toOwnedSlice`.
......@@ -611,9 +611,9 @@ pub fn ArrayListUnmanaged(comptime T: type) type {
611611/// or use `toOwnedSlice`.
612612///
613613/// Default initialization of this struct is deprecated; use `.empty` instead.
614pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {
614pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alignment) type {
615615 if (alignment) |a| {
616 if (a == @alignOf(T)) {
616 if (a.toByteUnits() == @alignOf(T)) {
617617 return ArrayListAlignedUnmanaged(T, null);
618618 }
619619 }
......@@ -637,10 +637,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
637637 .capacity = 0,
638638 };
639639
640 pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
640 pub const Slice = if (alignment) |a| ([]align(a.toByteUnits()) T) else []T;
641641
642642 pub fn SentinelSlice(comptime s: T) type {
643 return if (alignment) |a| ([:s]align(a) T) else [:s]T;
643 return if (alignment) |a| ([:s]align(a.toByteUnits()) T) else [:s]T;
644644 }
645645
646646 /// Initialize with capacity to hold `num` elements.
......@@ -1913,7 +1913,7 @@ test "ArrayList(u8) implements writer" {
19131913 try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items);
19141914 }
19151915 {
1916 var list = ArrayListAligned(u8, 2).init(a);
1916 var list = ArrayListAligned(u8, .@"2").init(a);
19171917 defer list.deinit();
19181918
19191919 const writer = list.writer();
......@@ -1940,7 +1940,7 @@ test "ArrayListUnmanaged(u8) implements writer" {
19401940 try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items);
19411941 }
19421942 {
1943 var list: ArrayListAlignedUnmanaged(u8, 2) = .empty;
1943 var list: ArrayListAlignedUnmanaged(u8, .@"2") = .empty;
19441944 defer list.deinit(a);
19451945
19461946 const writer = list.writer(a);
......@@ -2126,7 +2126,7 @@ test "toOwnedSliceSentinel" {
21262126test "accepts unaligned slices" {
21272127 const a = testing.allocator;
21282128 {
2129 var list = std.ArrayListAligned(u8, 8).init(a);
2129 var list = std.ArrayListAligned(u8, .@"8").init(a);
21302130 defer list.deinit();
21312131
21322132 try list.appendSlice(&.{ 0, 1, 2, 3 });
......@@ -2136,7 +2136,7 @@ test "accepts unaligned slices" {
21362136 try testing.expectEqualSlices(u8, list.items, &.{ 0, 8, 9, 6, 7, 2, 3 });
21372137 }
21382138 {
2139 var list: std.ArrayListAlignedUnmanaged(u8, 8) = .empty;
2139 var list: std.ArrayListAlignedUnmanaged(u8, .@"8") = .empty;
21402140 defer list.deinit(a);
21412141
21422142 try list.appendSlice(a, &.{ 0, 1, 2, 3 });
lib/std/bounded_array.zig+11-10
......@@ -2,6 +2,7 @@ const std = @import("std.zig");
22const assert = std.debug.assert;
33const mem = std.mem;
44const testing = std.testing;
5const Alignment = std.mem.Alignment;
56
67/// A structure with an array and a length, that can be used as a slice.
78///
......@@ -16,7 +17,7 @@ const testing = std.testing;
1617/// var a_clone = a; // creates a copy - the structure doesn't use any internal pointers
1718/// ```
1819pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
19 return BoundedArrayAligned(T, @alignOf(T), buffer_capacity);
20 return BoundedArrayAligned(T, .of(T), buffer_capacity);
2021}
2122
2223/// A structure with an array, length and alignment, that can be used as a
......@@ -34,12 +35,12 @@ pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
3435/// ```
3536pub fn BoundedArrayAligned(
3637 comptime T: type,
37 comptime alignment: u29,
38 comptime alignment: Alignment,
3839 comptime buffer_capacity: usize,
3940) type {
4041 return struct {
4142 const Self = @This();
42 buffer: [buffer_capacity]T align(alignment) = undefined,
43 buffer: [buffer_capacity]T align(alignment.toByteUnits()) = undefined,
4344 len: usize = 0,
4445
4546 /// Set the actual length of the slice.
......@@ -51,15 +52,15 @@ pub fn BoundedArrayAligned(
5152
5253 /// View the internal array as a slice whose size was previously set.
5354 pub fn slice(self: anytype) switch (@TypeOf(&self.buffer)) {
54 *align(alignment) [buffer_capacity]T => []align(alignment) T,
55 *align(alignment) const [buffer_capacity]T => []align(alignment) const T,
55 *align(alignment.toByteUnits()) [buffer_capacity]T => []align(alignment.toByteUnits()) T,
56 *align(alignment.toByteUnits()) const [buffer_capacity]T => []align(alignment.toByteUnits()) const T,
5657 else => unreachable,
5758 } {
5859 return self.buffer[0..self.len];
5960 }
6061
6162 /// View the internal array as a constant slice whose size was previously set.
62 pub fn constSlice(self: *const Self) []align(alignment) const T {
63 pub fn constSlice(self: *const Self) []align(alignment.toByteUnits()) const T {
6364 return self.slice();
6465 }
6566
......@@ -120,7 +121,7 @@ pub fn BoundedArrayAligned(
120121
121122 /// Resize the slice, adding `n` new elements, which have `undefined` values.
122123 /// The return value is a pointer to the array of uninitialized elements.
123 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {
124 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment.toByteUnits()) [n]T {
124125 const prev_len = self.len;
125126 try self.resize(self.len + n);
126127 return self.slice()[prev_len..][0..n];
......@@ -128,7 +129,7 @@ pub fn BoundedArrayAligned(
128129
129130 /// Resize the slice, adding `n` new elements, which have `undefined` values.
130131 /// The return value is a slice pointing to the uninitialized elements.
131 pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment) T {
132 pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment.toByteUnits()) T {
132133 const prev_len = self.len;
133134 try self.resize(self.len + n);
134135 return self.slice()[prev_len..][0..n];
......@@ -146,7 +147,7 @@ pub fn BoundedArrayAligned(
146147 /// This can be useful for writing directly into it.
147148 /// Note that such an operation must be followed up with a
148149 /// call to `resize()`
149 pub fn unusedCapacitySlice(self: *Self) []align(alignment) T {
150 pub fn unusedCapacitySlice(self: *Self) []align(alignment.toByteUnits()) T {
150151 return self.buffer[self.len..];
151152 }
152153
......@@ -399,7 +400,7 @@ test BoundedArray {
399400}
400401
401402test "BoundedArrayAligned" {
402 var a = try BoundedArrayAligned(u8, 16, 4).init(0);
403 var a = try BoundedArrayAligned(u8, .@"16", 4).init(0);
403404 try a.append(0);
404405 try a.append(0);
405406 try a.append(255);
lib/std/crypto/argon2.zig+1-1
......@@ -14,7 +14,7 @@ const pwhash = crypto.pwhash;
1414
1515const Thread = std.Thread;
1616const Blake2b512 = blake2.Blake2b512;
17const Blocks = std.ArrayListAligned([block_length]u64, 16);
17const Blocks = std.ArrayListAligned([block_length]u64, .@"16");
1818const H0 = [Blake2b512.digest_length + 8]u8;
1919
2020const EncodingError = crypto.errors.EncodingError;
lib/std/crypto/scrypt.zig+3-3
......@@ -195,11 +195,11 @@ pub fn kdf(
195195 params.r > max_int / 256 or
196196 n > max_int / 128 / @as(u64, params.r)) return KdfError.WeakParameters;
197197
198 const xy = try allocator.alignedAlloc(u32, 16, 64 * params.r);
198 const xy = try allocator.alignedAlloc(u32, .@"16", 64 * params.r);
199199 defer allocator.free(xy);
200 const v = try allocator.alignedAlloc(u32, 16, 32 * n * params.r);
200 const v = try allocator.alignedAlloc(u32, .@"16", 32 * n * params.r);
201201 defer allocator.free(v);
202 var dk = try allocator.alignedAlloc(u8, 16, params.p * 128 * params.r);
202 var dk = try allocator.alignedAlloc(u8, .@"16", params.p * 128 * params.r);
203203 defer allocator.free(dk);
204204
205205 try pwhash.pbkdf2(dk, password, salt, 1, HmacSha256);
lib/std/fs/Dir.zig+3-3
......@@ -1960,7 +1960,7 @@ pub fn readFile(self: Dir, file_path: []const u8, buffer: []u8) ![]u8 {
19601960/// On WASI, `file_path` should be encoded as valid UTF-8.
19611961/// On other platforms, `file_path` is an opaque sequence of bytes with no particular encoding.
19621962pub fn readFileAlloc(self: Dir, allocator: mem.Allocator, file_path: []const u8, max_bytes: usize) ![]u8 {
1963 return self.readFileAllocOptions(allocator, file_path, max_bytes, null, @alignOf(u8), null);
1963 return self.readFileAllocOptions(allocator, file_path, max_bytes, null, .of(u8), null);
19641964}
19651965
19661966/// On success, caller owns returned buffer.
......@@ -1977,9 +1977,9 @@ pub fn readFileAllocOptions(
19771977 file_path: []const u8,
19781978 max_bytes: usize,
19791979 size_hint: ?usize,
1980 comptime alignment: u29,
1980 comptime alignment: std.mem.Alignment,
19811981 comptime optional_sentinel: ?u8,
1982) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
1982) !(if (optional_sentinel) |s| [:s]align(alignment.toByteUnits()) u8 else []align(alignment.toByteUnits()) u8) {
19831983 var file = try self.openFile(file_path, .{});
19841984 defer file.close();
19851985
lib/std/fs/File.zig+4-3
......@@ -1133,7 +1133,7 @@ pub fn updateTimes(
11331133/// On success, caller owns returned buffer.
11341134/// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
11351135pub fn readToEndAlloc(self: File, allocator: Allocator, max_bytes: usize) ![]u8 {
1136 return self.readToEndAllocOptions(allocator, max_bytes, null, @alignOf(u8), null);
1136 return self.readToEndAllocOptions(allocator, max_bytes, null, .of(u8), null);
11371137}
11381138
11391139/// Reads all the bytes from the current position to the end of the file.
......@@ -1147,9 +1147,9 @@ pub fn readToEndAllocOptions(
11471147 allocator: Allocator,
11481148 max_bytes: usize,
11491149 size_hint: ?usize,
1150 comptime alignment: u29,
1150 comptime alignment: Alignment,
11511151 comptime optional_sentinel: ?u8,
1152) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
1152) !(if (optional_sentinel) |s| [:s]align(alignment.toByteUnits()) u8 else []align(alignment.toByteUnits()) u8) {
11531153 // If no size hint is provided fall back to the size=0 code path
11541154 const size = size_hint orelse 0;
11551155
......@@ -1782,3 +1782,4 @@ const windows = std.os.windows;
17821782const Os = std.builtin.Os;
17831783const maxInt = std.math.maxInt;
17841784const is_windows = builtin.os.tag == .windows;
1785const Alignment = std.mem.Alignment;
lib/std/hash_map.zig+3-2
......@@ -6,6 +6,7 @@ const math = std.math;
66const mem = std.mem;
77const Allocator = mem.Allocator;
88const Wyhash = std.hash.Wyhash;
9const Alignment = std.mem.Alignment;
910
1011pub fn getAutoHashFn(comptime K: type, comptime Context: type) (fn (Context, K) u64) {
1112 comptime {
......@@ -1460,7 +1461,7 @@ pub fn HashMapUnmanaged(
14601461 const header_align = @alignOf(Header);
14611462 const key_align = if (@sizeOf(K) == 0) 1 else @alignOf(K);
14621463 const val_align = if (@sizeOf(V) == 0) 1 else @alignOf(V);
1463 const max_align = comptime @max(header_align, key_align, val_align);
1464 const max_align: Alignment = comptime .fromByteUnits(@max(header_align, key_align, val_align));
14641465
14651466 const new_cap: usize = new_capacity;
14661467 const meta_size = @sizeOf(Header) + new_cap * @sizeOf(Metadata);
......@@ -1472,7 +1473,7 @@ pub fn HashMapUnmanaged(
14721473 const vals_start = std.mem.alignForward(usize, keys_end, val_align);
14731474 const vals_end = vals_start + new_cap * @sizeOf(V);
14741475
1475 const total_size = std.mem.alignForward(usize, vals_end, max_align);
1476 const total_size = max_align.forward(vals_end);
14761477
14771478 const slice = try allocator.alignedAlloc(u8, max_align, total_size);
14781479 const ptr: [*]u8 = @ptrCast(slice.ptr);
lib/std/heap.zig+19-18
......@@ -7,6 +7,7 @@ const mem = std.mem;
77const c = std.c;
88const Allocator = std.mem.Allocator;
99const windows = std.os.windows;
10const Alignment = std.mem.Alignment;
1011
1112pub const ArenaAllocator = @import("heap/arena_allocator.zig").ArenaAllocator;
1213pub const SmpAllocator = @import("heap/SmpAllocator.zig");
......@@ -153,7 +154,7 @@ const CAllocator = struct {
153154 return @alignCast(@ptrCast(ptr - @sizeOf(usize)));
154155 }
155156
156 fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
157 fn alignedAlloc(len: usize, alignment: Alignment) ?[*]u8 {
157158 const alignment_bytes = alignment.toByteUnits();
158159 if (supports_posix_memalign) {
159160 // The posix_memalign only accepts alignment values that are a
......@@ -201,7 +202,7 @@ const CAllocator = struct {
201202 fn alloc(
202203 _: *anyopaque,
203204 len: usize,
204 alignment: mem.Alignment,
205 alignment: Alignment,
205206 return_address: usize,
206207 ) ?[*]u8 {
207208 _ = return_address;
......@@ -212,7 +213,7 @@ const CAllocator = struct {
212213 fn resize(
213214 _: *anyopaque,
214215 buf: []u8,
215 alignment: mem.Alignment,
216 alignment: Alignment,
216217 new_len: usize,
217218 return_address: usize,
218219 ) bool {
......@@ -233,7 +234,7 @@ const CAllocator = struct {
233234 fn remap(
234235 context: *anyopaque,
235236 memory: []u8,
236 alignment: mem.Alignment,
237 alignment: Alignment,
237238 new_len: usize,
238239 return_address: usize,
239240 ) ?[*]u8 {
......@@ -245,7 +246,7 @@ const CAllocator = struct {
245246 fn free(
246247 _: *anyopaque,
247248 buf: []u8,
248 alignment: mem.Alignment,
249 alignment: Alignment,
249250 return_address: usize,
250251 ) void {
251252 _ = alignment;
......@@ -281,7 +282,7 @@ const raw_c_allocator_vtable: Allocator.VTable = .{
281282fn rawCAlloc(
282283 context: *anyopaque,
283284 len: usize,
284 alignment: mem.Alignment,
285 alignment: Alignment,
285286 return_address: usize,
286287) ?[*]u8 {
287288 _ = context;
......@@ -299,7 +300,7 @@ fn rawCAlloc(
299300fn rawCResize(
300301 context: *anyopaque,
301302 memory: []u8,
302 alignment: mem.Alignment,
303 alignment: Alignment,
303304 new_len: usize,
304305 return_address: usize,
305306) bool {
......@@ -314,7 +315,7 @@ fn rawCResize(
314315fn rawCRemap(
315316 context: *anyopaque,
316317 memory: []u8,
317 alignment: mem.Alignment,
318 alignment: Alignment,
318319 new_len: usize,
319320 return_address: usize,
320321) ?[*]u8 {
......@@ -327,7 +328,7 @@ fn rawCRemap(
327328fn rawCFree(
328329 context: *anyopaque,
329330 memory: []u8,
330 alignment: mem.Alignment,
331 alignment: Alignment,
331332 return_address: usize,
332333) void {
333334 _ = context;
......@@ -425,7 +426,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
425426 fn alloc(
426427 ctx: *anyopaque,
427428 len: usize,
428 alignment: mem.Alignment,
429 alignment: Alignment,
429430 ra: usize,
430431 ) ?[*]u8 {
431432 const self: *Self = @ptrCast(@alignCast(ctx));
......@@ -436,7 +437,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
436437 fn resize(
437438 ctx: *anyopaque,
438439 buf: []u8,
439 alignment: mem.Alignment,
440 alignment: Alignment,
440441 new_len: usize,
441442 ra: usize,
442443 ) bool {
......@@ -451,7 +452,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
451452 fn remap(
452453 context: *anyopaque,
453454 memory: []u8,
454 alignment: mem.Alignment,
455 alignment: Alignment,
455456 new_len: usize,
456457 return_address: usize,
457458 ) ?[*]u8 {
......@@ -466,7 +467,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
466467 fn free(
467468 ctx: *anyopaque,
468469 buf: []u8,
469 alignment: mem.Alignment,
470 alignment: Alignment,
470471 ra: usize,
471472 ) void {
472473 const self: *Self = @ptrCast(@alignCast(ctx));
......@@ -512,7 +513,7 @@ test PageAllocator {
512513 }
513514
514515 if (builtin.os.tag == .windows) {
515 const slice = try allocator.alignedAlloc(u8, page_size_min, 128);
516 const slice = try allocator.alignedAlloc(u8, .fromByteUnits(page_size_min), 128);
516517 slice[0] = 0x12;
517518 slice[127] = 0x34;
518519 allocator.free(slice);
......@@ -609,7 +610,7 @@ pub fn testAllocatorAligned(base_allocator: mem.Allocator) !void {
609610 const allocator = validationAllocator.allocator();
610611
611612 // Test a few alignment values, smaller and bigger than the type's one
612 inline for ([_]u29{ 1, 2, 4, 8, 16, 32, 64 }) |alignment| {
613 inline for ([_]Alignment{ .@"1", .@"2", .@"4", .@"8", .@"16", .@"32", .@"64" }) |alignment| {
613614 // initial
614615 var slice = try allocator.alignedAlloc(u8, alignment, 10);
615616 try testing.expect(slice.len == 10);
......@@ -640,7 +641,7 @@ pub fn testAllocatorLargeAlignment(base_allocator: mem.Allocator) !void {
640641 var align_mask: usize = undefined;
641642 align_mask = @shlWithOverflow(~@as(usize, 0), @as(Allocator.Log2Align, @ctz(large_align)))[0];
642643
643 var slice = try allocator.alignedAlloc(u8, large_align, 500);
644 var slice = try allocator.alignedAlloc(u8, .fromByteUnits(large_align), 500);
644645 try testing.expect(@intFromPtr(slice.ptr) & align_mask == @intFromPtr(slice.ptr));
645646
646647 if (allocator.resize(slice, 100)) {
......@@ -669,7 +670,7 @@ pub fn testAllocatorAlignedShrink(base_allocator: mem.Allocator) !void {
669670 const debug_allocator = fib.allocator();
670671
671672 const alloc_size = pageSize() * 2 + 50;
672 var slice = try allocator.alignedAlloc(u8, 16, alloc_size);
673 var slice = try allocator.alignedAlloc(u8, .@"16", alloc_size);
673674 defer allocator.free(slice);
674675
675676 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
......@@ -679,7 +680,7 @@ pub fn testAllocatorAlignedShrink(base_allocator: mem.Allocator) !void {
679680 // fail, because of this high over-alignment we want to have.
680681 while (@intFromPtr(slice.ptr) == mem.alignForward(usize, @intFromPtr(slice.ptr), pageSize() * 32)) {
681682 try stuff_to_free.append(slice);
682 slice = try allocator.alignedAlloc(u8, 16, alloc_size);
683 slice = try allocator.alignedAlloc(u8, .@"16", alloc_size);
683684 }
684685 while (stuff_to_free.pop()) |item| {
685686 allocator.free(item);
lib/std/heap/WasmAllocator.zig+1-1
......@@ -218,7 +218,7 @@ test "very large allocation" {
218218}
219219
220220test "realloc" {
221 var slice = try test_ally.alignedAlloc(u8, @alignOf(u32), 1);
221 var slice = try test_ally.alignedAlloc(u8, .of(u32), 1);
222222 defer test_ally.free(slice);
223223 slice[0] = 0x12;
224224
lib/std/heap/arena_allocator.zig+8-7
......@@ -2,6 +2,7 @@ const std = @import("../std.zig");
22const assert = std.debug.assert;
33const mem = std.mem;
44const Allocator = std.mem.Allocator;
5const Alignment = std.mem.Alignment;
56
67/// This allocator takes an existing allocator, wraps it, and provides an interface where
78/// you can allocate and then free it all together. Calls to free an individual item only
......@@ -41,7 +42,7 @@ pub const ArenaAllocator = struct {
4142 data: usize,
4243 node: std.SinglyLinkedList.Node = .{},
4344 };
44 const BufNode_alignment: mem.Alignment = .fromByteUnits(@alignOf(BufNode));
45 const BufNode_alignment: Alignment = .fromByteUnits(@alignOf(BufNode));
4546
4647 pub fn init(child_allocator: Allocator) ArenaAllocator {
4748 return (State{}).promote(child_allocator);
......@@ -181,7 +182,7 @@ pub const ArenaAllocator = struct {
181182 return buf_node;
182183 }
183184
184 fn alloc(ctx: *anyopaque, n: usize, alignment: mem.Alignment, ra: usize) ?[*]u8 {
185 fn alloc(ctx: *anyopaque, n: usize, alignment: Alignment, ra: usize) ?[*]u8 {
185186 const self: *ArenaAllocator = @ptrCast(@alignCast(ctx));
186187 _ = ra;
187188
......@@ -214,7 +215,7 @@ pub const ArenaAllocator = struct {
214215 }
215216 }
216217
217 fn resize(ctx: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize) bool {
218 fn resize(ctx: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
218219 const self: *ArenaAllocator = @ptrCast(@alignCast(ctx));
219220 _ = alignment;
220221 _ = ret_addr;
......@@ -242,14 +243,14 @@ pub const ArenaAllocator = struct {
242243 fn remap(
243244 context: *anyopaque,
244245 memory: []u8,
245 alignment: mem.Alignment,
246 alignment: Alignment,
246247 new_len: usize,
247248 return_address: usize,
248249 ) ?[*]u8 {
249250 return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null;
250251 }
251252
252 fn free(ctx: *anyopaque, buf: []u8, alignment: mem.Alignment, ret_addr: usize) void {
253 fn free(ctx: *anyopaque, buf: []u8, alignment: Alignment, ret_addr: usize) void {
253254 _ = alignment;
254255 _ = ret_addr;
255256
......@@ -279,9 +280,9 @@ test "reset with preheating" {
279280 const total_size: usize = random.intRangeAtMost(usize, 256, 16384);
280281 while (alloced_bytes < total_size) {
281282 const size = random.intRangeAtMost(usize, 16, 256);
282 const alignment = 32;
283 const alignment: Alignment = .@"32";
283284 const slice = try arena_allocator.allocator().alignedAlloc(u8, alignment, size);
284 try std.testing.expect(std.mem.isAligned(@intFromPtr(slice.ptr), alignment));
285 try std.testing.expect(alignment.check(@intFromPtr(slice.ptr)));
285286 try std.testing.expectEqual(size, slice.len);
286287 alloced_bytes += slice.len;
287288 }
lib/std/heap/debug_allocator.zig+6-6
......@@ -1120,7 +1120,7 @@ test "realloc" {
11201120 defer std.testing.expect(gpa.deinit() == .ok) catch @panic("leak");
11211121 const allocator = gpa.allocator();
11221122
1123 var slice = try allocator.alignedAlloc(u8, @alignOf(u32), 1);
1123 var slice = try allocator.alignedAlloc(u8, .of(u32), 1);
11241124 defer allocator.free(slice);
11251125 slice[0] = 0x12;
11261126
......@@ -1234,7 +1234,7 @@ test "shrink large object to large object with larger alignment" {
12341234 const debug_allocator = fba.allocator();
12351235
12361236 const alloc_size = default_page_size * 2 + 50;
1237 var slice = try allocator.alignedAlloc(u8, 16, alloc_size);
1237 var slice = try allocator.alignedAlloc(u8, .@"16", alloc_size);
12381238 defer allocator.free(slice);
12391239
12401240 const big_alignment: usize = default_page_size * 2;
......@@ -1244,7 +1244,7 @@ test "shrink large object to large object with larger alignment" {
12441244 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
12451245 while (mem.isAligned(@intFromPtr(slice.ptr), big_alignment)) {
12461246 try stuff_to_free.append(slice);
1247 slice = try allocator.alignedAlloc(u8, 16, alloc_size);
1247 slice = try allocator.alignedAlloc(u8, .@"16", alloc_size);
12481248 }
12491249 while (stuff_to_free.pop()) |item| {
12501250 allocator.free(item);
......@@ -1308,7 +1308,7 @@ test "realloc large object to larger alignment" {
13081308 var fba = std.heap.FixedBufferAllocator.init(&debug_buffer);
13091309 const debug_allocator = fba.allocator();
13101310
1311 var slice = try allocator.alignedAlloc(u8, 16, default_page_size * 2 + 50);
1311 var slice = try allocator.alignedAlloc(u8, .@"16", default_page_size * 2 + 50);
13121312 defer allocator.free(slice);
13131313
13141314 const big_alignment: usize = default_page_size * 2;
......@@ -1316,7 +1316,7 @@ test "realloc large object to larger alignment" {
13161316 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
13171317 while (mem.isAligned(@intFromPtr(slice.ptr), big_alignment)) {
13181318 try stuff_to_free.append(slice);
1319 slice = try allocator.alignedAlloc(u8, 16, default_page_size * 2 + 50);
1319 slice = try allocator.alignedAlloc(u8, .@"16", default_page_size * 2 + 50);
13201320 }
13211321 while (stuff_to_free.pop()) |item| {
13221322 allocator.free(item);
......@@ -1402,7 +1402,7 @@ test "large allocations count requested size not backing size" {
14021402 var gpa: DebugAllocator(.{ .enable_memory_limit = true }) = .{};
14031403 const allocator = gpa.allocator();
14041404
1405 var buf = try allocator.alignedAlloc(u8, 1, default_page_size + 1);
1405 var buf = try allocator.alignedAlloc(u8, .@"1", default_page_size + 1);
14061406 try std.testing.expectEqual(default_page_size + 1, gpa.total_requested_bytes);
14071407 buf = try allocator.realloc(buf, 1);
14081408 try std.testing.expectEqual(1, gpa.total_requested_bytes);
lib/std/heap/memory_pool.zig+12-11
......@@ -1,4 +1,5 @@
11const std = @import("../std.zig");
2const Alignment = std.mem.Alignment;
23
34const debug_mode = @import("builtin").mode == .Debug;
45
......@@ -8,14 +9,14 @@ pub const MemoryPoolError = error{OutOfMemory};
89/// Use this when you need to allocate a lot of objects of the same type,
910/// because It outperforms general purpose allocators.
1011pub fn MemoryPool(comptime Item: type) type {
11 return MemoryPoolAligned(Item, @alignOf(Item));
12 return MemoryPoolAligned(Item, .of(Item));
1213}
1314
1415/// A memory pool that can allocate objects of a single type very quickly.
1516/// Use this when you need to allocate a lot of objects of the same type,
1617/// because It outperforms general purpose allocators.
17pub fn MemoryPoolAligned(comptime Item: type, comptime alignment: u29) type {
18 if (@alignOf(Item) == alignment) {
18pub fn MemoryPoolAligned(comptime Item: type, comptime alignment: Alignment) type {
19 if (@alignOf(Item) == comptime alignment.toByteUnits()) {
1920 return MemoryPoolExtra(Item, .{});
2021 } else {
2122 return MemoryPoolExtra(Item, .{ .alignment = alignment });
......@@ -24,7 +25,7 @@ pub fn MemoryPoolAligned(comptime Item: type, comptime alignment: u29) type {
2425
2526pub const Options = struct {
2627 /// The alignment of the memory pool items. Use `null` for natural alignment.
27 alignment: ?u29 = null,
28 alignment: ?Alignment = null,
2829
2930 /// If `true`, the memory pool can allocate additional items after a initial setup.
3031 /// If `false`, the memory pool will not allocate further after a call to `initPreheated`.
......@@ -43,17 +44,17 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type
4344 pub const item_size = @max(@sizeOf(Node), @sizeOf(Item));
4445
4546 // This needs to be kept in sync with Node.
46 const node_alignment = @alignOf(*anyopaque);
47 const node_alignment: Alignment = .of(*anyopaque);
4748
4849 /// Alignment of the memory pool items. This is not necessarily the same
4950 /// as `@alignOf(Item)` as the pool also uses the items for internal means.
50 pub const item_alignment = @max(node_alignment, pool_options.alignment orelse @alignOf(Item));
51 pub const item_alignment: Alignment = node_alignment.max(pool_options.alignment orelse .of(Item));
5152
5253 const Node = struct {
53 next: ?*align(item_alignment) @This(),
54 next: ?*align(item_alignment.toByteUnits()) @This(),
5455 };
55 const NodePtr = *align(item_alignment) Node;
56 const ItemPtr = *align(item_alignment) Item;
56 const NodePtr = *align(item_alignment.toByteUnits()) Node;
57 const ItemPtr = *align(item_alignment.toByteUnits()) Item;
5758
5859 arena: std.heap.ArenaAllocator,
5960 free_list: ?NodePtr = null,
......@@ -143,7 +144,7 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type
143144 pool.free_list = node;
144145 }
145146
146 fn allocNew(pool: *Pool) MemoryPoolError!*align(item_alignment) [item_size]u8 {
147 fn allocNew(pool: *Pool) MemoryPoolError!*align(item_alignment.toByteUnits()) [item_size]u8 {
147148 const mem = try pool.arena.allocator().alignedAlloc(u8, item_alignment, item_size);
148149 return mem[0..item_size]; // coerce slice to array pointer
149150 }
......@@ -213,7 +214,7 @@ test "greater than pointer manual alignment" {
213214 data: u64,
214215 };
215216
216 var pool = MemoryPoolAligned(Foo, 16).init(std.testing.allocator);
217 var pool = MemoryPoolAligned(Foo, .@"16").init(std.testing.allocator);
217218 defer pool.deinit();
218219
219220 const foo: *align(16) Foo = try pool.create();
lib/std/io.zig+2-2
......@@ -5,7 +5,6 @@ const c = std.c;
55const is_windows = builtin.os.tag == .windows;
66const windows = std.os.windows;
77const posix = std.posix;
8
98const math = std.math;
109const assert = std.debug.assert;
1110const fs = std.fs;
......@@ -13,6 +12,7 @@ const mem = std.mem;
1312const meta = std.meta;
1413const File = std.fs.File;
1514const Allocator = std.mem.Allocator;
15const Alignment = std.mem.Alignment;
1616
1717fn getStdOutHandle() posix.fd_t {
1818 if (is_windows) {
......@@ -104,7 +104,7 @@ pub fn GenericReader(
104104
105105 pub inline fn readAllArrayListAligned(
106106 self: Self,
107 comptime alignment: ?u29,
107 comptime alignment: ?Alignment,
108108 array_list: *std.ArrayListAligned(u8, alignment),
109109 max_append_size: usize,
110110 ) (error{StreamTooLong} || Allocator.Error || Error)!void {
lib/std/io/Reader.zig+2-1
......@@ -54,7 +54,7 @@ pub fn readAllArrayList(
5454
5555pub fn readAllArrayListAligned(
5656 self: Self,
57 comptime alignment: ?u29,
57 comptime alignment: ?Alignment,
5858 array_list: *std.ArrayListAligned(u8, alignment),
5959 max_append_size: usize,
6060) anyerror!void {
......@@ -379,6 +379,7 @@ const assert = std.debug.assert;
379379const mem = std.mem;
380380const testing = std.testing;
381381const native_endian = @import("builtin").target.cpu.arch.endian();
382const Alignment = std.mem.Alignment;
382383
383384test {
384385 _ = @import("Reader/test.zig");
lib/std/math.zig+6-8
......@@ -4,6 +4,7 @@ const float = @import("math/float.zig");
44const assert = std.debug.assert;
55const mem = std.mem;
66const testing = std.testing;
7const Alignment = std.mem.Alignment;
78
89/// Euler's number (e)
910pub const e = 2.71828182845904523536028747135266249775724709369995;
......@@ -1084,19 +1085,16 @@ test cast {
10841085
10851086pub const AlignCastError = error{UnalignedMemory};
10861087
1087fn AlignCastResult(comptime alignment: u29, comptime Ptr: type) type {
1088fn AlignCastResult(comptime alignment: Alignment, comptime Ptr: type) type {
10881089 var ptr_info = @typeInfo(Ptr);
1089 ptr_info.pointer.alignment = alignment;
1090 ptr_info.pointer.alignment = alignment.toByteUnits();
10901091 return @Type(ptr_info);
10911092}
10921093
10931094/// Align cast a pointer but return an error if it's the wrong alignment
1094pub fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!AlignCastResult(alignment, @TypeOf(ptr)) {
1095 const addr = @intFromPtr(ptr);
1096 if (addr % alignment != 0) {
1097 return error.UnalignedMemory;
1098 }
1099 return @alignCast(ptr);
1095pub fn alignCast(comptime alignment: Alignment, ptr: anytype) AlignCastError!AlignCastResult(alignment, @TypeOf(ptr)) {
1096 if (alignment.check(@intFromPtr(ptr))) return @alignCast(ptr);
1097 return error.UnalignedMemory;
11001098}
11011099
11021100/// Asserts `int > 0`.
lib/std/mem.zig+4-15
......@@ -38,6 +38,10 @@ pub const Alignment = enum(math.Log2Int(usize)) {
3838 return @enumFromInt(@ctz(n));
3939 }
4040
41 pub inline fn of(comptime T: type) Alignment {
42 return comptime fromByteUnits(@alignOf(T));
43 }
44
4145 pub fn order(lhs: Alignment, rhs: Alignment) std.math.Order {
4246 return std.math.order(@intFromEnum(lhs), @intFromEnum(rhs));
4347 }
......@@ -166,21 +170,6 @@ pub fn validationWrap(allocator: anytype) ValidationAllocator(@TypeOf(allocator)
166170 return ValidationAllocator(@TypeOf(allocator)).init(allocator);
167171}
168172
169/// An allocator helper function. Adjusts an allocation length satisfy `len_align`.
170/// `full_len` should be the full capacity of the allocation which may be greater
171/// than the `len` that was requested. This function should only be used by allocators
172/// that are unaffected by `len_align`.
173pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {
174 assert(alloc_len > 0);
175 assert(alloc_len >= len_align);
176 assert(full_len >= alloc_len);
177 if (len_align == 0)
178 return alloc_len;
179 const adjusted = alignBackwardAnyAlign(usize, full_len, len_align);
180 assert(adjusted >= alloc_len);
181 return adjusted;
182}
183
184173test "Allocator basics" {
185174 try testing.expectError(error.OutOfMemory, testing.failing_allocator.alloc(u8, 1));
186175 try testing.expectError(error.OutOfMemory, testing.failing_allocator.allocSentinel(u8, 1, 0));
lib/std/mem/Allocator.zig+30-19
......@@ -152,9 +152,9 @@ pub inline fn rawFree(a: Allocator, memory: []u8, alignment: Alignment, ret_addr
152152pub fn create(a: Allocator, comptime T: type) Error!*T {
153153 if (@sizeOf(T) == 0) {
154154 const ptr = comptime std.mem.alignBackward(usize, math.maxInt(usize), @alignOf(T));
155 return @as(*T, @ptrFromInt(ptr));
155 return @ptrFromInt(ptr);
156156 }
157 const ptr: *T = @ptrCast(try a.allocBytesWithAlignment(@alignOf(T), @sizeOf(T), @returnAddress()));
157 const ptr: *T = @ptrCast(try a.allocBytesWithAlignment(.of(T), @sizeOf(T), @returnAddress()));
158158 return ptr;
159159}
160160
......@@ -186,7 +186,7 @@ pub fn allocWithOptions(
186186 comptime Elem: type,
187187 n: usize,
188188 /// null means naturally aligned
189 comptime optional_alignment: ?u29,
189 comptime optional_alignment: ?Alignment,
190190 comptime optional_sentinel: ?Elem,
191191) Error!AllocWithOptionsPayload(Elem, optional_alignment, optional_sentinel) {
192192 return self.allocWithOptionsRetAddr(Elem, n, optional_alignment, optional_sentinel, @returnAddress());
......@@ -197,7 +197,7 @@ pub fn allocWithOptionsRetAddr(
197197 comptime Elem: type,
198198 n: usize,
199199 /// null means naturally aligned
200 comptime optional_alignment: ?u29,
200 comptime optional_alignment: ?Alignment,
201201 comptime optional_sentinel: ?Elem,
202202 return_address: usize,
203203) Error!AllocWithOptionsPayload(Elem, optional_alignment, optional_sentinel) {
......@@ -210,11 +210,11 @@ pub fn allocWithOptionsRetAddr(
210210 }
211211}
212212
213fn AllocWithOptionsPayload(comptime Elem: type, comptime alignment: ?u29, comptime sentinel: ?Elem) type {
213fn AllocWithOptionsPayload(comptime Elem: type, comptime alignment: ?Alignment, comptime sentinel: ?Elem) type {
214214 if (sentinel) |s| {
215 return [:s]align(alignment orelse @alignOf(Elem)) Elem;
215 return [:s]align(if (alignment) |a| a.toByteUnits() else @alignOf(Elem)) Elem;
216216 } else {
217 return []align(alignment orelse @alignOf(Elem)) Elem;
217 return []align(if (alignment) |a| a.toByteUnits() else @alignOf(Elem)) Elem;
218218 }
219219}
220220
......@@ -239,9 +239,9 @@ pub fn alignedAlloc(
239239 self: Allocator,
240240 comptime T: type,
241241 /// null means naturally aligned
242 comptime alignment: ?u29,
242 comptime alignment: ?Alignment,
243243 n: usize,
244) Error![]align(alignment orelse @alignOf(T)) T {
244) Error![]align(if (alignment) |a| a.toByteUnits() else @alignOf(T)) T {
245245 return self.allocAdvancedWithRetAddr(T, alignment, n, @returnAddress());
246246}
247247
......@@ -249,27 +249,38 @@ pub inline fn allocAdvancedWithRetAddr(
249249 self: Allocator,
250250 comptime T: type,
251251 /// null means naturally aligned
252 comptime alignment: ?u29,
252 comptime alignment: ?Alignment,
253253 n: usize,
254254 return_address: usize,
255) Error![]align(alignment orelse @alignOf(T)) T {
256 const a = alignment orelse @alignOf(T);
257 const ptr: [*]align(a) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
255) Error![]align(if (alignment) |a| a.toByteUnits() else @alignOf(T)) T {
256 const a = comptime (alignment orelse Alignment.fromByteUnits(@alignOf(T)));
257 const ptr: [*]align(a.toByteUnits()) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
258258 return ptr[0..n];
259259}
260260
261fn allocWithSizeAndAlignment(self: Allocator, comptime size: usize, comptime alignment: u29, n: usize, return_address: usize) Error![*]align(alignment) u8 {
261fn allocWithSizeAndAlignment(
262 self: Allocator,
263 comptime size: usize,
264 comptime alignment: Alignment,
265 n: usize,
266 return_address: usize,
267) Error![*]align(alignment.toByteUnits()) u8 {
262268 const byte_count = math.mul(usize, size, n) catch return Error.OutOfMemory;
263269 return self.allocBytesWithAlignment(alignment, byte_count, return_address);
264270}
265271
266fn allocBytesWithAlignment(self: Allocator, comptime alignment: u29, byte_count: usize, return_address: usize) Error![*]align(alignment) u8 {
272fn allocBytesWithAlignment(
273 self: Allocator,
274 comptime alignment: Alignment,
275 byte_count: usize,
276 return_address: usize,
277) Error![*]align(alignment.toByteUnits()) u8 {
267278 if (byte_count == 0) {
268 const ptr = comptime std.mem.alignBackward(usize, math.maxInt(usize), alignment);
269 return @as([*]align(alignment) u8, @ptrFromInt(ptr));
279 const ptr = comptime alignment.backward(math.maxInt(usize));
280 return @as([*]align(alignment.toByteUnits()) u8, @ptrFromInt(ptr));
270281 }
271282
272 const byte_ptr = self.rawAlloc(byte_count, .fromByteUnits(alignment), return_address) orelse return Error.OutOfMemory;
283 const byte_ptr = self.rawAlloc(byte_count, alignment, return_address) orelse return Error.OutOfMemory;
273284 @memset(byte_ptr[0..byte_count], undefined);
274285 return @alignCast(byte_ptr);
275286}
......@@ -378,7 +389,7 @@ pub fn reallocAdvanced(
378389 const Slice = @typeInfo(@TypeOf(old_mem)).pointer;
379390 const T = Slice.child;
380391 if (old_mem.len == 0) {
381 return self.allocAdvancedWithRetAddr(T, Slice.alignment, new_n, return_address);
392 return self.allocAdvancedWithRetAddr(T, .fromByteUnits(Slice.alignment), new_n, return_address);
382393 }
383394 if (new_n == 0) {
384395 self.free(old_mem);
lib/std/meta/trailer_flags.zig+1-1
......@@ -144,7 +144,7 @@ test TrailerFlags {
144144 .b = true,
145145 .c = true,
146146 });
147 const slice = try testing.allocator.alignedAlloc(u8, 8, flags.sizeInBytes());
147 const slice = try testing.allocator.alignedAlloc(u8, .@"8", flags.sizeInBytes());
148148 defer testing.allocator.free(slice);
149149
150150 flags.set(slice.ptr, .b, false);
lib/std/multi_array_list.zig+2-10
......@@ -350,11 +350,7 @@ pub fn MultiArrayList(comptime T: type) type {
350350 assert(new_len <= self.capacity);
351351 assert(new_len <= self.len);
352352
353 const other_bytes = gpa.alignedAlloc(
354 u8,
355 @alignOf(Elem),
356 capacityInBytes(new_len),
357 ) catch {
353 const other_bytes = gpa.alignedAlloc(u8, .of(Elem), capacityInBytes(new_len)) catch {
358354 const self_slice = self.slice();
359355 inline for (fields, 0..) |field_info, i| {
360356 if (@sizeOf(field_info.type) != 0) {
......@@ -440,11 +436,7 @@ pub fn MultiArrayList(comptime T: type) type {
440436 /// `new_capacity` must be greater or equal to `len`.
441437 pub fn setCapacity(self: *Self, gpa: Allocator, new_capacity: usize) !void {
442438 assert(new_capacity >= self.len);
443 const new_bytes = try gpa.alignedAlloc(
444 u8,
445 @alignOf(Elem),
446 capacityInBytes(new_capacity),
447 );
439 const new_bytes = try gpa.alignedAlloc(u8, .of(Elem), capacityInBytes(new_capacity));
448440 if (self.len == 0) {
449441 gpa.free(self.allocatedBytes());
450442 self.bytes = new_bytes.ptr;
lib/std/process.zig+1-1
......@@ -1256,7 +1256,7 @@ pub fn argsAlloc(allocator: Allocator) ![][:0]u8 {
12561256 const slice_sizes = slice_list.items;
12571257 const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
12581258 const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
1259 const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes);
1259 const buf = try allocator.alignedAlloc(u8, .of([]u8), total_bytes);
12601260 errdefer allocator.free(buf);
12611261
12621262 const result_slice_list = mem.bytesAsSlice([:0]u8, buf[0..slice_list_bytes]);
lib/std/zig.zig+1-1
......@@ -543,7 +543,7 @@ pub fn readSourceFileToEndAlloc(gpa: Allocator, input: std.fs.File, size_hint: ?
543543 gpa,
544544 max_src_size,
545545 size_hint,
546 @alignOf(u8),
546 .of(u8),
547547 0,
548548 ) catch |err| switch (err) {
549549 error.ConnectionResetByPeer => unreachable,
lib/std/zon/parse.zig+1-1
......@@ -679,7 +679,7 @@ const Parser = struct {
679679 const slice = try self.gpa.allocWithOptions(
680680 pointer.child,
681681 nodes.len,
682 pointer.alignment,
682 .fromByteUnits(pointer.alignment),
683683 pointer.sentinel(),
684684 );
685685 errdefer self.gpa.free(slice);
src/InternPool.zig+6-6
......@@ -255,7 +255,7 @@ pub fn trackZir(
255255 const new_map_capacity = map_header.capacity * 2;
256256 const new_map_buf = try arena.allocator().alignedAlloc(
257257 u8,
258 Map.alignment,
258 .fromByteUnits(Map.alignment),
259259 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
260260 );
261261 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
......@@ -350,7 +350,7 @@ pub fn rehashTrackedInsts(
350350 defer arena_state.* = arena.state;
351351 const new_map_buf = try arena.allocator().alignedAlloc(
352352 u8,
353 Map.alignment,
353 .fromByteUnits(Map.alignment),
354354 Map.entries_offset + want_capacity * @sizeOf(Map.Entry),
355355 );
356356 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
......@@ -1296,7 +1296,7 @@ const Local = struct {
12961296 defer mutable.arena.* = arena.state;
12971297 const buf = try arena.allocator().alignedAlloc(
12981298 u8,
1299 alignment,
1299 .fromByteUnits(alignment),
13001300 bytes_offset + View.capacityInBytes(capacity),
13011301 );
13021302 var new_list: ListSelf = .{ .bytes = @ptrCast(buf[bytes_offset..].ptr) };
......@@ -7547,7 +7547,7 @@ fn getOrPutKeyEnsuringAdditionalCapacity(
75477547 }
75487548 const new_map_buf = try arena.allocator().alignedAlloc(
75497549 u8,
7550 Map.alignment,
7550 .fromByteUnits(Map.alignment),
75517551 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
75527552 );
75537553 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
......@@ -11666,7 +11666,7 @@ pub fn getOrPutTrailingString(
1166611666 const new_map_capacity = map_header.capacity * 2;
1166711667 const new_map_buf = try arena.allocator().alignedAlloc(
1166811668 u8,
11669 Map.alignment,
11669 .fromByteUnits(Map.alignment),
1167011670 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
1167111671 );
1167211672 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
......@@ -12586,7 +12586,7 @@ const GlobalErrorSet = struct {
1258612586 const new_map_capacity = map_header.capacity * 2;
1258712587 const new_map_buf = try arena.allocator().alignedAlloc(
1258812588 u8,
12589 Map.alignment,
12589 .fromByteUnits(Map.alignment),
1259012590 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
1259112591 );
1259212592 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
src/Package/Fetch.zig+1-1
......@@ -640,7 +640,7 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {
640640 try fs.path.join(arena, &.{ pkg_root.sub_path, Manifest.basename }),
641641 Manifest.max_bytes,
642642 null,
643 1,
643 .@"1",
644644 0,
645645 ) catch |err| switch (err) {
646646 error.FileNotFound => return,
src/main.zig+1-1
......@@ -7510,7 +7510,7 @@ fn loadManifest(
75107510 Package.Manifest.basename,
75117511 Package.Manifest.max_bytes,
75127512 null,
7513 1,
7513 .@"1",
75147514 0,
75157515 ) catch |err| switch (err) {
75167516 error.FileNotFound => {
test/src/Cases.zig+1-1
......@@ -358,7 +358,7 @@ fn addFromDirInner(
358358 current_file.* = filename;
359359
360360 const max_file_size = 10 * 1024 * 1024;
361 const src = try iterable_dir.readFileAllocOptions(ctx.arena, filename, max_file_size, null, 1, 0);
361 const src = try iterable_dir.readFileAllocOptions(ctx.arena, filename, max_file_size, null, .@"1", 0);
362362
363363 // Parse the manifest
364364 var manifest = try TestManifest.parse(ctx.arena, src);