-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
55 lines (44 loc) · 1.25 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package riverdb
import (
"github.com/246859/river/entry"
"unsafe"
)
//go:noescape
//go:linkname memhash runtime.memhash
func memhash(p unsafe.Pointer, h, s uintptr) uintptr
// FastRand is a fast thread local random function.
//
//go:linkname FastRand runtime.fastrand
func FastRand() uint32
var h = FastRand()
// memHash is the hash function used by go map, it utilizes available hardware instructions(behaves
// as aeshash if aes instruction is available).
// NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func memHash(data []byte) uint64 {
ptr := unsafe.Pointer(unsafe.SliceData(data))
return uint64(memhash(ptr, uintptr(h), uintptr(len(data))))
}
func isExpiredOrDeleted(en entry.Entry) bool {
return entry.IsExpired(en.TTL) || en.Type != entry.DataEntryType
}
// BitFlag 64-bit mask is used to store different status information
type BitFlag uint64
func (bf *BitFlag) Store(flags ...uint64) {
for _, flag := range flags {
*bf |= BitFlag(flag)
}
}
func (bf *BitFlag) Check(flags ...uint64) bool {
var f uint64
for _, flag := range flags {
f |= flag
}
return *bf&BitFlag(f) != 0
}
func (bf *BitFlag) Revoke(flags ...uint64) {
var f uint64
for _, flag := range flags {
f |= flag
}
*bf ^= BitFlag(f)
}