github.com/cilium/statedb@v0.3.2/part/cache.go (about)

     1  package part
     2  
     3  import "unsafe"
     4  
     5  const nodeMutatedSize = 32 // must be power-of-two
     6  
     7  type nodeMutated[T any] struct {
     8  	ptrs [nodeMutatedSize]*header[T]
     9  	used bool
    10  }
    11  
    12  func (p *nodeMutated[T]) put(ptr *header[T]) {
    13  	ptrInt := uintptr(unsafe.Pointer(ptr))
    14  	p.ptrs[slot(ptrInt)] = ptr
    15  	p.used = true
    16  }
    17  
    18  func (p *nodeMutated[T]) exists(ptr *header[T]) bool {
    19  	ptrInt := uintptr(unsafe.Pointer(ptr))
    20  	return p.ptrs[slot(ptrInt)] == ptr
    21  }
    22  
    23  func slot(p uintptr) int {
    24  	var slot uint8
    25  	// use some relevant bits from the pointer
    26  	slot = slot + uint8(p>>4)
    27  	slot = slot + uint8(p>>12)
    28  	slot = slot + uint8(p>>20)
    29  	return int(slot & (nodeMutatedSize - 1))
    30  }
    31  
    32  func (p *nodeMutated[T]) clear() {
    33  	if p.used {
    34  		clear(p.ptrs[:])
    35  	}
    36  	p.used = false
    37  }