github.com/aaa256/atlantis@v0.0.0-20210707112435-42ee889287a2/core/vm/memory.go (about)

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