有关IOS内存读写冲突

有关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就不会有内存访问冲突

posted @ 2023-04-05 14:16  R1cardo  阅读(107)  评论(0编辑  收藏  举报