]> git.cworth.org Git - apitrace/blob - common/trace_fast_callset.cpp
retrace: Implement glxCopySubBufferMESA
[apitrace] / common / trace_fast_callset.cpp
1 /*********************************************************************
2  *
3  * Copyright 2006 Keith Packard and Carl Worth
4  * Copyright 2012 Intel Corporation
5  * Copyright 2012 VMware, Inc.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  *********************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <limits>
33
34 #include "os.hpp"
35 #include "trace_fast_callset.hpp"
36
37 using namespace trace;
38
39 #define MAX_LEVEL 16
40
41 FastCallRange::FastCallRange(CallNo first, CallNo last, int level)
42 {
43     this->first = first;
44     this->last = last;
45     this->level = level;
46
47     next = new FastCallRange*[level];
48 }
49
50 bool
51 FastCallRange::contains(CallNo call_no) const
52 {
53     return (first <= call_no && last >= call_no);
54 }
55
56 bool
57 FastCallSet::empty(void) const
58 {
59     return max_level == 0;
60 }
61
62 FastCallSet::FastCallSet(): head(0, 0, MAX_LEVEL)
63 {
64     head.first = std::numeric_limits<CallNo>::max();
65     head.last = std::numeric_limits<CallNo>::min();
66
67     for (int i = 0; i < MAX_LEVEL; i++)
68         head.next[i] = NULL;
69
70     max_level = 0;
71 }
72
73 /*
74  * Generate a random level number, distributed
75  * so that each level is 1/4 as likely as the one before
76  *
77  * Note that level numbers run 1 <= level < MAX_LEVEL
78  */
79 static int
80 random_level (void)
81 {
82     /* tricky bit -- each bit is '1' 75% of the time */
83     long int bits = os::random() | os::random();
84     int level = 1;
85
86     while (level < MAX_LEVEL)
87     {
88         if (bits & 1)
89             break;
90         level++;
91         bits >>= 1;
92     }
93
94     return level;
95 }
96
97 void
98 FastCallSet::add(CallNo first, CallNo last)
99 {
100     FastCallRange **update[MAX_LEVEL];
101     FastCallRange *node, *next;
102     int i, level;
103
104     /* Find node immediately before insertion point. */
105     node = &head;
106     for (i = max_level - 1; i >= 0; i--) {
107         while (node->next[i] && first > node->next[i]->last) {
108             node = node->next[i];
109         }
110         update[i] = &node->next[i];
111     }
112
113     /* Can we contain first by expanding tail of current range by 1? */
114     if (node != &head && node->last == first - 1) {
115
116         node->last = last;
117         goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
118
119     }
120
121     /* Current range could not contain first, look at next. */
122     node = node->next[0];
123
124     if (node) {
125         /* Do nothing if new range is already entirely contained. */
126         if (node->first <= first && node->last >= last) {
127             return;
128         }
129
130         /* If last is already included, we can simply expand
131          * node->first to fully include the range. */
132         if (node->first <= last && node->last >= last) {
133             node->first = first;
134             return;
135         }
136
137         /* This is our candidate node if first is contained */
138         if (node->first <= first && node->last >= first) {
139             node->last = last;
140             goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
141         }
142     }
143
144     /* Not possible to expand any existing node, so create a new one. */
145
146     level = random_level();
147
148     /* Handle first node of this level. */
149     if (level > max_level) {
150         level = max_level + 1;
151         update[max_level] = &head.next[max_level];
152         max_level = level;
153     }
154
155     node = new FastCallRange(first, last, level);
156
157     /* Perform insertion into all lists. */
158     for (i = 0; i < level; i++) {
159         node->next[i] = *update[i];
160         *update[i] = node;
161     }
162
163 MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES:
164     next = node->next[0];
165     while (next && next->first <= node->last + 1) {
166         if (next->last > node->last)
167             node->last = next->last;
168
169         /* Delete node 'next' */
170         for (i = 0; i < node->level && i < next->level; i++) {
171             node->next[i] = next->next[i];
172         }
173
174         for (; i < next->level; i++) {
175             *update[i] = next->next[i];
176         }
177
178         delete next;
179
180         next = node->next[0];
181     }
182 }
183
184 void
185 FastCallSet::add(CallNo call_no)
186 {
187     this->add(call_no, call_no);
188 }
189
190 bool
191 FastCallSet::contains(CallNo call_no) const
192 {
193     const FastCallRange *node;
194     int i;
195
196     node = &head;
197     for (i = max_level - 1; i >= 0; i--) {
198         while (node->next[i] && call_no > node->next[i]->last) {
199             node = node->next[i];
200         }
201     }
202
203     node = node->next[0];
204
205     if (node == NULL)
206         return false;
207
208     return node->contains(call_no);
209 }