1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
protocol PoolElement {
init()
func evictedFromPool()
}
class Pool<Element: PoolElement> {
private let maxSize: Int
private var elements: [Element]
init(maxSize: Int) {
self.maxSize = maxSize
self.elements = [Element]()
}
deinit {
for e in elements {
e.evictedFromPool()
}
}
func get() -> Element {
if elements.isEmpty {
return Element()
}
else {
return elements.removeLast()
}
}
func put(_ e: Element) {
if (elements.count == maxSize) {
e.evictedFromPool()
}
else {
elements.append(e)
}
}
}
/// A ``PooledBuffer`` is used to track an allocation of memory required
/// by a `Channel` or `EventLoopGroup`.
///
/// ``PooledBuffer`` is a reference type with inline storage. It is intended to
/// be bound to a single thread, and ensures that the allocation it stores does not
/// get freed before the buffer is out of use.
struct PooledBuffer: PoolElement {
private static let sentinelValue = MemorySentinel(0xdeadbeef)
private let storage: BackingStorage
init() {
self.storage = .create(iovectorCount: Socket.writevLimitIOVectors)
self.configureSentinel()
}
func evictedFromPool() {
self.validateSentinel()
}
func withUnsafePointers<ReturnValue>(
_ body: (UnsafeMutableBufferPointer<IOVector>, UnsafeMutableBufferPointer<Unmanaged<AnyObject>>) throws -> ReturnValue
) rethrows -> ReturnValue {
defer {
self.validateSentinel()
}
return try self.storage.withUnsafeMutableTypedPointers { iovecPointer, ownerPointer, _ in
try body(iovecPointer, ownerPointer)
}
}
/// Yields buffer pointers containing this ``PooledBuffer``'s readable bytes. You may hold a pointer to those bytes
/// even after the closure has returned iff you model the lifetime of those bytes correctly using the `Unmanaged`
/// instance. If you don't require the pointer after the closure returns, use ``withUnsafePointers``.
///
/// If you escape the pointer from the closure, you _must_ call `storageManagement.retain()` to get ownership to
/// the bytes and you also must call `storageManagement.release()` if you no longer require those bytes. Calls to
/// `retain` and `release` must be balanced.
///
/// - parameters:
/// - body: The closure that will accept the yielded pointers and the `storageManagement`.
/// - returns: The value returned by `body`.
func withUnsafePointersWithStorageManagement<ReturnValue>(
_ body: (UnsafeMutableBufferPointer<IOVector>, UnsafeMutableBufferPointer<Unmanaged<AnyObject>>, Unmanaged<AnyObject>) throws -> ReturnValue
) rethrows -> ReturnValue {
let storageRef: Unmanaged<AnyObject> = Unmanaged.passUnretained(self.storage)
return try self.storage.withUnsafeMutableTypedPointers { iovecPointer, ownerPointer, _ in
try body(iovecPointer, ownerPointer, storageRef)
}
}
private func configureSentinel() {
self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
sentinelPointer.pointee = Self.sentinelValue
}
}
private func validateSentinel() {
self.storage.withUnsafeMutableTypedPointers { _, _, sentinelPointer in
precondition(sentinelPointer.pointee == Self.sentinelValue, "Detected memory handling error!")
}
}
}
extension PooledBuffer {
fileprivate typealias MemorySentinel = UInt32
fileprivate struct PooledBufferHead {
let iovectorCount: Int
let spaceForIOVectors: Int
let spaceForBufferOwners: Int
init(iovectorCount: Int) {
var spaceForIOVectors = MemoryLayout<IOVector>.stride * iovectorCount
spaceForIOVectors.roundUpToAlignment(for: Unmanaged<AnyObject>.self)
var spaceForBufferOwners = MemoryLayout<Unmanaged<AnyObject>>.stride * iovectorCount
spaceForBufferOwners.roundUpToAlignment(for: MemorySentinel.self)
self.iovectorCount = iovectorCount
self.spaceForIOVectors = spaceForIOVectors
self.spaceForBufferOwners = spaceForBufferOwners
}
var totalByteCount: Int {
self.spaceForIOVectors + self.spaceForBufferOwners + MemoryLayout<MemorySentinel>.size
}
var iovectorOffset: Int {
0
}
var bufferOwnersOffset: Int {
self.spaceForIOVectors
}
var memorySentinelOffset: Int {
self.spaceForIOVectors + self.spaceForBufferOwners
}
}
fileprivate final class BackingStorage: ManagedBuffer<PooledBufferHead, UInt8> {
static func create(iovectorCount: Int) -> Self {
let head = PooledBufferHead(iovectorCount: iovectorCount)
let baseStorage = Self.create(minimumCapacity: head.totalByteCount) { _ in
head
}
// Here we set up our memory bindings.
let storage = unsafeDowncast(baseStorage, to: Self.self)
storage.withUnsafeMutablePointers { headPointer, tailPointer in
UnsafeRawPointer(tailPointer + headPointer.pointee.iovectorOffset).bindMemory(to: IOVector.self, capacity: iovectorCount)
UnsafeRawPointer(tailPointer + headPointer.pointee.bufferOwnersOffset).bindMemory(to: Unmanaged<AnyObject>.self, capacity: iovectorCount)
UnsafeRawPointer(tailPointer + headPointer.pointee.memorySentinelOffset).bindMemory(to: MemorySentinel.self, capacity: 1)
}
return storage
}
func withUnsafeMutableTypedPointers<ReturnType>(
_ body: (UnsafeMutableBufferPointer<IOVector>, UnsafeMutableBufferPointer<Unmanaged<AnyObject>>, UnsafeMutablePointer<MemorySentinel>) throws -> ReturnType
) rethrows -> ReturnType {
return try self.withUnsafeMutablePointers { headPointer, tailPointer in
let iovecPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.iovectorOffset).assumingMemoryBound(to: IOVector.self)
let ownersPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.bufferOwnersOffset).assumingMemoryBound(to: Unmanaged<AnyObject>.self)
let sentinelPointer = UnsafeMutableRawPointer(tailPointer + headPointer.pointee.memorySentinelOffset).assumingMemoryBound(to: MemorySentinel.self)
let iovecBufferPointer = UnsafeMutableBufferPointer(
start: iovecPointer, count: headPointer.pointee.iovectorCount
)
let ownersBufferPointer = UnsafeMutableBufferPointer(
start: ownersPointer, count: headPointer.pointee.iovectorCount
)
return try body(iovecBufferPointer, ownersBufferPointer, sentinelPointer)
}
}
}
}
extension Int {
fileprivate mutating func roundUpToAlignment<Type>(for: Type.Type) {
// Alignment is always positive, we can use unchecked subtraction here.
let alignmentGuide = MemoryLayout<Type>.alignment &- 1
// But we can't use unchecked addition.
self = (self + alignmentGuide) & (~alignmentGuide)
}
}
struct PooledMsgBuffer: PoolElement {
private typealias MemorySentinel = UInt32
private static let sentinelValue = MemorySentinel(0xdeadbeef)
private struct PooledMsgBufferHead {
let count: Int
let spaceForMsgHdrs: Int
let spaceForAddresses: Int
let spaceForControlData: Int
init(count: Int) {
var spaceForMsgHdrs = MemoryLayout<MMsgHdr>.stride * count
spaceForMsgHdrs.roundUpToAlignment(for: sockaddr_storage.self)
var spaceForAddress = MemoryLayout<sockaddr_storage>.stride * count
spaceForAddress.roundUpToAlignment(for: MemorySentinel.self)
var spaceForControlData = (UnsafeControlMessageStorage.bytesPerMessage * count)
spaceForControlData.roundUpToAlignment(for: cmsghdr.self)
self.count = count
self.spaceForMsgHdrs = spaceForMsgHdrs
self.spaceForAddresses = spaceForAddress
self.spaceForControlData = spaceForControlData
}
var totalByteCount: Int {
self.spaceForMsgHdrs + self.spaceForAddresses + self.spaceForControlData + MemoryLayout<MemorySentinel>.size
}
var msgHdrsOffset: Int {
0
}
var addressesOffset: Int {
self.spaceForMsgHdrs
}
var controlDataOffset: Int {
self.spaceForMsgHdrs + self.spaceForAddresses
}
var memorySentinelOffset: Int {
return self.spaceForMsgHdrs + self.spaceForAddresses + self.spaceForControlData
}
}
private class BackingStorage: ManagedBuffer<PooledMsgBufferHead, UInt8> {
static func create(count: Int) -> Self {
let head = PooledMsgBufferHead(count: count)
let baseStorage = Self.create(minimumCapacity: head.totalByteCount) { _ in
head
}
let storage = unsafeDowncast(baseStorage, to: Self.self)
storage.withUnsafeMutablePointers { headPointer, tailPointer in
UnsafeRawPointer(tailPointer + headPointer.pointee.msgHdrsOffset).bindMemory(to: MMsgHdr.self, capacity: count)
UnsafeRawPointer(tailPointer + headPointer.pointee.addressesOffset).bindMemory(to: sockaddr_storage.self, capacity: count)
// space for control message data not needed to be bound
UnsafeRawPointer(tailPointer + headPointer.pointee.memorySentinelOffset).bindMemory(to: MemorySentinel.self, capacity: 1)
}
return storage
}
func withUnsafeMutableTypedPointers<ReturnType>(
_ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>, UnsafeControlMessageStorage, 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 controlDataPointer = UnsafeMutableRawBufferPointer(start: tailPointer + headPointer.pointee.controlDataOffset, count: headPointer.pointee.spaceForControlData)
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
)
let controlMessageStorage = UnsafeControlMessageStorage.makeNotOwning(
bytesPerMessage: UnsafeControlMessageStorage.bytesPerMessage,
buffer: controlDataPointer)
return try body(msgHdrsBufferPointer, addressesBufferPointer, controlMessageStorage, 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>, UnsafeControlMessageStorage) throws -> ReturnValue
) rethrows -> ReturnValue {
defer {
self.validateSentinel()
}
return try self.storage.withUnsafeMutableTypedPointers { msgs, addresses, controlMessageStorage, _ in
return try body(msgs, addresses, controlMessageStorage)
}
}
func withUnsafePointersWithStorageManagement<ReturnValue>(
_ body: (UnsafeMutableBufferPointer<MMsgHdr>, UnsafeMutableBufferPointer<sockaddr_storage>, UnsafeControlMessageStorage, Unmanaged<AnyObject>) throws -> ReturnValue
) rethrows -> ReturnValue {
let storageRef: Unmanaged<AnyObject> = Unmanaged.passUnretained(self.storage)
return try self.storage.withUnsafeMutableTypedPointers { msgs, addresses, controlMessageStorage, _ in
try body(msgs, addresses, controlMessageStorage, storageRef)
}
}
}
|