github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/vm/intpool.go (about)

     1  package vm
     2  
     3  import "math/big"
     4  
     5  var checkVal = big.NewInt(-42)
     6  
     7  const poolLimit = 256
     8  
     9  // intPool is a pool of big integers that
    10  // can be reused for all big.Int operations.
    11  type intPool struct {
    12  	pool *Stack
    13  }
    14  
    15  func newIntPool() *intPool {
    16  	return &intPool{pool: newstack()}
    17  }
    18  
    19  // get retrieves a big int from the pool, allocating one if the pool is empty.
    20  // Note, the returned int's value is arbitrary and will not be zeroed!
    21  func (p *intPool) get() *big.Int {
    22  	if p.pool.len() > 0 {
    23  		return p.pool.pop()
    24  	}
    25  	return new(big.Int)
    26  }
    27  
    28  // getZero retrieves a big int from the pool, setting it to zero or allocating
    29  // a new one if the pool is empty.
    30  func (p *intPool) getZero() *big.Int {
    31  	if p.pool.len() > 0 {
    32  		return p.pool.pop().SetUint64(0)
    33  	}
    34  	return new(big.Int)
    35  }
    36  
    37  // put returns an allocated big int to the pool to be later reused by get calls.
    38  // Note, the values as saved as is; neither put nor get zeroes the ints out!
    39  func (p *intPool) put(is ...*big.Int) {
    40  	if len(p.pool.data) > poolLimit {
    41  		return
    42  	}
    43  	for _, i := range is {
    44  		// verifyPool is a build flag. Pool verification makes sure the integrity
    45  		// of the integer pool by comparing values to a default value.
    46  		if verifyPool {
    47  			i.Set(checkVal)
    48  		}
    49  		p.pool.push(i)
    50  	}
    51  }