|
6 | 6 |
|
7 | 7 | **Source file:** [Memory.sol](../../src/unsafe/Memory.sol)
|
8 | 8 |
|
9 |
| -**Example usage:** [MemoryExamples.sol](../../examples/unsafe/MemoryExamples.sol) |
10 |
| - |
11 | 9 | **Tests source file:** [memory.sol](../../test/unsafe/memory.sol)
|
12 | 10 |
|
13 | 11 | **Perf (gas usage) source file:** [memory.sol](../../perf/unsafe/memory.sol)
|
|
18 | 16 | The `unsafe` package contains code that performs potentially unsafe operations, for example reading and writing directly from memory. The memory library is used to work with memory directly; there are methods for copying memory, equals-checks, and converting from and to Solidity types.
|
19 | 17 |
|
20 | 18 | In these docs, the word stored at memory address `n` is denoted `Memory[n]`. It is implicitly of type `uint`, but the regular index access syntax is used to point to individual bytes, e.g. byte number `m` of the word stored at address `n` is `Memory[n][m]`. Additionally, the free memory pointer is referred to `FMP`.
|
21 |
| - |
22 |
| -## Functions |
23 |
| - |
24 |
| -- [equals(uint, uint, uint)](#equalsuint-uint-uint) |
25 |
| -- [equals(uint, uint, bytes memory)](#equalsuint-uint-bytes-memory) |
26 |
| -- [allocate(uint)](#allocateuint) |
27 |
| -- [copy(uint, uint, uint)](#copyuint-uint-uint) |
28 |
| -- [ptr(bytes)](#ptrbytes) |
29 |
| -- [dataPtr(bytes)](#dataptrbytes) |
30 |
| -- [fromBytes(bytes)](#frombytesbytes) |
31 |
| -- [toBytes(uint, uint)](#tobytesuint-uint) |
32 |
| -- [toUint(uint)](#touintuint) |
33 |
| -- [toBytes32(uint)](#tobytes32uint) |
34 |
| -- [toByte(uint, uint8)](#tobyteuint-uint8) |
35 |
| - |
36 |
| -*** |
37 |
| - |
38 |
| -### equals(uint, uint, uint) |
39 |
| - |
40 |
| -`function equals(uint addr, uint addr2, uint len) internal pure returns (bool)` |
41 |
| - |
42 |
| -Checks if the data stored in two segments of memory is the same. |
43 |
| - |
44 |
| -##### params |
45 |
| - |
46 |
| -- `uint addr`: The first memory address. |
47 |
| -- `uint addr2`: The second memory address. |
48 |
| -- `uint len`: The number of bytes to check. |
49 |
| - |
50 |
| - |
51 |
| -##### returns |
52 |
| - |
53 |
| -- `bool`: `true` if the bytes are equal, otherwise false. |
54 |
| - |
55 |
| -##### ensures |
56 |
| - |
57 |
| -- `equals(addr1, addr2, len) => 'n' in [0, len), Memory[addr1 + n][0] == Memory[addr2 + n][0]` |
58 |
| -##### gascosts |
59 |
| - |
60 |
| -- Equals check of 16 bytes: **149** |
61 |
| -- Equals check of one word: **149** |
62 |
| -- Equals check of ten words: **278** |
63 |
| -- Equals check of one hundred words: **1647** |
64 |
| - |
65 |
| -*** |
66 |
| - |
67 |
| -### equals(uint, uint, bytes memory) |
68 |
| - |
69 |
| -`function equals(uint addr, uint len, bytes memory bts) internal pure returns (bool)` |
70 |
| - |
71 |
| -Checks if the data stored in a segment of memory is the same as (part of) the data stored by a `bytes memory`. |
72 |
| - |
73 |
| -##### params |
74 |
| - |
75 |
| -- `uint addr`: The memory address. |
76 |
| -- `uint len`: The number of bytes to check. |
77 |
| -- `bytes memory bts`: The bytes array. |
78 |
| - |
79 |
| -##### requires |
80 |
| - |
81 |
| -- len <= bts.length |
82 |
| - |
83 |
| -##### returns |
84 |
| - |
85 |
| -- `bool`: `true` if the bytes are equal, otherwise false. |
86 |
| - |
87 |
| -##### ensures |
88 |
| - |
89 |
| -- `equals(addr, len, bts) => 'n' in [0, len), Memory[addr + n][0] == uint8(bts[n])` |
90 |
| -##### gascosts |
91 |
| - |
92 |
| -- Equals check of one word: **256** |
93 |
| - |
94 |
| -*** |
95 |
| - |
96 |
| -### allocate(uint) |
97 |
| - |
98 |
| -`function allocate(uint numBytes) internal pure returns (uint addr)` |
99 |
| - |
100 |
| -Allocates a section of memory, and initializes the bytes to `0`. |
101 |
| - |
102 |
| -##### params |
103 |
| - |
104 |
| -- `uint numBytes`: The number of bytes that should be allocated. |
105 |
| - |
106 |
| - |
107 |
| -##### returns |
108 |
| - |
109 |
| -- `uint addr`: The (starting) address of the newly allocated segment. |
110 |
| - |
111 |
| -##### ensures |
112 |
| - |
113 |
| -- `addr = allocate(len) => FMP += len && for 'n' in [0, len), Memory[addr + n] == 0` |
114 |
| -##### gascosts |
115 |
| - |
116 |
| -- Allocation of one word: **245** |
117 |
| -- Allocation of ten words: **983** |
118 |
| - |
119 |
| -*** |
120 |
| - |
121 |
| -### copy(uint, uint, uint) |
122 |
| - |
123 |
| -`function copy(uint src, uint dest, uint len) internal pure` |
124 |
| - |
125 |
| -Copy a segment of memory. |
126 |
| - |
127 |
| -##### params |
128 |
| - |
129 |
| -- `uint src`: The source address. |
130 |
| -- `uint dest`: The destination address. |
131 |
| -- `uint len`: The number of bytes to copy. |
132 |
| - |
133 |
| - |
134 |
| - |
135 |
| -##### ensures |
136 |
| - |
137 |
| -- `copy(src, dest, len) => for 'n' in [0, len), Memory[dest + n][0] == Memory[src + n][0]` |
138 |
| -##### gascosts |
139 |
| - |
140 |
| -- Copying 16 bytes: **210** |
141 |
| -- Copying one word: **307** |
142 |
| -- Copying ten words: **1153** |
143 |
| - |
144 |
| -*** |
145 |
| - |
146 |
| -### ptr(bytes) |
147 |
| - |
148 |
| -`function ptr(bytes bts) internal pure returns (uint addr)` |
149 |
| - |
150 |
| -Get a memory pointer to a `bytes` array. |
151 |
| - |
152 |
| -##### params |
153 |
| - |
154 |
| -- `bytes bts`: The `bytes` array. |
155 |
| - |
156 |
| - |
157 |
| -##### returns |
158 |
| - |
159 |
| -- `uint addr`: The pointer to the `bytes` in memory. |
160 |
| - |
161 |
| -##### gascosts |
162 |
| - |
163 |
| -- Fixed: **55** |
164 |
| - |
165 |
| -*** |
166 |
| - |
167 |
| -### dataPtr(bytes) |
168 |
| - |
169 |
| -`function dataPtr(bytes bts) internal pure returns (uint addr)` |
170 |
| - |
171 |
| -Get a memory pointer to the data section of a `bytes` array. |
172 |
| - |
173 |
| -##### params |
174 |
| - |
175 |
| -- `bytes bts`: The `bytes` array. |
176 |
| - |
177 |
| - |
178 |
| -##### returns |
179 |
| - |
180 |
| -- `uint addr`: The pointer to the data section of the `bytes` in memory. |
181 |
| - |
182 |
| -##### gascosts |
183 |
| - |
184 |
| -- Fixed: **61** |
185 |
| - |
186 |
| -*** |
187 |
| - |
188 |
| -### fromBytes(bytes) |
189 |
| - |
190 |
| -`function fromBytes(bytes bts) internal pure returns (uint addruint len)` |
191 |
| - |
192 |
| -Get the address of the data stored by a `bytes memory`, and its length. |
193 |
| - |
194 |
| -##### params |
195 |
| - |
196 |
| -- `bytes bts`: The `bytes`. |
197 |
| - |
198 |
| - |
199 |
| -##### returns |
200 |
| - |
201 |
| -- `uint addr`: The pointer to the data section of the `bytes` in memory. |
202 |
| -- `uint len`: The length of the `bytes`. |
203 |
| - |
204 |
| -##### gascosts |
205 |
| - |
206 |
| -- Fixed: **77** |
207 |
| - |
208 |
| -*** |
209 |
| - |
210 |
| -### toBytes(uint, uint) |
211 |
| - |
212 |
| -`function toBytes(uint addr, uint len) internal pure returns (bytes memory bts)` |
213 |
| - |
214 |
| -Create a `bytes memory` from a starting address and a length. NOTE this will copy the bytes. |
215 |
| - |
216 |
| -##### params |
217 |
| - |
218 |
| -- `uint addr`: The memory address. |
219 |
| -- `uint len`: The length. |
220 |
| - |
221 |
| - |
222 |
| -##### returns |
223 |
| - |
224 |
| -- `bytes memory bts`: The new `bytes` array. |
225 |
| - |
226 |
| -##### gascosts |
227 |
| - |
228 |
| -- Create a one word long byte array: **532** |
229 |
| - |
230 |
| -*** |
231 |
| - |
232 |
| -### toUint(uint) |
233 |
| - |
234 |
| -`function toUint(uint addr) internal pure returns (uint n)` |
235 |
| - |
236 |
| -Get a word of memory as a `uint` |
237 |
| - |
238 |
| -##### params |
239 |
| - |
240 |
| -- `uint addr`: The memory address. |
241 |
| - |
242 |
| - |
243 |
| -##### returns |
244 |
| - |
245 |
| -- `uint n`: The word as a `uint`. |
246 |
| - |
247 |
| -##### gascosts |
248 |
| - |
249 |
| -- Fixed: **58** |
250 |
| - |
251 |
| -*** |
252 |
| - |
253 |
| -### toBytes32(uint) |
254 |
| - |
255 |
| -`function toBytes32(uint addr) internal pure returns (bytes32 bts)` |
256 |
| - |
257 |
| -Get a word of memory as a `bytes32` |
258 |
| - |
259 |
| -##### params |
260 |
| - |
261 |
| -- `uint addr`: The memory address. |
262 |
| - |
263 |
| - |
264 |
| -##### returns |
265 |
| - |
266 |
| -- `bytes32 bts`: The word as a `bytes32`. |
267 |
| - |
268 |
| -##### gascosts |
269 |
| - |
270 |
| -- Fixed: **58** |
271 |
| - |
272 |
| -*** |
273 |
| - |
274 |
| -### toByte(uint, uint8) |
275 |
| - |
276 |
| -`function toByte(uint addr, uint8 index) internal pure returns (byte b)` |
277 |
| - |
278 |
| -Get a single byte from memory. |
279 |
| - |
280 |
| -##### params |
281 |
| - |
282 |
| -- `uint addr`: The memory address. |
283 |
| -- `uint8 index`: The index of the byte. |
284 |
| - |
285 |
| -##### requires |
286 |
| - |
287 |
| -- `0 <= index < 32` |
288 |
| - |
289 |
| -##### returns |
290 |
| - |
291 |
| -- `byte b`: The byte. |
292 |
| - |
293 |
| -##### ensures |
294 |
| - |
295 |
| -- `b = toByte(addr, index) => b == toBytes32(addr)[index]` |
296 |
| -##### gascosts |
297 |
| - |
298 |
| -- Fixed: **125** |
299 |
| - |
0 commit comments