stack.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 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 (
  18. "sync"
  19. "github.com/holiman/uint256"
  20. )
  21. var stackPool = sync.Pool{
  22. New: func() interface{} {
  23. return &Stack{data: make([]uint256.Int, 0, 16)}
  24. },
  25. }
  26. // Stack is an object for basic stack operations. Items popped to the stack are
  27. // expected to be changed and modified. stack does not take care of adding newly
  28. // initialised objects.
  29. type Stack struct {
  30. data []uint256.Int
  31. }
  32. func newstack() *Stack {
  33. return stackPool.Get().(*Stack)
  34. }
  35. func returnStack(s *Stack) {
  36. s.data = s.data[:0]
  37. stackPool.Put(s)
  38. }
  39. // Data returns the underlying uint256.Int array.
  40. func (st *Stack) Data() []uint256.Int {
  41. return st.data
  42. }
  43. func (st *Stack) push(d *uint256.Int) {
  44. // NOTE push limit (1024) is checked in baseCheck
  45. st.data = append(st.data, *d)
  46. }
  47. func (st *Stack) pop() (ret uint256.Int) {
  48. ret = st.data[len(st.data)-1]
  49. st.data = st.data[:len(st.data)-1]
  50. return
  51. }
  52. func (st *Stack) len() int {
  53. return len(st.data)
  54. }
  55. func (st *Stack) swap(n int) {
  56. st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
  57. }
  58. func (st *Stack) dup(n int) {
  59. st.push(&st.data[st.len()-n])
  60. }
  61. func (st *Stack) peek() *uint256.Int {
  62. return &st.data[st.len()-1]
  63. }
  64. // Back returns the n'th item in stack
  65. func (st *Stack) Back(n int) *uint256.Int {
  66. return &st.data[st.len()-n-1]
  67. }