Skip to content

Commit 595302e

Browse files
committed
[SE-0101] Migrate sizeof family to MemoryLayout struct
1 parent 3b9a917 commit 595302e

12 files changed

+42
-42
lines changed

Foundation/Data.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
194194
///
195195
/// - parameter buffer: A buffer pointer to copy. The size is calculated from `SourceType` and `buffer.count`.
196196
public init<SourceType>(buffer: UnsafeBufferPointer<SourceType>) {
197-
_wrapped = _SwiftNSData(immutableObject: NSData(bytes: buffer.baseAddress, length: sizeof(SourceType.self) * buffer.count))
197+
_wrapped = _SwiftNSData(immutableObject: NSData(bytes: buffer.baseAddress, length: MemoryLayout<SourceType>.size * buffer.count))
198198
}
199199

200200
/// Initialize a `Data` with the contents of an Array.
@@ -314,7 +314,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
314314
public func withUnsafeBytes<ResultType, ContentType>(_ body: @noescape (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
315315
let bytes = _getUnsafeBytesPointer()
316316
defer { _fixLifetime(self)}
317-
let contentPtr = bytes.bindMemory(to: ContentType.self, capacity: count / strideof(ContentType.self))
317+
let contentPtr = bytes.bindMemory(to: ContentType.self, capacity: count / MemoryLayout<ContentType>.stride)
318318
return try body(contentPtr)
319319
}
320320

@@ -331,7 +331,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
331331
public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: @noescape (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
332332
let mutableBytes = _getUnsafeMutableBytesPointer()
333333
defer { _fixLifetime(self)}
334-
let contentPtr = mutableBytes.bindMemory(to: ContentType.self, capacity: count / strideof(ContentType.self))
334+
let contentPtr = mutableBytes.bindMemory(to: ContentType.self, capacity: count / MemoryLayout<ContentType>.stride)
335335
return try body(contentPtr)
336336
}
337337

@@ -362,7 +362,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
362362

363363
/// Copy the contents of the data into a buffer.
364364
///
365-
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `sizeof(DestinationType) * buffer.count` then the first N bytes will be copied into the buffer.
365+
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `MemoryLayout<DestinationType>.size * buffer.count` then the first N bytes will be copied into the buffer.
366366
/// - precondition: The range must be within the bounds of the data. Otherwise `fatalError` is called.
367367
/// - parameter buffer: A buffer to copy the data into.
368368
/// - parameter range: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into `buffer` is copied.
@@ -380,9 +380,9 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
380380
precondition(r.upperBound >= 0)
381381
precondition(r.upperBound <= cnt, "The range is outside the bounds of the data")
382382

383-
copyRange = r.lowerBound..<(r.lowerBound + Swift.min(buffer.count * sizeof(DestinationType.self), r.count))
383+
copyRange = r.lowerBound..<(r.lowerBound + Swift.min(buffer.count * MemoryLayout<DestinationType>.size, r.count))
384384
} else {
385-
copyRange = 0..<Swift.min(buffer.count * sizeof(DestinationType.self), cnt)
385+
copyRange = 0..<Swift.min(buffer.count * MemoryLayout<DestinationType>.size, cnt)
386386
}
387387

388388
guard !copyRange.isEmpty else { return 0 }
@@ -470,7 +470,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
470470
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
471471
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) {
472472
_applyUnmanagedMutation {
473-
$0.append(buffer.baseAddress!, length: buffer.count * sizeof(SourceType.self))
473+
$0.append(buffer.baseAddress!, length: buffer.count * MemoryLayout<SourceType>.size)
474474
}
475475
}
476476

Foundation/DateInterval.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
156156
public var hashValue: Int {
157157
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
158158
return withUnsafeMutablePointer(to: &buf) {
159-
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(sizeof(UInt.self) * 2)))
159+
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2)))
160160
}
161161
}
162162

Foundation/IndexPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
158158
if count == 0 {
159159
_indexes = []
160160
} else {
161-
var ptr = malloc(count * sizeof(Element.self))?.bindMemory(to: Element.self, capacity: count * sizeof(Element.self))
161+
var ptr = malloc(count * MemoryLayout<Element>.size)?.bindMemory(to: Element.self, capacity: count * MemoryLayout<Element>.size)
162162
defer { free(ptr) }
163163

164164
nsIndexPath.getIndexes(ptr!, range: NSMakeRange(0, count))

Foundation/NSArray.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
382382
let hash = item.hash
383383
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
384384
}
385-
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * sizeof(Int.self), deallocator: .custom({ _ in
385+
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * MemoryLayout<Int>.size, deallocator: .custom({ _ in
386386
buffer.deallocate(capacity: size)
387387
buffer.deinitialize(count: size)
388388
}))

Foundation/NSCFString.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ internal class _NSCFString : NSMutableString {
5959

6060
internal final class _NSCFConstantString : _NSCFString {
6161
internal var _ptr : UnsafePointer<UInt8> {
62-
let offset = sizeof(OpaquePointer.self) + sizeof(Int32.self) + sizeof(Int32.self) + sizeof(_CFInfo.self)
62+
let offset = MemoryLayout<OpaquePointer>.size + MemoryLayout<Int32>.size + MemoryLayout<Int32>.size + MemoryLayout<_CFInfo>.size
6363
let ptr = Unmanaged.passUnretained(self).toOpaque()
6464
return ptr.load(fromByteOffset: offset, as: UnsafePointer<UInt8>.self)
6565
}
6666
internal var _length : UInt32 {
67-
let offset = sizeof(OpaquePointer.self) + sizeof(Int32.self) + sizeof(Int32.self) + sizeof(_CFInfo.self) + sizeof(UnsafePointer<UInt8>.self)
67+
let offset = MemoryLayout<OpaquePointer>.size + MemoryLayout<Int32>.size + MemoryLayout<Int32>.size + MemoryLayout<_CFInfo>.size + MemoryLayout<UnsafePointer<UInt8>>.size
6868
let ptr = Unmanaged.passUnretained(self).toOpaque()
6969
return ptr.load(fromByteOffset: offset, as: UInt32.self)
7070
}

Foundation/NSCoder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class NSCoder : NSObject {
2222
deinit {
2323
for buffer in _pendingBuffers {
2424
// Cannot deinitialize a pointer to unknown type.
25-
buffer.0.deallocate(bytes: buffer.1, alignedTo: alignof(Int.self))
25+
buffer.0.deallocate(bytes: buffer.1, alignedTo: MemoryLayout<Int>.alignment)
2626
}
2727
}
2828

@@ -159,7 +159,7 @@ public class NSCoder : NSObject {
159159
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self))
160160
}
161161
// we cannot autorelease here so instead the pending buffers will manage the lifespan of the returned data... this is wasteful but good enough...
162-
let result = UnsafeMutableRawPointer.allocate(bytes: Int(length), alignedTo: alignof(Int.self))
162+
let result = UnsafeMutableRawPointer.allocate(bytes: Int(length), alignedTo: MemoryLayout<Int>.alignment)
163163
decodeValue(ofObjCType: "c", at: result)
164164
lengthp.pointee = Int(length)
165165
_pendingBuffers.append((result, Int(length)))

Foundation/NSGeometry.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public func ==(lhs: CGPoint, rhs: CGPoint) -> Bool {
102102
extension CGPoint: NSSpecialValueCoding {
103103
init(bytes: UnsafeRawPointer) {
104104
self.x = bytes.load(as: CGFloat.self)
105-
self.y = bytes.load(fromByteOffset: strideof(CGFloat.self), as: CGFloat.self)
105+
self.y = bytes.load(fromByteOffset: MemoryLayout<CGFloat>.stride, as: CGFloat.self)
106106
}
107107

108108
init?(coder aDecoder: NSCoder) {
@@ -167,7 +167,7 @@ public func ==(lhs: CGSize, rhs: CGSize) -> Bool {
167167
extension CGSize: NSSpecialValueCoding {
168168
init(bytes: UnsafeRawPointer) {
169169
self.width = bytes.load(as: CGFloat.self)
170-
self.height = bytes.load(fromByteOffset: strideof(CGFloat.self), as: CGFloat.self)
170+
self.height = bytes.load(fromByteOffset: MemoryLayout<CGFloat>.stride, as: CGFloat.self)
171171
}
172172

173173
init?(coder aDecoder: NSCoder) {
@@ -248,10 +248,10 @@ extension CGRect: NSSpecialValueCoding {
248248
init(bytes: UnsafeRawPointer) {
249249
self.origin = CGPoint(
250250
x: bytes.load(as: CGFloat.self),
251-
y: bytes.load(fromByteOffset: 1 * strideof(CGFloat.self), as: CGFloat.self))
251+
y: bytes.load(fromByteOffset: 1 * MemoryLayout<CGFloat>.stride, as: CGFloat.self))
252252
self.size = CGSize(
253-
width: bytes.load(fromByteOffset: 2 * strideof(CGFloat.self), as: CGFloat.self),
254-
height: bytes.load(fromByteOffset: 3 * strideof(CGFloat.self), as: CGFloat.self))
253+
width: bytes.load(fromByteOffset: 2 * MemoryLayout<CGFloat>.stride, as: CGFloat.self),
254+
height: bytes.load(fromByteOffset: 3 * MemoryLayout<CGFloat>.stride, as: CGFloat.self))
255255
}
256256

257257
init?(coder aDecoder: NSCoder) {
@@ -344,9 +344,9 @@ public struct NSEdgeInsets {
344344
extension NSEdgeInsets: NSSpecialValueCoding {
345345
init(bytes: UnsafeRawPointer) {
346346
self.top = bytes.load(as: CGFloat.self)
347-
self.left = bytes.load(fromByteOffset: strideof(CGFloat.self), as: CGFloat.self)
348-
self.bottom = bytes.load(fromByteOffset: 2 * strideof(CGFloat.self), as: CGFloat.self)
349-
self.right = bytes.load(fromByteOffset: 3 * strideof(CGFloat.self), as: CGFloat.self)
347+
self.left = bytes.load(fromByteOffset: MemoryLayout<CGFloat>.stride, as: CGFloat.self)
348+
self.bottom = bytes.load(fromByteOffset: 2 * MemoryLayout<CGFloat>.stride, as: CGFloat.self)
349+
self.right = bytes.load(fromByteOffset: 3 * MemoryLayout<CGFloat>.stride, as: CGFloat.self)
350350
}
351351

352352
init?(coder aDecoder: NSCoder) {

Foundation/NSHost.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class Host: NSObject {
9090
res = info.ai_next
9191
continue
9292
}
93-
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? sizeof(sockaddr_in6.self) : sizeof(sockaddr_in.self))
93+
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
9494
let lookupInfo = { (content: inout [String], flags: Int32) in
9595
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: 1024)
9696
if (getnameinfo(info.ai_addr, sa_len, hname, 1024, nil, 0, flags) == 0) {

Foundation/NSNumber.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ extension NSNumber : CustomPlaygroundQuickLookable {
493493
case kCFNumberDoubleType:
494494
return .double(self.doubleValue)
495495
case kCFNumberCGFloatType:
496-
if sizeof(CGFloat.self) == sizeof(Float32.self) {
496+
if MemoryLayout<CGFloat>.size == MemoryLayout<Float32>.size {
497497
return .float(self.floatValue)
498498
} else {
499499
return .double(self.doubleValue)

Foundation/NSObjCRuntime.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,22 @@ extension _NSSimpleObjCType {
8484
// mapping of ObjC types to sizes and alignments (note that .Int is 32-bit)
8585
// FIXME use a generic function, unfortuantely this seems to promote the size to 8
8686
private let _NSObjCSizesAndAlignments : Dictionary<_NSSimpleObjCType, (Int, Int)> = [
87-
.ID : ( sizeof(AnyObject.self), alignof(AnyObject.self) ),
88-
.Class : ( sizeof(AnyClass.self), alignof(AnyClass.self) ),
89-
.Char : ( sizeof(CChar.self), alignof(CChar.self) ),
90-
.UChar : ( sizeof(UInt8.self), alignof(UInt8.self) ),
91-
.Short : ( sizeof(Int16.self), alignof(Int16.self) ),
92-
.UShort : ( sizeof(UInt16.self), alignof(UInt16.self) ),
93-
.Int : ( sizeof(Int32.self), alignof(Int32.self) ),
94-
.UInt : ( sizeof(UInt32.self), alignof(UInt32.self) ),
95-
.Long : ( sizeof(Int32.self), alignof(Int32.self) ),
96-
.ULong : ( sizeof(UInt32.self), alignof(UInt32.self) ),
97-
.LongLong : ( sizeof(Int64.self), alignof(Int64.self) ),
98-
.ULongLong : ( sizeof(UInt64.self), alignof(UInt64.self) ),
99-
.Float : ( sizeof(Float.self), alignof(Float.self) ),
100-
.Double : ( sizeof(Double.self), alignof(Double.self) ),
101-
.Bool : ( sizeof(Bool.self), alignof(Bool.self) ),
102-
.CharPtr : ( sizeof(UnsafePointer<CChar>.self), alignof(UnsafePointer<CChar>.self))
87+
.ID : ( MemoryLayout<AnyObject>.size, MemoryLayout<AnyObject>.alignment ),
88+
.Class : ( MemoryLayout<AnyClass>.size, MemoryLayout<AnyClass>.alignment ),
89+
.Char : ( MemoryLayout<CChar>.size, MemoryLayout<CChar>.alignment ),
90+
.UChar : ( MemoryLayout<UInt8>.size, MemoryLayout<UInt8>.alignment ),
91+
.Short : ( MemoryLayout<Int16>.size, MemoryLayout<Int16>.alignment ),
92+
.UShort : ( MemoryLayout<UInt16>.size, MemoryLayout<UInt16>.alignment ),
93+
.Int : ( MemoryLayout<Int32>.size, MemoryLayout<Int32>.alignment ),
94+
.UInt : ( MemoryLayout<UInt32>.size, MemoryLayout<UInt32>.alignment ),
95+
.Long : ( MemoryLayout<Int32>.size, MemoryLayout<Int32>.alignment ),
96+
.ULong : ( MemoryLayout<UInt32>.size, MemoryLayout<UInt32>.alignment ),
97+
.LongLong : ( MemoryLayout<Int64>.size, MemoryLayout<Int64>.alignment ),
98+
.ULongLong : ( MemoryLayout<UInt64>.size, MemoryLayout<UInt64>.alignment ),
99+
.Float : ( MemoryLayout<Float>.size, MemoryLayout<Float>.alignment ),
100+
.Double : ( MemoryLayout<Double>.size, MemoryLayout<Double>.alignment ),
101+
.Bool : ( MemoryLayout<Bool>.size, MemoryLayout<Bool>.alignment ),
102+
.CharPtr : ( MemoryLayout<UnsafePointer<CChar>>.size, MemoryLayout<UnsafePointer<CChar>>.alignment)
103103
]
104104

105105
internal func _NSGetSizeAndAlignment(_ type: _NSSimpleObjCType,

Foundation/NSRange.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension NSRange {
5454
extension NSRange: NSSpecialValueCoding {
5555
init(bytes: UnsafeRawPointer) {
5656
self.location = bytes.load(as: Int.self)
57-
self.length = bytes.load(fromByteOffset: strideof(Int.self), as: Int.self)
57+
self.length = bytes.load(fromByteOffset: MemoryLayout<Int>.stride, as: Int.self)
5858
}
5959

6060
init?(coder aDecoder: NSCoder) {

Foundation/NSSwiftRuntime.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal func _CFSwiftIsEqual(_ cf1: AnyObject, cf2: AnyObject) -> Bool {
6767
// Ivars in _NSCF* types must be zeroed via an unsafe accessor to avoid deinit of potentially unsafe memory to accces as an object/struct etc since it is stored via a foreign object graph
6868
internal func _CFZeroUnsafeIvars<T>(_ arg: inout T) {
6969
withUnsafeMutablePointer(to: &arg) { (ptr: UnsafeMutablePointer<T>) -> Void in
70-
bzero(unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self), sizeof(T.self))
70+
bzero(unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self), MemoryLayout<T>.size)
7171
}
7272
}
7373

0 commit comments

Comments
 (0)