The type of the elements in the buffer being temporarily allocated.
withUnsafeTemporaryAllocation(of:capacity:_:)
Availability
- iOS 15.4+
- iPadOS 15.4+
- macOS 12.3+
- Mac Catalyst 15.4+
- tvOS 15.4+
- watchOS 8.5+
- Xcode 13.3+
Technology
- Swift Standard Library Swift標準ライブラリ
Declaration 宣言
func withUnsafeTemporaryAllocation<T, R>(of type: T.Type, capacity: Int
, _ body: (UnsafeMutableBufferPointer
<T>) throws -> R) rethrows -> R
Parameters パラメータ
type
capacity
The capacity of the buffer pointer being temporarily allocated.
body
A closure to invoke and to which the allocated buffer pointer should be passed.
Return Value 戻り値
Whatever is returned by body
.
Discussion 解説
This function is useful for cheaply allocating storage for a sequence of values for a brief duration. Storage may be allocated on the heap or on the stack, depending on the required size and alignment.
When body
is called, the contents of the buffer pointer passed to it are in an unspecified, uninitialized state. body
is responsible for initializing the buffer pointer before it is used and for deinitializing it before returning, but deallocation is automatic.
The implementation may allocate a larger buffer pointer than is strictly necessary to contain capacity
values of type type
. The behavior of a program that attempts to access any such additional storage is undefined.
The buffer pointer passed to body
(as well as any pointers to elements in the buffer) must not escape. It will be deallocated when body
returns and cannot be used afterward.