lib/choir/src/backends/artifact/model/payload/buffer.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const payload = @import("root.zig");
 3 
 4 const Allocator = std.mem.Allocator;
 5 
 6 pub const BufferFormat = enum(u8) {
 7     raw = 1,
 8     machine_code = 2,
 9     object_file = 3,
10     webassembly_module = 4,
11     assembly = 5,
12     source_text = 6,
13     debug_section = 7,
14     vendor = 8,
15 };
16 
17 pub const ArtifactBuffer = struct {
18     name: []const u8,
19     format: BufferFormat,
20     bytes: []const u8,
21     alignment: usize = 1,
22 
23     pub fn dupe(self: ArtifactBuffer, allocator: Allocator) Allocator.Error!ArtifactBuffer {
24         const name = try payload.slice.dupe(allocator, self.name);
25         errdefer payload.slice.free(allocator, name);
26         return .{
27             .name = name,
28             .format = self.format,
29             .bytes = try payload.slice.dupe(allocator, self.bytes),
30             .alignment = self.alignment,
31         };
32     }
33 
34     pub fn deinit(self: ArtifactBuffer, allocator: Allocator) void {
35         payload.slice.free(allocator, self.name);
36         payload.slice.free(allocator, self.bytes);
37     }
38 
39     pub fn eql(self: ArtifactBuffer, other: ArtifactBuffer) bool {
40         return self.format == other.format and
41             self.alignment == other.alignment and
42             std.mem.eql(u8, self.name, other.name) and
43             std.mem.eql(u8, self.bytes, other.bytes);
44     }
45 };