Skip to main content

MemoryUtils

Overview​

License: MIT​

library MemoryUtils

A library that provides utility functions for memory manipulation in Solidity.

Functions info​

copy​

function copy(
bytes memory source_
) internal view returns (bytes memory destination_)

Copies the contents of the source bytes to the destination bytes. Strings can be cast to bytes in order to use this function.

Parameters:

NameTypeDescription
source_bytesThe source bytes to copy from.

Return values:

NameTypeDescription
destination_bytesThe newly allocated bytes.

copy​

function copy(
bytes32[] memory source_
) internal view returns (bytes32[] memory destination_)

Copies the contents of the source bytes32 array to the destination bytes32 array. uint256[], address[] array can be casted to bytes32[] via TypeCaster library.

Parameters:

NameTypeDescription
source_bytes32[]The source bytes32 array to copy from.

Return values:

NameTypeDescription
destination_bytes32[]The newly allocated bytes32 array.

unsafeCopy​

function unsafeCopy(
uint256 sourcePointer_,
uint256 destinationPointer_,
uint256 size_
) internal view

Copies memory from one location to another efficiently via identity precompile.

This function does not account for free memory pointer and should be used with caution.

This signature of calling identity precompile is: staticcall(gas(), address(0x04), argsOffset, argsSize, retOffset, retSize)

Parameters:

NameTypeDescription
sourcePointer_uint256The offset in the memory from which to copy.
destinationPointer_uint256The offset in the memory where the result will be copied.
size_uint256The size of the memory to copy.

getPointer​

function getPointer(
bytes memory data_
) internal pure returns (uint256 pointer_)

Returns the memory pointer to the given bytes starting position including the length.

getPointer​

function getPointer(
bytes32[] memory data_
) internal pure returns (uint256 pointer_)

Returns the memory pointer to the given bytes starting position including the length. Cast uint256[] and address[] to bytes32[] via TypeCaster library.

getDataPointer​

function getDataPointer(
bytes memory data_
) internal pure returns (uint256 pointer_)

Returns the memory pointer to the given bytes data starting position skipping the length.

getDataPointer​

function getDataPointer(
bytes32[] memory data_
) internal pure returns (uint256 pointer_)

Returns the memory pointer to the given bytes data starting position skipping the length. Cast uint256[] and address[] to bytes32[] via TypeCaster library.