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

Code before:
        var byteCount = self.bufferSize
        debugOnly {
            byteCount += MemoryLayout<UInt32>.stride
        }
        self.buffer = UnsafeMutableRawPointer.allocate(byteCount: byteCount, alignment: MemoryLayout<IOVector>.alignment)
        debugOnly {
            self.buffer.storeBytes(of: 0xdeadbee, toByteOffset: self.bufferSize, as: UInt32.self)
        }
    }

    func evictedFromPool() {
        debugOnly {
            assert(0xdeadbee == self.buffer.load(fromByteOffset: self.bufferSize, as: UInt32.self))
        }
        self.buffer.deallocate()
    }

    func get() -> (UnsafeMutableBufferPointer<IOVector>, UnsafeMutableBufferPointer<Unmanaged<AnyObject>>) {
        let count = Socket.writevLimitIOVectors
        let iovecs = self.buffer.bindMemory(to: IOVector.self, capacity: count)
        let storageRefs = (self.buffer + (count * MemoryLayout<IOVector>.stride)).bindMemory(to: Unmanaged<AnyObject>.self, capacity: count)
        assert(iovecs >= self.buffer.bindMemory(to: IOVector.self, capacity: count))
        assert(iovecs <= (self.buffer + self.bufferSize).bindMemory(to: IOVector.self, capacity: count))
        assert(storageRefs >= self.buffer.bindMemory(to: Unmanaged<AnyObject>.self, capacity: count))
        assert(storageRefs <= (self.buffer + self.bufferSize).bindMemory(to: Unmanaged<AnyObject>.self, capacity: count))
        assert(UnsafeMutableRawPointer(iovecs + count) == UnsafeMutableRawPointer(storageRefs))
        assert(UnsafeMutableRawPointer(storageRefs + count) <= (self.buffer + bufferSize))
        return (UnsafeMutableBufferPointer(start: iovecs, count: count), UnsafeMutableBufferPointer(start: storageRefs, count: count))
    }
}

/// `EventLoop` implementation that uses a `Selector` to get notified once there is more I/O or tasks to process.
/// The whole processing of I/O and tasks is done by a `NIOThread` that is tied to the `SelectableEventLoop`. This `NIOThread`
/// is guaranteed to never change!
@usableFromInline
internal final class SelectableEventLoop: EventLoop {

    static let strictModeEnabled: Bool = {
        switch getenv("SWIFTNIO_STRICT").map({ String.init(cString: $0).lowercased() }) {
        case "true", "y", "yes", "on", "1":
            return true
        default:
            return false
        }
    }()

    /// The different state in the lifecycle of an `EventLoop` seen from _outside_ the `EventLoop`.
    private enum ExternalState {
        /// `EventLoop` is open and so can process more work.
        case open
        /// `EventLoop` is currently in the process of closing.

Review comment (refactor):
Don't bind the memory in these assertions, the duplicate bind makes it hard to reason about. Erase the pointers to raw instead.

Code after:
        var byteCount = self.bufferSize
        debugOnly {
            byteCount += MemoryLayout<UInt32>.stride
        }
        self.buffer = UnsafeMutableRawPointer.allocate(byteCount: byteCount, alignment: MemoryLayout<IOVector>.alignment)
        debugOnly {
            self.buffer.storeBytes(of: 0xdeadbee, toByteOffset: self.bufferSize, as: UInt32.self)
        }
    }

    func evictedFromPool() {
        debugOnly {
            assert(0xdeadbee == self.buffer.load(fromByteOffset: self.bufferSize, as: UInt32.self))
        }
        self.buffer.deallocate()
    }

    func get() -> (UnsafeMutableBufferPointer<IOVector>, UnsafeMutableBufferPointer<Unmanaged<AnyObject>>) {
        let count = Socket.writevLimitIOVectors
        let iovecs = self.buffer.bindMemory(to: IOVector.self, capacity: count)
        let storageRefs = (self.buffer + (count * MemoryLayout<IOVector>.stride)).bindMemory(to: Unmanaged<AnyObject>.self, capacity: count)
        assert(UnsafeMutableRawPointer(iovecs) >= self.buffer)
        assert(UnsafeMutableRawPointer(iovecs) <= (self.buffer + self.bufferSize))
        assert(UnsafeMutableRawPointer(storageRefs) >= self.buffer)
        assert(UnsafeMutableRawPointer(storageRefs) <= (self.buffer + self.bufferSize))
        assert(UnsafeMutableRawPointer(iovecs + count) == UnsafeMutableRawPointer(storageRefs))
        assert(UnsafeMutableRawPointer(storageRefs + count) <= (self.buffer + bufferSize))
        return (UnsafeMutableBufferPointer(start: iovecs, count: count), UnsafeMutableBufferPointer(start: storageRefs, count: count))
    }
}

/// `EventLoop` implementation that uses a `Selector` to get notified once there is more I/O or tasks to process.
/// The whole processing of I/O and tasks is done by a `NIOThread` that is tied to the `SelectableEventLoop`. This `NIOThread`
/// is guaranteed to never change!
@usableFromInline
internal final class SelectableEventLoop: EventLoop {

    static let strictModeEnabled: Bool = {
        switch getenv("SWIFTNIO_STRICT").map({ String.init(cString: $0).lowercased() }) {
        case "true", "y", "yes", "on", "1":
            return true
        default:
            return false
        }
    }()

    /// The different state in the lifecycle of an `EventLoop` seen from _outside_ the `EventLoop`.
    private enum ExternalState {
        /// `EventLoop` is open and so can process more work.
        case open
        /// `EventLoop` is currently in the process of closing.