@@ -244,7 +244,7 @@ public struct UnsafeRawPointer: _Pointer {
244
244
/// - Parameter other: The typed pointer to convert.
245
245
@_transparent
246
246
public init < T> ( @_nonEphemeral _ other: UnsafeMutablePointer < T > ) {
247
- _rawValue = other. _rawValue
247
+ _rawValue = other. _rawValue
248
248
}
249
249
250
250
/// Creates a new raw pointer from the given typed pointer.
@@ -257,8 +257,8 @@ public struct UnsafeRawPointer: _Pointer {
257
257
/// result is `nil`.
258
258
@_transparent
259
259
public init ? < T> ( @_nonEphemeral _ other: UnsafeMutablePointer < T > ? ) {
260
- guard let unwrapped = other else { return nil }
261
- _rawValue = unwrapped. _rawValue
260
+ guard let unwrapped = other else { return nil }
261
+ _rawValue = unwrapped. _rawValue
262
262
}
263
263
264
264
/// Deallocates the previously allocated memory block referenced by this pointer.
@@ -429,7 +429,7 @@ public struct UnsafeRawPointer: _Pointer {
429
429
MemoryLayout < T > . alignment. _builtinWordValue)
430
430
return Builtin . loadRaw ( alignedPointer)
431
431
#else
432
- return Builtin . loadRaw ( rawPointer)
432
+ return Builtin . loadRaw ( rawPointer)
433
433
#endif
434
434
}
435
435
@@ -455,13 +455,25 @@ public struct UnsafeRawPointer: _Pointer {
455
455
/// - Returns: A new instance of type `T`, read from the raw bytes at
456
456
/// `offset`. The returned instance isn't associated
457
457
/// with the value in the range of memory referenced by this pointer.
458
+ @inlinable
458
459
@_alwaysEmitIntoClient
459
460
public func loadUnaligned< T> (
460
461
fromByteOffset offset: Int = 0 ,
461
462
as type: T . Type
462
463
) -> T {
463
464
_debugPrecondition ( _isPOD ( T . self) )
464
- return Builtin . loadRaw ( ( self + offset) . _rawValue)
465
+ return withUnsafeTemporaryAllocation ( of: T . self, capacity: 1 ) {
466
+ let temporary = $0. baseAddress. _unsafelyUnwrappedUnchecked
467
+ Builtin . int_memcpy_RawPointer_RawPointer_Int64 (
468
+ temporary. _rawValue,
469
+ ( self + offset) . _rawValue,
470
+ UInt64 ( MemoryLayout< T> . size) . _value,
471
+ /*volatile:*/ false . _value
472
+ )
473
+ return temporary. pointee
474
+ }
475
+ //FIXME: reimplement with `loadRaw` when supported in SIL (rdar://96956089)
476
+ // e.g. Builtin.loadRaw((self + offset)._rawValue)
465
477
}
466
478
}
467
479
@@ -1180,13 +1192,25 @@ public struct UnsafeMutableRawPointer: _Pointer {
1180
1192
/// - Returns: A new instance of type `T`, read from the raw bytes at
1181
1193
/// `offset`. The returned instance isn't associated
1182
1194
/// with the value in the range of memory referenced by this pointer.
1195
+ @inlinable
1183
1196
@_alwaysEmitIntoClient
1184
1197
public func loadUnaligned< T> (
1185
1198
fromByteOffset offset: Int = 0 ,
1186
1199
as type: T . Type
1187
1200
) -> T {
1188
1201
_debugPrecondition ( _isPOD ( T . self) )
1189
- return Builtin . loadRaw ( ( self + offset) . _rawValue)
1202
+ return withUnsafeTemporaryAllocation ( of: T . self, capacity: 1 ) {
1203
+ let temporary = $0. baseAddress. _unsafelyUnwrappedUnchecked
1204
+ Builtin . int_memcpy_RawPointer_RawPointer_Int64 (
1205
+ temporary. _rawValue,
1206
+ ( self + offset) . _rawValue,
1207
+ UInt64 ( MemoryLayout< T> . size) . _value,
1208
+ /*volatile:*/ false . _value
1209
+ )
1210
+ return temporary. pointee
1211
+ }
1212
+ //FIXME: reimplement with `loadRaw` when supported in SIL (rdar://96956089)
1213
+ // e.g. Builtin.loadRaw((self + offset)._rawValue)
1190
1214
}
1191
1215
1192
1216
/// Stores the given value's bytes into raw memory at the specified offset.
0 commit comments