Wednesday, 15 April 2015

ios - C array memory deallocation from swift -



ios - C array memory deallocation from swift -

var cpuinfo: processor_info_array_t = nil var numcpuinfo: mach_msg_type_number_t = 0 var corestotalusage: float = 0.0 var numcpusu: natural_t = 0 allow err = host_processor_info(mach_host_self(), processor_cpu_load_info, &numcpusu, &cpuinfo, &numcpuinfo); assert(err == kern_success, "failed phone call host_processor_info")

hi, calling above c api host_processor_info process load informations swift, no problem there. cpuinfo inout parameter (pointer) that, on return, point construction containing cpu info allocated api. caller reponsible deallocating memory; can objective c haven't had luck in swift. know wrap phone call objective c extension i'm trying larn swift , like, if possible, avoid obj-c solution.

in obj-c deallocate with:

size_t cpuinfosize = sizeof(integer_t) * numcpuinfo; vm_deallocate(mach_task_self(), (vm_address_t) cpuinfo, cpuinfosize)

cpuinfo in swift unsafemutablepointer not convertible vm_address_t.

any help appreciated, thanks.

processor_info_array_t pointer type, , vm_address_t integer type (ultimately alias uint). (judging comments in <i386/vm_types.h> might historical reasons.) way convert pointer integer (of same size) in swift unsafebitcast.

mach_init.h defines

extern mach_port_t mach_task_self_; #define mach_task_self() mach_task_self_

only extern variable visible in swift, not macro.

this gives:

let cpuinfosize = vm_size_t(sizeof(integer_t)) * vm_size_t(numcpuinfo) vm_deallocate(mach_task_self_, unsafebitcast(cpuinfo, vm_address_t.self), cpuinfosize)

ios osx swift interop xcode6

No comments:

Post a Comment