]> git.cworth.org Git - apitrace/blob - common/trace_fast_callset.cpp
Rename trim::CallSet to trace::FastCallSet
[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 "trace_fast_callset.hpp"
35
36 using namespace trace;
37
38 #define MAX_LEVEL 16
39
40 FastCallRange::FastCallRange(CallNo first, CallNo last, int level)
41 {
42     this->first = first;
43     this->last = last;
44     this->level = level;
45
46     next = new FastCallRange*[level];
47 }
48
49 bool
50 FastCallRange::contains(CallNo call_no)
51 {
52     return (first <= call_no && last >= call_no);
53 }
54
55 FastCallSet::FastCallSet(): head(0, 0, MAX_LEVEL)
56 {
57     head.first = std::numeric_limits<CallNo>::max();
58     head.last = std::numeric_limits<CallNo>::min();
59
60     for (int i = 0; i < MAX_LEVEL; i++)
61         head.next[i] = NULL;
62
63     max_level = 0;
64 }
65
66 /*
67  * Generate a random level number, distributed
68  * so that each level is 1/4 as likely as the one before
69  *
70  * Note that level numbers run 1 <= level < MAX_LEVEL
71  */
72 static int
73 random_level (void)
74 {
75     /* tricky bit -- each bit is '1' 75% of the time */
76     long int bits = random() | random();
77     int level = 1;
78
79     while (level < MAX_LEVEL)
80     {
81         if (bits & 1)
82             break;
83         level++;
84         bits >>= 1;
85     }
86
87     return level;
88 }
89
90 void
91 FastCallSet::add(CallNo first, CallNo last)
92 {
93     FastCallRange **update[MAX_LEVEL];
94     FastCallRange *node, *next;
95     int i, level;
96
97     /* Find node immediately before insertion point. */
98     node = &head;
99     for (i = max_level - 1; i >= 0; i--) {
100         while (node->next[i] && first > node->next[i]->last) {
101             node = node->next[i];
102         }
103         update[i] = &node->next[i];
104     }
105
106     /* Can we contain first by expanding tail of current range by 1? */
107     if (node != &head && node->last == first - 1) {
108
109         node->last = last;
110         goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
111
112     }
113
114     /* Current range could not contain first, look at next. */
115     node = node->next[0];
116
117     if (node) {
118         /* Do nothing if new range is already entirely contained. */
119         if (node->first <= first && node->last >= last) {
120             return;
121         }
122
123         /* If last is already included, we can simply expand
124          * node->first to fully include the range. */
125         if (node->first <= last && node->last >= last) {
126             node->first = first;
127             return;
128         }
129
130         /* This is our candidate node if first is contained */
131         if (node->first <= first && node->last >= first) {
132             node->last = last;
133             goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
134         }
135     }
136
137     /* Not possible to expand any existing node, so create a new one. */
138
139     level = random_level();
140
141     /* Handle first node of this level. */
142     if (level > max_level) {
143         level = max_level + 1;
144         update[max_level] = &head.next[max_level];
145         max_level = level;
146     }
147
148     node = new FastCallRange(first, last, level);
149
150     /* Perform insertion into all lists. */
151     for (i = 0; i < level; i++) {
152         node->next[i] = *update[i];
153         *update[i] = node;
154     }
155
156 MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES:
157     next = node->next[0];
158     while (next && next->first <= node->last + 1) {
159         if (next->last > node->last)
160             node->last = next->last;
161
162         /* Delete node 'next' */
163         for (i = 0; i < node->level && i < next->level; i++) {
164             node->next[i] = next->next[i];
165         }
166
167         for (; i < next->level; i++) {
168             *update[i] = next->next[i];
169         }
170
171         delete next;
172
173         next = node->next[0];
174     }
175 }
176
177 void
178 FastCallSet::add(CallNo call_no)
179 {
180     this->add(call_no, call_no);
181 }
182
183 bool
184 FastCallSet::contains(CallNo call_no)
185 {
186     FastCallRange *node;
187     int i;
188
189     node = &head;
190     for (i = max_level - 1; i >= 0; i--) {
191         while (node->next[i] && call_no > node->next[i]->last) {
192             node = node->next[i];
193         }
194     }
195
196     node = node->next[0];
197
198     if (node == NULL)
199         return false;
200
201     return node->contains(call_no);
202 }