Skip to content

Commit

Permalink
(127135444) Add ProcessInfo implementations for other platforms (#559)
Browse files Browse the repository at this point in the history
* Add ProcessInfo implementations for other platforms

* Update Platform.getHostname for Windows

Co-authored-by: Saleem Abdulrasool <[email protected]>

* Add alternate platform implementations of ProcessInfo tests

* Clean up windows switch statement

* Use nonzeroBitCount on Windows

* Add Linux implementation for retrieving active processor count

* Apply Windows additions from code review

Co-authored-by: Saleem Abdulrasool <[email protected]>

* Apply additional Windows suggestions

* TotalMemoryKB -> totalMemoryKB

* Fix conditional os directive

* Update process name

---------

Co-authored-by: Saleem Abdulrasool <[email protected]>
  • Loading branch information
jmschonfeld and compnerd committed Apr 29, 2024
1 parent aabac27 commit ad9d6d4
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 50 deletions.
19 changes: 18 additions & 1 deletion Sources/FoundationEssentials/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,29 @@ extension Platform {
extension Platform {
#if !FOUNDATION_FRAMEWORK
static func getHostname() -> String {
#if os(Windows)
var dwLength: DWORD = 0
guard GetComputerNameExW(ComputerNameDnsHostname, nil, &dwLength) == ERROR_MORE_DATA else {
// FIXME: should we log an error?
return "localhost"
}
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
dwLength -= 1 // null-terminator reservation
guard GetComputerNameExW(ComputerNameDnsHostname, $0.baseAddress!, &dwLength) else {
return "localhost"
}
return String(decodingCString: $0.baseAddress, as: UTF16.self)
}
#elseif os(WASI) // WASI does not have uname
return "localhost"
#else
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: Platform.MAX_HOSTNAME_LENGTH + 1) {
guard gethostname($0.baseAddress!, numericCast(Platform.MAX_HOSTNAME_LENGTH)) == 0 else {
return ""
return "localhost"
}
return String(cString: $0.baseAddress!)
}
#endif
}
#endif // !FOUNDATION_FRAMEWORK
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/FoundationEssentials/ProcessInfo/ProcessInfo+API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public struct OperatingSystemVersion: Hashable, Codable, Sendable {
public let majorVersion: Int
public let minorVersion: Int
public let patchVersion: Int

public init() {
self.init(majorVersion: 0, minorVersion: 0, patchVersion: 0)
}

public init(majorVersion: Int, minorVersion: Int, patchVersion: Int) {
self.majorVersion = majorVersion
self.minorVersion = minorVersion
self.patchVersion = patchVersion
}
}

// MARK: - Getting Computer Information
Expand Down

0 comments on commit ad9d6d4

Please sign in to comment.