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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user