const std = @import("std"); const Self = @This(); size: usize = 0, name: [24]u8 = undefined, fd: std.posix.fd_t = undefined, pub fn init_random() !Self { var buf: [8]u8 = undefined; try std.posix.getrandom(&buf); var self: Self = .{}; const name = std.fmt.bufPrint(&self.name, "/wl.shm.{x}{x}{x}{x}{x}{x}{x}{x}", .{ buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], }) catch unreachable; self.fd = try std.posix.memfd_create(name, 0); return self; } pub fn truncate(self: *Self, size: usize) !void { try std.posix.ftruncate(self.fd, size); self.size = size; } pub fn mmap(self: *Self) ![]u8 { return std.posix.mmap( null, self.size, std.os.linux.PROT.READ | std.os.linux.PROT.WRITE, .{ .TYPE = .SHARED, .ANONYMOUS = true, }, self.fd, 0, ); } pub fn close(self: *Self) void { std.posix.close(self.fd); self.* = undefined; }