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