github.com/bamzi/go-ethereum@v1.6.7-0.20170704111104-138f26c93af1/core/vm/memory.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import "fmt" 20 21 // Memory implements a simple memory model for the ethereum virtual machine. 22 type Memory struct { 23 store []byte 24 lastGasCost uint64 25 lastReturn []byte 26 } 27 28 func NewMemory() *Memory { 29 return &Memory{} 30 } 31 32 // Set sets offset + size to value 33 func (m *Memory) Set(offset, size uint64, value []byte) { 34 // length of store may never be less than offset + size. 35 // The store should be resized PRIOR to setting the memory 36 if size > uint64(len(m.store)) { 37 panic("INVALID memory: store empty") 38 } 39 40 // It's possible the offset is greater than 0 and size equals 0. This is because 41 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 42 if size > 0 { 43 copy(m.store[offset:offset+size], value) 44 } 45 } 46 47 // Resize resizes the memory to size 48 func (m *Memory) Resize(size uint64) { 49 if uint64(m.Len()) < size { 50 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 51 } 52 } 53 54 // Get returns offset + size as a new slice 55 func (self *Memory) Get(offset, size int64) (cpy []byte) { 56 if size == 0 { 57 return nil 58 } 59 60 if len(self.store) > int(offset) { 61 cpy = make([]byte, size) 62 copy(cpy, self.store[offset:offset+size]) 63 64 return 65 } 66 67 return 68 } 69 70 // GetPtr returns the offset + size 71 func (self *Memory) GetPtr(offset, size int64) []byte { 72 if size == 0 { 73 return nil 74 } 75 76 if len(self.store) > int(offset) { 77 return self.store[offset : offset+size] 78 } 79 80 return nil 81 } 82 83 // Len returns the length of the backing slice 84 func (m *Memory) Len() int { 85 return len(m.store) 86 } 87 88 // Data returns the backing slice 89 func (m *Memory) Data() []byte { 90 return m.store 91 } 92 93 func (m *Memory) Print() { 94 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 95 if len(m.store) > 0 { 96 addr := 0 97 for i := 0; i+32 <= len(m.store); i += 32 { 98 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 99 addr++ 100 } 101 } else { 102 fmt.Println("-- empty --") 103 } 104 fmt.Println("####################") 105 }