use nullable slice for tracking chain allocations

This commit is contained in:
Muki Kiboigo
2025-11-26 11:14:30 -08:00
parent 2ddaa351ab
commit 45c7184fde
4 changed files with 19 additions and 28 deletions

View File

@@ -40,13 +40,6 @@ const Factory = @This();
_page: *Page,
_slab: SlabAllocator,
pub const FactoryAllocationKind = union(enum) {
/// Allocated as part of a Factory PrototypeChain
chain: []u8,
/// Allocated standalone via factory.create()
standalone,
};
fn PrototypeChain(comptime types: []const type) type {
return struct {
const Self = @This();
@@ -103,7 +96,7 @@ fn PrototypeChain(comptime types: []const type) type {
fn setRoot(self: *const Self, comptime T: type) void {
const ptr = self.get(0);
ptr.* = .{ ._type = unionInit(T, self.get(1)), ._allocation = FactoryAllocationKind{ .chain = self.memory } };
ptr.* = .{ ._type = unionInit(T, self.get(1)), ._allocation = self.memory };
}
fn setMiddle(self: *const Self, comptime index: usize, comptime T: type) void {
@@ -164,7 +157,11 @@ pub fn eventTarget(self: *Factory, child: anytype) !*@TypeOf(child) {
&.{ EventTarget, @TypeOf(child) },
).allocate(allocator);
chain.setRoot(EventTarget.Type);
const event_ptr = chain.get(0);
event_ptr.* = .{
._type = unionInit(EventTarget.Type, chain.get(1)),
._allocation = chain.memory,
};
chain.setLeaf(1, child);
return chain.get(1);
@@ -183,7 +180,7 @@ pub fn event(self: *Factory, typ: []const u8, child: anytype) !*@TypeOf(child) {
event_ptr.* = .{
._type = unionInit(Event.Type, chain.get(1)),
._type_string = try String.init(self._page.arena, typ, .{}),
._allocation = FactoryAllocationKind{ .chain = chain.memory },
._allocation = chain.memory,
};
chain.setLeaf(1, child);
@@ -201,7 +198,7 @@ pub fn blob(self: *Factory, child: anytype) !*@TypeOf(child) {
const blob_ptr = chain.get(0);
blob_ptr.* = .{
._type = unionInit(Blob.Type, chain.get(1)),
._allocation = FactoryAllocationKind{ .chain = chain.memory },
._allocation = chain.memory,
.slice = "",
.mime = "",
};
@@ -295,14 +292,11 @@ pub fn destroy(self: *Factory, value: anytype) void {
}
}
const allocation_kind = self.destroyChain(value, true) orelse return;
switch (allocation_kind) {
.chain => |buf| allocator.free(buf),
.standalone => {},
}
const chain_memory = self.destroyChain(value, true) orelse return;
allocator.free(chain_memory);
}
fn destroyChain(self: *Factory, value: anytype, comptime first: bool) ?FactoryAllocationKind {
fn destroyChain(self: *Factory, value: anytype, comptime first: bool) ?[]u8 {
const S = reflect.Struct(@TypeOf(value));
const allocator = self._slab.allocator();

View File

@@ -21,14 +21,13 @@ const Writer = std.Io.Writer;
const js = @import("../js/js.zig");
const Page = @import("../Page.zig");
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
/// https://w3c.github.io/FileAPI/#blob-section
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob
const Blob = @This();
_type: Type,
_allocation: FactoryAllocationKind,
_allocation: ?[]u8,
/// Immutable slice of blob.
/// Note that another blob may hold a pointer/slice to this,
@@ -81,7 +80,7 @@ pub fn init(
return page._factory.create(Blob{
._type = .generic,
._allocation = .standalone,
._allocation = null,
.slice = slice,
.mime = mime,
});
@@ -271,7 +270,7 @@ pub fn getSlice(
return page._factory.create(Blob{
._type = .generic,
._allocation = .standalone,
._allocation = null,
.slice = slice[start..end],
.mime = mime,
});
@@ -279,7 +278,7 @@ pub fn getSlice(
return page._factory.create(Blob{
._type = .generic,
._allocation = .standalone,
._allocation = null,
.slice = slice,
.mime = mime,
});

View File

@@ -20,14 +20,13 @@ const std = @import("std");
const js = @import("../js/js.zig");
const Page = @import("../Page.zig");
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
const EventTarget = @import("EventTarget.zig");
const String = @import("../../string.zig").String;
pub const Event = @This();
_type: Type,
_allocation: FactoryAllocationKind,
_allocation: ?[]u8,
_bubbles: bool = false,
_cancelable: bool = false,
@@ -68,7 +67,7 @@ pub fn init(typ: []const u8, opts_: ?Options, page: *Page) !*Event {
return page._factory.create(Event{
._type = .generic,
._allocation = .standalone,
._allocation = null,
._bubbles = opts.bubbles,
._time_stamp = time_stamp,
._cancelable = opts.cancelable,

View File

@@ -21,14 +21,13 @@ const js = @import("../js/js.zig");
const Page = @import("../Page.zig");
const RegisterOptions = @import("../EventManager.zig").RegisterOptions;
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
const Event = @import("Event.zig");
const EventTarget = @This();
_type: Type,
_allocation: FactoryAllocationKind,
_allocation: ?[]u8,
pub const Type = union(enum) {
node: *@import("Node.zig"),
@@ -124,7 +123,7 @@ pub const JsApi = struct {
const testing = @import("../../testing.zig");
test "WebApi: EventTarget" {
// we create thousands of these per page. Nothing should bloat it.
try testing.expectEqual(16, @sizeOf(EventTarget));
try testing.expectEqual(32, @sizeOf(EventTarget));
try testing.htmlRunner("events.html", .{});
}