data fields for proc_getallinfo struct

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)")
                        }
                   }
              }
         }
    }
                    

I’m not sure what’s going on with your code but I’m able to create a proc_taskallinfo structure just fine:

  1. Using Xcode 15.3, I created a new project from the macOS > Command Line Tool template.

  2. I replaced main.swift with this code:

    import Foundation
    
    func main() {
        let info = proc_taskallinfo()
        print(info)
    }
    
    main()
    
  3. It builds and runs just fine.

ps Don’t do this:

@_silgen_name("kernMem")

I’m not sure why you’re doing it, but it’s an exercise fraught with much danger. If you want to call C code from Swift, use a bridging header (in an executable target) or a separate module (in a library target). Alternatively, you can call the libproc routines directly from Swift, which is what I do.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

data fields for proc_getallinfo struct
 
 
Q