github.com/hsfzxjy/dgo/go@v0.2.0/keepalive/holder.go (about)

     1  package keepalive
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"unsafe"
     7  )
     8  
     9  type node struct {
    10  	data unsafe.Pointer
    11  	next *node
    12  }
    13  
    14  var pool = sync.Pool{
    15  	New: func() any { return new(node) },
    16  }
    17  
    18  type Holder struct {
    19  	head *node
    20  }
    21  
    22  func (h *Holder) Add(data unsafe.Pointer) {
    23  	node := pool.Get().(*node)
    24  	node.data = data
    25  	node.next = h.head
    26  	h.head = node
    27  }
    28  
    29  func (h *Holder) AddString(s string) {
    30  	node := pool.Get().(*node)
    31  	sheader := (*reflect.StringHeader)(unsafe.Pointer(&s))
    32  	node.data = unsafe.Pointer(sheader.Data)
    33  	node.next = h.head
    34  	h.head = node
    35  }
    36  
    37  func (h Holder) Free() {
    38  	n := h.head
    39  	for n != nil {
    40  		p := n.next
    41  		n.data = nil
    42  		n.next = nil
    43  		pool.Put(n)
    44  		n = p
    45  	}
    46  }