mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 16:56:09 +03:00
terminal/kitty: minor stylistic changes to shm
This commit is contained in:
@ -102,21 +102,29 @@ pub const LoadingImage = struct {
|
||||
t: command.Transmission,
|
||||
path: []const u8,
|
||||
) !void {
|
||||
switch (builtin.target.os.tag) {
|
||||
.windows => {
|
||||
// TODO: support shared memory on windows
|
||||
// windows is currently unsupported, does it support shm?
|
||||
if (comptime builtin.target.os.tag == .windows) {
|
||||
return error.UnsupportedMedium;
|
||||
},
|
||||
else => {
|
||||
// libc is required for shm_open
|
||||
if (comptime !builtin.link_libc) return error.UnsupportedMedium;
|
||||
}
|
||||
|
||||
const pathz = try alloc.dupeZ(u8, path);
|
||||
defer alloc.free(pathz);
|
||||
// libc is required for shm_open
|
||||
if (comptime !builtin.link_libc) {
|
||||
return error.UnsupportedMedium;
|
||||
}
|
||||
|
||||
// Since we're only supporting posix then max_path_bytes should
|
||||
// be enough to stack allocate the path.
|
||||
var buf: [std.fs.max_path_bytes]u8 = undefined;
|
||||
const pathz = std.fmt.bufPrintZ(&buf, "{s}", .{path}) catch return error.InvalidData;
|
||||
|
||||
const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), 0);
|
||||
switch (std.posix.errno(fd)) {
|
||||
.SUCCESS => {
|
||||
.SUCCESS => {},
|
||||
else => |err| {
|
||||
log.warn("unable to open shared memory {s}: {}", .{ path, err });
|
||||
return error.InvalidData;
|
||||
},
|
||||
}
|
||||
defer _ = std.c.close(fd);
|
||||
defer _ = std.c.shm_unlink(pathz);
|
||||
|
||||
@ -124,31 +132,31 @@ pub const LoadingImage = struct {
|
||||
log.warn("unable to fstat shared memory {s}: {}", .{ path, err });
|
||||
return error.InvalidData;
|
||||
};
|
||||
|
||||
if (stat.size <= 0) return error.InvalidData;
|
||||
|
||||
const size: usize = @intCast(stat.size);
|
||||
|
||||
const map = std.posix.mmap(null, size, std.c.PROT.READ, std.c.MAP{ .TYPE = .SHARED }, fd, 0) catch |err| {
|
||||
const map = std.posix.mmap(
|
||||
null,
|
||||
size,
|
||||
std.c.PROT.READ,
|
||||
std.c.MAP{ .TYPE = .SHARED },
|
||||
fd,
|
||||
0,
|
||||
) catch |err| {
|
||||
log.warn("unable to mmap shared memory {s}: {}", .{ path, err });
|
||||
return error.InvalidData;
|
||||
};
|
||||
defer std.posix.munmap(map);
|
||||
|
||||
const start: usize = @intCast(t.offset);
|
||||
const end: usize = if (t.size > 0) @min(@as(usize, @intCast(t.offset)) + @as(usize, @intCast(t.size)), size) else size;
|
||||
const end: usize = if (t.size > 0) @min(
|
||||
@as(usize, @intCast(t.offset)) + @as(usize, @intCast(t.size)),
|
||||
size,
|
||||
) else size;
|
||||
|
||||
assert(self.data.items.len == 0);
|
||||
try self.data.appendSlice(alloc, map[start..end]);
|
||||
},
|
||||
|
||||
else => |err| {
|
||||
log.warn("unable to open shared memory {s}: {}", .{ path, err });
|
||||
return error.InvalidData;
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the data from a temporary file and returns it. This allocates
|
||||
|
Reference in New Issue
Block a user