Skip to content

Commit f32a5d3

Browse files
committed
std: eradicate u29 and embrace std.mem.Alignment
1 parent ec28888 commit f32a5d3

32 files changed

+153
-156
lines changed

lib/compiler/aro/aro/Parser.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ value_map: Tree.ValueMap,
101101

102102
// buffers used during compilation
103103
syms: SymbolStack = .{},
104-
strings: std.ArrayListAligned(u8, 4),
104+
strings: std.ArrayListAligned(u8, .@"4"),
105105
labels: std.ArrayList(Label),
106106
list_buf: NodeList,
107107
decl_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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
25-
pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
25+
pub 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.
614-
pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {
614+
pub 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" {
21262126
test "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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std.zig");
22
const assert = std.debug.assert;
33
const mem = std.mem;
44
const testing = std.testing;
5+
const 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
/// ```
1819
pub 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
/// ```
3536
pub 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,15 +121,15 @@ 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];
127128
}
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

401402
test "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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const pwhash = crypto.pwhash;
1414

1515
const Thread = std.Thread;
1616
const Blake2b512 = blake2.Blake2b512;
17-
const Blocks = std.ArrayListAligned([block_length]u64, 16);
17+
const Blocks = std.ArrayListAligned([block_length]u64, .@"16");
1818
const H0 = [Blake2b512.digest_length + 8]u8;
1919

2020
const EncodingError = crypto.errors.EncodingError;

lib/std/crypto/scrypt.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

0 commit comments

Comments
 (0)