Skip to content

Commit dcd80d5

Browse files
committed
Made memory improvements
1 parent f652111 commit dcd80d5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/diamond/DiamondUpgradeFacet.sol

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,15 @@ contract DiamondUpgradeFacet {
256256
/**
257257
* 6. Update Free Memory Pointer
258258
* New Free Memory Pointer is after the copied selectors.
259-
* Notice that this is not 32-byte aligned.
259+
* Solidity requires the Free Memory Pointer to be aligned to 32 bytes.
260+
* 1. add(ptr, size) - end of copied selectors
261+
* 2. add(..., 0x1f) - round up to next 32-byte boundary
262+
* 3. and(..., not(0x1f)) - clear lower 5 bits to align to 32 bytes
260263
*/
261-
mstore(0x40, add(ptr, size))
264+
mstore(
265+
0x40,
266+
and(add(add(ptr, size), 0x1f), 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0)
267+
)
262268
}
263269
}
264270

0 commit comments

Comments
 (0)