diff --git a/Sources/NIO/IntegerTypes.swift b/Sources/NIO/IntegerTypes.swift index 2915ae52c..e0cf571b9 100644 --- a/Sources/NIO/IntegerTypes.swift +++ b/Sources/NIO/IntegerTypes.swift @@ -75,7 +75,8 @@ struct _UInt56 { static let bitWidth: Int = 56 - static let max: _UInt56 = .init((1 << 56) - 1) + private static let initializeUInt64 : UInt64 = (1 << 56) - 1 + static let max: _UInt56 = .init(initializeUInt64) static let min: _UInt56 = .init(0) } diff --git a/Sources/NIO/RecvByteBufferAllocator.swift b/Sources/NIO/RecvByteBufferAllocator.swift index b1a682c2a..698687b5a 100644 --- a/Sources/NIO/RecvByteBufferAllocator.swift +++ b/Sources/NIO/RecvByteBufferAllocator.swift @@ -55,7 +55,7 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator { private var nextReceiveBufferSize: Int private var decreaseNow: Bool - private static let maximumAllocationSize = 1 << 31 + private static let maximumAllocationSize = 1 << 30 public init() { self.init(minimum: 64, initial: 2048, maximum: 65536) @@ -93,7 +93,7 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator { // Here we need to be careful with 32-bit systems: if maximum is too large then any shift or multiply will overflow, which // we don't want. Instead we check, and clamp to this current value if we overflow. let upperBoundCandidate = Int(truncatingIfNeeded: Int64(self.nextReceiveBufferSize) &<< 1) - let upperBound = upperBoundCandidate == 0 ? self.nextReceiveBufferSize : upperBoundCandidate + let upperBound = upperBoundCandidate <= 0 ? self.nextReceiveBufferSize : upperBoundCandidate if actualReadBytes <= lowerBound && lowerBound >= self.minimum { if self.decreaseNow { @@ -102,7 +102,8 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator { } else { self.decreaseNow = true } - } else if actualReadBytes >= self.nextReceiveBufferSize && upperBound <= self.maximum { + } else if actualReadBytes >= self.nextReceiveBufferSize && upperBound <= self.maximum && + self.nextReceiveBufferSize != upperBound { self.nextReceiveBufferSize = upperBound self.decreaseNow = false mayGrow = true diff --git a/Tests/NIOTests/RecvByteBufAllocatorTest.swift b/Tests/NIOTests/RecvByteBufAllocatorTest.swift index dfc84d343..3e03eb4ca 100644 --- a/Tests/NIOTests/RecvByteBufAllocatorTest.swift +++ b/Tests/NIOTests/RecvByteBufAllocatorTest.swift @@ -117,8 +117,8 @@ final class AdaptiveRecvByteBufferAllocatorTest: XCTestCase { } let buffer = self.adaptive.buffer(allocator: self.allocator) - XCTAssertEqual(buffer.capacity, 1 << 31) - XCTAssertEqual(self.adaptive.maximum, 1 << 31) + XCTAssertEqual(buffer.capacity, 1 << 30) + XCTAssertEqual(self.adaptive.maximum, 1 << 30) XCTAssertEqual(self.adaptive.minimum, 0) } @@ -136,8 +136,8 @@ final class AdaptiveRecvByteBufferAllocatorTest: XCTestCase { } let adaptive = AdaptiveRecvByteBufferAllocator(minimum: targetValue, initial: targetValue + 1, maximum: targetValue + 2) - XCTAssertEqual(adaptive.minimum, 1 << 31) - XCTAssertEqual(adaptive.maximum, 1 << 31) - XCTAssertEqual(adaptive.initial, 1 << 31) + XCTAssertEqual(adaptive.minimum, 1 << 30) + XCTAssertEqual(adaptive.maximum, 1 << 30) + XCTAssertEqual(adaptive.initial, 1 << 30) } } diff --git a/Tests/NIOTests/SystemTest.swift b/Tests/NIOTests/SystemTest.swift index 176ca208a..e71b3a62d 100644 --- a/Tests/NIOTests/SystemTest.swift +++ b/Tests/NIOTests/SystemTest.swift @@ -62,6 +62,21 @@ class SystemTest: XCTestCase { private static let cmsghdr_secondDataCount = 1 private static let cmsghdr_firstType = IP_RECVDSTADDR private static let cmsghdr_secondType = IP_RECVTOS + #elseif os(Android) && arch(arm) + private static let cmsghdrExample: [UInt8] = [0x10, 0x00, 0x00, 0x00, // Length 16 including header + 0x00, 0x00, 0x00, 0x00, // IPPROTO_IP + 0x08, 0x00, 0x00, 0x00, // IP_PKTINFO + 0x7F, 0x00, 0x00, 0x01, // 127.0.0.1 + 0x0D, 0x00, 0x00, 0x00, // Length 13 including header + 0x00, 0x00, 0x00, 0x00, // IPPROTO_IP + 0x01, 0x00, 0x00, 0x00, // IP_TOS + 0x01, 0x00, 0x00, 0x00] // ECT-1 (1 byte) + private static let cmsghdr_secondStartPosition = 16 + private static let cmsghdr_firstDataStart = 12 + private static let cmsghdr_firstDataCount = 4 + private static let cmsghdr_secondDataCount = 1 + private static let cmsghdr_firstType = IP_PKTINFO + private static let cmsghdr_secondType = IP_TOS #elseif os(Linux) || os(Android) // Example twin data options captured on Linux private static let cmsghdrExample: [UInt8] = [