withTemporaryAllocation(byteCount:alignment:_:)
Provides scoped access to an output raw span with the specified byte count and alignment.
Declaration
func withTemporaryAllocation<R, E>(byteCount: Int, alignment: Int, _ body: @_lifetime(0: copy 0) (inout OutputRawSpan) throws(E) -> R) throws(E) -> R where E : Error, R : ~CopyableParameters
- byteCount:
The number of bytes to temporarily allocate.
byteCountmust not be negative. - alignment:
The alignment of the new, temporary region of allocated memory, in bytes.
alignmentmust be a whole power of 2. - body:
A closure to invoke and to which the allocated output raw span should be passed.
Return Value
Whatever is returned by body.
Discussion
This function is useful for cheaply allocating raw storage 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, it is passed an empty OutputRawSpan. body may append bytes to the output raw span. After body returns, deallocation is automatic.