Skip to content

add macOS handling for totalSystemMemory #24903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

imomaliev
Copy link

@imomaliev imomaliev commented Aug 18, 2025

Hi, I just started learing zig. I was trying to fetch total system memory on macOS and noticed that totalSystemMemory does not support it.

This PR adds it.

I believe there are a few issues with totalSystemMemory for .freebsd and std.posix.sysctlbynameZ, but I do not know what the correct approach to take here

  1. totalSystemMemory for .freebsd returns usize instead of u64, I believe this should be fixed
  2. totalSystemMemory for .freebsd does not handle all the errors from std.posix.sysctlbynameZ.
  3. Last but not least, std.posix.sysctlbynameZ never returns NameTooLong. So either we should change the return type, or, as @andrewrk did in 3640303 we should add name_len handling. I did not find any mention of ENAMETOOLONG in manpages for freebsd/posix. So I am having a hard time understanding which way is correct here.

Just for the context I know that there were discussions in #16350 that resulted in the rollback of patches that already implemented this functionality before. Please let me know what should be done to meet contributing standards to avoid same fate as previous contributor 🙂

I am ready to provide patches for 1 and 2 in this PR or create separate one

Thanks!

@alexrp alexrp self-assigned this Aug 18, 2025
@imomaliev imomaliev force-pushed the feat/totalsystemmemory-macos branch from 41a5088 to 9492937 Compare August 18, 2025 18:20
@imomaliev imomaliev changed the title add macOS switch for totalSystemMemory add macOS handling for totalSystemMemory Aug 18, 2025
@alexrp
Copy link
Member

alexrp commented Aug 18, 2025

totalSystemMemory for .freebsd returns usize instead of u64, I believe this should be fixed

u64 looks right to me.

totalSystemMemory for .freebsd does not handle all the errors from std.posix.sysctlbynameZ.

This doesn't matter too much since I'll get to it when I do #6389 anyway. But yes, it probably should.

Last but not least, std.posix.sysctlbynameZ never returns NameTooLong. So either we should change the return type, or, as @andrewrk did in 3640303 we should add name_len handling. I did not find any mention of ENAMETOOLONG in manpages for freebsd/posix. So I am having a hard time understanding which way is correct here.

I think just remove the dead NameTooLong prong.

@imomaliev
Copy link
Author

@alexrp Thanks. Fixed 1, but decided to fix 3 in separate PR #24909

@@ -1762,7 +1762,19 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
error.NameTooLong, error.UnknownName => unreachable,
else => return error.UnknownTotalSystemMemory,
};
return @as(usize, @intCast(physmem));
return @as(u64, @intCast(physmem));
Copy link
Member

@alexrp alexrp Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of physmem should be changed to u64, making the cast unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants