From 6848e7b8d724e7aa5760e19ec9aaa06f11febcdd Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Thu, 5 Jul 2018 09:00:42 +0100 Subject: [PATCH] ByteBufferAllocator: switch to size_t where appropriate (#503) Motivation: In a few situations we used `Int` in places where malloc/realloc actually use `size_t`. On 64-bit platforms this doesn't actually matter but on 32-bit it does! Modifications: - replace `Int` with `size_t` where appropriate Result: - should be able to use more than 2GB on 32-bit platforms --- Sources/NIO/ByteBuffer-core.swift | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index f4aa06cce..1b78c1cbb 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -let sysMalloc: @convention(c) (Int) -> UnsafeMutableRawPointer? = malloc -let sysRealloc: @convention(c) (UnsafeMutableRawPointer?, Int) -> UnsafeMutableRawPointer? = realloc +let sysMalloc: @convention(c) (size_t) -> UnsafeMutableRawPointer? = malloc +let sysRealloc: @convention(c) (UnsafeMutableRawPointer?, size_t) -> UnsafeMutableRawPointer? = realloc let sysFree: @convention(c) (UnsafeMutableRawPointer?) -> Void = free #if !swift(>=4.1) @@ -82,10 +82,10 @@ 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?, + internal init(hookedMalloc: @escaping @convention(c) (size_t) -> UnsafeMutableRawPointer?, + hookedRealloc: @escaping @convention(c) (UnsafeMutableRawPointer?, size_t) -> UnsafeMutableRawPointer?, hookedFree: @escaping @convention(c) (UnsafeMutableRawPointer?) -> Void, - hookedMemcpy: @escaping @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, Int) -> Void) { + hookedMemcpy: @escaping @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, size_t) -> Void) { #if !arch(arm) // only complain on 64-bit, this is unfortunate reality on 32-bit assert(MemoryLayout.size <= 3 * MemoryLayout.size, "ByteBuffer has size \(MemoryLayout.size) which is larger than the built-in storage of the existential containers.") @@ -104,10 +104,10 @@ 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 malloc: @convention(c) (size_t) -> UnsafeMutableRawPointer? + internal let realloc: @convention(c) (UnsafeMutableRawPointer?, size_t) -> UnsafeMutableRawPointer? internal let free: @convention(c) (UnsafeMutableRawPointer?) -> Void - internal let memcpy: @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, Int) -> Void + internal let memcpy: @convention(c) (UnsafeMutableRawPointer, UnsafeRawPointer, size_t) -> Void } @@ -236,10 +236,9 @@ 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(size_t(bytes))! /* bind the memory so we can assume it elsewhere to be bound to UInt8 */ - ptr.bindMemory(to: UInt8.self, capacity: bytes) + ptr.bindMemory(to: UInt8.self, capacity: Int(bytes)) return ptr } @@ -257,13 +256,13 @@ public struct ByteBuffer { public func reallocSlice(_ slice: Range, capacity: _Capacity) -> _Storage { assert(slice.count <= capacity) let new = self.allocateStorage(capacity: capacity) - self.allocator.memcpy(new.bytes, self.bytes.advanced(by: Int(slice.lowerBound)), slice.count) + self.allocator.memcpy(new.bytes, self.bytes.advanced(by: Int(slice.lowerBound)), size_t(slice.count)) return new } public func reallocStorage(capacity minimumNeededCapacity: _Capacity) { let newCapacity = minimumNeededCapacity.nextPowerOf2ClampedToMax() - let ptr = self.allocator.realloc(self.bytes, Int(newCapacity))! + let ptr = self.allocator.realloc(self.bytes, size_t(newCapacity))! /* bind the memory so we can assume it elsewhere to be bound to UInt8 */ ptr.bindMemory(to: UInt8.self, capacity: Int(newCapacity)) self.bytes = ptr