github.com/puellanivis/breton@v0.2.16/lib/glog/bufpool.go (about)

     1  package glog
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"sync"
     7  )
     8  
     9  var bufPool = sync.Pool{
    10  	New: func() interface{} {
    11  		return new(bytes.Buffer)
    12  	},
    13  }
    14  
    15  // getBuffer returns a new, ready-to-use buffer.
    16  func getBuffer() *bytes.Buffer {
    17  	b := bufPool.Get().(*bytes.Buffer)
    18  	b.Reset()
    19  	return b
    20  }
    21  
    22  // putBuffer returns a buffer to the free list.
    23  func putBuffer(b *bytes.Buffer) {
    24  	if b.Len() >= 256 {
    25  		// Let big buffers die a natural death.
    26  		return
    27  	}
    28  
    29  	bufPool.Put(b)
    30  }
    31  
    32  func writeTwo(buf *bytes.Buffer, i int) {
    33  	if i < 10 {
    34  		buf.WriteByte('0')
    35  	}
    36  	buf.WriteString(strconv.Itoa(i))
    37  }
    38  
    39  func ensureNL(buf *bytes.Buffer) {
    40  	b := buf.Bytes()
    41  	if b[len(b)-1] != '\n' {
    42  		buf.WriteByte('\n')
    43  	}
    44  }