-
Notifications
You must be signed in to change notification settings - Fork 134
/
memory_intervals.go
185 lines (161 loc) · 5.43 KB
/
memory_intervals.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dependencygraph2
import (
"github.com/google/gapid/core/math/interval"
)
// memoryWrite stores a WriteMemEffect, together with a memory span affected by that write.
// The span may be smaller than the whole write.
type memoryWrite struct {
// effect WriteMemEffect
node NodeID
span interval.U64Span
}
// memoryWriteList represents a collection of memory writes, together with the regions of memory affected by each write.
// memoryWriteList implements the `interval.MutableList` interface, enabling the algorithms in `interval` for efficient queries and updates.
type memoryWriteList []memoryWrite
// Length returns the number of elements in the list
// Implements `interval.List.Length`
func (l *memoryWriteList) Length() int {
return len(*l)
}
// GetSpan returns the span for the element at index in the list
// Implements `interval.List.GetSpan`
func (l *memoryWriteList) GetSpan(index int) interval.U64Span {
return (*l)[index].span
}
// SetSpan sets the span for the element at index in the list
// Implements `interval.MutableList.SetSpan`
func (l *memoryWriteList) SetSpan(index int, span interval.U64Span) {
(*l)[index].span = span
}
// New creates a new element at the specifed index with the specified span
// Implements `interval.MutableList.New`
func (l *memoryWriteList) New(index int, span interval.U64Span) {
(*l)[index].span = span
}
// Copy count list entries
// Implements `interval.MutableList.Copy`
func (l *memoryWriteList) Copy(to, from, count int) {
copy((*l)[to:to+count], (*l)[from:from+count])
}
// Resize adjusts the length of the array
// Implements `interval.MutableList.Resize`
func (l *memoryWriteList) Resize(length int) {
if cap(*l) > length {
*l = (*l)[:length]
} else {
old := *l
capacity := cap(*l) * 2
if capacity < length {
capacity = length
}
*l = make(memoryWriteList, length, capacity)
copy(*l, old)
}
}
// memoryAccess stores a memory span together with a bool indicating whether that span has been written (true) or only read (false)
type memoryAccess struct {
mode AccessMode
span interval.U64Span
}
// memoryAccessList represents a collection of memory access
// memoryAccessList implements the `interval.MutableList` interface, enabling the algorithms in `interval` for efficient queries and updates.
type memoryAccessList []memoryAccess
// Length returns the number of elements in the list
// Implements `interval.List.Length`
func (l *memoryAccessList) Length() int {
return len(*l)
}
// GetSpan returns the span for the element at index in the list
// Implements `interval.List.GetSpan`
func (l *memoryAccessList) GetSpan(index int) interval.U64Span {
return (*l)[index].span
}
// SetSpan sets the span for the element at index in the list
// Implements `interval.MutableList.SetSpan`
func (l *memoryAccessList) SetSpan(index int, span interval.U64Span) {
(*l)[index].span = span
}
// New creates a new element at the specifed index with the specified span
// Implements `interval.MutableList.New`
func (l *memoryAccessList) New(index int, span interval.U64Span) {
(*l)[index].span = span
}
// Copy count list entries
// Implements `interval.MutableList.Copy`
func (l *memoryAccessList) Copy(to, from, count int) {
copy((*l)[to:to+count], (*l)[from:from+count])
}
// Resize adjusts the length of the array
// Implements `interval.MutableList.Resize`
func (l *memoryAccessList) Resize(length int) {
if cap(*l) > length {
*l = (*l)[:length]
} else {
old := *l
capacity := cap(*l) * 2
if capacity < length {
capacity = length
}
*l = make(memoryAccessList, length, capacity)
copy(*l, old)
}
}
func (l memoryAccessList) GetValue(index int) interface{} {
return l[index].mode
}
func (l *memoryAccessList) SetValue(index int, value interface{}) {
(*l)[index].mode = value.(AccessMode)
}
func (l *memoryAccessList) Insert(index int, count int) {
*l = append(*l, make(memoryAccessList, count)...)
if index != len(*l) && count > 0 {
copy((*l)[index+count:], (*l)[index:])
}
}
func (l *memoryAccessList) Delete(index int, count int) {
if index+count != len(*l) && count > 0 {
copy((*l)[index:], (*l)[index+count:])
}
*l = (*l)[:len(*l)-count]
}
func (l *memoryAccessList) AddRead(s interval.U64Span) {
f := func(x interface{}) interface{} {
if x == nil {
return ACCESS_READ
}
// There is already an access. Always mark the plain read, but be
// careful about the dependency read: if the same node already accessed
// before with a write, then the read is not relevant for dependency
// since it will be reading what the very same node just wrote.
m := x.(AccessMode)
m |= ACCESS_PLAIN_READ
if m&ACCESS_DEP_WRITE == 0 {
m |= ACCESS_DEP_READ
}
return m
}
interval.Update(l, s, f)
}
func (l *memoryAccessList) AddWrite(s interval.U64Span) {
f := func(x interface{}) interface{} {
if x == nil {
return ACCESS_WRITE
}
m := x.(AccessMode)
return m | ACCESS_WRITE
}
interval.Update(l, s, f)
}