github.com/leslie-fei/fastcache@v0.0.0-20240520092641-b7a9eb05711f/utils.go (about)

     1  package fastcache
     2  
     3  import "unsafe"
     4  
     5  // NodeTo node data convert to *T
     6  func NodeTo[T any](node *DataNode) *T {
     7  	dataPtr := uintptr(unsafe.Pointer(node)) + sizeOfDataNode
     8  	return (*T)(unsafe.Pointer(dataPtr))
     9  }
    10  
    11  func RangeNode(base uintptr, offset uint64, f func(node *DataNode) bool) {
    12  	if offset == 0 {
    13  		return
    14  	}
    15  	for node := ToDataNode(base, offset); node != nil; node = ToDataNode(base, offset) {
    16  		if !f(node) {
    17  			return
    18  		}
    19  	}
    20  }
    21  
    22  func ToDataNode(base uintptr, offset uint64) *DataNode {
    23  	if offset == 0 {
    24  		return nil
    25  	}
    26  	return (*DataNode)(unsafe.Pointer(base + uintptr(offset)))
    27  }