diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2019-01-19 03:11:27 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2019-01-22 13:41:21 +0800 |
commit | f9986a6342ab68f6fdf347afee05c798384d7195 (patch) | |
tree | e03ca0897365cd86e9eaf628a35a2fab5af02565 /contracts/utils | |
parent | 8d367a09fe504905da505e6bd090446cbdf5ccb6 (diff) | |
download | dexon-0x-contracts-f9986a6342ab68f6fdf347afee05c798384d7195.tar.gz dexon-0x-contracts-f9986a6342ab68f6fdf347afee05c798384d7195.tar.zst dexon-0x-contracts-f9986a6342ab68f6fdf347afee05c798384d7195.zip |
Move LibAddressArray to utils package
Diffstat (limited to 'contracts/utils')
-rw-r--r-- | contracts/utils/contracts/test/TestConstants.sol (renamed from contracts/utils/contracts/test/TestConstants/TestConstants.sol) | 4 | ||||
-rw-r--r-- | contracts/utils/contracts/test/TestLibBytes.sol (renamed from contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol) | 4 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/LibAddressArray.sol | 84 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/LibBytes.sol (renamed from contracts/utils/contracts/utils/LibBytes/LibBytes.sol) | 0 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/Ownable.sol (renamed from contracts/utils/contracts/utils/Ownable/Ownable.sol) | 2 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/ReentrancyGuard.sol (renamed from contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol) | 0 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/SafeMath.sol (renamed from contracts/utils/contracts/utils/SafeMath/SafeMath.sol) | 0 | ||||
-rw-r--r-- | contracts/utils/contracts/utils/interfaces/IOwnable.sol (renamed from contracts/utils/contracts/utils/Ownable/IOwnable.sol) | 0 |
8 files changed, 89 insertions, 5 deletions
diff --git a/contracts/utils/contracts/test/TestConstants/TestConstants.sol b/contracts/utils/contracts/test/TestConstants.sol index 3c852173b..372e0af24 100644 --- a/contracts/utils/contracts/test/TestConstants/TestConstants.sol +++ b/contracts/utils/contracts/test/TestConstants.sol @@ -16,9 +16,9 @@ */ -pragma solidity 0.4.24; +pragma solidity ^0.4.24; -import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; +import "../utils/LibBytes.sol"; // solhint-disable max-line-length diff --git a/contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol b/contracts/utils/contracts/test/TestLibBytes.sol index 444a3e717..c1a5866d0 100644 --- a/contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol +++ b/contracts/utils/contracts/test/TestLibBytes.sol @@ -16,9 +16,9 @@ */ -pragma solidity 0.4.24; +pragma solidity ^0.4.24; -import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; +import "../utils/LibBytes.sol"; contract TestLibBytes { diff --git a/contracts/utils/contracts/utils/LibAddressArray.sol b/contracts/utils/contracts/utils/LibAddressArray.sol new file mode 100644 index 000000000..892c486f1 --- /dev/null +++ b/contracts/utils/contracts/utils/LibAddressArray.sol @@ -0,0 +1,84 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity ^0.4.24; + +import "./LibBytes.sol"; + + +library LibAddressArray { + + /// @dev Append a new address to an array of addresses. + /// The `addressArray` may need to be reallocated to make space + /// for the new address. Because of this we return the resulting + /// memory location of `addressArray`. + /// @param addressToAppend Address to append. + /// @return Array of addresses: [... addressArray, addressToAppend] + function append(address[] memory addressArray, address addressToAppend) + internal pure + returns (address[]) + { + // Get stats on address array and free memory + uint256 freeMemPtr = 0; + uint256 addressArrayBeginPtr = 0; + uint256 addressArrayEndPtr = 0; + uint256 addressArrayLength = addressArray.length; + uint256 addressArrayMemSizeInBytes = 32 + (32 * addressArrayLength); + assembly { + freeMemPtr := mload(0x40) + addressArrayBeginPtr := addressArray + addressArrayEndPtr := add(addressArray, addressArrayMemSizeInBytes) + } + + // Cases for `freeMemPtr`: + // `freeMemPtr` == `addressArrayEndPtr`: Nothing occupies memory after `addressArray` + // `freeMemPtr` > `addressArrayEndPtr`: Some value occupies memory after `addressArray` + // `freeMemPtr` < `addressArrayEndPtr`: Memory has not been managed properly. + require( + freeMemPtr >= addressArrayEndPtr, + "INVALID_FREE_MEMORY_PTR" + ); + + // If free memory begins at the end of `addressArray` + // then we can append `addressToAppend` directly. + // Otherwise, we must copy the array to free memory + // before appending new values to it. + if (freeMemPtr > addressArrayEndPtr) { + LibBytes.memCopy(freeMemPtr, addressArrayBeginPtr, addressArrayMemSizeInBytes); + assembly { + addressArray := freeMemPtr + addressArrayBeginPtr := addressArray + } + } + + // Append `addressToAppend` + addressArrayLength += 1; + addressArrayMemSizeInBytes += 32; + addressArrayEndPtr = addressArrayBeginPtr + addressArrayMemSizeInBytes; + freeMemPtr = addressArrayEndPtr; + assembly { + // Store new array length + mstore(addressArray, addressArrayLength) + + // Update `freeMemPtr` + mstore(0x40, freeMemPtr) + } + addressArray[addressArrayLength - 1] = addressToAppend; + return addressArray; + } +} diff --git a/contracts/utils/contracts/utils/LibBytes/LibBytes.sol b/contracts/utils/contracts/utils/LibBytes.sol index 4ee6228d5..4ee6228d5 100644 --- a/contracts/utils/contracts/utils/LibBytes/LibBytes.sol +++ b/contracts/utils/contracts/utils/LibBytes.sol diff --git a/contracts/utils/contracts/utils/Ownable/Ownable.sol b/contracts/utils/contracts/utils/Ownable.sol index aa74a72d2..f67f241a4 100644 --- a/contracts/utils/contracts/utils/Ownable/Ownable.sol +++ b/contracts/utils/contracts/utils/Ownable.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.24; -import "./IOwnable.sol"; +import "./interfaces/IOwnable.sol"; contract Ownable is diff --git a/contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol b/contracts/utils/contracts/utils/ReentrancyGuard.sol index 1a02c88a4..1a02c88a4 100644 --- a/contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol +++ b/contracts/utils/contracts/utils/ReentrancyGuard.sol diff --git a/contracts/utils/contracts/utils/SafeMath/SafeMath.sol b/contracts/utils/contracts/utils/SafeMath.sol index d7a4a603e..d7a4a603e 100644 --- a/contracts/utils/contracts/utils/SafeMath/SafeMath.sol +++ b/contracts/utils/contracts/utils/SafeMath.sol diff --git a/contracts/utils/contracts/utils/Ownable/IOwnable.sol b/contracts/utils/contracts/utils/interfaces/IOwnable.sol index c0cbfddfd..c0cbfddfd 100644 --- a/contracts/utils/contracts/utils/Ownable/IOwnable.sol +++ b/contracts/utils/contracts/utils/interfaces/IOwnable.sol |