File tree 3 files changed +28
-44
lines changed
3 files changed +28
-44
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,20 @@ func (pc Call) Format(s fmt.State, c rune) {
112
112
}
113
113
}
114
114
115
+ // Callers returns a Trace for the current goroutine with element 0
116
+ // identifying the calling function.
117
+ func Callers () Trace {
118
+ pcs := poolBuf ()
119
+ pcs = pcs [:cap (pcs )]
120
+ n := runtime .Callers (2 , pcs )
121
+ cs := make ([]Call , n )
122
+ for i , pc := range pcs [:n ] {
123
+ cs [i ] = Call (pc )
124
+ }
125
+ putPoolBuf (pcs )
126
+ return cs
127
+ }
128
+
115
129
// name returns the import path qualified name of the function containing the
116
130
// call.
117
131
func (pc Call ) name () string {
Original file line number Diff line number Diff line change @@ -10,16 +10,10 @@ var pcStackPool = sync.Pool{
10
10
New : func () interface {} { return make ([]uintptr , 1000 ) },
11
11
}
12
12
13
- // Callers returns a Trace for the current goroutine with element 0
14
- // identifying the calling function.
15
- func Callers () Trace {
16
- pcs := pcStackPool .Get ().([]uintptr )
17
- pcs = pcs [:cap (pcs )]
18
- n := runtime .Callers (2 , pcs )
19
- cs := make ([]Call , n )
20
- for i , pc := range pcs [:n ] {
21
- cs [i ] = Call (pc )
22
- }
23
- pcStackPool .Put (pcs )
24
- return cs
13
+ func poolBuf () []uintptr {
14
+ return pcStackPool .Get ().([]uintptr )
15
+ }
16
+
17
+ func putPoolBuf (p []uintptr ) {
18
+ pcStackPool .Put (p )
25
19
}
Original file line number Diff line number Diff line change 2
2
3
3
package stack
4
4
5
- import (
6
- "runtime"
7
- )
8
-
9
5
const (
10
6
stackPoolSize = 64
11
7
)
12
8
13
- type stackPool struct {
14
- c chan []uintptr
15
- }
16
-
17
- func newStackPool () * stackPool {
18
- return & stackPool {c : make (chan []uintptr , stackPoolSize )}
19
- }
9
+ var (
10
+ pcStackPool = make (chan []uintptr , stackPoolSize )
11
+ )
20
12
21
- func ( p * stackPool ) Get () []uintptr {
13
+ func poolBuf () []uintptr {
22
14
select {
23
- case st := <- p . c :
24
- return st
15
+ case p := <- pcStackPool :
16
+ return p
25
17
default :
26
18
return make ([]uintptr , 1000 )
27
19
}
28
20
}
29
21
30
- func ( p * stackPool ) Put ( st []uintptr ) {
22
+ func putPoolBuf ( p []uintptr ) {
31
23
select {
32
- case p . c <- st :
24
+ case pcStackPool <- p :
33
25
default :
34
26
}
35
27
}
36
-
37
- var pcStackPool = newStackPool ()
38
-
39
- // Callers returns a Trace for the current goroutine with element 0
40
- // identifying the calling function.
41
- func Callers () Trace {
42
- pcs := pcStackPool .Get ()
43
- pcs = pcs [:cap (pcs )]
44
- n := runtime .Callers (2 , pcs )
45
- cs := make ([]Call , n )
46
- for i , pc := range pcs [:n ] {
47
- cs [i ] = Call (pc )
48
- }
49
- pcStackPool .Put (pcs )
50
- return cs
51
- }
You can’t perform that action at this time.
0 commit comments