github.com/ethereum/go-ethereum@v1.16.1/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 (
    20  	"sync"
    21  
    22  	"github.com/holiman/uint256"
    23  )
    24  
    25  var memoryPool = sync.Pool{
    26  	New: func() any {
    27  		return &Memory{}
    28  	},
    29  }
    30  
    31  // Memory implements a simple memory model for the ethereum virtual machine.
    32  type Memory struct {
    33  	store       []byte
    34  	lastGasCost uint64
    35  }
    36  
    37  // NewMemory returns a new memory model.
    38  func NewMemory() *Memory {
    39  	return memoryPool.Get().(*Memory)
    40  }
    41  
    42  // Free returns the memory to the pool.
    43  func (m *Memory) Free() {
    44  	// To reduce peak allocation, return only smaller memory instances to the pool.
    45  	const maxBufferSize = 16 << 10
    46  	if cap(m.store) <= maxBufferSize {
    47  		m.store = m.store[:0]
    48  		m.lastGasCost = 0
    49  		memoryPool.Put(m)
    50  	}
    51  }
    52  
    53  // Set sets offset + size to value
    54  func (m *Memory) Set(offset, size uint64, value []byte) {
    55  	// It's possible the offset is greater than 0 and size equals 0. This is because
    56  	// the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
    57  	if size > 0 {
    58  		// length of store may never be less than offset + size.
    59  		// The store should be resized PRIOR to setting the memory
    60  		if offset+size > uint64(len(m.store)) {
    61  			panic("invalid memory: store empty")
    62  		}
    63  		copy(m.store[offset:offset+size], value)
    64  	}
    65  }
    66  
    67  // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
    68  // 32 bytes.
    69  func (m *Memory) Set32(offset uint64, val *uint256.Int) {
    70  	// length of store may never be less than offset + size.
    71  	// The store should be resized PRIOR to setting the memory
    72  	if offset+32 > uint64(len(m.store)) {
    73  		panic("invalid memory: store empty")
    74  	}
    75  	// Fill in relevant bits
    76  	val.PutUint256(m.store[offset:])
    77  }
    78  
    79  // Resize resizes the memory to size
    80  func (m *Memory) Resize(size uint64) {
    81  	if uint64(m.Len()) < size {
    82  		m.store = append(m.store, make([]byte, size-uint64(m.Len()))...)
    83  	}
    84  }
    85  
    86  // GetCopy returns offset + size as a new slice
    87  func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {
    88  	if size == 0 {
    89  		return nil
    90  	}
    91  
    92  	// memory is always resized before being accessed, no need to check bounds
    93  	cpy = make([]byte, size)
    94  	copy(cpy, m.store[offset:offset+size])
    95  	return
    96  }
    97  
    98  // GetPtr returns the offset + size
    99  func (m *Memory) GetPtr(offset, size uint64) []byte {
   100  	if size == 0 {
   101  		return nil
   102  	}
   103  
   104  	// memory is always resized before being accessed, no need to check bounds
   105  	return m.store[offset : offset+size]
   106  }
   107  
   108  // Len returns the length of the backing slice
   109  func (m *Memory) Len() int {
   110  	return len(m.store)
   111  }
   112  
   113  // Data returns the backing slice
   114  func (m *Memory) Data() []byte {
   115  	return m.store
   116  }
   117  
   118  // Copy copies data from the src position slice into the dst position.
   119  // The source and destination may overlap.
   120  // OBS: This operation assumes that any necessary memory expansion has already been performed,
   121  // and this method may panic otherwise.
   122  func (m *Memory) Copy(dst, src, len uint64) {
   123  	if len == 0 {
   124  		return
   125  	}
   126  	copy(m.store[dst:], m.store[src:src+len])
   127  }