Repository: apple/swift-nio
File: Sources/NIOPosix/Pool.swift (Swift)

Code before:
            _ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>, UnsafeMutablePointer<MemorySentinel>) throws -> ReturnType
        ) rethrows -> ReturnType {
            return try self.withUnsafeMutablePointers { headPointer, tailPointer in
                let msgHdrsPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.msgHdrsOffset).assumingMemoryBound(to: MMsgHdr.self)
                let addressesPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.addressesOffset).assumingMemoryBound(to: sockaddr_storage.self)
                let sentinelPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.memorySentinelOffset).assumingMemoryBound(to: MemorySentinel.self)

                let msgHdrsBufferPointer = UnsafeMutableBufferPointer(
                    start: msgHdrsPointer, count: headPointer.pointee.count
                )
                let addressesBufferPointer = UnsafeMutableBufferPointer(
                    start: addressesPointer, count: headPointer.pointee.count
                )
                return try body(msgHdrsBufferPointer, addressesBufferPointer, sentinelPointer)
            }
        }
    }

    private func validateSentinel() {
        self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
            precondition(sentinelPointer.pointee == Self.sentinelValue, "Detected memory handling error!")
        }
    }

    private var storage: BackingStorage
    public var controlMessageStorage: UnsafeControlMessageStorage

    init() {
        self.storage = .create(count: Socket.writevLimitIOVectors)
        self.controlMessageStorage = UnsafeControlMessageStorage.allocate(msghdrCount: Socket.writevLimitIOVectors)

        self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
            sentinelPointer.pointee = Self.sentinelValue
        }
    }

    deinit {
        self.controlMessageStorage.deallocate()
    }

    func evictedFromPool() {
        self.validateSentinel()
    }

    func withUnsafePointers<ReturnValue>(
        _ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>) throws -> ReturnValue
    ) rethrows -> ReturnValue {
        defer {
            self.validateSentinel()
        }
        return try self.storage.withUnsafeMutableTypedPointers { msgs, addresses, _ in

Review comment (refactor):
I think this thing is why this is currently a class. Presumably we're going to have to pool this too, so my gut instinct is that we should omit the change to this right now, and have a separate change to clean up the lifecycle management and move this to a pool.

Code after:
            _ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>, UnsafeMutablePointer<MemorySentinel>) throws -> ReturnType
        ) rethrows -> ReturnType {
            return try self.withUnsafeMutablePointers { headPointer, tailPointer in
                let msgHdrsPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.msgHdrsOffset).assumingMemoryBound(to: MMsgHdr.self)
                let addressesPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.addressesOffset).assumingMemoryBound(to: sockaddr_storage.self)
                let sentinelPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.memorySentinelOffset).assumingMemoryBound(to: MemorySentinel.self)

                let msgHdrsBufferPointer = UnsafeMutableBufferPointer(
                    start: msgHdrsPointer, count: headPointer.pointee.count
                )
                let addressesBufferPointer = UnsafeMutableBufferPointer(
                    start: addressesPointer, count: headPointer.pointee.count
                )
                return try body(msgHdrsBufferPointer, addressesBufferPointer, sentinelPointer)
            }
        }
    }

    private func validateSentinel() {
        self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
            precondition(sentinelPointer.pointee == Self.sentinelValue, "Detected memory handling error!")
        }
    }

    private var storage: BackingStorage

    init() {
        self.storage = .create(count: Socket.writevLimitIOVectors)
        self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
            sentinelPointer.pointee = Self.sentinelValue
        }
    }

    func evictedFromPool() {
        self.validateSentinel()
    }

    func withUnsafePointers<ReturnValue>(
        _ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>) throws -> ReturnValue
    ) rethrows -> ReturnValue {
        defer {
            self.validateSentinel()
        }
        return try self.storage.withUnsafeMutableTypedPointers { msgs, addresses, _ in
            return try body(msgs, addresses)
        }
    }

    func withUnsafePointersWithStorageManagement<ReturnValue>(
            _ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>, Unmanaged<AnyObject>) throws -> ReturnValue
    ) rethrows -> ReturnValue {