Add Memory utility library (#5189)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
This commit is contained in:
44
contracts/utils/Memory.sol
Normal file
44
contracts/utils/Memory.sol
Normal file
@ -0,0 +1,44 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
/**
|
||||
* @dev Utilities to manipulate memory.
|
||||
*
|
||||
* Memory is a contiguous and dynamic byte array in which Solidity stores non-primitive types.
|
||||
* This library provides functions to manipulate pointers to this dynamic array.
|
||||
*
|
||||
* WARNING: When manipulating memory, make sure to follow the Solidity documentation
|
||||
* guidelines for https://docs.soliditylang.org/en/v0.8.20/assembly.html#memory-safety[Memory Safety].
|
||||
*/
|
||||
library Memory {
|
||||
type Pointer is bytes32;
|
||||
|
||||
/// @dev Returns a `Pointer` to the current free `Pointer`.
|
||||
function getFreeMemoryPointer() internal pure returns (Pointer ptr) {
|
||||
assembly ("memory-safe") {
|
||||
ptr := mload(0x40)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets the free `Pointer` to a specific value.
|
||||
*
|
||||
* WARNING: Everything after the pointer may be overwritten.
|
||||
**/
|
||||
function setFreeMemoryPointer(Pointer ptr) internal pure {
|
||||
assembly ("memory-safe") {
|
||||
mstore(0x40, ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// @dev `Pointer` to `bytes32`. Expects a pointer to a properly ABI-encoded `bytes` object.
|
||||
function asBytes32(Pointer ptr) internal pure returns (bytes32) {
|
||||
return Pointer.unwrap(ptr);
|
||||
}
|
||||
|
||||
/// @dev `bytes32` to `Pointer`. Expects a pointer to a properly ABI-encoded `bytes` object.
|
||||
function asPointer(bytes32 value) internal pure returns (Pointer) {
|
||||
return Pointer.wrap(value);
|
||||
}
|
||||
}
|
||||
@ -38,6 +38,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
|
||||
* {Panic}: A library to revert with https://docs.soliditylang.org/en/v0.8.20/control-structures.html#panic-via-assert-and-error-via-require[Solidity panic codes].
|
||||
* {Comparators}: A library that contains comparator functions to use with the {Heap} library.
|
||||
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
|
||||
* {Memory}: A utility library to manipulate memory.
|
||||
* {InteroperableAddress}: Library for formatting and parsing ERC-7930 interoperable addresses.
|
||||
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
|
||||
* {Time}: A library that provides helpers for manipulating time-related objects, including a `Delay` type.
|
||||
@ -135,6 +136,8 @@ Ethereum contracts have no native concept of an interface, so applications must
|
||||
|
||||
{{CAIP10}}
|
||||
|
||||
{{Memory}}
|
||||
|
||||
{{InteroperableAddress}}
|
||||
|
||||
{{Blockhash}}
|
||||
|
||||
Reference in New Issue
Block a user