I have some c code that returns memory usage of a current task on my machine and recently redacted it to use the proc_getallinfio struct so I can instead retrieve systemwide memory usage. im calling that code in swift however im getting the error "Initializer 'init(_:)' requires that 'proc_taskallinfo' conform to 'BinaryInteger'" and im not sure what the appropriate field is to pass that works with proc_getallinfo struct. resident_size does not work in this context.
import IOKit
import Foundation
@_silgen_name("kernMem")
func kernMem(storeMemData: UnsafeMutablePointer <proc_taskallinfo>) -> kern_return_t
@main
struct MacStatAppApp: App {
@State public var printMemory: String = "" //dynamic state object to store data that will be passed to swiftUI
var body: some Scene {
WindowGroup {
ContentView(printMemory: $printMemory) //binding for printMemory to pass data to contentview
.onAppear {
var storeMemData = proc_taskallinfo() //define pointer
let result = kernMem(storeMemData: &storeMemData)
if result == KERN_SUCCESS {
let memoryUsage = Double(storeMemData) / (1024.0 * 1024.0 * 1024.0) //conversion for GB, 1024 to the power of 3
print(String(format: "memory usage: %.2f GB", memoryUsage))
} else {
print("failed to obtain memory usage data:\(result)")
}
}
}
}
}