github.com/klaytn/klaytn@v1.10.2/blockchain/vm/intpool.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/vm/intpool.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package vm 22 23 import ( 24 "math/big" 25 "sync" 26 ) 27 28 var checkVal = big.NewInt(-42) 29 30 const poolLimit = 256 31 32 // intPool is a pool of big integers that 33 // can be reused for all big.Int operations. 34 type intPool struct { 35 pool *Stack 36 } 37 38 func newIntPool() *intPool { 39 return &intPool{pool: newstack()} 40 } 41 42 // get retrieves a big int from the pool, allocating one if the pool is empty. 43 // Note, the returned int's value is arbitrary and will not be zeroed! 44 func (p *intPool) get() *big.Int { 45 if p.pool.len() > 0 { 46 return p.pool.pop() 47 } 48 return new(big.Int) 49 } 50 51 // getZero retrieves a big int from the pool, setting it to zero or allocating 52 // a new one if the pool is empty. 53 func (p *intPool) getZero() *big.Int { 54 if p.pool.len() > 0 { 55 return p.pool.pop().SetUint64(0) 56 } 57 return new(big.Int) 58 } 59 60 // put returns an allocated big int to the pool to be later reused by get calls. 61 // Note, the values as saved as is; neither put nor get zeroes the ints out! 62 func (p *intPool) put(is ...*big.Int) { 63 if len(p.pool.data) > poolLimit { 64 return 65 } 66 for _, i := range is { 67 // verifyPool is a build flag. Pool verification makes sure the integrity 68 // of the integer pool by comparing values to a default value. 69 if verifyPool { 70 i.Set(checkVal) 71 } 72 p.pool.push(i) 73 } 74 } 75 76 // The intPool pool's default capacity 77 const poolDefaultCap = 25 78 79 // intPoolPool manages a pool of intPools. 80 type intPoolPool struct { 81 pools []*intPool 82 lock sync.Mutex 83 } 84 85 var poolOfIntPools = &intPoolPool{ 86 pools: make([]*intPool, 0, poolDefaultCap), 87 } 88 89 // get is looking for an available pool to return. 90 func (ipp *intPoolPool) get() *intPool { 91 ipp.lock.Lock() 92 defer ipp.lock.Unlock() 93 94 if len(poolOfIntPools.pools) > 0 { 95 ip := ipp.pools[len(ipp.pools)-1] 96 ipp.pools = ipp.pools[:len(ipp.pools)-1] 97 return ip 98 } 99 return newIntPool() 100 } 101 102 // put a pool that has been allocated with get. 103 func (ipp *intPoolPool) put(ip *intPool) { 104 ipp.lock.Lock() 105 defer ipp.lock.Unlock() 106 107 if len(ipp.pools) < cap(ipp.pools) { 108 ipp.pools = append(ipp.pools, ip) 109 } 110 }