有关IOS内存读写冲突

1|0有关IOS内存读写冲突

在写内存相关代码时,获取已使用内存代码中报错

let hostPort: mach_port_t = mach_host_self() var host_size = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.stride / MemoryLayout<integer_t>.stride) var pagesize: vm_size_t = 0 host_page_size(hostPort, &pagesize) var vmStat1: vm_statistics = vm_statistics_data_t() let status: kern_return_t = withUnsafeMutableBytes(of: &vmStat1) { let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat1) / MemoryLayout<Int32>.stride) return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size) } if status == KERN_SUCCESS { let usedMemory = Int64(vm_size_t(vmStat1.active_count + vmStat1.inactive_count + vmStat1.wire_count) * pagesize) return usedMemory } else { print("Failed to get Virtual memory inforor") return 0 }

这一段代码报错

let status: kern_return_t = withUnsafeMutableBytes(of: &vmStat1) { let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat1) / MemoryLayout<Int32>.stride) return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size) }

WX20230331-091708_2x

Overlapping accesses to 'vmStat1', but modification requires exclusive access; consider copying to a local variable

对“vmStat1”的重叠访问,但修改需要独占访问权限;考虑复制到局部变量

看了一下

withUnsafeMutableBytes(of: &vmStat1)这里是用了inout关键字

@inlinable public func withUnsafeMutableBytes<T, Result>(of value: inout T, _ body: (UnsafeMutableRawBufferPointer) throws -> Result) rethrows -> Result

相当于将vmStat1的指针传了进去,拿到了vmStat1的写权限

而下一段代码

let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat1) / MemoryLayout<Int32>.stride)又拿到了vmStat1的读权限

而内存访问冲突:对同一块内存,同时进行读写操作,或者同时进行多个写入操作时,就会造成内存访问冲突。

根据编译器建议,可以拷贝给一个局部变量vmStat

也就是

var vmStat = vmStat1 let status: kern_return_t = withUnsafeMutableBytes(of: &vmStat) { let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat1) / MemoryLayout<Int32>.stride) return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size) } vmStat1 = vmStat

将这个局部变量的指针传进去做修改,修改完之后在将其值赋回给vmStat1

这样就不会同时对vmStat1进行读写操作导致内存冲突

withUnsafeMutableBytes这个方法中的注释中也有写到

WX20230331-093023_2x

body这个闭包持续时间中,也就是

{ let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat1) / MemoryLayout<Int32>.stride) return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size) }

这一段代码,是无法直接访问到value也就是上面的vmStat1变量的,只有在这个闭包return之后才能访问,所以将vmStat1拷贝到vmStatvalue现在是vmStat,在闭包中访问vmStat1就不会有内存访问冲突


__EOF__

本文作者R1cardo
本文链接https://www.cnblogs.com/r1cardo/p/17289372.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   R1cardo  阅读(130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示