2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
40
40
/// byteCount: count * MemoryLayout<Point>.stride,
41
41
/// alignment: MemoryLayout<Point>.alignment)
42
42
@frozen // namespace
43
- public enum MemoryLayout < T> {
43
+ public enum MemoryLayout < T: ~ Copyable> { }
44
+
45
+ extension MemoryLayout where T: ~ Copyable {
44
46
/// The contiguous memory footprint of `T`, in bytes.
45
47
///
46
48
/// A type's size does not include any dynamically allocated or out of line
@@ -50,6 +52,7 @@ public enum MemoryLayout<T> {
50
52
/// When allocating memory for multiple instances of `T` using an unsafe
51
53
/// pointer, use a multiple of the type's stride instead of its size.
52
54
@_transparent
55
+ @_alwaysEmitIntoClient
53
56
public static var size : Int {
54
57
return Int ( Builtin . sizeof ( T . self) )
55
58
}
@@ -62,6 +65,7 @@ public enum MemoryLayout<T> {
62
65
/// trades runtime performance for space efficiency. This value is always
63
66
/// positive.
64
67
@_transparent
68
+ @_alwaysEmitIntoClient
65
69
public static var stride : Int {
66
70
return Int ( Builtin . strideof ( T . self) )
67
71
}
@@ -71,12 +75,36 @@ public enum MemoryLayout<T> {
71
75
/// Use the `alignment` property for a type when allocating memory using an
72
76
/// unsafe pointer. This value is always positive.
73
77
@_transparent
78
+ @_alwaysEmitIntoClient
74
79
public static var alignment : Int {
75
80
return Int ( Builtin . alignof ( T . self) )
76
81
}
77
82
}
78
83
79
84
extension MemoryLayout {
85
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
86
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
87
+ @usableFromInline
88
+ internal static var size : Int {
89
+ return Int ( Builtin . sizeof ( T . self) )
90
+ }
91
+
92
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
93
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
94
+ @usableFromInline
95
+ internal static var stride : Int {
96
+ return Int ( Builtin . strideof ( T . self) )
97
+ }
98
+
99
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
100
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
101
+ @usableFromInline
102
+ internal static var alignment : Int {
103
+ return Int ( Builtin . alignof ( T . self) )
104
+ }
105
+ }
106
+
107
+ extension MemoryLayout where T: ~ Copyable {
80
108
/// Returns the contiguous memory footprint of the given instance.
81
109
///
82
110
/// The result does not include any dynamically allocated or out of line
@@ -100,7 +128,8 @@ extension MemoryLayout {
100
128
/// - Parameter value: A value representative of the type to describe.
101
129
/// - Returns: The size, in bytes, of the given value's type.
102
130
@_transparent
103
- public static func size( ofValue value: T ) -> Int {
131
+ @_alwaysEmitIntoClient
132
+ public static func size( ofValue value: borrowing T ) -> Int {
104
133
return MemoryLayout . size
105
134
}
106
135
@@ -128,7 +157,8 @@ extension MemoryLayout {
128
157
/// - Parameter value: A value representative of the type to describe.
129
158
/// - Returns: The stride, in bytes, of the given value's type.
130
159
@_transparent
131
- public static func stride( ofValue value: T ) -> Int {
160
+ @_alwaysEmitIntoClient
161
+ public static func stride( ofValue value: borrowing T ) -> Int {
132
162
return MemoryLayout . stride
133
163
}
134
164
@@ -153,10 +183,36 @@ extension MemoryLayout {
153
183
/// - Returns: The default memory alignment, in bytes, of the given value's
154
184
/// type. This value is always positive.
155
185
@_transparent
156
- public static func alignment( ofValue value: T ) -> Int {
186
+ @_alwaysEmitIntoClient
187
+ public static func alignment( ofValue value: borrowing T ) -> Int {
157
188
return MemoryLayout . alignment
158
189
}
190
+ }
159
191
192
+ extension MemoryLayout {
193
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
194
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
195
+ @usableFromInline
196
+ internal static func size( ofValue value: borrowing T ) -> Int {
197
+ return MemoryLayout . size
198
+ }
199
+
200
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
201
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
202
+ @usableFromInline
203
+ internal static func stride( ofValue value: borrowing T ) -> Int {
204
+ return MemoryLayout . stride
205
+ }
206
+
207
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
208
+ @available ( swift, obsoleted: 5.11 ) // Legacy ABI compatibility
209
+ @usableFromInline
210
+ internal static func alignment( ofValue value: borrowing T ) -> Int {
211
+ return MemoryLayout . alignment
212
+ }
213
+ }
214
+
215
+ extension MemoryLayout {
160
216
/// Returns the offset of an inline stored property within a type's in-memory
161
217
/// representation.
162
218
///
@@ -226,12 +282,15 @@ extension MemoryLayout {
226
282
@_transparent
227
283
@_unavailableInEmbedded
228
284
public static func offset( of key: PartialKeyPath < T > ) -> Int ? {
285
+ // FIXME(noncopyableGenerics): The new (implicit) `where T: Copyable`
286
+ // extension constraint currently changes the mangling of this from a
287
+ // standalone function to an extension method.
229
288
return key. _storedInlineOffset
230
289
}
231
290
}
232
291
233
292
// Not-yet-public alignment conveniences
234
- extension MemoryLayout {
293
+ extension MemoryLayout where T : ~ Copyable {
235
294
internal static var _alignmentMask : Int { return alignment - 1 }
236
295
237
296
internal static func _roundingUpToAlignment( _ value: Int ) -> Int {
0 commit comments