1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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;
}
|