wip between-user padding

This commit is contained in:
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

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);
}