Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Sources/NIO/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
//
//===----------------------------------------------------------------------===//

let sysMalloc = malloc
let sysRealloc = realloc
let sysFree = free
let sysMalloc: @convention(c) (Int) -> UnsafeMutableRawPointer? = malloc
let sysRealloc: @convention(c) (UnsafeMutableRawPointer?, Int) -> UnsafeMutableRawPointer? = realloc
let sysFree: @convention(c) (UnsafeMutableRawPointer?) -> Void = free

#if !swift(>=4.1)
public extension UnsafeMutableRawPointer {
Expand Down Expand Up @@ -45,9 +45,9 @@ public struct ByteBufferAllocator {
hookedMemcpy: { $0.copyMemory(from: $1, byteCount: $2) })
}

internal init(hookedMalloc: @escaping @convention(c) (Int) -> UnsafeMutableRawPointer,
hookedRealloc: @escaping @convention(c) (UnsafeMutableRawPointer, Int) -> UnsafeMutableRawPointer,
hookedFree: @escaping @convention(c) (UnsafeMutableRawPointer) -> Void,
internal init(hookedMalloc: @escaping @convention(c) (Int) -> UnsafeMutableRawPointer?,
hookedRealloc: @escaping @convention(c) (UnsafeMutableRawPointer?, Int) -> UnsafeMutableRawPointer?,
hookedFree: @escaping @convention(c) (UnsafeMutableRawPointer?) -> Void,
hookedMemcpy: @escaping @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, Int) -> Void) {
assert(MemoryLayout<ByteBuffer>.size <= 3 * MemoryLayout<Int>.size,
"ByteBuffer has size \(MemoryLayout<ByteBuffer>.size) which is larger than the built-in storage of the existential containers.")
Expand All @@ -65,9 +65,9 @@ public struct ByteBufferAllocator {
return ByteBuffer(allocator: self, startingCapacity: capacity)
}

internal let malloc: @convention(c) (Int) -> UnsafeMutableRawPointer
internal let realloc: @convention(c) (UnsafeMutableRawPointer, Int) -> UnsafeMutableRawPointer
internal let free: @convention(c) (UnsafeMutableRawPointer) -> Void
internal let malloc: @convention(c) (Int) -> UnsafeMutableRawPointer?
internal let realloc: @convention(c) (UnsafeMutableRawPointer?, Int) -> UnsafeMutableRawPointer?
internal let free: @convention(c) (UnsafeMutableRawPointer?) -> Void
internal let memcpy: @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, Int) -> Void

}
Expand Down Expand Up @@ -194,7 +194,7 @@ public struct ByteBuffer {

private static func allocateAndPrepareRawMemory(bytes: Capacity, allocator: Allocator) -> UnsafeMutableRawPointer {
let bytes = Int(bytes)
let ptr = allocator.malloc(bytes)
let ptr = allocator.malloc(bytes)!
/* bind the memory so we can assume it elsewhere to be bound to UInt8 */
ptr.bindMemory(to: UInt8.self, capacity: bytes)
return ptr
Expand All @@ -219,7 +219,7 @@ public struct ByteBuffer {
}

public func reallocStorage(capacity: Capacity) {
let ptr = self.allocator.realloc(self.bytes, Int(capacity))
let ptr = self.allocator.realloc(self.bytes, Int(capacity))!
/* bind the memory so we can assume it elsewhere to be bound to UInt8 */
ptr.bindMemory(to: UInt8.self, capacity: Int(capacity))
self.bytes = ptr
Expand Down
10 changes: 5 additions & 5 deletions Tests/NIOTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1224,25 +1224,25 @@ private enum AllocationExpectationState: Int {
}

private var testAllocationOfReallyBigByteBuffer_state = AllocationExpectationState.begin
private func testAllocationOfReallyBigByteBuffer_freeHook(_ ptr: UnsafeMutableRawPointer) -> Void {
private func testAllocationOfReallyBigByteBuffer_freeHook(_ ptr: UnsafeMutableRawPointer?) -> Void {
precondition(AllocationExpectationState.reallocDone == testAllocationOfReallyBigByteBuffer_state)
testAllocationOfReallyBigByteBuffer_state = .freeDone
/* free the pointer initially produced by malloc and then rebased by realloc offsetting it back */
free(ptr.advanced(by: Int(Int32.max)))
free(ptr?.advanced(by: Int(Int32.max)))
}

private func testAllocationOfReallyBigByteBuffer_mallocHook(_ size: Int) -> UnsafeMutableRawPointer! {
private func testAllocationOfReallyBigByteBuffer_mallocHook(_ size: Int) -> UnsafeMutableRawPointer? {
precondition(AllocationExpectationState.begin == testAllocationOfReallyBigByteBuffer_state)
testAllocationOfReallyBigByteBuffer_state = .mallocDone
/* return a 16 byte pointer here, good enough to write an integer in there */
return malloc(16)
}

private func testAllocationOfReallyBigByteBuffer_reallocHook(_ ptr: UnsafeMutableRawPointer, _ count: Int) -> UnsafeMutableRawPointer! {
private func testAllocationOfReallyBigByteBuffer_reallocHook(_ ptr: UnsafeMutableRawPointer?, _ count: Int) -> UnsafeMutableRawPointer? {
precondition(AllocationExpectationState.mallocDone == testAllocationOfReallyBigByteBuffer_state)
testAllocationOfReallyBigByteBuffer_state = .reallocDone
/* rebase this pointer by -Int32.max so that the byte copy extending the ByteBuffer below will land at actual index 0 into this buffer ;) */
return ptr.advanced(by: -Int(Int32.max))
return ptr!.advanced(by: -Int(Int32.max))
}

private func testAllocationOfReallyBigByteBuffer_memcpyHook(_ dst: UnsafeMutableRawPointer, _ src: UnsafeRawPointer, _ count: Int) -> Void {
Expand Down
8 changes: 4 additions & 4 deletions Tests/NIOTests/CodecTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import XCTest

private var testDecoderIsNotQuadratic_mallocs = 0
private var testDecoderIsNotQuadratic_reallocs = 0
private func testDecoderIsNotQuadratic_freeHook(_ ptr: UnsafeMutableRawPointer) -> Void {
private func testDecoderIsNotQuadratic_freeHook(_ ptr: UnsafeMutableRawPointer?) -> Void {
free(ptr)
}

private func testDecoderIsNotQuadratic_mallocHook(_ size: Int) -> UnsafeMutableRawPointer {
private func testDecoderIsNotQuadratic_mallocHook(_ size: Int) -> UnsafeMutableRawPointer? {
testDecoderIsNotQuadratic_mallocs += 1
return malloc(size)!
return malloc(size)
}

private func testDecoderIsNotQuadratic_reallocHook(_ ptr: UnsafeMutableRawPointer, _ count: Int) -> UnsafeMutableRawPointer {
private func testDecoderIsNotQuadratic_reallocHook(_ ptr: UnsafeMutableRawPointer?, _ count: Int) -> UnsafeMutableRawPointer? {
testDecoderIsNotQuadratic_reallocs += 1
return realloc(ptr, count)
}
Expand Down