-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_binding_list.go
120 lines (107 loc) · 3.18 KB
/
mem_binding_list.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
// Copyright (C) 2017 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 vulkan
import (
"fmt"
"github.com/google/gapid/core/math/interval"
)
type shrinkOutOfMemBindingBound struct {
binding memBinding
offset uint64
size uint64
}
func (e shrinkOutOfMemBindingBound) Error() string {
return fmt.Sprintf("Out of the bound of memBinding's span: %v, with shrink(offset: %v, size: %v)",
e.binding.span(), e.offset, e.size)
}
type memBinding interface {
span() interval.U64Span
size() uint64
shrink(offset, size uint64) error
duplicate() memBinding
}
type memBindingList []memBinding
// Implements the interval.List interface.
func (l memBindingList) Length() int {
return len(l)
}
func (l memBindingList) GetSpan(index int) interval.U64Span {
return l[index].span()
}
// Add new memBinding to the memBindingList
func addBinding(l memBindingList, b memBinding) (memBindingList, error) {
var err error
first, count := interval.Intersect(l, b.span())
if count == 0 {
// no conflict
i := interval.Search(l, func(span interval.U64Span) bool {
return span.Start >= b.span().End
})
l = append(l[:i], append(memBindingList{b}, l[i:]...)...)
return l, nil
} else {
// has conflits, truncate the existing spans to remove conflict, then add
// the incoming bind again as if there is no conflict. Note that it is
// guaranteed that there is no conflict among the existing spans
i := first
for i < first+count {
sp := l.GetSpan(i)
if sp.Start < b.span().Start &&
sp.End <= b.span().End &&
sp.End > b.span().Start {
// truncate the tail of sp
overlap := sp.End - b.span().Start
if err = l[i].shrink(0, l[i].size()-overlap); err != nil {
return nil, err
}
i++
} else if sp.Start >= b.span().Start &&
sp.End > b.span().End &&
sp.Start < b.span().End {
// truncate the head of sp
overlap := b.span().End - sp.Start
if err = l[i].shrink(overlap, l[i].size()-overlap); err != nil {
return nil, err
}
i++
} else if sp.Start < b.span().Start &&
sp.End > b.span().End {
// split sp
newB := l[i].duplicate()
if err = newB.shrink(b.span().End-sp.Start, newB.size()-b.span().End+sp.Start); err != nil {
return nil, err
}
if err = l[i].shrink(0, sp.End-b.span().Start); err != nil {
return nil, err
}
l, err = addBinding(l, newB)
if err != nil {
return nil, err
}
// Should not have any other intersects
break
} else if sp.Start >= b.span().Start &&
sp.End <= b.span().End {
// remove sp, no need to i++
l = append(l[:i], l[i+1:]...)
count--
}
}
l, err = addBinding(l, b)
if err != nil {
return nil, err
}
}
return l, nil
}