2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
import
|
2018-07-05 12:41:01 +00:00
|
|
|
sequtils,
|
|
|
|
eth_common/eth_types,
|
2018-06-15 09:11:25 +00:00
|
|
|
../constants, ../errors, ../logging, ../validation, ../utils/bytes,
|
|
|
|
./utils/utils_numeric
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Memory* = ref object
|
|
|
|
logger*: Logger
|
2018-01-29 17:40:22 +00:00
|
|
|
bytes*: seq[byte]
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
proc newMemory*: Memory =
|
|
|
|
new(result)
|
|
|
|
result.bytes = @[]
|
2018-02-07 09:50:15 +00:00
|
|
|
result.logger = logging.getLogger("memory.Memory")
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
proc len*(memory: Memory): int =
|
2018-02-06 19:20:06 +00:00
|
|
|
result = memory.bytes.len
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-05-07 12:41:54 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc extend*(memory: var Memory; startPos: Natural; size: Natural) =
|
2018-01-16 17:05:20 +00:00
|
|
|
if size == 0:
|
|
|
|
return
|
2018-07-05 12:41:01 +00:00
|
|
|
var newSize = ceil32(startPos + size)
|
2018-05-25 10:25:19 +00:00
|
|
|
if newSize <= len(memory):
|
2018-01-16 17:05:20 +00:00
|
|
|
return
|
2018-05-25 10:25:19 +00:00
|
|
|
var sizeToExtend = newSize - len(memory)
|
|
|
|
memory.bytes = memory.bytes.concat(repeat(0.byte, sizeToExtend))
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
proc newMemory*(size: Natural): Memory =
|
2018-01-24 13:31:24 +00:00
|
|
|
result = newMemory()
|
2018-05-25 10:25:19 +00:00
|
|
|
result.extend(0, size)
|
2018-01-24 13:31:24 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc read*(memory: var Memory, startPos: Natural, size: Natural): seq[byte] =
|
|
|
|
result = memory.bytes[startPos ..< (startPos + size)]
|
2018-01-22 22:23:07 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc write*(memory: var Memory, startPos: Natural, value: openarray[byte]) =
|
2018-05-30 16:11:15 +00:00
|
|
|
let size = value.len
|
2018-02-06 19:20:06 +00:00
|
|
|
if size == 0:
|
|
|
|
return
|
2018-04-06 14:52:10 +00:00
|
|
|
#echo size
|
2018-07-05 12:41:01 +00:00
|
|
|
#echo startPos
|
|
|
|
#validateGte(startPos, 0)
|
2018-02-20 17:27:43 +00:00
|
|
|
#validateGte(size, 0)
|
2018-07-05 12:41:01 +00:00
|
|
|
validateLte(startPos + size, memory.len)
|
2018-02-06 19:20:06 +00:00
|
|
|
let index = memory.len
|
2018-07-05 12:41:01 +00:00
|
|
|
if memory.len < startPos + size:
|
|
|
|
memory.bytes = memory.bytes.concat(repeat(0.byte, memory.len - (startPos + size))) # TODO: better logarithmic scaling?
|
2018-02-06 19:20:06 +00:00
|
|
|
|
|
|
|
for z, b in value:
|
2018-07-05 12:41:01 +00:00
|
|
|
memory.bytes[z + startPos] = b
|
2018-01-29 17:40:22 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc writePaddingBytes*(memory: var Memory,
|
|
|
|
startPos, numberOfBytes: Natural,
|
|
|
|
paddingValue = 0.byte) =
|
|
|
|
let endPos = startPos + numberOfBytes
|
|
|
|
assert endPos < memory.len
|
|
|
|
for i in startPos ..< endPos:
|
|
|
|
memory.bytes[i] = paddingValue
|
|
|
|
|
|
|
|
template write*(memory: var Memory, startPos: Natural, size: Natural, value: cstring) =
|
|
|
|
memory.write(startPos, value.toBytes)
|
2018-06-15 09:11:25 +00:00
|
|
|
# TODO ~ O(n^3):
|
|
|
|
# - there is a string allocation with $ (?)
|
|
|
|
# - then a conversion to seq (= new allocation) with toBytes
|
|
|
|
# - then writing to memory
|