github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/vm/intpool.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package vm
    13  
    14  import "math/big"
    15  
    16  var checkVal = big.NewInt(-42)
    17  
    18  const poolLimit = 256
    19  
    20  // intPool is a pool of big integers that
    21  // can be reused for all big.Int operations.
    22  type intPool struct {
    23  	pool *Stack
    24  }
    25  
    26  func newIntPool() *intPool {
    27  	return &intPool{pool: newstack()}
    28  }
    29  
    30  func (p *intPool) get() *big.Int {
    31  	if p.pool.len() > 0 {
    32  		return p.pool.pop()
    33  	}
    34  	return new(big.Int)
    35  }
    36  func (p *intPool) put(is ...*big.Int) {
    37  	if len(p.pool.data) > poolLimit {
    38  		return
    39  	}
    40  
    41  	for _, i := range is {
    42  		// verifyPool is a build flag. Pool verification makes sure the integrity
    43  		// of the integer pool by comparing values to a default value.
    44  		if verifyPool {
    45  			i.Set(checkVal)
    46  		}
    47  
    48  		p.pool.push(i)
    49  	}
    50  }