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

     1  package fastcache
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  //go:linkname memmove runtime.memmove
     8  //go:noescape
     9  func memmove(dst, src unsafe.Pointer, size uintptr)
    10  
    11  //go:linkname memequal runtime.memequal
    12  //go:noescape
    13  func memequal(a, b unsafe.Pointer, size uintptr) bool
    14  
    15  const (
    16  	KB = 1024
    17  	MB = 1024 * KB
    18  	GB = 1024 * MB
    19  )
    20  
    21  // Memory 内存块抽象
    22  type Memory interface {
    23  	// Attach attach memory
    24  	Attach() error
    25  	// Detach detach memory
    26  	Detach() error
    27  	// Ptr first ptr
    28  	Ptr() unsafe.Pointer
    29  	// Size memory total size
    30  	Size() uint64
    31  	// PtrOffset offset get ptr
    32  	PtrOffset(offset uint64) unsafe.Pointer
    33  	// Travel memory
    34  	Travel(skipOffset uint64, fn func(ptr unsafe.Pointer, size uint64) uint64)
    35  }