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 Jul 15, 2024
1 parent 5e62882 commit 2c61cfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 16 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 All @@ -109,13 +113,17 @@ fn queryPageSize() usize {
}
switch (builtin.os.tag) {
.linux => size = if (builtin.link_libc) @intCast(std.c.sysconf(std.c._SC.PAGESIZE)) else std.os.linux.getauxval(std.elf.AT_PAGESZ),
.macos => blk: { size = @import("c/darwin.zig").machTaskForSelf().getPageSize() catch break :blk; },
.macos => blk: {
size = @import("c/darwin.zig").machTaskForSelf().getPageSize() catch break :blk;
},
.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

0 comments on commit 2c61cfe

Please sign in to comment.