aboutsummaryrefslogtreecommitdiff
path: root/mirai/kata/attachment.zig
diff options
context:
space:
mode:
Diffstat (limited to 'mirai/kata/attachment.zig')
-rw-r--r--mirai/kata/attachment.zig26
1 files changed, 26 insertions, 0 deletions
diff --git a/mirai/kata/attachment.zig b/mirai/kata/attachment.zig
index 05bea8a..9d9ed35 100644
--- a/mirai/kata/attachment.zig
+++ b/mirai/kata/attachment.zig
@@ -1,6 +1,7 @@
//! Attachment management
const attachment_limits = @import("../common/limits/attachment.zig");
+const kata_limits = @import("../common/limits/kata.zig");
pub const Type = enum {
Unit,
@@ -31,3 +32,28 @@ pub const Attachment = struct {
device_type: ?DeviceType = null,
};
+
+const POOL_SIZE = kata_limits.MAX_KATAS * kata_limits.MAX_ATTACHMENTS;
+var pool: [POOL_SIZE]Attachment = undefined;
+var pool_used: [POOL_SIZE]bool = [_]bool{false} ** POOL_SIZE;
+
+pub fn alloc() ?*Attachment {
+ for (&pool, 0..) |*entry, i| {
+ if (!pool_used[i]) {
+ pool_used[i] = true;
+ entry.* = Attachment{};
+ return entry;
+ }
+ }
+ return null;
+}
+
+pub fn free(ptr: *Attachment) void {
+ const addr = @intFromPtr(ptr);
+ const base = @intFromPtr(&pool[0]);
+ const size = @sizeOf(Attachment);
+ if (addr >= base and addr < base + POOL_SIZE * size) {
+ const idx = (addr - base) / size;
+ pool_used[idx] = false;
+ }
+}