lib/accy/src/kernel/library/catalog/artifact/owned.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2
3 const artifact_product = @import("../../../../artifact/model/root.zig");
4 const kernel = @import("../../../root.zig");
5 const descriptor_mod = @import("../root.zig");
6 const entry = @import("../../root.zig").entry;
7
8 const model = @import("model.zig");
9 const request_mod = @import("request.zig");
10 const specialized = @import("specialized/root.zig");
11
12 const OwnedDescriptor = descriptor_mod.OwnedDescriptor;
13 const OwnedKernelCallArtifactRegistry = model.OwnedKernelCallArtifactRegistry;
14
15 pub fn createOwnedKernelCallArtifact(
16 allocator: std.mem.Allocator,
17 handle: kernel.BackendHandle,
18 descriptor: OwnedDescriptor,
19 options: entry.ArtifactOptions,
20 ) !kernel.OwnedKernelCallArtifact {
21 return request_mod.createKernelCallArtifact(allocator, handle, .{
22 .target = descriptor.descriptor.metadata.target,
23 .version = descriptor.descriptor.metadata.version,
24 .options = options,
25 }) catch |err| switch (err) {
26 error.UnknownKernelLibraryEntry => try specialized.createSpecializedKernelCallArtifact(allocator, handle, descriptor, options),
27 else => err,
28 };
29 }
30
31 pub fn createOwnedKernelCallArtifactRegistry(
32 allocator: std.mem.Allocator,
33 handle: kernel.BackendHandle,
34 descriptors: []const OwnedDescriptor,
35 options: entry.ArtifactOptions,
36 ) !OwnedKernelCallArtifactRegistry {
37 if (descriptors.len == 0) return .{ .allocator = allocator };
38
39 const artifacts = try allocator.alloc(kernel.OwnedKernelCallArtifact, descriptors.len);
40 var artifact_count: usize = 0;
41 errdefer {
42 for (artifacts[0..artifact_count]) |*artifact| artifact.deinit();
43 allocator.free(artifacts);
44 }
45
46 const entries = try allocator.alloc(artifact_product.KernelCallArtifact, descriptors.len);
47 errdefer allocator.free(entries);
48
49 for (descriptors, 0..) |descriptor, index| {
50 artifacts[index] = try createOwnedKernelCallArtifact(allocator, handle, descriptor, options);
51 artifact_count += 1;
52 entries[index] = artifacts[index].entry();
53 }
54
55 const index = try artifact_product.buildKernelCallRegistryIndex(allocator, entries);
56 errdefer artifact_product.deinitKernelCallRegistryIndex(allocator, index);
57
58 return .{
59 .allocator = allocator,
60 .artifacts = artifacts,
61 .entries = entries,
62 .index = index,
63 };
64 }