feat(buffers): add support for buffer references

This commit is contained in:
CJ van den Berg 2025-01-26 17:19:58 +01:00
parent 90b443a487
commit 4d3d91a744
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 15 additions and 0 deletions

View file

@ -292,6 +292,7 @@ pub fn build_exe(
.root_source_file = b.path("src/buffer/Buffer.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "thespian", .module = thespian_mod },
},
});

View file

@ -1,4 +1,5 @@
const std = @import("std");
const tp = @import("thespian");
const Buffer = @import("Buffer.zig");
const Self = @This();
@ -109,3 +110,16 @@ pub fn delete_all(self: *Self) void {
}
self.buffers.clearRetainingCapacity();
}
pub fn buffer_from_ref(self: *Self, buffer_ref: usize) ?*Buffer {
var i = self.buffers.iterator();
while (i.next()) |p|
if (@intFromPtr(p.value_ptr.*) == buffer_ref)
return p.value_ptr.*;
tp.trace(tp.channel.debug, .{ "buffer_from_ref", "failed", buffer_ref });
return null;
}
pub fn buffer_to_ref(_: *Self, buffer: *Buffer) usize {
return @intFromPtr(buffer);
}