mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 08:18:59 +00:00
use nullable slice for tracking chain allocations
This commit is contained in:
@@ -40,13 +40,6 @@ const Factory = @This();
|
|||||||
_page: *Page,
|
_page: *Page,
|
||||||
_slab: SlabAllocator,
|
_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 {
|
fn PrototypeChain(comptime types: []const type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@@ -103,7 +96,7 @@ fn PrototypeChain(comptime types: []const type) type {
|
|||||||
|
|
||||||
fn setRoot(self: *const Self, comptime T: type) void {
|
fn setRoot(self: *const Self, comptime T: type) void {
|
||||||
const ptr = self.get(0);
|
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 {
|
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) },
|
&.{ EventTarget, @TypeOf(child) },
|
||||||
).allocate(allocator);
|
).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);
|
chain.setLeaf(1, child);
|
||||||
|
|
||||||
return chain.get(1);
|
return chain.get(1);
|
||||||
@@ -183,7 +180,7 @@ pub fn event(self: *Factory, typ: []const u8, child: anytype) !*@TypeOf(child) {
|
|||||||
event_ptr.* = .{
|
event_ptr.* = .{
|
||||||
._type = unionInit(Event.Type, chain.get(1)),
|
._type = unionInit(Event.Type, chain.get(1)),
|
||||||
._type_string = try String.init(self._page.arena, typ, .{}),
|
._type_string = try String.init(self._page.arena, typ, .{}),
|
||||||
._allocation = FactoryAllocationKind{ .chain = chain.memory },
|
._allocation = chain.memory,
|
||||||
};
|
};
|
||||||
chain.setLeaf(1, child);
|
chain.setLeaf(1, child);
|
||||||
|
|
||||||
@@ -201,7 +198,7 @@ pub fn blob(self: *Factory, child: anytype) !*@TypeOf(child) {
|
|||||||
const blob_ptr = chain.get(0);
|
const blob_ptr = chain.get(0);
|
||||||
blob_ptr.* = .{
|
blob_ptr.* = .{
|
||||||
._type = unionInit(Blob.Type, chain.get(1)),
|
._type = unionInit(Blob.Type, chain.get(1)),
|
||||||
._allocation = FactoryAllocationKind{ .chain = chain.memory },
|
._allocation = chain.memory,
|
||||||
.slice = "",
|
.slice = "",
|
||||||
.mime = "",
|
.mime = "",
|
||||||
};
|
};
|
||||||
@@ -295,14 +292,11 @@ pub fn destroy(self: *Factory, value: anytype) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const allocation_kind = self.destroyChain(value, true) orelse return;
|
const chain_memory = self.destroyChain(value, true) orelse return;
|
||||||
switch (allocation_kind) {
|
allocator.free(chain_memory);
|
||||||
.chain => |buf| allocator.free(buf),
|
|
||||||
.standalone => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 S = reflect.Struct(@TypeOf(value));
|
||||||
const allocator = self._slab.allocator();
|
const allocator = self._slab.allocator();
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,13 @@ const Writer = std.Io.Writer;
|
|||||||
|
|
||||||
const js = @import("../js/js.zig");
|
const js = @import("../js/js.zig");
|
||||||
const Page = @import("../Page.zig");
|
const Page = @import("../Page.zig");
|
||||||
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
|
|
||||||
|
|
||||||
/// https://w3c.github.io/FileAPI/#blob-section
|
/// https://w3c.github.io/FileAPI/#blob-section
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob
|
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob
|
||||||
const Blob = @This();
|
const Blob = @This();
|
||||||
|
|
||||||
_type: Type,
|
_type: Type,
|
||||||
_allocation: FactoryAllocationKind,
|
_allocation: ?[]u8,
|
||||||
|
|
||||||
/// Immutable slice of blob.
|
/// Immutable slice of blob.
|
||||||
/// Note that another blob may hold a pointer/slice to this,
|
/// Note that another blob may hold a pointer/slice to this,
|
||||||
@@ -81,7 +80,7 @@ pub fn init(
|
|||||||
|
|
||||||
return page._factory.create(Blob{
|
return page._factory.create(Blob{
|
||||||
._type = .generic,
|
._type = .generic,
|
||||||
._allocation = .standalone,
|
._allocation = null,
|
||||||
.slice = slice,
|
.slice = slice,
|
||||||
.mime = mime,
|
.mime = mime,
|
||||||
});
|
});
|
||||||
@@ -271,7 +270,7 @@ pub fn getSlice(
|
|||||||
|
|
||||||
return page._factory.create(Blob{
|
return page._factory.create(Blob{
|
||||||
._type = .generic,
|
._type = .generic,
|
||||||
._allocation = .standalone,
|
._allocation = null,
|
||||||
.slice = slice[start..end],
|
.slice = slice[start..end],
|
||||||
.mime = mime,
|
.mime = mime,
|
||||||
});
|
});
|
||||||
@@ -279,7 +278,7 @@ pub fn getSlice(
|
|||||||
|
|
||||||
return page._factory.create(Blob{
|
return page._factory.create(Blob{
|
||||||
._type = .generic,
|
._type = .generic,
|
||||||
._allocation = .standalone,
|
._allocation = null,
|
||||||
.slice = slice,
|
.slice = slice,
|
||||||
.mime = mime,
|
.mime = mime,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,14 +20,13 @@ const std = @import("std");
|
|||||||
const js = @import("../js/js.zig");
|
const js = @import("../js/js.zig");
|
||||||
|
|
||||||
const Page = @import("../Page.zig");
|
const Page = @import("../Page.zig");
|
||||||
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
|
|
||||||
const EventTarget = @import("EventTarget.zig");
|
const EventTarget = @import("EventTarget.zig");
|
||||||
const String = @import("../../string.zig").String;
|
const String = @import("../../string.zig").String;
|
||||||
|
|
||||||
pub const Event = @This();
|
pub const Event = @This();
|
||||||
|
|
||||||
_type: Type,
|
_type: Type,
|
||||||
_allocation: FactoryAllocationKind,
|
_allocation: ?[]u8,
|
||||||
|
|
||||||
_bubbles: bool = false,
|
_bubbles: bool = false,
|
||||||
_cancelable: 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{
|
return page._factory.create(Event{
|
||||||
._type = .generic,
|
._type = .generic,
|
||||||
._allocation = .standalone,
|
._allocation = null,
|
||||||
._bubbles = opts.bubbles,
|
._bubbles = opts.bubbles,
|
||||||
._time_stamp = time_stamp,
|
._time_stamp = time_stamp,
|
||||||
._cancelable = opts.cancelable,
|
._cancelable = opts.cancelable,
|
||||||
|
|||||||
@@ -21,14 +21,13 @@ const js = @import("../js/js.zig");
|
|||||||
|
|
||||||
const Page = @import("../Page.zig");
|
const Page = @import("../Page.zig");
|
||||||
const RegisterOptions = @import("../EventManager.zig").RegisterOptions;
|
const RegisterOptions = @import("../EventManager.zig").RegisterOptions;
|
||||||
const FactoryAllocationKind = @import("../Factory.zig").FactoryAllocationKind;
|
|
||||||
|
|
||||||
const Event = @import("Event.zig");
|
const Event = @import("Event.zig");
|
||||||
|
|
||||||
const EventTarget = @This();
|
const EventTarget = @This();
|
||||||
|
|
||||||
_type: Type,
|
_type: Type,
|
||||||
_allocation: FactoryAllocationKind,
|
_allocation: ?[]u8,
|
||||||
|
|
||||||
pub const Type = union(enum) {
|
pub const Type = union(enum) {
|
||||||
node: *@import("Node.zig"),
|
node: *@import("Node.zig"),
|
||||||
@@ -124,7 +123,7 @@ pub const JsApi = struct {
|
|||||||
const testing = @import("../../testing.zig");
|
const testing = @import("../../testing.zig");
|
||||||
test "WebApi: EventTarget" {
|
test "WebApi: EventTarget" {
|
||||||
// we create thousands of these per page. Nothing should bloat it.
|
// 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", .{});
|
try testing.htmlRunner("events.html", .{});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user