remove 3 bytes from PackedUser

additional_gids will be stored separately.
This commit is contained in:
2022-02-22 15:16:45 +02:00
committed by Motiejus Jakštys
parent 78dc63669d
commit e06cac38b2
2 changed files with 31 additions and 39 deletions

View File

@@ -9,13 +9,13 @@ const cast = std.math.cast;
pub const PackedUser = packed struct {
uid: u32,
gid: u32,
additional_gids_offset: u29,
shell_here: bool,
shell_len_or_idx: u6,
home_len: u6,
name_is_a_suffix: bool,
name_len: u5,
gecos_len: u8,
padding: u5,
// blobSize returns the length of the blob storing string values.
pub fn blobLength(self: *const PackedUser) usize {
@@ -93,13 +93,13 @@ pub const UserWriter = struct {
var puser = PackedUser{
.uid = user.uid,
.gid = user.gid,
.additional_gids_offset = std.math.maxInt(u29), // needs second pass
.shell_here = self.shellIndexFn(user.shell) == null,
.shell_len_or_idx = self.shellIndexFn(user.shell) orelse shell_len,
.home_len = home_len,
.name_is_a_suffix = std.mem.endsWith(u8, user.home, user.name),
.name_len = name_len,
.gecos_len = gecos_len,
.padding = 0,
};
try self.appendTo.appendSlice(std.mem.asBytes(&puser));
@@ -144,12 +144,10 @@ pub const UserReader = struct {
if (it.index == it.ur.blob.len) return null;
assert(it.index < it.ur.blob.len);
// https://github.com/ziglang/zig/issues/1095
const packedUserSizeHere = @sizeOf(PackedUser);
const endUser = it.index + packedUserSizeHere;
const endUser = it.index + @sizeOf(PackedUser);
var packedUser = std.mem.bytesAsValue(
PackedUser,
it.ur.blob[it.index..endUser][0..packedUserSizeHere],
it.ur.blob[it.index..endUser][0..@sizeOf(PackedUser)],
);
const startBlob = endUser;
const endBlob = startBlob + packedUser.blobLength();
@@ -217,19 +215,8 @@ pub const UserReader = struct {
const testing = std.testing;
test "PackedUser alignment" {
// byte-aligned
try testing.expectEqual(0, @rem(@bitSizeOf(PackedUser), 8));
const bytes = @divExact(@bitSizeOf(PackedUser), 8);
// External padding (PackedUserAlignmentBits) must be higher or equal to
// the "internal" PackedUser alignment. By aligning PackedUser we are also
// working around https://github.com/ziglang/zig/issues/10958 ; PackedUser
// cannot be converted from/to [@bitSizeOf(PackedUser)/8]u8;
// asBytes/bytesAsValue use @sizeOf, which is larger. Now we are putting no
// more than 1, but it probably could be higher.
try testing.expect(@sizeOf(PackedUser) - bytes <= 1);
test "PackedUser internal and external alignment" {
try testing.expectEqual(@bitSizeOf(PackedUser), @sizeOf(PackedUser) * 8);
}
fn testShellIndex(shell: []const u8) ?u6 {