summaryrefslogtreecommitdiff
path: root/mupdf.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-08-09 10:41:33 -0300
committerGabriel Schneider <[email protected]>2026-08-10 09:17:07 -0300
commit9085cb5bfdd0b78ff3a62c0c71fc231dd7b5052a (patch)
tree91fce278abc85e4f7491de8b5f02184c8a2d5a56 /mupdf.zig
parent2b1fac6fecbf7d7cc08f9dad24687b05e9968c9a (diff)
downloadpardes-9085cb5bfdd0b78ff3a62c0c71fc231dd7b5052a.tar.gz
pardes-9085cb5bfdd0b78ff3a62c0c71fc231dd7b5052a.zip
replace ArrayLists with bounded storage
Diffstat (limited to 'mupdf.zig')
-rw-r--r--mupdf.zig36
1 files changed, 27 insertions, 9 deletions
diff --git a/mupdf.zig b/mupdf.zig
index c80f678d..a407ea09 100644
--- a/mupdf.zig
+++ b/mupdf.zig
@@ -248,17 +248,23 @@ fn flatCSources(b: *std.Build, io: std.Io, dependency: *std.Build.Dependency, re
std.debug.panic("MuPDF source directory is missing: {s}", .{relative_dir});
defer dir.close(io);
- var files: std.ArrayList([]const u8) = .empty;
+ const max_flat_sources = 4096;
+ var bounded: [max_flat_sources][]const u8 = undefined;
+ var count: usize = 0;
var it = dir.iterate();
while (it.next(io) catch @panic("read MuPDF source directory")) |entry| {
// Some filesystems report d_type as unknown. The selected directories
// are flat and immutable, so the suffix is the reliable predicate.
if (!std.mem.endsWith(u8, entry.name, ".c")) continue;
- files.append(b.allocator, b.fmt("{s}/{s}", .{ relative_dir, entry.name })) catch @panic("OOM");
+ if (count == bounded.len) std.debug.panic("MuPDF source directory exceeds {d} C files: {s}", .{ bounded.len, relative_dir });
+ bounded[count] = b.fmt("{s}/{s}", .{ relative_dir, entry.name });
+ count += 1;
}
- std.mem.sort([]const u8, files.items, {}, lessThan);
- if (files.items.len == 0) std.debug.panic("MuPDF source directory has no C files: {s}", .{relative_dir});
- return files.items;
+ if (count == 0) std.debug.panic("MuPDF source directory has no C files: {s}", .{relative_dir});
+ const files = b.allocator.alloc([]const u8, count) catch @panic("OOM");
+ @memcpy(files, bounded[0..count]);
+ std.mem.sort([]const u8, files, {}, lessThan);
+ return files;
}
fn readMakelists(b: *std.Build, io: std.Io, dependency: *std.Build.Dependency) []const u8 {
@@ -271,7 +277,7 @@ fn readMakelists(b: *std.Build, io: std.Io, dependency: *std.Build.Dependency) [
}
fn makeSources(b: *std.Build, makelists: []const u8, variable: []const u8) []const []const u8 {
- var files: std.ArrayList([]const u8) = .empty;
+ var count: usize = 0;
var lines = std.mem.splitScalar(u8, makelists, '\n');
while (lines.next()) |raw_line| {
const line = std.mem.trim(u8, raw_line, " \t\r");
@@ -279,11 +285,23 @@ fn makeSources(b: *std.Build, makelists: []const u8, variable: []const u8) []con
const assignment = std.mem.trimStart(u8, line[variable.len..], " \t");
if (!std.mem.startsWith(u8, assignment, "+=")) continue;
const path = std.mem.trim(u8, assignment[2..], " \t\r");
+ if (std.mem.endsWith(u8, path, ".c")) count += 1;
+ }
+ if (count == 0) std.debug.panic("MuPDF Makelists variable has no C sources: {s}", .{variable});
+ const files = b.allocator.alloc([]const u8, count) catch @panic("OOM");
+ lines = std.mem.splitScalar(u8, makelists, '\n');
+ var file_i: usize = 0;
+ while (lines.next()) |raw_line| {
+ const line = std.mem.trim(u8, raw_line, " \t\r");
+ if (!std.mem.startsWith(u8, line, variable)) continue;
+ const assignment = std.mem.trimStart(u8, line[variable.len..], " \t");
+ if (!std.mem.startsWith(u8, assignment, "+=")) continue;
+ const path = std.mem.trim(u8, assignment[2..], " \t\r");
if (!std.mem.endsWith(u8, path, ".c")) continue;
- files.append(b.allocator, b.dupe(path)) catch @panic("OOM");
+ files[file_i] = b.dupe(path);
+ file_i += 1;
}
- if (files.items.len == 0) std.debug.panic("MuPDF Makelists variable has no C sources: {s}", .{variable});
- return files.items;
+ return files;
}
fn lessThan(_: void, a: []const u8, b: []const u8) bool {