-
Notifications
You must be signed in to change notification settings - Fork 416
/
location_index.go
84 lines (75 loc) · 1.92 KB
/
location_index.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
package fastdelta
// locationIndex links location IDs to the addresses, mappings, and function
// IDs referenced by the location
type locationIndex struct {
fastTable []location
slowTable map[uint64]location
}
type location struct {
address uint64
included bool
}
func (l *locationIndex) Reset() {
l.fastTable = l.fastTable[:0]
for k := range l.slowTable {
delete(l.slowTable, k)
}
}
// Insert associates the given address, mapping ID, and function IDs with the
// given location ID
func (l *locationIndex) Insert(id, address uint64) {
loc := location{address: address}
if l.slowTable == nil && id == uint64(len(l.fastTable)+1) {
l.fastTable = append(l.fastTable, loc)
} else {
if l.slowTable == nil {
l.slowTable = make(map[uint64]location, len(l.fastTable))
for i, oldLoc := range l.fastTable {
l.slowTable[uint64(i)+1] = oldLoc
}
}
l.slowTable[id] = loc
}
}
func (l *locationIndex) MarkIncluded(id uint64) {
// TODO(fg) duplicated with get() function below
if l.slowTable == nil {
id--
if id >= uint64(len(l.fastTable)) {
return
}
l.fastTable[id].included = true
} else {
loc, ok := l.slowTable[id]
if ok {
loc.included = true
l.slowTable[id] = loc
}
}
}
func (l *locationIndex) Included(id uint64) bool {
loc, _ := l.get(id)
return loc.included
}
// Get returns the address associated with the given location ID
func (l *locationIndex) Get(id uint64) (uint64, bool) {
loc, ok := l.get(id)
return loc.address, ok
}
func (l *locationIndex) get(id uint64) (loc location, ok bool) {
if l.slowTable == nil {
id--
if id >= uint64(len(l.fastTable)) {
return
}
ok = true
loc = l.fastTable[id]
} else {
loc, ok = l.slowTable[id]
}
return
}