wip between-user padding

This commit is contained in:
Motiejus Jakštys 2022-02-19 18:18:14 +02:00 committed by Motiejus Jakštys
parent 4e45c6e5a9
commit 2fc925923f
4 changed files with 37 additions and 28 deletions

29
src/padding.zig Normal file
View File

@ -0,0 +1,29 @@
const std = @import("std");
// rounds up a u12 to the nearest factor of 4 and returns the difference
// (padding)
pub fn roundUp4Padding(comptime T: type, n: T) T {
return roundUp4(T, n) - n;
}
// rounds up a u12 to the nearest factor of 4.
pub fn roundUp4(comptime T: type, n: T) T {
return ((n + 3) & ~@as(T, 3));
}
const testing = std.testing;
test "padding" {
try testing.expectEqual(roundUp4Padding(u12, 0), 0);
try testing.expectEqual(roundUp4Padding(u12, 1), 3);
try testing.expectEqual(roundUp4Padding(u12, 2), 2);
try testing.expectEqual(roundUp4Padding(u12, 3), 1);
try testing.expectEqual(roundUp4Padding(u12, 4), 0);
try testing.expectEqual(roundUp4Padding(u12, 40), 0);
try testing.expectEqual(roundUp4Padding(u12, 41), 3);
try testing.expectEqual(roundUp4Padding(u12, 42), 2);
try testing.expectEqual(roundUp4Padding(u12, 43), 1);
try testing.expectEqual(roundUp4Padding(u12, 44), 0);
try testing.expectEqual(roundUp4Padding(u12, 4091), 1);
try testing.expectEqual(roundUp4Padding(u12, 4092), 0);
}

View File

@ -1,4 +1,5 @@
const std = @import("std");
const pad = @import("padding.zig");
const Allocator = std.mem.Allocator;
const PriorityDequeue = std.PriorityDequeue;
const StringArrayHashMap = std.StringArrayHashMap;
@ -70,7 +71,7 @@ pub const ShellWriter = struct {
});
fullOffset += len;
const padding = roundUp4Padding(fullOffset);
const padding = pad.roundUp4Padding(u12, fullOffset);
fullOffset += padding;
//const stderr = std.io.getStdErr().writer();
//stderr.print("\n", .{}) catch unreachable;
@ -167,17 +168,6 @@ pub const ShellWriter = struct {
}
};
// rounds up a u12 to the nearest factor of 4 and returns the difference
// (padding)
inline fn roundUp4Padding(n: u12) u12 {
return roundUp4(n) - n;
}
// rounds up a u12 to the nearest factor of 4.
inline fn roundUp4(n: u12) u12 {
return ((n + 3) & ~@intCast(u12, 3));
}
// ShellIndex is an index to the shell strings. As shell can be up to 64 bytes
// (1<<6), maximum number of shells is 63 (1<<6-1), the maximum location offset
// is 1<<12. To make location resolvable in 10 bits, all shells will be padded
@ -219,7 +209,7 @@ test "basic shellpopcon" {
try testing.expectEqual(sections.getIndex(nobody), null);
try testing.expectEqual(
sections.sectionBlob().len,
roundUp4(bash.len) + roundUp4(zsh.len) + roundUp4(long.len),
pad.roundUp4(u12, bash.len) + pad.roundUp4(u12, zsh.len) + pad.roundUp4(u12, long.len),
);
const shellReader = ShellReader.init(
@ -232,18 +222,3 @@ test "basic shellpopcon" {
try testing.expectEqual(shellReader.sectionIndex.len, 3);
}
test "padding" {
try testing.expectEqual(roundUp4Padding(@intCast(u12, 0)), 0);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 1)), 3);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 2)), 2);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 3)), 1);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 4)), 0);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 40)), 0);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 41)), 3);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 42)), 2);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 43)), 1);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 44)), 0);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 4091)), 1);
try testing.expectEqual(roundUp4Padding(@intCast(u12, 4092)), 0);
}

View File

@ -3,4 +3,5 @@ test "turbonss test suite" {
_ = @import("shell.zig");
_ = @import("header.zig");
_ = @import("user.zig");
_ = @import("padding.zig");
}

View File

@ -1,4 +1,5 @@
const std = @import("std");
const pad = @import("padding.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
@ -72,6 +73,9 @@ pub const UserWriter = struct {
if (puser.shell_here) {
try self.appendTo.appendSlice(user.shell);
}
const padding = pad.roundUp4Padding(u64, self.appendTo.items.len);
try self.appendTo.appendNTimes(0, padding);
}
};