github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/vm/memory.go (about) 1 package vm 2 3 import ( 4 "fmt" 5 "math/big" 6 7 "github.com/neatio-net/neatio/utilities/common/math" 8 ) 9 10 type Memory struct { 11 store []byte 12 lastGasCost uint64 13 } 14 15 func NewMemory() *Memory { 16 return &Memory{} 17 } 18 19 func (m *Memory) Set(offset, size uint64, value []byte) { 20 21 if size > 0 { 22 23 if offset+size > uint64(len(m.store)) { 24 panic("invalid memory: store empty") 25 } 26 copy(m.store[offset:offset+size], value) 27 } 28 } 29 30 func (m *Memory) Set32(offset uint64, val *big.Int) { 31 32 if offset+32 > uint64(len(m.store)) { 33 panic("invalid memory: store empty") 34 } 35 36 copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) 37 38 math.ReadBits(val, m.store[offset:offset+32]) 39 } 40 41 func (m *Memory) Resize(size uint64) { 42 if uint64(m.Len()) < size { 43 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 44 } 45 } 46 47 func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { 48 if size == 0 { 49 return nil 50 } 51 52 if len(m.store) > int(offset) { 53 cpy = make([]byte, size) 54 copy(cpy, m.store[offset:offset+size]) 55 56 return 57 } 58 59 return 60 } 61 62 func (m *Memory) GetPtr(offset, size int64) []byte { 63 if size == 0 { 64 return nil 65 } 66 67 if len(m.store) > int(offset) { 68 return m.store[offset : offset+size] 69 } 70 71 return nil 72 } 73 74 func (m *Memory) Len() int { 75 return len(m.store) 76 } 77 78 func (m *Memory) Data() []byte { 79 return m.store 80 } 81 82 func (m *Memory) Print() { 83 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 84 if len(m.store) > 0 { 85 addr := 0 86 for i := 0; i+32 <= len(m.store); i += 32 { 87 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 88 addr++ 89 } 90 } else { 91 fmt.Println("-- empty --") 92 } 93 fmt.Println("####################") 94 }