Skip to content

Commit e4301ca

Browse files
committed
Add ForEachRoot iterator.
1 parent 88784b5 commit e4301ca

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

gocore/dwarf.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,16 @@ func (p *Program) typeHeap() {
425425
}
426426

427427
// Get typings starting at roots.
428-
for _, r := range p.globals {
429-
p.typeObject(r.Addr, r.Type, p.proc, add)
430-
}
431428
fr := &frameReader{p: p}
432-
for _, g := range p.goroutines {
433-
for _, f := range g.frames {
434-
fr.live = f.live
435-
for _, r := range f.roots {
436-
p.typeObject(r.Addr, r.Type, fr, add)
437-
}
429+
p.ForEachRoot(func(r *Root) bool {
430+
if r.Live != nil {
431+
fr.live = r.Live
432+
p.typeObject(r.Addr, r.Type, fr, add)
433+
} else {
434+
p.typeObject(r.Addr, r.Type, p.proc, add)
438435
}
439-
}
436+
return true
437+
})
440438

441439
// Propagate typings through the heap.
442440
for len(work) > 0 {

gocore/object.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ func (p *Program) ForEachObject(fn func(x Object) bool) {
196196
}
197197
}
198198

199+
// ForEachRoot calls fn with each garbage collection root.
200+
// If fn returns false, ForEachRoot returns immediately.
201+
func (p *Program) ForEachRoot(fn func(r *Root) bool) {
202+
for _, r := range p.globals {
203+
if !fn(r) {
204+
return
205+
}
206+
}
207+
for _, g := range p.goroutines {
208+
for _, f := range g.frames {
209+
for _, r := range f.roots {
210+
if !fn(r) {
211+
return
212+
}
213+
}
214+
}
215+
}
216+
}
217+
199218
// Addr returns the starting address of x.
200219
func (p *Program) Addr(x Object) core.Address {
201220
return core.Address(x)

0 commit comments

Comments
 (0)