Skip to main content

Mojo module

unsafe_pointer

Implements unsafe pointer types for manual memory management.

This module provides UnsafePointer and related type aliases for direct memory manipulation with explicit control over mutability, origins, and address spaces. It includes the alloc() function for heap allocation and comprehensive methods for loading, storing, and managing pointer lifetimes. These types enable low-level memory operations, interfacing with C code, and building custom data structures.

comptime values

ImmutOpaquePointer

comptime ImmutOpaquePointer[origin: ImmutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = UnsafePointer[NoneType, origin, address_space=address_space]

An immutable opaque pointer, equivalent to the C const void* type.

Parameters

  • origin (ImmutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

ImmutUnsafePointer

comptime ImmutUnsafePointer[type: AnyType, origin: ImmutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = UnsafePointer[type, origin, address_space=address_space]

An immutable unsafe pointer.

Parameters

  • type (AnyType): The pointee type.
  • origin (ImmutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

MutOpaquePointer

comptime MutOpaquePointer[origin: MutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = UnsafePointer[NoneType, origin, address_space=address_space]

A mutable opaque pointer, equivalent to the C void* type.

Parameters

  • origin (MutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

MutUnsafePointer

comptime MutUnsafePointer[type: AnyType, origin: MutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = UnsafePointer[type, origin, address_space=address_space]

A mutable unsafe pointer.

Parameters

  • type (AnyType): The pointee type.
  • origin (MutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

OpaquePointer

comptime OpaquePointer[mut: Bool, //, origin: Origin[mut=mut], *, address_space: AddressSpace = AddressSpace.GENERIC] = UnsafePointer[NoneType, origin, address_space=address_space]

An opaque pointer, equivalent to the C (const) void* type.

Parameters

  • mut (Bool): Whether the pointer is mutable.
  • origin (Origin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

Structs

  • UnsafePointer: UnsafePointer represents an indirect reference to one or more values of type T consecutively in memory, and can refer to uninitialized memory.

Functions

  • alloc: Allocates contiguous storage for count elements of type with alignment alignment.

Was this page helpful?