From 1ed2472742356ef2e42ec3c470bfa1845035654e Mon Sep 17 00:00:00 2001 From: Nikolay Govorov Date: Tue, 27 Jan 2026 23:41:47 +0000 Subject: [PATCH] Drop LimitedAllocator --- src/Config.zig | 21 ----------- src/LimitedAllocator.zig | 75 ---------------------------------------- src/Server.zig | 10 ++---- 3 files changed, 3 insertions(+), 103 deletions(-) delete mode 100644 src/LimitedAllocator.zig diff --git a/src/Config.zig b/src/Config.zig index 5edecbb6..9c51525a 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -130,13 +130,6 @@ pub fn maxConnections(self: *const Config) u16 { }; } -pub fn maxMemoryPerTab(self: *const Config) usize { - return switch (self.mode) { - .serve => |opts| @intCast(opts.max_memory_per_tab), - else => unreachable, - }; -} - pub fn userAgent(self: *const Config, allocator: Allocator) ![:0]const u8 { const base = "User-Agent: Lightpanda/1.0"; if (self.userAgentSuffix()) |suffix| { @@ -158,7 +151,6 @@ pub const Serve = struct { timeout: u31 = 10, max_connections: u16 = 16, max_tabs_per_connection: u16 = 8, - max_memory_per_tab: u64 = 512 * 1024 * 1024, max_pending_connections: u16 = 128, common: Common = .{}, }; @@ -452,19 +444,6 @@ fn parseServeArgs( continue; } - if (std.mem.eql(u8, "--max_tab_memory", opt)) { - const str = args.next() orelse { - log.fatal(.app, "missing argument value", .{ .arg = "--max_tab_memory" }); - return error.InvalidArgument; - }; - - serve.max_memory_per_tab = std.fmt.parseInt(u64, str, 10) catch |err| { - log.fatal(.app, "invalid argument value", .{ .arg = "--max_tab_memory", .err = err }); - return error.InvalidArgument; - }; - continue; - } - if (std.mem.eql(u8, "--max_pending_connections", opt)) { const str = args.next() orelse { log.fatal(.app, "missing argument value", .{ .arg = "--max_pending_connections" }); diff --git a/src/LimitedAllocator.zig b/src/LimitedAllocator.zig deleted file mode 100644 index 92411cb6..00000000 --- a/src/LimitedAllocator.zig +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (C) 2023-2026 Lightpanda (Selecy SAS) -// -// Francis Bouvier -// Pierre Tachoire -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -const std = @import("std"); -const Allocator = std.mem.Allocator; - -const LimitedAllocator = @This(); - -parent: Allocator, -limit: usize, -allocated: usize = 0, - -pub fn init(parent: Allocator, limit: usize) LimitedAllocator { - return .{ .parent = parent, .limit = limit }; -} - -pub fn allocator(self: *LimitedAllocator) Allocator { - return .{ .ptr = self, .vtable = &vtable }; -} - -const vtable: Allocator.VTable = .{ - .alloc = alloc, - .resize = resize, - .remap = remap, - .free = free, -}; - -fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 { - const self: *LimitedAllocator = @ptrCast(@alignCast(ctx)); - if (self.allocated + len > self.limit) return null; - const result = self.parent.rawAlloc(len, alignment, ret_addr); - if (result != null) self.allocated += len; - return result; -} - -fn resize(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool { - const self: *LimitedAllocator = @ptrCast(@alignCast(ctx)); - if (new_len > memory.len and self.allocated + new_len - memory.len > self.limit) return false; - if (self.parent.rawResize(memory, alignment, new_len, ret_addr)) { - if (new_len > memory.len) self.allocated += new_len - memory.len else self.allocated -= memory.len - new_len; - return true; - } - return false; -} - -fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { - const self: *LimitedAllocator = @ptrCast(@alignCast(ctx)); - if (new_len > memory.len and self.allocated + new_len - memory.len > self.limit) return null; - const result = self.parent.rawRemap(memory, alignment, new_len, ret_addr); - if (result != null) { - if (new_len > memory.len) self.allocated += new_len - memory.len else self.allocated -= memory.len - new_len; - } - return result; -} - -fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void { - const self: *LimitedAllocator = @ptrCast(@alignCast(ctx)); - self.parent.rawFree(memory, alignment, ret_addr); - self.allocated -= memory.len; -} diff --git a/src/Server.zig b/src/Server.zig index c3215253..f4a6b810 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -33,7 +33,6 @@ const CDP = @import("cdp/cdp.zig").CDP; const Http = @import("http/Http.zig"); const HttpClient = @import("http/Client.zig"); const ThreadPool = @import("ThreadPool.zig"); -const LimitedAllocator = @import("LimitedAllocator.zig"); const Server = @This(); @@ -130,20 +129,17 @@ fn shutdownConnection(socket: posix.socket_t) void { fn handleConnection(self: *Server, socket: posix.socket_t, timeout_ms: u32) void { defer posix.close(socket); - var limited = LimitedAllocator.init(self.allocator, self.app.config.maxMemoryPerTab()); - const client_allocator = limited.allocator(); - // Client is HUGE (> 512KB) because it has a large read buffer. // V8 crashes if this is on the stack (likely related to its size). - const client = client_allocator.create(Client) catch |err| { + const client = self.allocator.create(Client) catch |err| { log.err(.app, "CDP client create", .{ .err = err }); return; }; - defer client_allocator.destroy(client); + defer self.allocator.destroy(client); client.* = Client.init( socket, - client_allocator, + self.allocator, self.app, self.json_version_response, timeout_ms,