github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/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 type Memory struct { 22 store []byte 23 } 24 25 func NewMemory() *Memory { 26 return &Memory{nil} 27 } 28 29 func (m *Memory) Set(offset, size uint64, value []byte) { 30 // length of store may never be less than offset + size. 31 // The store should be resized PRIOR to setting the memory 32 if size > uint64(len(m.store)) { 33 panic("INVALID memory: store empty") 34 } 35 36 // It's possible the offset is greater than 0 and size equals 0. This is because 37 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 38 if size > 0 { 39 copy(m.store[offset:offset+size], value) 40 } 41 } 42 43 func (m *Memory) Resize(size uint64) { 44 if uint64(m.Len()) < size { 45 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 46 } 47 } 48 49 func (self *Memory) Get(offset, size int64) (cpy []byte) { 50 if size == 0 { 51 return nil 52 } 53 54 if len(self.store) > int(offset) { 55 cpy = make([]byte, size) 56 copy(cpy, self.store[offset:offset+size]) 57 58 return 59 } 60 61 return 62 } 63 64 func (self *Memory) GetPtr(offset, size int64) []byte { 65 if size == 0 { 66 return nil 67 } 68 69 if len(self.store) > int(offset) { 70 return self.store[offset : offset+size] 71 } 72 73 return nil 74 } 75 76 func (m *Memory) Len() int { 77 return len(m.store) 78 } 79 80 func (m *Memory) Data() []byte { 81 return m.store 82 } 83 84 func (m *Memory) Print() { 85 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 86 if len(m.store) > 0 { 87 addr := 0 88 for i := 0; i+32 <= len(m.store); i += 32 { 89 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 90 addr++ 91 } 92 } else { 93 fmt.Println("-- empty --") 94 } 95 fmt.Println("####################") 96 }