egor10_4
egor10_4

Reputation: 402

How to use `proc_pid_rusage` result in Swift using UnsafeMutablePointer?

I tried this code to get result from proc_pid_rusage:

import Foundation

let p1 = UnsafeMutablePointer<rusage_info_t?>.allocate(capacity: 1)

let spid = ProcessInfo.processInfo.processIdentifier
let status = proc_pid_rusage(spid, 0, p1)
if status == KERN_SUCCESS {
    // ...
    let umrp : rusage_info_t = p1.pointee!
    let a = umrp.load(as:rusage_info_v0.self)
    print("User time \(a.ri_user_time)")
}

print("Status \(status)")

rusage_info_t, as Xcode says, is defined as:

public typealias rusage_info_t = UnsafeMutableRawPointer

I assume that it should point to a struct rusage_info_v0 as I specified flavor = 0. But I get this runtime error:

Swift/UnsafeRawPointer.swift:1203: Fatal error: load from misaligned raw pointer
Trace/BPT trap: 5

I don't understand what alignment should be and how should I specify that.

Upvotes: 1

Views: 187

Answers (0)

Related Questions