1 /*********************************************************************
3 * Copyright 2006 Keith Packard and Carl Worth
4 * Copyright 2012 Intel Corporation
5 * Copyright 2012 VMware, Inc.
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:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
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
28 *********************************************************************/
34 #include "trace_fast_callset.hpp"
36 using namespace trace;
40 FastCallRange::FastCallRange(CallNo first, CallNo last, int level)
46 next = new FastCallRange*[level];
50 FastCallRange::contains(CallNo call_no) const
52 return (first <= call_no && last >= call_no);
56 FastCallSet::empty(void) const
58 return max_level == 0;
61 FastCallSet::FastCallSet(): head(0, 0, MAX_LEVEL)
63 head.first = std::numeric_limits<CallNo>::max();
64 head.last = std::numeric_limits<CallNo>::min();
66 for (int i = 0; i < MAX_LEVEL; i++)
73 * Generate a random level number, distributed
74 * so that each level is 1/4 as likely as the one before
76 * Note that level numbers run 1 <= level < MAX_LEVEL
81 /* tricky bit -- each bit is '1' 75% of the time */
82 long int bits = random() | random();
85 while (level < MAX_LEVEL)
97 FastCallSet::add(CallNo first, CallNo last)
99 FastCallRange **update[MAX_LEVEL];
100 FastCallRange *node, *next;
103 /* Find node immediately before insertion point. */
105 for (i = max_level - 1; i >= 0; i--) {
106 while (node->next[i] && first > node->next[i]->last) {
107 node = node->next[i];
109 update[i] = &node->next[i];
112 /* Can we contain first by expanding tail of current range by 1? */
113 if (node != &head && node->last == first - 1) {
116 goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
120 /* Current range could not contain first, look at next. */
121 node = node->next[0];
124 /* Do nothing if new range is already entirely contained. */
125 if (node->first <= first && node->last >= last) {
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) {
136 /* This is our candidate node if first is contained */
137 if (node->first <= first && node->last >= first) {
139 goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
143 /* Not possible to expand any existing node, so create a new one. */
145 level = random_level();
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];
154 node = new FastCallRange(first, last, level);
156 /* Perform insertion into all lists. */
157 for (i = 0; i < level; i++) {
158 node->next[i] = *update[i];
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;
168 /* Delete node 'next' */
169 for (i = 0; i < node->level && i < next->level; i++) {
170 node->next[i] = next->next[i];
173 for (; i < next->level; i++) {
174 *update[i] = next->next[i];
179 next = node->next[0];
184 FastCallSet::add(CallNo call_no)
186 this->add(call_no, call_no);
190 FastCallSet::contains(CallNo call_no) const
192 const FastCallRange *node;
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];
202 node = node->next[0];
207 return node->contains(call_no);