Skip to content

Commit 7a18aae

Browse files
Merge pull request #32 from wongak/stack_build_tag
add build tag for Go 1.3+. See #31
2 parents 35e462c + a619218 commit 7a18aae

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: go
2+
3+
go:
4+
- 1.0
5+
- 1.1
6+
- 1.2
7+
- 1.3
8+
- release
9+
- tip

stack/stack.go

+14-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path/filepath"
88
"runtime"
99
"strings"
10-
"sync"
1110
)
1211

1312
// Call records a single function invocation from a goroutine stack. It is a
@@ -113,6 +112,20 @@ func (pc Call) Format(s fmt.State, c rune) {
113112
}
114113
}
115114

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+
116129
// name returns the import path qualified name of the function containing the
117130
// call.
118131
func (pc Call) name() string {
@@ -151,24 +164,6 @@ func (pcs Trace) Format(s fmt.State, c rune) {
151164
s.Write([]byte("]"))
152165
}
153166

154-
var pcStackPool = sync.Pool{
155-
New: func() interface{} { return make([]uintptr, 1000) },
156-
}
157-
158-
// Callers returns a Trace for the current goroutine with element 0
159-
// identifying the calling function.
160-
func Callers() Trace {
161-
pcs := pcStackPool.Get().([]uintptr)
162-
pcs = pcs[:cap(pcs)]
163-
n := runtime.Callers(2, pcs)
164-
cs := make([]Call, n)
165-
for i, pc := range pcs[:n] {
166-
cs[i] = Call(pc)
167-
}
168-
pcStackPool.Put(pcs)
169-
return cs
170-
}
171-
172167
// TrimBelow returns a slice of the Trace with all entries below pc removed.
173168
func (pcs Trace) TrimBelow(pc Call) Trace {
174169
for len(pcs) > 0 && pcs[0] != pc {

stack/stack_pool.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// +build go1.3
2+
3+
package stack
4+
5+
import (
6+
"sync"
7+
)
8+
9+
var pcStackPool = sync.Pool{
10+
New: func() interface{} { return make([]uintptr, 1000) },
11+
}
12+
13+
func poolBuf() []uintptr {
14+
return pcStackPool.Get().([]uintptr)
15+
}
16+
17+
func putPoolBuf(p []uintptr) {
18+
pcStackPool.Put(p)
19+
}

stack/stack_pool_chan.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build !go1.3
2+
3+
package stack
4+
5+
const (
6+
stackPoolSize = 64
7+
)
8+
9+
var (
10+
pcStackPool = make(chan []uintptr, stackPoolSize)
11+
)
12+
13+
func poolBuf() []uintptr {
14+
select {
15+
case p := <-pcStackPool:
16+
return p
17+
default:
18+
return make([]uintptr, 1000)
19+
}
20+
}
21+
22+
func putPoolBuf(p []uintptr) {
23+
select {
24+
case pcStackPool <- p:
25+
default:
26+
}
27+
}

0 commit comments

Comments
 (0)