UnsafeMutableRawPointer

struct UnsafeMutableRawPointer

A raw pointer for accessing untyped data. This provides no automatic memory management, no type safety, and no alignment guarantees. This implements Strideable to provide a view of byte-addressable memory.

Inheritance Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, Equatable, Hashable, Strideable View Protocol Hierarchy →
Import import Swift

Initializers

init(_: OpaquePointer)

Converts an opaque pointer to an UnsafeMutableRawPointer.

Declaration

init(_ other: OpaquePointer)
init(_: UnsafeMutableRawPointer)

Creates an UnsafeMutableRawPointer from another UnsafeMutableRawPointer.

Declaration

init(_ other: UnsafeMutableRawPointer)
init<T>(_: AutoreleasingUnsafeMutablePointer<T>)

Convert from AutoreleasingUnsafeMutablePointer.

Declaration

init<T>(_ other: AutoreleasingUnsafeMutablePointer<T>)
init<T>(_: UnsafeMutablePointer<T>)

Converts an UnsafeMutablePointer to an UnsafeMutableRawPointer.

Declaration

init<T>(_ other: UnsafeMutablePointer<T>)
init(mutating:)

Converts an UnsafeRawPointer to an UnsafeMutableRawPointer.

Declaration

init(mutating other: UnsafeRawPointer)
init?(_: OpaquePointer?)

Converts an opaque pointer to an UnsafeMutableRawPointer.

Returns nil if from is nil.

Declaration

init?(_ other: OpaquePointer?)
init?(_: UnsafeMutableRawPointer?)

Creates an UnsafeMutableRawPointer from another UnsafeMutableRawPointer.

Returns nil if other is nil.

Declaration

init?(_ other: UnsafeMutableRawPointer?)
init?<T>(_: AutoreleasingUnsafeMutablePointer<T>?)

Convert other AutoreleasingUnsafeMutablePointer.

Returns nil if other is nil.

Declaration

init?<T>(_ other: AutoreleasingUnsafeMutablePointer<T>?)
init?<T>(_: UnsafeMutablePointer<T>?)

Converts an UnsafeMutablePointer to an UnsafeMutableRawPointer.

Returns nil if other is nil.

Declaration

init?<T>(_ other: UnsafeMutablePointer<T>?)
init?(bitPattern: Int)

Converts a pattern of bits to an UnsafeMutableRawPointer.

Returns nil if bitPattern is zero.

Declaration

init?(bitPattern: Int)
init?(bitPattern: UInt)

Converts a pattern of bits to an UnsafeMutableRawPointer.

Returns nil if bitPattern is zero.

Declaration

init?(bitPattern: UInt)
init?(mutating:)

Converts an UnsafeRawPointer to an UnsafeMutableRawPointer.

Returns nil if other is nil.

Declaration

init?(mutating other: UnsafeRawPointer?)

Instance Variables

var customMirror: Mirror

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

var customMirror: Mirror { get }
var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for this instance.

If this type has value semantics, the PlaygroundQuickLook instance should be unaffected by subsequent mutations.

Declaration

var customPlaygroundQuickLook: PlaygroundQuickLook { get }
var debugDescription: String

A textual representation of the pointer, suitable for debugging.

Declaration

var debugDescription: String { get }
var hashValue: Int

The pointer's hash value.

The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.

Declaration

var hashValue: Int { get }

Static Methods

static func allocate(bytes:alignedTo:)

Allocates and points at uninitialized memory for size bytes with alignedTo alignment.

Postcondition: The memory is allocated, but not initialized.

Declaration

static func allocate(bytes size: Int, alignedTo: Int) -> UnsafeMutableRawPointer

Instance Methods

func advanced(by:)

Returns self + n.

Declaration

func advanced(by n: Int) -> UnsafeMutableRawPointer
func assumingMemoryBound(_:)

Converts from an UnsafeMutableRawPointer to UnsafeMutablePointer<T> given that the region of memory starting at self is already bound to type T.

Precondition: The memory is bound to 'T' starting at self for some unspecified capacity.

Warning: Accessing memory via the returned pointer is undefined if the if the memory has not been bound to T.

Declaration

func assumingMemoryBound<T>(to: T.Type) -> UnsafeMutablePointer<T>
func bindMemory(to:capacity:)

Binds the allocated memory to type T and returns an UnsafeMutablePointer<T> to the bound memory at self.

Precondition: The memory is uninitialized. Postcondition: The memory is bound to 'T' starting at self continuing through self + count * MemoryLayout<T>.stride Warning: A memory location may only be bound to one type at a time. The behavior of accessing memory as type U while it is bound to an unrelated type T is undefined.

Declaration

func bindMemory<T>(to type: T.Type, capacity count: Int) -> UnsafeMutablePointer<T>
func copyBytes(from:count:)

Copies count bytes from source into memory at self.

Precondition: count is non-negative.

Precondition: The memory at source..<source + count is initialized to some trivial type T.

Precondition: If the memory at self..<self+count is bound to a type U, then U is a trivial type, the underlying pointers source and self are properly aligned for type U, and count is a multiple of MemoryLayout<U>.stride.

Postcondition: The memory at self..<self+count is initialized to raw bytes. If the memory is bound to type U, then it contains values of type U.

Declaration

func copyBytes(from source: UnsafeRawPointer, count: Int)
func deallocate(_:alignedTo:)

Deallocates uninitialized memory allocated for bytes number of bytes with alignedTo alignment.

Precondition: The memory is not initialized. Postcondition: The memory has been deallocated.

Declaration

func deallocate(bytes: Int, alignedTo: Int)
func distance(to:)

Returns x - self.

Declaration

func distance(to x: UnsafeMutableRawPointer) -> Int
func initializeMemory(_:from:)

Initializes memory starting at self with the elements of source and binds the initialized memory to type T.

Returns an UnsafeMutablePointer<T> this memory.

Precondition: The memory at self..<self + source.count * MemoryLayout<T>.stride is uninitialized.

Postcondition: The memory at self..<self + source.count * MemoryLayout<T>.stride is bound to type T.

Postcondition: The T values at self..<self + source.count * MemoryLayout<T>.stride are initialized.

TODO: Optimize where C is a ContiguousArrayBuffer.

Declaration

func initializeMemory<C : Collection>(as: C.Iterator.Element.Type, from source: C) -> UnsafeMutablePointer<C.Iterator.Element>
func initializeMemory(as:at:count:to:)

Initializes this memory location self + strideof(T) * index with count consecutive copies of value and binds the initialized memory to type T.

Returns an UnsafeMutablePointer<T> to this memory.

Precondition: The memory at self + index * strideof(T)..<self + (index + count) * strideof(T) is uninitialized.

Precondition: The underlying pointer is properly aligned for accessing T.

Precondition: index is non-negative.

Precondition: count is non-negative.

Postcondition: The memory at (self + strideof(T) * index)..<(self + strideof(T) * index) + count is bound to type T and initialized; the value should eventually be destroyed or moved from to avoid leaks.

Declaration

func initializeMemory<T>(as type: T.Type, at index: Int = default, count: Int = default, to value: T) -> UnsafeMutablePointer<T>
func initializeMemory(as:from:count:)

Initializes memory starting at self with count T values beginning at source and binds the initialized memory to type T.

Returns an UnsafeMutablePointer<T> this memory.

Precondition: count >= 0 Precondition: The memory regions source..<source + count and self..<self + count * MemoryLayout<T>.stride do not overlap. Precondition: The memory at self..<self + count * MemoryLayout<T>.stride is uninitialized, and the T values at source..<source + count are initialized. Precondition: The underlying pointer is properly aligned for accessing T. Postcondition: The memory at self..<self + count * MemoryLayout<T>.stride is bound to type T. Postcondition: The T values at self..<self + count * MemoryLayout<T>.stride and source..<source + count are initialized.

Declaration

func initializeMemory<T>(as type: T.Type, from source: UnsafePointer<T>, count: Int) -> UnsafeMutablePointer<T>
func load(fromByteOffset:as:)

Reads raw bytes from memory at self + offset and constructs a value of type T.

Precondition: The underlying pointer plus offset is properly aligned for accessing T.

Precondition: The memory is initialized to a value of some type, U, such that T is layout compatible with U.

Declaration

func load<T>(fromByteOffset offset: Int = default, as type: T.Type) -> T
func moveInitializeMemory(as:from:count:)

Initializes memory starting at self with count T values beginning at source, binds the initialized memory to type T, and returns the source memory to an uninitialized state.

Returns an UnsafeMutablePointer<T> this memory.

Precondition: count >= 0

Precondition: The memory at self..<self + count * MemoryLayout<T>.stride is uninitialized and the T values at source..<source + count are initialized.

Postcondition: The memory at self..<self + count * MemoryLayout<T>.stride is bound to type T.

Postcondition: The T values at self..<self + count * MemoryLayout<T>.stride are initialized and the memory at source..<source + count is uninitialized.

Declaration

func moveInitializeMemory<T>(as type: T.Type, from source: UnsafeMutablePointer<T>, count: Int) -> UnsafeMutablePointer<T>
func storeBytes(of:toByteOffset:as:)

Stores a value's bytes into raw memory at self + offset.

Precondition: The underlying pointer plus offset is properly aligned for storing type T.

Precondition: T is a trivial type.

Precondition: The memory is uninitialized, or initialized to some trivial type U such that T and U are mutually layout compatible.

Postcondition: The memory is initialized to raw bytes. If the memory is bound to type U, then it now contains a value of type U.

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.

Note: Storing a copy of a nontrivial value into memory requires that the user know the type of value previously in memory, and requires initialization or assignment of the memory. This can be achieved via a typed UnsafeMutablePointer or via a raw pointer self, as follows, where U is the previous type and T is the copied value's type: let typedPtr = self.bindMemory(to: U.self, capacity: 1) typedPtr.deinitialize(count: 1) self.initializeMemory(as: T.self, to: newValue)

Declaration

func storeBytes<T>(of value: T, toByteOffset offset: Int = default, as: T.Type)