-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
91 lines (78 loc) · 1.49 KB
/
cache.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package loc
import "sync"
type (
nfl struct {
name string
file string
line int
}
)
var (
locmu sync.Mutex
locc = map[PC]nfl{}
)
// NameFileLine returns function name, file and line number for location.
//
// This works only in the same binary where location was captured.
//
// This functions is a little bit modified version of runtime.(*Frames).Next().
func (l PC) NameFileLine() (name, file string, line int) {
if l == 0 {
return
}
locmu.Lock()
c, ok := locc[l]
locmu.Unlock()
if ok {
return c.name, c.file, c.line
}
name, file, line = l.nameFileLine()
if file != "" {
file = cropFilename(file, name)
}
locmu.Lock()
locc[l] = nfl{
name: name,
file: file,
line: line,
}
locmu.Unlock()
return
}
// SetCache sets name, file and line for the PC.
// It allows to work with PC in another binary the same as in original.
func SetCache(l PC, name, file string, line int) {
locmu.Lock()
if name == "" && file == "" && line == 0 {
delete(locc, l)
} else {
locc[l] = nfl{
name: name,
file: file,
line: line,
}
}
locmu.Unlock()
}
func SetCacheBytes(l PC, name, file []byte, line int) {
locmu.Lock()
if name == nil && file == nil && line == 0 {
delete(locc, l)
} else {
x := locc[l]
if x.line != line || x.name != string(name) || x.file != string(file) {
locc[l] = nfl{
name: string(name),
file: string(file),
line: line,
}
}
}
locmu.Unlock()
}
func Cached(l PC) (ok bool) {
locmu.Lock()
_, ok = locc[l]
locmu.Unlock()
return
}