-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr.go
59 lines (48 loc) · 1.04 KB
/
ptr.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
56
57
58
59
package com
import "unsafe"
// PtrFlag defines the metadata used to encode pointers.
type PtrFlag byte
// These constants determine the values of PtrFlag.
const (
NotNil PtrFlag = iota
Nil
Mapping
)
// NewPtrMap creates a new PtrMap.
func NewPtrMap() *PtrMap {
return &PtrMap{
m: make(map[unsafe.Pointer]int),
}
}
// PtrMap helps to implement pointer mapping.
type PtrMap struct {
sn int
m map[unsafe.Pointer]int
}
func (m *PtrMap) Put(ptr unsafe.Pointer) (key int) {
key = m.sn
m.m[ptr] = key
m.sn++
return
}
func (m *PtrMap) Get(ptr unsafe.Pointer) (key int, pst bool) {
key, pst = m.m[ptr]
return
}
// NewReversePtrMap creates a new ReversePtrMap.
func NewReversePtrMap() ReversePtrMap {
return ReversePtrMap{
m: make(map[int]unsafe.Pointer),
}
}
// ReversePtrMap helps to implement pointer mapping.
type ReversePtrMap struct {
m map[int]unsafe.Pointer
}
func (mp ReversePtrMap) Put(key int, ptr unsafe.Pointer) {
mp.m[key] = ptr
}
func (mp ReversePtrMap) Get(key int) (ptr unsafe.Pointer, pst bool) {
ptr, pst = mp.m[key]
return
}