forked from alianse777/solidity-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.sol
102 lines (92 loc) · 3.07 KB
/
Memory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
pragma solidity ^0.4.0;
/*
# Copyright (C) 2017 alianse777
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* WARNING: These functions are used to perform low-level memory access
* and may cause security risk when used improperly.
*/
contract STDMemory {
// Memory uint[] pointer functions
/** @dev Returns memory address of uint[] object
* @param data is an uint[] array to referrence
* @return ptr - pointer to array
*/
function m_ref_uint(uint [] data) internal pure returns (uint ptr) {
assembly {
let retptr := data
ptr := retptr
}
}
/** @dev Returns dereferrenced array
* @param ptr size
* @return array of given size
*/
function m_unref_uint(uint ptr, uint size) internal pure returns(uint []) {
uint [] memory data = new uint[](size);
assembly {
data := ptr
}
return data;
}
/** @dev Used to test memory pointers
* @return true - OK, false - error
*/
function m_test_uint() internal pure returns (bool) {
uint [] memory dt = new uint[](3);
for(uint i = 0;i < dt.length;i++) {
dt[i] = i;
}
uint wptr = m_ref_uint(dt);
uint [] memory data;
data = m_unref_uint(wptr, 3);
return data.length == 3 && data[0] == 0 &&data[1] == 1 && data[2] == 2;
}
// Memory int [] pointer functions
/** @dev Returns memory address of int[] object
* @param data is an int[] array to referrence
* @return pointer to array
*/
function m_ref_int(int [] data) internal pure returns (uint ptr) {
assembly {
let retptr := data
ptr := retptr
}
}
/** @dev Returns dereferrenced array
* @param ptr size
* @return array of given size
*/
function m_unref_int(uint ptr, uint size) internal pure returns(int []) {
int [] memory data = new int[](size);
assembly {
data := ptr
}
return data;
}
/** @dev Used to test memory pointers
* @return true - OK, false - error
*/
function m_test_int() internal pure returns (bool) {
int [] memory dt = new int[](3);
for(uint i = 0;i < dt.length;i++) {
dt[i] = int(i);
}
uint wptr = m_ref_int(dt);
int [] memory data;
data = m_unref_int(wptr, 3);
return data.length == 3 && data[0] == 0 && data[1] == 1 && data[2] == 2;
}
}