Last active
August 29, 2015 14:25
-
-
Save obscuren/2f3d98dcab675134e7d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type IntPool struct { | |
pool chan *big.Int | |
} | |
// NewPool creates a new pool of big.Int | |
func NewIntPool(max int) *IntPool { | |
return &IntPool{ | |
pool: make(chan *big.Int, max), | |
} | |
} | |
// Borrow an Int from the pool. | |
func (p *IntPool) Borrow() *big.Int { | |
var c *big.Int | |
select { | |
case c = <-p.pool: | |
default: | |
c = new(big.Int) | |
} | |
return c | |
} | |
// Return returns an Intto the pool. | |
func (p *IntPool) Return(i ...*big.Int) { | |
for _, c := range i { | |
select { | |
case p.pool <- c: | |
default: | |
// let it go, let it go... | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment