Skip to content

Commit a2ae072

Browse files
glessardlorentey
andcommitted
[stdlib] alignment-checking functions for pointers
Co-authored-by: Karoy Lorentey <[email protected]>
1 parent 95a96f5 commit a2ae072

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,15 @@ extension Unsafe${Mutable}BufferPointer:
795795
% end
796796
}
797797

798+
extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
799+
@safe
800+
@_alwaysEmitIntoClient
801+
public func _isWellAligned() -> Bool {
802+
guard let p = baseAddress else { return true }
803+
return p._isWellAligned()
804+
}
805+
}
806+
798807
extension Unsafe${Mutable}BufferPointer {
799808
% if not Mutable:
800809
/// Creates a buffer over the same memory as the given buffer slice.

stdlib/public/core/UnsafePointer.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ extension UnsafePointer where Pointee: ~Copyable {
469469
}
470470
}
471471

472+
extension UnsafePointer where Pointee: ~Copyable {
473+
@safe
474+
@_alwaysEmitIntoClient
475+
public func _isWellAligned() -> Bool {
476+
(Int(bitPattern: self) & (MemoryLayout<Pointee>.alignment &- 1)) == 0
477+
}
478+
}
472479

473480
/// A pointer for accessing and manipulating data of a
474481
/// specific type.
@@ -1382,3 +1389,11 @@ extension UnsafeMutablePointer where Pointee: ~Copyable {
13821389
)._unsafelyUnwrappedUnchecked
13831390
}
13841391
}
1392+
1393+
extension UnsafeMutablePointer where Pointee: ~Copyable {
1394+
@safe
1395+
@_alwaysEmitIntoClient
1396+
public func _isWellAligned() -> Bool {
1397+
(Int(bitPattern: self) & (MemoryLayout<Pointee>.alignment &- 1)) == 0
1398+
}
1399+
}

0 commit comments

Comments
 (0)