1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
|
//===--- UnsafeRawBufferPointer.swift.gyb ---------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%import gyb
% for mutable in (True, False):
% Self = 'UnsafeMutableRawBufferPointer' if mutable else 'UnsafeRawBufferPointer'
% Mutable = 'Mutable' if mutable else ''
/// A ${Mutable.lower()} nonowning collection interface to the bytes in a
/// region of memory.
///
/// You can use an `${Self}` instance in low-level operations to eliminate
/// uniqueness checks and release mode bounds checks. Bounds checks are always
/// performed in debug mode.
///
% if mutable:
/// An `${Self}` instance is a view of the raw bytes in a region of memory.
/// Each byte in memory is viewed as a `UInt8` value independent of the type
/// of values held in that memory. Reading from and writing to memory through
/// a raw buffer are untyped operations. Accessing this collection's bytes
/// does not bind the underlying memory to `UInt8`.
///
/// In addition to its collection interface, an `${Self}`
/// instance also supports the following methods provided by
/// `UnsafeMutableRawPointer`, including bounds checks in debug mode:
///
/// - `load(fromByteOffset:as:)`
/// - `loadUnaligned(fromByteOffset:as:)`
/// - `storeBytes(of:toByteOffset:as:)`
/// - `copyMemory(from:)`
% else:
/// An `${Self}` instance is a view of the raw bytes in a region of memory.
/// Each byte in memory is viewed as a `UInt8` value independent of the type
/// of values held in that memory. Reading from memory through a raw buffer is
/// an untyped operation.
///
/// In addition to its collection interface, an `${Self}`
/// instance also supports the `load(fromByteOffset:as:)`
/// and `loadUnaligned(fromByteOffset:as:)` methods provided by
/// `UnsafeRawPointer`, including bounds checks in debug mode.
% end
///
/// To access the underlying memory through typed operations, the memory must
/// be bound to a trivial type.
///
/// - Note: A *trivial type* can be copied bit for bit with no indirection
/// or reference-counting operations. Generally, native Swift types that do
/// not contain strong or weak references or other forms of indirection are
/// trivial, as are imported C structs and enums. Copying memory that
/// contains values of nontrivial types can only be done safely with a typed
/// pointer. Copying bytes directly from nontrivial, in-memory values does
/// not produce valid copies and can only be done by calling a C API, such as
/// `memmove()`.
///
/// ${Self} Semantics
/// =================
///
/// An `${Self}` instance is a view into memory and does not own the memory
/// that it references. Copying a variable or constant of type `${Self}` does
/// not copy the underlying memory. However, initializing another collection
/// with an `${Self}` instance copies bytes out of the referenced memory and
/// into the new collection.
///
/// The following example uses `someBytes`, an `${Self}` instance, to
/// demonstrate the difference between assigning a buffer pointer and using a
/// buffer pointer as the source for another collection's elements. Here, the
/// assignment to `destBytes` creates a new, nonowning buffer pointer
/// covering the first `n` bytes of the memory that `someBytes`
/// references---nothing is copied:
///
/// var destBytes = someBytes[0..<n]
///
/// Next, the bytes referenced by `destBytes` are copied into `byteArray`, a
/// new `[UInt8]` array, and then the remainder of `someBytes` is appended to
/// `byteArray`:
///
/// var byteArray: [UInt8] = Array(destBytes)
/// byteArray += someBytes[n..<someBytes.count]
% if mutable:
///
/// Assigning into a ranged subscript of an `${Self}` instance copies bytes
/// into the memory. The next `n` bytes of the memory that `someBytes`
/// references are copied in this code:
///
/// destBytes[0..<n] = someBytes[n..<(n + n)]
% end
@frozen
public struct Unsafe${Mutable}RawBufferPointer {
@usableFromInline
internal let _position, _end: Unsafe${Mutable}RawPointer?
/// Creates a buffer over the specified number of contiguous bytes starting
/// at the given pointer.
///
/// - Parameters:
/// - start: The address of the memory that starts the buffer. If `starts`
/// is `nil`, `count` must be zero. However, `count` may be zero even
/// for a non-`nil` `start`.
/// - count: The number of bytes to include in the buffer. `count` must not
/// be negative.
@inlinable
public init(
@_nonEphemeral start: Unsafe${Mutable}RawPointer?, count: Int
) {
_debugPrecondition(count >= 0, "${Self} with negative count")
_debugPrecondition(count == 0 || start != nil,
"${Self} has a nil start and nonzero count")
_position = start
_end = start.map { $0 + _assumeNonNegative(count) }
}
}
@available(*, unavailable)
extension Unsafe${Mutable}RawBufferPointer: Sendable {}
%if not mutable:
extension UnsafeRawBufferPointer {
/// An iterator over the bytes viewed by a raw buffer pointer.
@frozen
public struct Iterator {
@usableFromInline
internal var _position, _end: UnsafeRawPointer?
@inlinable
internal init(_position: UnsafeRawPointer?, _end: UnsafeRawPointer?) {
self._position = _position
self._end = _end
}
}
}
@available(*, unavailable)
extension UnsafeRawBufferPointer.Iterator: Sendable { }
extension UnsafeRawBufferPointer.Iterator: IteratorProtocol, Sequence {
/// Advances to the next byte and returns it, or `nil` if no next byte
/// exists.
///
/// Once `nil` has been returned, all subsequent calls return `nil`.
///
/// - Returns: The next sequential byte in the raw buffer if another byte
/// exists; otherwise, `nil`.
@inlinable
public mutating func next() -> UInt8? {
if _position == _end { return nil }
// We can do an unchecked unwrap here by borrowing invariants from the pointer.
// For a validly constructed buffer pointer, the only way _position can be nil is
// if _end is also nil. We checked that case above. Thus, we can safely do an
// unchecked unwrap here.
//
// Additionally, validly constructed buffer pointers also have an _end that is
// strictly greater than or equal to _position, and so we do not need to do checked
// arithmetic here as we cannot possibly overflow.
//
// We check these invariants in debug builds to defend against invalidly constructed
// pointers.
_debugPrecondition(_position! < _end!)
let position = _position._unsafelyUnwrappedUnchecked
let result = position.load(as: UInt8.self)
_position = position + 1
return result
}
}
%else:
extension UnsafeMutableRawBufferPointer {
public typealias Iterator = UnsafeRawBufferPointer.Iterator
}
%end
extension Unsafe${Mutable}RawBufferPointer: Sequence {
public typealias SubSequence = Slice<${Self}>
/// Returns an iterator over the bytes of this sequence.
@inlinable
public func makeIterator() -> Iterator {
return Iterator(_position: _position, _end: _end)
}
/// Copies the elements of `self` to the memory at `destination.baseAddress`,
/// stopping when either `self` or `destination` is exhausted.
///
/// - Returns: an iterator over any remaining elements of `self` and the
/// number of elements copied.
@_alwaysEmitIntoClient
public func _copyContents(
initializing destination: UnsafeMutableBufferPointer<UInt8>
) -> (Iterator, UnsafeMutableBufferPointer<UInt8>.Index) {
guard let s = _position, let e = _end, e > s, !destination.isEmpty else {
return (makeIterator(), 0)
}
let destinationAddress = destination.baseAddress._unsafelyUnwrappedUnchecked
let d = UnsafeMutableRawPointer(destinationAddress)
let n = Swift.min(destination.count, s.distance(to: e))
d.copyMemory(from: s, byteCount: n)
return (Iterator(_position: s.advanced(by: n), _end: e), n)
}
}
extension Unsafe${Mutable}RawBufferPointer: ${Mutable}Collection {
// TODO: Specialize `index` and `formIndex` and
// `_failEarlyRangeCheck` as in `UnsafeBufferPointer`.
public typealias Element = UInt8
public typealias Index = Int
public typealias Indices = Range<Int>
/// Always zero, which is the index of the first byte in a nonempty buffer.
@inlinable
public var startIndex: Index {
return 0
}
/// The "past the end" position---that is, the position one greater than the
/// last valid subscript argument.
///
/// The `endIndex` property of an `Unsafe${Mutable}RawBufferPointer`
/// instance is always identical to `count`.
@inlinable
public var endIndex: Index {
return count
}
@inlinable
public var indices: Indices {
// Not checked because init forbids negative count.
return Indices(uncheckedBounds: (startIndex, endIndex))
}
/// Accesses the byte at the given offset in the memory region as a `UInt8`
/// value.
///
/// - Parameter i: The offset of the byte to access. `i` must be in the range
/// `0..<count`.
@inlinable
public subscript(i: Int) -> Element {
get {
_debugPrecondition(i >= 0)
_debugPrecondition(i < endIndex)
return _position._unsafelyUnwrappedUnchecked.load(fromByteOffset: i, as: UInt8.self)
}
% if mutable:
nonmutating set {
_debugPrecondition(i >= 0)
_debugPrecondition(i < endIndex)
_position._unsafelyUnwrappedUnchecked.storeBytes(of: newValue, toByteOffset: i, as: UInt8.self)
}
% end # mutable
}
/// Accesses the bytes in the specified memory region.
///
/// - Parameter bounds: The range of byte offsets to access. The upper and
/// lower bounds of the range must be in the range `0...count`.
@inlinable
public subscript(bounds: Range<Int>) -> SubSequence {
get {
_debugPrecondition(bounds.lowerBound >= startIndex)
_debugPrecondition(bounds.upperBound <= endIndex)
return Slice(base: self, bounds: bounds)
}
% if mutable:
nonmutating set {
_debugPrecondition(bounds.lowerBound >= startIndex)
_debugPrecondition(bounds.upperBound <= endIndex)
_debugPrecondition(bounds.count == newValue.count)
if !newValue.isEmpty {
(baseAddress! + bounds.lowerBound).copyMemory(
from: newValue.base.baseAddress! + newValue.startIndex,
byteCount: newValue.count)
}
}
% end # mutable
}
% if mutable:
/// Exchanges the byte values at the specified indices
/// in this buffer's memory.
///
/// Both parameters must be valid indices of the buffer, and not
/// equal to `endIndex`. Passing the same index as both `i` and `j` has no
/// effect.
///
/// - Parameters:
/// - i: The index of the first byte to swap.
/// - j: The index of the second byte to swap.
@inlinable
public func swapAt(_ i: Int, _ j: Int) {
guard i != j else { return }
_debugPrecondition(i >= 0 && j >= 0)
_debugPrecondition(i < endIndex && j < endIndex)
let pi = (_position! + i)
let pj = (_position! + j)
let tmp = pi.load(fromByteOffset: 0, as: UInt8.self)
pi.copyMemory(from: pj, byteCount: MemoryLayout<UInt8>.size)
pj.storeBytes(of: tmp, toByteOffset: 0, as: UInt8.self)
}
% end # mutable
/// The number of bytes in the buffer.
///
/// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
/// a buffer can have a `count` of zero even with a non-`nil` base address.
@inlinable
public var count: Int {
if let pos = _position {
// Unsafely unwrapped because init forbids end being nil if _position
// isn't.
_internalInvariant(_end != nil)
return _assumeNonNegative(_end._unsafelyUnwrappedUnchecked - pos)
}
return 0
}
}
extension Unsafe${Mutable}RawBufferPointer: RandomAccessCollection { }
extension Unsafe${Mutable}RawBufferPointer {
% if mutable:
/// Allocates uninitialized memory with the specified size and alignment.
///
/// You are in charge of managing the allocated memory. Be sure to deallocate
/// any memory that you manually allocate.
///
/// The allocated memory is not bound to any specific type and must be bound
/// before performing any typed operations. If you are using the memory for
/// a specific type, allocate memory using the
/// `UnsafeMutablePointerBuffer.allocate(capacity:)` static method instead.
///
/// - Parameters:
/// - byteCount: The number of bytes to allocate. `byteCount` must not be
/// negative.
/// - alignment: The alignment of the new region of allocated memory, in
/// bytes. `alignment` must be a whole power of 2.
/// - Returns: A buffer pointer to a newly allocated region of memory aligned
/// to `alignment`.
@inlinable
public static func allocate(
byteCount: Int, alignment: Int
) -> UnsafeMutableRawBufferPointer {
let base = UnsafeMutableRawPointer.allocate(
byteCount: byteCount, alignment: alignment)
return UnsafeMutableRawBufferPointer(start: base, count: byteCount)
}
% end # mutable
/// Deallocates the memory block previously allocated at this buffer pointer’s
/// base address.
///
/// This buffer pointer's `baseAddress` must be `nil` or a pointer to a memory
/// block previously returned by a Swift allocation method. If `baseAddress` is
/// `nil`, this function does nothing. Otherwise, the memory must not be initialized
/// or `Pointee` must be a trivial type. This buffer pointer's byte `count` must
/// be equal to the originally allocated size of the memory block.
@inlinable
public func deallocate() {
_position?.deallocate()
}
/// Returns a new instance of the given type, read from the buffer pointer's
/// raw memory at the specified byte offset.
///
/// The memory at `offset` bytes from this buffer pointer's `baseAddress`
/// must be properly aligned for accessing `T` and initialized to `T` or
/// another type that is layout compatible with `T`.
///
/// You can use this method to create new values from the buffer pointer's
/// underlying bytes. The following example creates two new `Int32`
/// instances from the memory referenced by the buffer pointer `someBytes`.
/// The bytes for `a` are copied from the first four bytes of `someBytes`,
/// and the bytes for `b` are copied from the next four bytes.
///
/// let a = someBytes.load(as: Int32.self)
/// let b = someBytes.load(fromByteOffset: 4, as: Int32.self)
///
/// The memory to read for the new instance must not extend beyond the buffer
/// pointer's memory region---that is, `offset + MemoryLayout<T>.size` must
/// be less than or equal to the buffer pointer's `count`.
///
/// - Parameters:
/// - offset: The offset, in bytes, into the buffer pointer's memory at
/// which to begin reading data for the new instance. The buffer pointer
/// plus `offset` must be properly aligned for accessing an instance of
/// type `T`. The default is zero.
/// - type: The type to use for the newly constructed instance. The memory
/// must be initialized to a value of a type that is layout compatible
/// with `type`.
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
/// memory.
@inlinable
public func load<T>(fromByteOffset offset: Int = 0, as type: T.Type) -> T {
_debugPrecondition(offset >= 0, "${Self}.load with negative offset")
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
"${Self}.load out of bounds")
return baseAddress!.load(fromByteOffset: offset, as: T.self)
}
// FIXME(NCG): Add a consuming analogue of `load`, like `move(fromByteOffset:as:_:)` (in the mutable variant)
// FIXME(NCG): Add a borrow analogue of `load`, like `withBorrow(fromByteOffset:as:_:)`
/// Returns a new instance of the given type, constructed from the raw memory
/// at the specified offset.
///
/// This function only supports loading trivial types.
/// A trivial type does not contain any reference-counted property
/// within its in-memory stored representation.
/// The memory at `offset` bytes into the buffer must be laid out
/// identically to the in-memory representation of `T`.
///
/// You can use this method to create new values from the buffer pointer's
/// underlying bytes. The following example creates two new `Int32`
/// instances from the memory referenced by the buffer pointer `someBytes`.
/// The bytes for `a` are copied from the first four bytes of `someBytes`,
/// and the bytes for `b` are copied from the fourth through seventh bytes.
///
/// let a = someBytes.loadUnaligned(as: Int32.self)
/// let b = someBytes.loadUnaligned(fromByteOffset: 3, as: Int32.self)
///
/// The memory to read for the new instance must not extend beyond the buffer
/// pointer's memory region---that is, `offset + MemoryLayout<T>.size` must
/// be less than or equal to the buffer pointer's `count`.
///
/// - Parameters:
/// - offset: The offset, in bytes, into the buffer pointer's memory at
/// which to begin reading data for the new instance. The default is zero.
/// - type: The type to use for the newly constructed instance. The memory
/// must be initialized to a value of a type that is layout compatible
/// with `type`.
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
/// memory.
@_alwaysEmitIntoClient
public func loadUnaligned<T : BitwiseCopyable>(
fromByteOffset offset: Int = 0,
as type: T.Type
) -> T {
_debugPrecondition(offset >= 0, "${Self}.load with negative offset")
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
"${Self}.load out of bounds")
return baseAddress!.loadUnaligned(fromByteOffset: offset, as: T.self)
}
@_alwaysEmitIntoClient
public func loadUnaligned<T>(
fromByteOffset offset: Int = 0,
as type: T.Type
) -> T {
_debugPrecondition(offset >= 0, "${Self}.load with negative offset")
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
"${Self}.load out of bounds")
return baseAddress!.loadUnaligned(fromByteOffset: offset, as: T.self)
}
% if mutable:
/// Stores a value's bytes into the buffer pointer's raw memory at the
/// specified byte offset.
///
/// The type `T` to be stored must be a trivial type. The memory must also be
/// uninitialized, initialized to `T`, or initialized to another trivial
/// type that is layout compatible with `T`.
///
/// The memory written to must not extend beyond the buffer pointer's memory
/// region---that is, `offset + MemoryLayout<T>.size` must be less than or
/// equal to the buffer pointer's `count`.
///
/// After calling `storeBytes(of:toByteOffset:as:)`, the memory is
/// initialized to the raw bytes of `value`. If the memory is bound to a
/// type `U` that is layout compatible with `T`, then it contains a value of
/// type `U`. Calling `storeBytes(of:toByteOffset:as:)` does not change the
/// bound type of the memory.
///
/// - Note: A trivial type can be copied with just a bit-for-bit copy without
/// any indirection or reference-counting operations. Generally, native
/// Swift types that do not contain strong or weak references or other
/// forms of indirection are trivial, as are imported C structs and enums.
///
/// If you need to store into memory a copy of a value of a type that isn't
/// trivial, you cannot use the `storeBytes(of:toByteOffset:as:)` method.
/// Instead, you must know either initialize the memory or,
/// if you know the memory was already bound to `type`, assign to the memory.
///
/// - Parameters:
/// - value: The value to store as raw bytes.
/// - offset: The offset in bytes into the buffer pointer's memory to begin
/// writing bytes from the value. The default is zero.
/// - type: The type to use for the newly constructed instance. The memory
/// must be initialized to a value of a type that is layout compatible
/// with `type`.
@_alwaysEmitIntoClient
// This custom silgen name is chosen to not interfere with the old ABI
@_silgen_name("_swift_se0349_UnsafeMutableRawBufferPointer_storeBytes")
public func storeBytes<T>(
of value: T, toByteOffset offset: Int = 0, as type: T.Type
) {
_debugPrecondition(offset >= 0, "${Self}.storeBytes with negative offset")
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
"${Self}.storeBytes out of bounds")
let pointer = baseAddress._unsafelyUnwrappedUnchecked
pointer.storeBytes(of: value, toByteOffset: offset, as: T.self)
}
// This unavailable implementation uses the expected mangled name
// of `storeBytes<T>(of:toByteOffset:as:)`, and provides an entry point for
// any binary linked against the stdlib binary for Swift 5.6 and older.
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@_silgen_name("$sSw10storeBytes2of12toByteOffset2asyx_SixmtlF")
@usableFromInline func _legacy_se0349_storeBytes<T>(
of value: T, toByteOffset offset: Int = 0, as type: T.Type
) {
_debugPrecondition(offset >= 0, "${Self}.storeBytes with negative offset")
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
"${Self}.storeBytes out of bounds")
baseAddress!._legacy_se0349_storeBytes_internal(
of: value, toByteOffset: offset, as: T.self
)
}
/// Copies the bytes from the given buffer to this buffer's memory.
///
/// If the `source.count` bytes of memory referenced by this buffer are bound
/// to a type `T`, then `T` must be a trivial type, the underlying pointer
/// must be properly aligned for accessing `T`, and `source.count` must be a
/// multiple of `MemoryLayout<T>.stride`.
///
/// The memory referenced by `source` may overlap with the memory referenced
/// by this buffer.
///
/// After calling `copyMemory(from:)`, the first `source.count` bytes of
/// memory referenced by this buffer are initialized to raw bytes. If the
/// memory is bound to type `T`, then it contains values of type `T`.
///
/// - Parameter source: A buffer of raw bytes. `source.count` must
/// be less than or equal to this buffer's `count`.
@inlinable
public func copyMemory(from source: UnsafeRawBufferPointer) {
_debugPrecondition(source.count <= self.count,
"${Self}.copyMemory source has too many elements")
if let baseAddress = baseAddress, let sourceAddress = source.baseAddress {
baseAddress.copyMemory(from: sourceAddress, byteCount: source.count)
}
}
/// Copies from a collection of `UInt8` into this buffer's memory.
///
/// If the first `source.count` bytes of memory referenced by this buffer
/// are bound to a type `T`, then `T` must be a trivial type,
/// the underlying pointer must be properly aligned for accessing `T`,
/// and `source.count` must be a multiple of `MemoryLayout<T>.stride`.
///
/// After calling `copyBytes(from:)`, the first `source.count` bytes of memory
/// referenced by this buffer are initialized to raw bytes. If the memory is
/// bound to type `T`, then it contains values of type `T`.
///
/// - Parameter source: A collection of `UInt8` elements. `source.count` must
/// be less than or equal to this buffer's `count`.
@inlinable
public func copyBytes<C: Collection>(
from source: C
) where C.Element == UInt8 {
guard let position = _position else {
return
}
if source.withContiguousStorageIfAvailable({
(buffer: UnsafeBufferPointer<UInt8>) -> Void in
_debugPrecondition(source.count <= self.count,
"${Self}.copyBytes source has too many elements")
if let base = buffer.baseAddress {
position.copyMemory(from: base, byteCount: buffer.count)
}
}) != nil {
return
}
for (index, byteValue) in source.enumerated() {
_debugPrecondition(index < self.count,
"${Self}.copyBytes source has too many elements")
position.storeBytes(
of: byteValue, toByteOffset: index, as: UInt8.self)
}
}
% end # mutable
/// Creates a new buffer over the same memory as the given buffer.
///
/// - Parameter bytes: The buffer to convert.
@inlinable
public init(_ bytes: UnsafeMutableRawBufferPointer) {
self.init(start: bytes.baseAddress, count: bytes.count)
}
% if mutable:
/// Creates a new mutable buffer over the same memory as the given buffer.
///
/// - Parameter bytes: The buffer to convert.
@inlinable
public init(mutating bytes: UnsafeRawBufferPointer) {
self.init(start: UnsafeMutableRawPointer(mutating: bytes.baseAddress),
count: bytes.count)
}
% else:
/// Creates a new buffer over the same memory as the given buffer.
///
/// - Parameter bytes: The buffer to convert.
@inlinable
public init(_ bytes: UnsafeRawBufferPointer) {
self.init(start: bytes.baseAddress, count: bytes.count)
}
% end # !mutable
/// Creates a raw buffer over the contiguous bytes in the given typed buffer.
///
/// - Parameter buffer: The typed buffer to convert to a raw buffer. The
/// buffer's type `T` must be a trivial type.
@inlinable
@_preInverseGenerics
public init<T: ~Copyable>(_ buffer: UnsafeMutableBufferPointer<T>) {
self.init(start: buffer.baseAddress,
count: buffer.count * MemoryLayout<T>.stride)
}
% if not mutable:
/// Creates a raw buffer over the contiguous bytes in the given typed buffer.
///
/// - Parameter buffer: The typed buffer to convert to a raw buffer. The
/// buffer's type `T` must be a trivial type.
@inlinable
@_preInverseGenerics
public init<T: ~Copyable>(_ buffer: UnsafeBufferPointer<T>) {
self.init(start: buffer.baseAddress,
count: buffer.count * MemoryLayout<T>.stride)
}
% end # !mutable
% if not mutable:
/// Creates a raw buffer over the same memory as the given raw buffer slice,
/// with the indices rebased to zero.
///
/// The new buffer represents the same region of memory as the slice, but its
/// indices start at zero instead of at the beginning of the slice in the
/// original buffer. The following code creates `slice`, a slice covering
/// part of an existing buffer instance, then rebases it into a new `rebased`
/// buffer.
///
/// let slice = buffer[n...]
/// let rebased = UnsafeRawBufferPointer(rebasing: slice)
///
/// After this code has executed, the following are true:
///
/// - `rebased.startIndex == 0`
/// - `rebased[0] == slice[n]`
/// - `rebased[0] == buffer[n]`
/// - `rebased.count == slice.count`
///
/// - Parameter slice: The raw buffer slice to rebase.
@inlinable
public init(rebasing slice: Slice<UnsafeRawBufferPointer>) {
// NOTE: `Slice` does not guarantee that its start/end indices are valid
// in `base` -- it merely ensures that `startIndex <= endIndex`.
// We need manually check that we aren't given an invalid slice,
// or the resulting collection would allow access that was
// out-of-bounds with respect to the original base buffer.
// We only do this in debug builds to prevent a measurable performance
// degradation wrt passing around pointers not wrapped in a BufferPointer
// construct.
_debugPrecondition(
slice.startIndex >= 0 && slice.endIndex <= slice.base.count,
"Invalid slice")
let base = slice.base.baseAddress?.advanced(by: slice.startIndex)
let count = slice.endIndex &- slice.startIndex
self.init(start: base, count: count)
}
% end # !mutable
/// Creates a raw buffer over the same memory as the given raw buffer slice,
/// with the indices rebased to zero.
///
/// The new buffer represents the same region of memory as the slice, but its
/// indices start at zero instead of at the beginning of the slice in the
/// original buffer. The following code creates `slice`, a slice covering
/// part of an existing buffer instance, then rebases it into a new `rebased`
/// buffer.
///
/// let slice = buffer[n...]
/// let rebased = UnsafeRawBufferPointer(rebasing: slice)
///
/// After this code has executed, the following are true:
///
/// - `rebased.startIndex == 0`
/// - `rebased[0] == slice[n]`
/// - `rebased[0] == buffer[n]`
/// - `rebased.count == slice.count`
///
/// - Parameter slice: The raw buffer slice to rebase.
@inlinable
public init(rebasing slice: Slice<UnsafeMutableRawBufferPointer>) {
let base = slice.base.baseAddress?.advanced(by: slice.startIndex)
let count = slice.endIndex &- slice.startIndex
self.init(start: base, count: count)
}
/// A pointer to the first byte of the buffer.
///
/// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
/// a buffer can have a `count` of zero even with a non-`nil` base address.
@inlinable
public var baseAddress: Unsafe${Mutable}RawPointer? {
return _position
}
% if mutable:
/// Initializes the memory referenced by this buffer with the given value,
/// binds the memory to the value's type, and returns a typed buffer of the
/// initialized memory.
///
/// The memory referenced by this buffer must be uninitialized or
/// initialized to a trivial type, and must be properly aligned for
/// accessing `T`.
///
/// After calling this method on a raw buffer with non-nil `baseAddress` `b`,
/// the region starting at `b` and continuing up to
/// `b + self.count - self.count % MemoryLayout<T>.stride` is bound
/// to type `T` and is initialized. If `T` is a nontrivial type, you must
/// eventually deinitialize or move the values in this region to avoid leaks.
/// If `baseAddress` is `nil`, this function does nothing
/// and returns an empty buffer pointer.
///
/// - Parameters:
/// - type: The type to bind this buffer’s memory to.
/// - repeatedValue: The instance to copy into memory.
/// - Returns: A typed buffer of the memory referenced by this raw buffer.
/// The typed buffer contains `self.count / MemoryLayout<T>.stride`
/// instances of `T`.
@inlinable
@discardableResult
public func initializeMemory<T>(as type: T.Type, repeating repeatedValue: T)
-> UnsafeMutableBufferPointer<T> {
guard let base = _position else {
return .init(start: nil, count: 0)
}
let count = (_end._unsafelyUnwrappedUnchecked-base) / MemoryLayout<T>.stride
let initialized = base.initializeMemory(
as: type, repeating: repeatedValue, count: count
)
return .init(start: initialized, count: count)
}
/// Initializes the buffer's memory with the given elements, binding the
/// initialized memory to the elements' type.
///
/// When calling the `initializeMemory(as:from:)` method on a buffer `b`,
/// the memory referenced by `b` must be uninitialized or initialized to a
/// trivial type, and must be properly aligned for accessing `S.Element`.
/// The buffer must contain sufficient memory to accommodate
/// `source.underestimatedCount`.
///
/// This method initializes the buffer with elements from `source` until
/// `source` is exhausted or, if `source` is a sequence but not a collection,
/// the buffer has no more room for source's elements. After calling
/// `initializeMemory(as:from:)`, the memory referenced by the returned
/// `UnsafeMutableBufferPointer` instance is bound and initialized to type
/// `S.Element`. This method does not change
/// the binding state of the unused portion of `b`, if any.
///
/// - Parameters:
/// - type: The type of element to which this buffer's memory will be bound.
/// - source: A sequence of elements with which to initialize the buffer.
/// - Returns: An iterator to any elements of `source` that didn't fit in the
/// buffer, and a typed buffer of the written elements. The returned
/// buffer references memory starting at the same base address as this
/// buffer.
@inlinable
public func initializeMemory<S: Sequence>(
as type: S.Element.Type, from source: S
) -> (unwritten: S.Iterator, initialized: UnsafeMutableBufferPointer<S.Element>) {
var it = source.makeIterator()
var idx = startIndex
let elementStride = MemoryLayout<S.Element>.stride
// This has to be a debug precondition due to the cost of walking over some collections.
_debugPrecondition(source.underestimatedCount <= (count / elementStride),
"insufficient space to accommodate source.underestimatedCount elements")
guard let base = baseAddress else {
// this can be a precondition since only an invalid argument should be costly
_precondition(source.underestimatedCount == 0,
"no memory available to initialize from source")
return (it, UnsafeMutableBufferPointer(start: nil, count: 0))
}
_debugPrecondition(
Int(bitPattern: base) & (MemoryLayout<S.Element>.alignment-1) == 0,
"buffer base address must be properly aligned to access S.Element"
)
_internalInvariant(_end != nil)
for p in stride(from: base,
// only advance to as far as the last element that will fit
to: _end._unsafelyUnwrappedUnchecked - elementStride + 1,
by: elementStride
) {
// underflow is permitted -- e.g. a sequence into
// the spare capacity of an Array buffer
guard let x = it.next() else { break }
p.initializeMemory(as: S.Element.self, repeating: x, count: 1)
formIndex(&idx, offsetBy: elementStride)
}
return (it, UnsafeMutableBufferPointer(
start: base.assumingMemoryBound(to: S.Element.self),
count: idx / elementStride))
}
/// Initializes the buffer's memory with every element of the source,
/// binding the initialized memory to the elements' type.
///
/// When calling the `initializeMemory(as:fromContentsOf:)` method,
/// the memory referenced by the buffer must be uninitialized, or initialized
/// to a trivial type. The buffer must reference enough memory to store
/// `source.count` elements, and its `baseAddress` must be properly aligned
/// for accessing `C.Element`.
///
/// This method initializes the buffer with the contents of `source`
/// until `source` is exhausted.
/// After calling `initializeMemory(as:fromContentsOf:)`, the memory
/// referenced by the returned `UnsafeMutableBufferPointer` instance is bound
/// to the type `C.Element` and is initialized. This method does not change
/// the binding state of the unused portion of the buffer, if any.
///
/// - Note: The memory regions referenced by `source` and this buffer
/// must not overlap.
///
/// - Parameters:
/// - type: The type of element to which this buffer's memory will be bound.
/// - source: A collection of elements to be used to
/// initialize the buffer's storage.
/// - Returns: A typed buffer referencing the initialized elements.
/// The returned buffer references memory starting at the same
/// base address as this buffer, and its count is equal to `source.count`
@_alwaysEmitIntoClient
public func initializeMemory<C: Collection>(
as type: C.Element.Type,
fromContentsOf source: C
) -> UnsafeMutableBufferPointer<C.Element> {
let buffer: UnsafeMutableBufferPointer<C.Element>?
buffer = source.withContiguousStorageIfAvailable {
guard let sourceAddress = $0.baseAddress, !$0.isEmpty else {
return .init(start: nil, count: 0)
}
_debugPrecondition(
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
"buffer base address must be properly aligned to access C.Element"
)
_precondition(
$0.count * MemoryLayout<C.Element>.stride <= self.count,
"buffer cannot contain every element from source collection."
)
let start = baseAddress?.initializeMemory(
as: C.Element.self, from: sourceAddress, count: $0.count
)
return .init(start: start, count: $0.count)
}
if let buffer {
return buffer
}
guard let base = baseAddress else {
_precondition(
source.isEmpty,
"buffer cannot contain every element from source collection."
)
return .init(start: nil, count: 0)
}
_internalInvariant(_end != nil)
_debugPrecondition(
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
"buffer base address must be properly aligned to access C.Element"
)
var iterator = source.makeIterator()
var element = base
var initialized = 0
let end = _end._unsafelyUnwrappedUnchecked - MemoryLayout<C.Element>.stride
while element <= end {
guard let value = iterator.next() else {
return .init(start: .init(base._rawValue), count: initialized)
}
element.initializeMemory(as: C.Element.self, to: value)
element = element.advanced(by: MemoryLayout<C.Element>.stride)
initialized += 1
}
_precondition(
iterator.next() == nil,
"buffer cannot contain every element from source collection."
)
return .init(start: .init(base._rawValue), count: initialized)
}
/// Moves every element of an initialized source buffer into the
/// uninitialized memory referenced by this buffer, leaving the source memory
/// uninitialized and this buffer's memory initialized.
///
/// When calling the `moveInitializeMemory(as:fromContentsOf:)` method,
/// the memory referenced by the buffer must be uninitialized, or initialized
/// to a trivial type. The buffer must reference enough memory to store
/// `source.count` elements, and its `baseAddress` must be properly aligned
/// for accessing `C.Element`. After the method returns,
/// the memory referenced by the returned buffer is initialized and the
/// memory region underlying `source` is uninitialized.
///
/// This method initializes the buffer with the contents of `source`
/// until `source` is exhausted.
/// After calling `initializeMemory(as:fromContentsOf:)`, the memory
/// referenced by the returned `UnsafeMutableBufferPointer` instance is bound
/// to the type `T` and is initialized. This method does not change
/// the binding state of the unused portion of the buffer, if any.
///
/// - Note: The memory regions referenced by `source` and this buffer
/// may overlap.
///
/// - Parameters:
/// - type: The type of element to which this buffer's memory will be bound.
/// - source: A buffer referencing the values to copy.
/// The memory region underlying `source` must be initialized.
/// - Returns: A typed buffer referencing the initialized elements.
/// The returned buffer references memory starting at the same
/// base address as this buffer, and its count is equal to `source.count`.
@discardableResult
@_alwaysEmitIntoClient
public func moveInitializeMemory<T: ~Copyable>(
as type: T.Type,
fromContentsOf source: UnsafeMutableBufferPointer<T>
) -> UnsafeMutableBufferPointer<T> {
guard let sourceAddress = source.baseAddress, !source.isEmpty else {
return .init(start: nil, count: 0)
}
_debugPrecondition(
Int(bitPattern: baseAddress) & (MemoryLayout<T>.alignment-1) == 0,
"buffer base address must be properly aligned to access T"
)
_precondition(
source.count * MemoryLayout<T>.stride <= self.count,
"buffer cannot contain every element from source."
)
let initialized = baseAddress?.moveInitializeMemory(
as: T.self, from: sourceAddress, count: source.count
)
return .init(start: initialized, count: source.count)
}
/// Moves every element of an initialized source buffer slice into the
/// uninitialized memory referenced by this buffer, leaving the source memory
/// uninitialized and this buffer's memory initialized.
///
/// When calling the `moveInitializeMemory(as:fromContentsOf:)` method,
/// the memory referenced by the buffer must be uninitialized, or initialized
/// to a trivial type. The buffer must reference enough memory to store
/// `source.count` elements, and its `baseAddress` must be properly aligned
/// for accessing `C.Element`. After the method returns,
/// the memory referenced by the returned buffer is initialized and the
/// memory region underlying `source` is uninitialized.
///
/// This method initializes the buffer with the contents of `source`
/// until `source` is exhausted.
/// After calling `initializeMemory(as:fromContentsOf:)`, the memory
/// referenced by the returned `UnsafeMutableBufferPointer` instance is bound
/// to the type `T` and is initialized. This method does not change
/// the binding state of the unused portion of the buffer, if any.
///
/// - Note: The memory regions referenced by `source` and this buffer
/// may overlap.
///
/// - Parameters:
/// - type: The type of element to which this buffer's memory will be bound.
/// - source: A buffer referencing the values to copy.
/// The memory region underlying `source` must be initialized.
/// - Returns: A typed buffer referencing the initialized elements.
/// The returned buffer references memory starting at the same
/// base address as this buffer, and its count is equal to `source.count`.
@discardableResult
@_alwaysEmitIntoClient
public func moveInitializeMemory<T>(
as type: T.Type,
fromContentsOf source: Slice<UnsafeMutableBufferPointer<T>>
) -> UnsafeMutableBufferPointer<T> {
let rebased = UnsafeMutableBufferPointer(rebasing: source)
return moveInitializeMemory(as: T.self, fromContentsOf: rebased)
}
% end # mutable
/// Binds this buffer’s memory to the specified type and returns a typed
/// buffer of the bound memory.
///
/// Use the `bindMemory(to:)` method to bind the memory referenced
/// by this buffer to the type `T`. The memory must be uninitialized or
/// initialized to a type that is layout compatible with `T`. If the memory
/// is uninitialized, it is still uninitialized after being bound to `T`.
///
/// - Warning: A memory location may only be bound to one type at a time. The
/// behavior of accessing memory as a type unrelated to its bound type is
/// undefined.
///
/// - Parameters:
/// - type: The type `T` to bind the memory to.
/// - Returns: A typed buffer of the newly bound memory. The memory in this
/// region is bound to `T`, but has not been modified in any other way.
/// The typed buffer references `self.count / MemoryLayout<T>.stride`
/// instances of `T`.
@inlinable
@_transparent
@_preInverseGenerics
@discardableResult
public func bindMemory<T: ~Copyable>(
to type: T.Type
) -> Unsafe${Mutable}BufferPointer<T> {
guard let base = _position else {
return Unsafe${Mutable}BufferPointer<T>(start: nil, count: 0)
}
let capacity = count / MemoryLayout<T>.stride
Builtin.bindMemory(base._rawValue, capacity._builtinWordValue, type)
return Unsafe${Mutable}BufferPointer<T>(
start: Unsafe${Mutable}Pointer<T>(base._rawValue), count: capacity)
}
/// Executes the given closure while temporarily binding the buffer to
/// instances of type `T`.
///
/// Use this method when you have a buffer to raw memory and you need
/// to access that memory as instances of a given type `T`. Accessing
/// memory as a type `T` requires that the memory be bound to that type.
/// A memory location may only be bound to one type at a time, so accessing
/// the same memory as an unrelated type without first rebinding the memory
/// is undefined.
///
/// Any instance of `T` within the re-bound region may be initialized or
/// uninitialized. The memory underlying any individual instance of `T`
/// must have the same initialization state (i.e. initialized or
/// uninitialized.) Accessing a `T` whose underlying memory
/// is in a mixed initialization state shall be undefined behaviour.
///
/// If the byte count of the original buffer is not a multiple of
/// the stride of `T`, then the re-bound buffer is shorter
/// than the original buffer.
///
/// After executing `body`, this method rebinds memory back to its original
/// binding state. This can be unbound memory, or bound to a different type.
///
/// - Note: The buffer's base address must match the
/// alignment of `T` (as reported by `MemoryLayout<T>.alignment`).
/// That is, `Int(bitPattern: self.baseAddress) % MemoryLayout<T>.alignment`
/// must equal zero.
///
/// - Note: A raw buffer may represent memory that has been bound to a type.
/// If that is the case, then `T` must be layout compatible with the
/// type to which the memory has been bound. This requirement does not
/// apply if the raw buffer represents memory that has not been bound
/// to any type.
///
/// - Parameters:
/// - type: The type to temporarily bind the memory referenced by this
/// buffer.
/// - body: A closure that takes a typed pointer to the
/// same memory as this pointer, only bound to type `T`. The closure's
/// pointer argument is valid only for the duration of the closure's
/// execution. If `body` has a return value, that value is also used as
/// the return value for the `withMemoryRebound(to:capacity:_:)` method.
/// - buffer: The buffer temporarily bound to instances of `T`.
/// - Returns: The return value, if any, of the `body` closure parameter.
@_alwaysEmitIntoClient
public func withMemoryRebound<T: ~Copyable, E: Error, Result: ~Copyable>(
to type: T.Type,
_ body: (_ buffer: Unsafe${Mutable}BufferPointer<T>) throws(E) -> Result
) throws(E) -> Result {
guard let s = _position else {
return try body(.init(start: nil, count: 0))
}
_debugPrecondition(
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
"baseAddress must be a properly aligned pointer for type T"
)
// initializer ensures _end is nil only when _position is nil.
_internalInvariant(_end != nil)
let c = _assumeNonNegative(s.distance(to: _end._unsafelyUnwrappedUnchecked))
let n = c / MemoryLayout<T>.stride
let binding = Builtin.bindMemory(s._rawValue, n._builtinWordValue, T.self)
defer { Builtin.rebindMemory(s._rawValue, binding) }
return try body(.init(start: .init(s._rawValue), count: n))
}
/// Returns a typed buffer to the memory referenced by this buffer,
/// assuming that the memory is already bound to the specified type.
///
/// Use this method when you have a raw buffer to memory that has already
/// been bound to the specified type. The memory starting at this pointer
/// must be bound to the type `T`. Accessing memory through the returned
/// pointer is undefined if the memory has not been bound to `T`. To bind
/// memory to `T`, use `bindMemory(to:capacity:)` instead of this method.
///
/// - Note: The buffer's base address must match the
/// alignment of `T` (as reported by `MemoryLayout<T>.alignment`).
/// That is, `Int(bitPattern: self.baseAddress) % MemoryLayout<T>.alignment`
/// must equal zero.
///
/// - Parameter to: The type `T` that the memory has already been bound to.
/// - Returns: A typed pointer to the same memory as this raw pointer.
@_alwaysEmitIntoClient
public func assumingMemoryBound<T: ~Copyable>(
to: T.Type
) -> Unsafe${Mutable}BufferPointer<T> {
guard let s = _position else {
return .init(start: nil, count: 0)
}
// initializer ensures _end is nil only when _position is nil.
_internalInvariant(_end != nil)
let c = _assumeNonNegative(s.distance(to: _end._unsafelyUnwrappedUnchecked))
let n = c / MemoryLayout<T>.stride
return .init(start: .init(s._rawValue), count: n)
}
% if Mutable:
@_alwaysEmitIntoClient
public func withContiguousMutableStorageIfAvailable<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? {
#if $TypedThrows
try withMemoryRebound(to: Element.self) { b in
var buffer = b
defer {
_debugPrecondition(
(b.baseAddress, b.count) == (buffer.baseAddress, buffer.count),
"UnsafeMutableRawBufferPointer.withContiguousMutableStorageIfAvailable: replacing the buffer is not allowed"
)
}
return try body(&buffer)
}
#else
fatalError("unsupported compiler")
#endif
}
% end
@_alwaysEmitIntoClient
public func withContiguousStorageIfAvailable<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R? {
#if $TypedThrows
try withMemoryRebound(to: Element.self) {
try body(${ 'UnsafeBufferPointer<Element>($0)' if Mutable else '$0' })
}
#else
fatalError("unsupported compiler")
#endif
}
}
@_unavailableInEmbedded
extension Unsafe${Mutable}RawBufferPointer: CustomDebugStringConvertible {
/// A textual representation of the buffer, suitable for debugging.
public var debugDescription: String {
return "${Self}"
+ "(start: \(_position.map(String.init(describing:)) ?? "nil"), count: \(count))"
}
}
extension ${Self} {
@available(*, unavailable,
message: "use 'Unsafe${Mutable}RawBufferPointer(rebasing:)' to convert a slice into a zero-based raw buffer.")
public subscript(bounds: Range<Int>) -> ${Self} {
get { return ${Self}(start: nil, count: 0) }
% if mutable:
nonmutating set {}
% end # mutable
}
% if mutable:
@available(*, unavailable,
message: "use 'UnsafeRawBufferPointer(rebasing:)' to convert a slice into a zero-based raw buffer.")
public subscript(bounds: Range<Int>) -> UnsafeRawBufferPointer {
get { return UnsafeRawBufferPointer(start: nil, count: 0) }
nonmutating set {}
}
% end # mutable
}
% end # for mutable
/// Invokes the given closure with a mutable buffer pointer covering the raw
/// bytes of the given argument.
///
/// The buffer pointer argument to the `body` closure provides a collection
/// interface to the raw bytes of `value`. The buffer is the size of the
/// instance passed as `value` and does not include any remote storage.
///
/// - Parameters:
/// - value: An instance to temporarily access through a mutable raw buffer
/// pointer.
/// Note that the `inout` exclusivity rules mean that, like any other
/// `inout` argument, `value` cannot be directly accessed by other code
/// for the duration of `body`. Access must only occur through the pointer
/// argument to `body` until `body` returns.
/// - body: A closure that takes a raw buffer pointer to the bytes of `value`
/// as its sole argument. If the closure has a return value, that value is
/// also used as the return value of the `withUnsafeMutableBytes(of:_:)`
/// function. The buffer pointer argument is valid only for the duration
/// of the closure's execution.
/// - Returns: The return value, if any, of the `body` closure.
@_alwaysEmitIntoClient
public func withUnsafeMutableBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
of value: inout T,
_ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
let pointer = UnsafeMutableRawPointer(Builtin.addressof(&value))
return try body(.init(start: pointer, count: MemoryLayout<T>.size))
}
/// ABI: Historical withUnsafeMutableBytes(of:_:) rethrows,
/// expressed as "throws", which is ABI-compatible with "rethrows".
// FIXME(TypedThrows): Uncomment @_spi and revert rethrows
//@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@_silgen_name("$ss22withUnsafeMutableBytes2of_q_xz_q_SwKXEtKr0_lF")
@usableFromInline
func __abi_se0413_withUnsafeMutableBytes<T, Result>(
of value: inout T,
_ body: (UnsafeMutableRawBufferPointer) throws -> Result
) rethrows -> Result {
return try withUnsafeMutablePointer(to: &value) {
return try body(UnsafeMutableRawBufferPointer(
start: $0, count: MemoryLayout<T>.size))
}
}
/// Invokes the given closure with a buffer pointer covering the raw bytes of
/// the given argument.
///
/// This function is similar to `withUnsafeMutableBytes`, except that it
/// doesn't trigger stack protection for the pointer.
@_alwaysEmitIntoClient
public func _withUnprotectedUnsafeMutableBytes<
T: ~Copyable, E: Error, Result: ~Copyable
>(
of value: inout T,
_ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
#if $BuiltinUnprotectedAddressOf
let pointer = UnsafeMutableRawPointer(Builtin.unprotectedAddressOf(&value))
#else
let pointer = UnsafeMutableRawPointer(Builtin.addressof(&value))
#endif
return try body(.init(start: pointer, count: MemoryLayout<T>.size))
}
/// Invokes the given closure with a buffer pointer covering the raw bytes of
/// the given argument.
///
/// The buffer pointer argument to the `body` closure provides a collection
/// interface to the raw bytes of `value`. The buffer is the size of the
/// instance passed as `value` and does not include any remote storage.
///
/// - Parameters:
/// - value: An instance to temporarily access through a raw buffer pointer.
/// Note that the `inout` exclusivity rules mean that, like any other
/// `inout` argument, `value` cannot be directly accessed by other code
/// for the duration of `body`. Access must only occur through the pointer
/// argument to `body` until `body` returns.
/// - body: A closure that takes a raw buffer pointer to the bytes of `value`
/// as its sole argument. If the closure has a return value, that value is
/// also used as the return value of the `withUnsafeBytes(of:_:)`
/// function. The buffer pointer argument is valid only for the duration
/// of the closure's execution. It is undefined behavior to attempt to
/// mutate through the pointer by conversion to
/// `UnsafeMutableRawBufferPointer` or any other mutable pointer type.
/// If you want to mutate a value by writing through a pointer, use
/// `withUnsafeMutableBytes(of:_:)` instead.
/// - Returns: The return value, if any, of the `body` closure.
@_alwaysEmitIntoClient
public func withUnsafeBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
of value: inout T,
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
let address = UnsafeRawPointer(Builtin.addressof(&value))
return try body(.init(start: address, count: MemoryLayout<T>.size))
}
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
/// expressed as "throws", which is ABI-compatible with "rethrows".
// FIXME(TypedThrows): Uncomment @_spi and revert rethrows
//@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@_silgen_name("$ss15withUnsafeBytes2of_q_xz_q_SWKXEtKr0_lF")
@usableFromInline
func __abi_se0413_withUnsafeBytes<T, Result>(
of value: inout T,
_ body: (UnsafeRawBufferPointer) throws -> Result
) rethrows -> Result {
return try withUnsafePointer(to: &value) {
try body(UnsafeRawBufferPointer(start: $0, count: MemoryLayout<T>.size))
}
}
/// Invokes the given closure with a buffer pointer covering the raw bytes of
/// the given argument.
///
/// This function is similar to `withUnsafeBytes`, except that it
/// doesn't trigger stack protection for the pointer.
@_alwaysEmitIntoClient
public func _withUnprotectedUnsafeBytes<
T: ~Copyable, E: Error, Result: ~Copyable
>(
of value: inout T,
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
#if $BuiltinUnprotectedAddressOf
let p = UnsafeRawPointer(Builtin.unprotectedAddressOf(&value))
#else
let p = UnsafeRawPointer(Builtin.addressof(&value))
#endif
return try body(.init(start: p, count: MemoryLayout<T>.size))
}
/// Invokes the given closure with a buffer pointer covering the raw bytes of
/// the given argument.
///
/// The buffer pointer argument to the `body` closure provides a collection
/// interface to the raw bytes of `value`. The buffer is the size of the
/// instance passed as `value` and does not include any remote storage.
///
/// - Parameters:
/// - value: An instance to temporarily access through a raw buffer pointer.
/// - body: A closure that takes a raw buffer pointer to the bytes of `value`
/// as its sole argument. If the closure has a return value, that value is
/// also used as the return value of the `withUnsafeBytes(of:_:)`
/// function. The buffer pointer argument is valid only for the duration
/// of the closure's execution. It is undefined behavior to attempt to
/// mutate through the pointer by conversion to
/// `UnsafeMutableRawBufferPointer` or any other mutable pointer type.
/// If you want to mutate a value by writing through a pointer, use
/// `withUnsafeMutableBytes(of:_:)` instead.
/// - Returns: The return value, if any, of the `body` closure.
@_alwaysEmitIntoClient
public func withUnsafeBytes<
T: ~Copyable, E: Error, Result: ~Copyable
>(
of value: borrowing T,
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
return try body(.init(start: addr, count: MemoryLayout<T>.size))
}
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
/// expressed as "throws", which is ABI-compatible with "rethrows".
// FIXME(TypedThrows): Uncomment @_spi and revert rethrows
//@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@_silgen_name("$ss15withUnsafeBytes2of_q_x_q_SWKXEtKr0_lF")
@usableFromInline
func __abi_se0413_withUnsafeBytes<T, Result>(
of value: T,
_ body: (UnsafeRawBufferPointer) throws -> Result
) rethrows -> Result {
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
let buffer = UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
return try body(buffer)
}
/// Invokes the given closure with a buffer pointer covering the raw bytes of
/// the given argument.
///
/// This function is similar to `withUnsafeBytes`, except that it
/// doesn't trigger stack protection for the pointer.
@_alwaysEmitIntoClient
public func _withUnprotectedUnsafeBytes<
T: ~Copyable, E: Error, Result: ~Copyable
>(
of value: borrowing T,
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
#if $BuiltinUnprotectedAddressOf
let addr = UnsafeRawPointer(Builtin.unprotectedAddressOfBorrow(value))
#else
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
#endif
let buffer = UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
return try body(buffer)
}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End:
|