github.com/shaoyuan1943/gokcp@v0.0.0-20201026071748-5b36d4c87f61/pool.go (about)

     1  package gokcp
     2  
     3  import "sync"
     4  
     5  var (
     6  	segmentPool sync.Pool
     7  )
     8  
     9  // size is MSS
    10  func getSegment(size uint32) *segment {
    11  	if segmentPool.New == nil {
    12  		segmentPool.New = func() interface{} {
    13  			s := &segment{}
    14  			s.dataBuffer = make([]byte, int(size))
    15  			s.dataBuffer = s.dataBuffer[:0]
    16  			return s
    17  		}
    18  	}
    19  
    20  	return segmentPool.Get().(*segment)
    21  }
    22  
    23  func putSegment(seg *segment) {
    24  	seg.reset()
    25  	segmentPool.Put(seg)
    26  }