Drop LimitedAllocator

This commit is contained in:
Nikolay Govorov
2026-01-27 23:41:47 +00:00
parent 891e822afa
commit 1ed2472742
3 changed files with 3 additions and 103 deletions

View File

@@ -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" });

View File

@@ -1,75 +0,0 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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 <https://www.gnu.org/licenses/>.
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;
}

View File

@@ -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,