github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/intpool.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:36</date> 10 //</624450082627915776> 11 12 13 package vm 14 15 import ( 16 "math/big" 17 "sync" 18 ) 19 20 var checkVal = big.NewInt(-42) 21 22 const poolLimit = 256 23 24 //intpool是一个包含大整数的池, 25 //可用于所有大的.int操作。 26 type intPool struct { 27 pool *Stack 28 } 29 30 func newIntPool() *intPool { 31 return &intPool{pool: newstack()} 32 } 33 34 //get从池中检索一个大整数,如果池为空,则分配一个整数。 35 //注意,返回的int值是任意的,不会归零! 36 func (p *intPool) get() *big.Int { 37 if p.pool.len() > 0 { 38 return p.pool.pop() 39 } 40 return new(big.Int) 41 } 42 43 //GetZero从池中检索一个大整数,将其设置为零或分配 44 //如果池是空的,就换一个新的。 45 func (p *intPool) getZero() *big.Int { 46 if p.pool.len() > 0 { 47 return p.pool.pop().SetUint64(0) 48 } 49 return new(big.Int) 50 } 51 52 //Put返回一个分配给池的大int,以便稍后由get调用重用。 53 //注意,保存为原样的值;既不放置也不获取零,整数都不存在! 54 func (p *intPool) put(is ...*big.Int) { 55 if len(p.pool.data) > poolLimit { 56 return 57 } 58 for _, i := range is { 59 //VerifyPool是一个生成标志。池验证确保完整性 60 //通过将值与默认值进行比较来获得整数池的值。 61 if verifyPool { 62 i.Set(checkVal) 63 } 64 p.pool.push(i) 65 } 66 } 67 68 //Intpool池的默认容量 69 const poolDefaultCap = 25 70 71 //IntpoolPool管理Intpools池。 72 type intPoolPool struct { 73 pools []*intPool 74 lock sync.Mutex 75 } 76 77 var poolOfIntPools = &intPoolPool{ 78 pools: make([]*intPool, 0, poolDefaultCap), 79 } 80 81 //GET正在寻找可返回的可用池。 82 func (ipp *intPoolPool) get() *intPool { 83 ipp.lock.Lock() 84 defer ipp.lock.Unlock() 85 86 if len(poolOfIntPools.pools) > 0 { 87 ip := ipp.pools[len(ipp.pools)-1] 88 ipp.pools = ipp.pools[:len(ipp.pools)-1] 89 return ip 90 } 91 return newIntPool() 92 } 93 94 //放置已分配GET的池。 95 func (ipp *intPoolPool) put(ip *intPool) { 96 ipp.lock.Lock() 97 defer ipp.lock.Unlock() 98 99 if len(ipp.pools) < cap(ipp.pools) { 100 ipp.pools = append(ipp.pools, ip) 101 } 102 } 103