runtime page size detection

heap.zig: define new default page sizes
heap.zig: add min/max_page_size and their options
lib/std/c: add miscellaneous declarations
heap.zig: add pageSize() and its options
switch to new page sizes, especially in GPA/stdlib
mem.zig: remove page_size
This commit is contained in:
Archbirdplus
2024-10-20 14:55:57 -07:00
committed by Andrew Kelley
parent b0ed602d5d
commit 439667be04
29 changed files with 614 additions and 176 deletions

View File

@@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("std.zig");
const math = std.math;
const mem = std.mem;
const heap = std.heap;
const io = std.io;
const posix = std.posix;
const fs = std.fs;
@@ -1134,7 +1135,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, source_location: SourceLocation)
defer f.close();
// TODO fstat and make sure that the file has the correct size
var buf: [mem.page_size]u8 = undefined;
var buf: [4096]u8 = undefined;
var amt_read = try f.read(buf[0..]);
const line_start = seek: {
var current_line_start: usize = 0;
@@ -1237,7 +1238,7 @@ test printLineFromFileAnyOs {
const overlap = 10;
var writer = file.writer();
try writer.writeByteNTimes('a', mem.page_size - overlap);
try writer.writeByteNTimes('a', heap.min_page_size - overlap);
try writer.writeByte('\n');
try writer.writeByteNTimes('a', overlap);
@@ -1252,10 +1253,10 @@ test printLineFromFileAnyOs {
defer allocator.free(path);
var writer = file.writer();
try writer.writeByteNTimes('a', mem.page_size);
try writer.writeByteNTimes('a', heap.max_page_size);
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** mem.page_size) ++ "\n", output.items);
try expectEqualStrings(("a" ** heap.max_page_size) ++ "\n", output.items);
output.clearRetainingCapacity();
}
{
@@ -1265,18 +1266,18 @@ test printLineFromFileAnyOs {
defer allocator.free(path);
var writer = file.writer();
try writer.writeByteNTimes('a', 3 * mem.page_size);
try writer.writeByteNTimes('a', 3 * heap.max_page_size);
try expectError(error.EndOfFile, printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 }));
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "\n", output.items);
try expectEqualStrings(("a" ** (3 * heap.max_page_size)) ++ "\n", output.items);
output.clearRetainingCapacity();
try writer.writeAll("a\na");
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "a\n", output.items);
try expectEqualStrings(("a" ** (3 * heap.max_page_size)) ++ "a\n", output.items);
output.clearRetainingCapacity();
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 });
@@ -1290,7 +1291,7 @@ test printLineFromFileAnyOs {
defer allocator.free(path);
var writer = file.writer();
const real_file_start = 3 * mem.page_size;
const real_file_start = 3 * heap.min_page_size;
try writer.writeByteNTimes('\n', real_file_start);
try writer.writeAll("abc\ndef");