Skip to content

Commit

Permalink
page size: fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
archbirdplus committed Aug 8, 2024
1 parent 608947c commit c98e58c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ pub const _SC = switch (native_os) {
pub const PAGESIZE = 11;
pub const NPROCESSORS_ONLN = 15;
},
}
};

pub const SEEK = switch (native_os) {
.linux => linux.SEEK,
Expand Down
22 changes: 14 additions & 8 deletions lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const page_size_darwin: ?comptime_int = if (builtin.os.tag.isDarwin())
switch (builtin.cpu.arch) {
.x86, .x86_64 => 4 << 10,
.thumb, .thumbeb, .arm, .armeb, .aarch64, .aarch64_be, .aarch64_32 => 16 << 10,
else => null
else => null,
}
else null;
else
null;

// -- <https://devblogs.microsoft.com/oldnewthing/20210510-00/?p=105200>
const page_size_windows: ?comptime_int = if (builtin.os.tag == .windows and builtin.os.version_range.windows.min.isAtLeast(.xp)) blk: {
const page_size_windows: ?comptime_int = if (builtin.os.tag == .windows and builtin.os.version_range.windows.min.isAtLeast(.xp))
blk: {
break :blk switch (builtin.cpu.arch) {
.x86, .x86_64 => 4 << 10,
// SuperH => 4 << 10,
Expand All @@ -26,7 +28,7 @@ const page_size_windows: ?comptime_int = if (builtin.os.tag == .windows and buil
// DEC Alpha => 8 << 10,
// Itanium => 8 << 10,
.thumb, .thumbeb, .arm, .armeb, .aarch64, .aarch64_be, .aarch64_32 => 4 << 10,
else => null
else => null,
};
} else null;

Expand Down Expand Up @@ -88,7 +90,9 @@ var runtimePageSize = std.atomic.Value(usize).init(0);

/// Runtime detected page size.
pub inline fn pageSize() usize {
if (@inComptime()) { @compileError("pageSize() must NOT be used in comptime. Use page_size variants instead."); }
if (@inComptime()) {
@compileError("pageSize() must NOT be used in comptime. Use page_size variants instead.");
}
if (page_size == page_size_cap) {
assert(queryPageSize() == page_size);
return page_size;
Expand All @@ -100,7 +104,7 @@ pub inline fn pageSize() usize {
// Runtime queried page size.
fn queryPageSize() usize {
var size = runtimePageSize.load(.unordered);
if(size > 0) return size;
if (size > 0) return size;
defer {
std.debug.assert(size > 0);
std.debug.assert(size >= page_size);
Expand Down Expand Up @@ -132,15 +136,17 @@ fn queryPageSize() usize {
var vm_page_size: std.c.vm_size_t = undefined;
switch (std.c._host_page_size(std.c.mach_host_self(), &vm_page_size)) {
0 => size = vm_page_size,
else => {}
else => {},
}
},
.windows => {
var info: std.os.windows.SYSTEM_INFO = undefined;
std.os.windows.kernel32.GetSystemInfo(&info);
size = info.dwPageSize;
},
else => if (@hasDecl(std.c, "_SC") and @hasDecl(std.c._SC, "PAGE_SIZE")) { size = std.c.sysconf(std.c._SC.PAGE_SIZE); } else {},
else => if (@hasDecl(std.c, "_SC") and @hasDecl(std.c._SC, "PAGE_SIZE")) {
size = std.c.sysconf(std.c._SC.PAGE_SIZE);
} else {},
}
return size;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/std/heap/general_purpose_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,19 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
var small_bucket_count_cache = std.atomic.Value(usize).init(0);
fn small_bucket_count() usize {
const cached = small_bucket_count_cache.load(.monotonic);
if(cached != 0) { return cached; }
if (cached != 0) {
return cached;
}
const val = math.log2(pageSize());
small_bucket_count_cache.store(val, .monotonic);
return val;
}
var largest_bucket_object_size_cache = std.atomic.Value(usize).init(0);
fn largest_bucket_object_size() usize {
const cached = largest_bucket_object_size_cache.load(.monotonic);
if(cached != 0) { return cached; }
if (cached != 0) {
return cached;
}
const val = @as(usize, 1) << @truncate(small_bucket_count() - 1);
largest_bucket_object_size_cache.store(val, .monotonic);
return val;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/posix/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1323,5 +1323,5 @@ const CommonOpenFlags = packed struct {
test "pageSize() smoke test" {
const size = std.heap.pageSize();
// Check that pageSize is a power of 2.
std.debug.assert(size == (1 << @ctz(size));
std.debug.assert(size == (1 << @ctz(size)));
}

0 comments on commit c98e58c

Please sign in to comment.