github.com/lestrrat-go/jwx/v2@v2.0.21/internal/pool/pool.go (about)

     1  package pool
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"sync"
     7  )
     8  
     9  var bytesBufferPool = sync.Pool{
    10  	New: allocBytesBuffer,
    11  }
    12  
    13  func allocBytesBuffer() interface{} {
    14  	return &bytes.Buffer{}
    15  }
    16  
    17  func GetBytesBuffer() *bytes.Buffer {
    18  	//nolint:forcetypeassert
    19  	return bytesBufferPool.Get().(*bytes.Buffer)
    20  }
    21  
    22  func ReleaseBytesBuffer(b *bytes.Buffer) {
    23  	b.Reset()
    24  	bytesBufferPool.Put(b)
    25  }
    26  
    27  var bigIntPool = sync.Pool{
    28  	New: allocBigInt,
    29  }
    30  
    31  func allocBigInt() interface{} {
    32  	return &big.Int{}
    33  }
    34  
    35  func GetBigInt() *big.Int {
    36  	//nolint:forcetypeassert
    37  	return bigIntPool.Get().(*big.Int)
    38  }
    39  
    40  func ReleaseBigInt(i *big.Int) {
    41  	bigIntPool.Put(i.SetInt64(0))
    42  }
    43  
    44  var keyToErrorMapPool = sync.Pool{
    45  	New: allocKeyToErrorMap,
    46  }
    47  
    48  func allocKeyToErrorMap() interface{} {
    49  	return make(map[string]error)
    50  }
    51  
    52  func GetKeyToErrorMap() map[string]error {
    53  	//nolint:forcetypeassert
    54  	return keyToErrorMapPool.Get().(map[string]error)
    55  }
    56  
    57  func ReleaseKeyToErrorMap(m map[string]error) {
    58  	for key := range m {
    59  		delete(m, key)
    60  	}
    61  }