memory

Module

Defines functions for memory manipulations.

You can import these APIs from the memory package. For example:

from memory import memcmp

memcmp

memcmp[type: DType](s1: DTypePointer[type], s2: DTypePointer[type], count: Int) -> Int

Compares two buffers. Both strings are assumed to be of the same length.

Parameters:

  • type (DType): The element dtype.

Args:

  • s1 (DTypePointer[type]): The first buffer address.
  • s2 (DTypePointer[type]): The second buffer address.
  • count (Int): The number of elements in the buffers.

Returns:

Returns 0 if the bytes buffers are identical, 1 if s1 > s2, and -1 if s1 < s2. The comparison is performed by the first different byte in the buffer.

memcmp[type: AnyType](s1: Pointer[*"type"], s2: Pointer[*"type"], count: Int) -> Int

Compares two buffers. Both strings are assumed to be of the same length.

Parameters:

  • type (AnyType): The element type.

Args:

  • s1 (Pointer[*"type"]): The first buffer address.
  • s2 (Pointer[*"type"]): The second buffer address.
  • count (Int): The number of elements in the buffers.

Returns:

Returns 0 if the bytes strings are identical, 1 if s1 > s2, and -1 if s1 < s2. The comparison is performed by the first different byte in the byte strings.

memcpy

memcpy[type: AnyType](dest: Pointer[*"type"], src: Pointer[*"type"], count: Int)

Copies a memory area.

Parameters:

  • type (AnyType): The element type.

Args:

  • dest (Pointer[*"type"]): The destination pointer.
  • src (Pointer[*"type"]): The source pointer.
  • count (Int): The number of elements to copy.

memcpy[type: DType](dest: DTypePointer[type], src: DTypePointer[type], count: Int)

Copies a memory area.

Parameters:

  • type (DType): The element dtype.

Args:

  • dest (DTypePointer[type]): The destination pointer.
  • src (DTypePointer[type]): The source pointer.
  • count (Int): The number of elements to copy (not bytes!).

memcpy[type: DType, size: Dim](dest: Buffer[size, type], src: Buffer[size, type])

Copies a memory buffer from src to dest.

Parameters:

  • type (DType): The element dtype.
  • size (Dim): Number of elements in the buffer.

Args:

  • dest (Buffer[size, type]): The destination buffer.
  • src (Buffer[size, type]): The source buffer.

memset

memset[type: DType](ptr: DTypePointer[type], value: SIMD[ui8, 1], count: Int)

Fills memory with the given value.

Parameters:

  • type (DType): The element dtype.

Args:

  • ptr (DTypePointer[type]): Pointer to the beginning of the memory block to fill.
  • value (SIMD[ui8, 1]): The value to fill with.
  • count (Int): Number of elements to fill (in elements, not bytes).

memset_zero

memset_zero[type: DType](ptr: DTypePointer[type], count: Int)

Fills memory with zeros.

Parameters:

  • type (DType): The element dtype.

Args:

  • ptr (DTypePointer[type]): Pointer to the beginning of the memory block to fill.
  • count (Int): Number of elements to set (in elements, not bytes).

memset_zero[type: AnyType](ptr: Pointer[*"type"], count: Int)

Fills memory with zeros.

Parameters:

  • type (AnyType): The element type.

Args:

  • ptr (Pointer[*"type"]): Pointer to the beginning of the memory block to fill.
  • count (Int): Number of elements to fill (in elements, not bytes).

stack_allocation

stack_allocation[count: Int, type: DType]() -> DTypePointer[type]

Allocates data buffer space on the stack given a data type and number of elements.

Parameters:

  • count (Int): Number of elements to allocate memory for.
  • type (DType): The data type of each element.

Returns:

A data pointer of the given type pointing to the allocated space.

stack_allocation[count: Int, type: DType, alignment: Int]() -> DTypePointer[type]

Allocates data buffer space on the stack given a data type and number of elements.

Parameters:

  • count (Int): Number of elements to allocate memory for.
  • type (DType): The data type of each element.
  • alignment (Int): Address alignment of the allocated data.

Returns:

A data pointer of the given type pointing to the allocated space.

stack_allocation[count: Int, type: AnyType]() -> Pointer[*"type"]

Allocates data buffer space on the stack given a data type and number of elements.

Parameters:

  • count (Int): Number of elements to allocate memory for.
  • type (AnyType): The data type of each element.

Returns:

A data pointer of the given type pointing to the allocated space.

stack_allocation[count: Int, type: AnyType, alignment: Int]() -> Pointer[*"type"]

Allocates data buffer space on the stack given a data type and number of elements.

Parameters:

  • count (Int): Number of elements to allocate memory for.
  • type (AnyType): The data type of each element.
  • alignment (Int): Address alignment of the allocated data.

Returns:

A data pointer of the given type pointing to the allocated space.