1
Fork 0

replace padding functions with ones from std.mem

main
Motiejus Jakštys 2023-02-08 16:33:15 +02:00
parent 0ecd6172fc
commit 000080a781
5 changed files with 8 additions and 65 deletions

View File

@ -12,7 +12,6 @@ const BoundedArray = std.BoundedArray;
const ErrCtx = @import("ErrCtx.zig"); const ErrCtx = @import("ErrCtx.zig");
const Corpus = @import("Corpus.zig"); const Corpus = @import("Corpus.zig");
const pad = @import("padding.zig");
const compress = @import("compress.zig"); const compress = @import("compress.zig");
const Group = @import("Group.zig"); const Group = @import("Group.zig");
const CGroup = Group.CGroup; const CGroup = Group.CGroup;
@ -195,7 +194,7 @@ pub fn iov(self: *align(8) const DB) BoundedArray(os.iovec_const, DB_fields.len
.iov_base = bytes.ptr, .iov_base = bytes.ptr,
.iov_len = bytes.len, .iov_len = bytes.len,
}); });
const padding = pad.until(usize, section_length_bits, bytes.len); const padding = mem.alignForward(bytes.len, section_length) - bytes.len;
if (padding != 0) if (padding != 0)
result.appendAssumeCapacity(.{ result.appendAssumeCapacity(.{
.iov_base = zeroes, .iov_base = zeroes,
@ -592,7 +591,7 @@ fn usersSection(
gids.idx2offset[i], gids.idx2offset[i],
shells.shell2idx, shells.shell2idx,
); );
try pad.arrayList(&blob, PackedUser.alignment_bits); try blob.appendNTimes(0, mem.alignForward(blob.items.len, 8) - blob.items.len);
} }
return UsersSection{ return UsersSection{
.len = @intCast(u32, corpus.users.len), .len = @intCast(u32, corpus.users.len),
@ -680,7 +679,7 @@ fn groupsSection(
.members_offset = members_offset[i], .members_offset = members_offset[i],
}; };
try PackedGroup.packTo(&blob, group_stored); try PackedGroup.packTo(&blob, group_stored);
try pad.arrayList(&blob, PackedGroup.alignment_bits); try blob.appendNTimes(0, mem.alignForward(blob.items.len, 8) - blob.items.len);
} }
return GroupsSection{ return GroupsSection{
@ -727,7 +726,7 @@ pub fn nblocks_n(comptime T: type, nbytes: usize) T {
u64 => u70, u64 => u70,
else => @compileError("got " ++ @typeName(T) ++ ", only u8, u32 and u64 are supported"), else => @compileError("got " ++ @typeName(T) ++ ", only u8, u32 and u64 are supported"),
}; };
const upper = pad.roundUp(B, section_length_bits, @intCast(B, nbytes)); const upper = @intCast(B, mem.alignForward(nbytes, section_length));
assert(upper & (section_length - 1) == 0); assert(upper & (section_length - 1) == 0);
return @truncate(T, upper >> section_length_bits); return @truncate(T, upper >> section_length_bits);
} }

View File

@ -6,7 +6,6 @@ const Allocator = mem.Allocator;
const ArrayListAligned = std.ArrayListAligned; const ArrayListAligned = std.ArrayListAligned;
const BufSet = std.BufSet; const BufSet = std.BufSet;
const pad = @import("padding.zig");
const validate = @import("validate.zig"); const validate = @import("validate.zig");
const compress = @import("compress.zig"); const compress = @import("compress.zig");
const InvalidRecord = validate.InvalidRecord; const InvalidRecord = validate.InvalidRecord;
@ -55,7 +54,7 @@ pub fn fromBytes(bytes: []align(8) const u8) Entry {
.groupdata = bytes[start_blob..end_strings], .groupdata = bytes[start_blob..end_strings],
.members_offset = members_offset.value, .members_offset = members_offset.value,
}, },
.end = pad.roundUp(usize, alignment_bits, end_blob), .end = mem.alignForward(end_blob, 8),
}; };
} }
@ -145,7 +144,7 @@ test "PackedGroup construct" {
for (groups) |group| { for (groups) |group| {
try PackedGroup.packTo(&buf, group); try PackedGroup.packTo(&buf, group);
try pad.arrayList(&buf, PackedGroup.alignment_bits); try buf.appendNTimes(0, mem.alignForward(buf.items.len, 8) - buf.items.len);
} }
var i: u29 = 0; var i: u29 = 0;

View File

@ -7,7 +7,6 @@ const ArrayListAligned = std.ArrayListAligned;
const StringHashMap = std.StringHashMap; const StringHashMap = std.StringHashMap;
const fieldInfo = std.meta.fieldInfo; const fieldInfo = std.meta.fieldInfo;
const pad = @import("padding.zig");
const validate = @import("validate.zig"); const validate = @import("validate.zig");
const compress = @import("compress.zig"); const compress = @import("compress.zig");
const ShellReader = @import("shell.zig").ShellReader; const ShellReader = @import("shell.zig").ShellReader;
@ -99,7 +98,7 @@ pub fn fromBytes(blob: []align(8) const u8) Entry {
.var_payload = blob[start_var_payload..end_payload], .var_payload = blob[start_var_payload..end_payload],
.additional_gids_offset = gids_offset.value, .additional_gids_offset = gids_offset.value,
}, },
.end = pad.roundUp(usize, alignment_bits, end_payload), .end = mem.alignForward(end_payload, 8),
}; };
} }
@ -292,7 +291,7 @@ test "PackedUser construct section" {
defer shellIndex.deinit(); defer shellIndex.deinit();
for (users) |user| { for (users) |user| {
try PackedUser.packTo(&buf, user, additional_gids, shellIndex); try PackedUser.packTo(&buf, user, additional_gids, shellIndex);
try pad.arrayList(&buf, PackedUser.alignment_bits); try buf.appendNTimes(0, mem.alignForward(buf.items.len, 8) - buf.items.len);
} }
var i: u29 = 0; var i: u29 = 0;

View File

@ -1,53 +0,0 @@
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArrayListAligned = std.ArrayListAligned;
// rounds up an int to the nearest factor of nbits.
pub fn roundUp(comptime T: type, comptime nbits: u8, n: T) T {
comptime assert(nbits < @bitSizeOf(T));
const factor = comptime (1 << nbits) - 1;
return ((n + factor) & ~@as(T, factor));
}
// rounds up an integer to the nearest factor of nbits and returns the
// difference (padding)
pub fn until(comptime T: type, comptime nbits: u8, n: T) T {
return roundUp(T, nbits, n) - n;
}
// arrayList adds padding to an ArrayListAligned(u8, 8) for a given number of nbits
pub fn arrayList(arr: *ArrayListAligned(u8, 8), comptime nbits: u8) Allocator.Error!void {
const padding = until(u64, nbits, arr.items.len);
try arr.*.appendNTimes(0, padding);
}
const testing = std.testing;
test "padding" {
try testing.expectEqual(until(u12, 2, 0), 0);
try testing.expectEqual(until(u12, 2, 1), 3);
try testing.expectEqual(until(u12, 2, 2), 2);
try testing.expectEqual(until(u12, 2, 3), 1);
try testing.expectEqual(until(u12, 2, 4), 0);
try testing.expectEqual(until(u12, 2, 40), 0);
try testing.expectEqual(until(u12, 2, 41), 3);
try testing.expectEqual(until(u12, 2, 42), 2);
try testing.expectEqual(until(u12, 2, 43), 1);
try testing.expectEqual(until(u12, 2, 44), 0);
try testing.expectEqual(until(u12, 2, 4091), 1);
try testing.expectEqual(until(u12, 2, 4092), 0);
}
test "padding arrayList" {
var buf = try ArrayListAligned(u8, 8).initCapacity(testing.allocator, 16);
defer buf.deinit();
buf.appendAssumeCapacity(1);
try arrayList(&buf, 3);
try testing.expectEqual(buf.items.len, 8);
buf.appendAssumeCapacity(2);
try arrayList(&buf, 10);
try testing.expectEqual(buf.items.len, 1024);
}

View File

@ -10,7 +10,6 @@ test "turbonss test suite" {
_ = @import("libnss.zig"); _ = @import("libnss.zig");
_ = @import("PackedGroup.zig"); _ = @import("PackedGroup.zig");
_ = @import("PackedUser.zig"); _ = @import("PackedUser.zig");
_ = @import("padding.zig");
_ = @import("shell.zig"); _ = @import("shell.zig");
_ = @import("User.zig"); _ = @import("User.zig");
_ = @import("validate.zig"); _ = @import("validate.zig");