memory.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. package vm
  17. import "fmt"
  18. // Memory implements a simple memory model for the ethereum virtual machine.
  19. type Memory struct {
  20. store []byte
  21. lastGasCost uint64
  22. lastReturn []byte
  23. }
  24. func NewMemory() *Memory {
  25. return &Memory{}
  26. }
  27. // Set sets offset + size to value
  28. func (m *Memory) Set(offset, size uint64, value []byte) {
  29. // length of store may never be less than offset + size.
  30. // The store should be resized PRIOR to setting the memory
  31. if size > uint64(len(m.store)) {
  32. panic("INVALID memory: store empty")
  33. }
  34. // It's possible the offset is greater than 0 and size equals 0. This is because
  35. // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
  36. if size > 0 {
  37. copy(m.store[offset:offset+size], value)
  38. }
  39. }
  40. // Resize resizes the memory to size
  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. // Get returns offset + size as a new slice
  47. func (self *Memory) Get(offset, size int64) (cpy []byte) {
  48. if size == 0 {
  49. return nil
  50. }
  51. if len(self.store) > int(offset) {
  52. cpy = make([]byte, size)
  53. copy(cpy, self.store[offset:offset+size])
  54. return
  55. }
  56. return
  57. }
  58. // GetPtr returns the offset + size
  59. func (self *Memory) GetPtr(offset, size int64) []byte {
  60. if size == 0 {
  61. return nil
  62. }
  63. if len(self.store) > int(offset) {
  64. return self.store[offset : offset+size]
  65. }
  66. return nil
  67. }
  68. // Len returns the length of the backing slice
  69. func (m *Memory) Len() int {
  70. return len(m.store)
  71. }
  72. // Data returns the backing slice
  73. func (m *Memory) Data() []byte {
  74. return m.store
  75. }
  76. func (m *Memory) Print() {
  77. fmt.Printf("### mem %d bytes ###\n", len(m.store))
  78. if len(m.store) > 0 {
  79. addr := 0
  80. for i := 0; i+32 <= len(m.store); i += 32 {
  81. fmt.Printf("%03d: % x\n", addr, m.store[i:i+32])
  82. addr++
  83. }
  84. } else {
  85. fmt.Println("-- empty --")
  86. }
  87. fmt.Println("####################")
  88. }