From ed7c449ad24a1a6a6a50c8233d9a5bafb23fe413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Sun, 5 Jul 2026 00:28:04 -0400 Subject: [PATCH] rtl: add CachePadded --- src/rtl/cache_padded.zig | 18 ++++++++++++++++++ src/rtl/root.zig | 1 + 2 files changed, 19 insertions(+) create mode 100644 src/rtl/cache_padded.zig diff --git a/src/rtl/cache_padded.zig b/src/rtl/cache_padded.zig new file mode 100644 index 0000000..ba7aaf7 --- /dev/null +++ b/src/rtl/cache_padded.zig @@ -0,0 +1,18 @@ +const std = @import("std"); + +/// Type padded to a cache line. +pub fn CachePadded(comptime T: type) type { + return struct { + const cache_line = std.atomic.cache_line; + const value_size = @sizeOf(T); + const padded_size = std.mem.alignForward(usize, value_size, cache_line); + const pad = padded_size - value_size; + + value: T align(cache_line), + _pad: [pad]u8 = undefined, + + pub fn init(val: T) @This() { + return .{ .value = val }; + } + }; +} diff --git a/src/rtl/root.zig b/src/rtl/root.zig index 3404729..30385e2 100644 --- a/src/rtl/root.zig +++ b/src/rtl/root.zig @@ -15,6 +15,7 @@ pub const BitMap = @import("bitmap.zig").BitMap; pub const AtomicBitMap = @import("bitmap.zig").AtomicBitMap; pub const HandoffList = @import("handoff.zig").HandoffList; pub const LinkerSet = @import("linker_set.zig").LinkerSet; +pub const CachePadded = @import("cache_padded.zig").CachePadded; /// Asserts that a given type `T` matches the schema declared by `I`. /// This includes public methods and fields.