File: trace_fast_callset.cpp

package info (click to toggle)
apitrace 13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,752 kB
  • sloc: cpp: 183,974; python: 33,969; ansic: 25,566; sh: 169; makefile: 88; sed: 3
file content (210 lines) | stat: -rw-r--r-- 5,644 bytes parent folder | download | duplicates (6)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*********************************************************************
 *
 * Copyright 2006 Keith Packard and Carl Worth
 * Copyright 2012 Intel Corporation
 * Copyright 2012 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <limits>
#include <vector>

#include "os.hpp"
#include "trace_fast_callset.hpp"

using namespace trace;

#define MAX_LEVEL 16

FastCallRange::FastCallRange(CallNo first, CallNo last, int level)
: ref_counter(0)
{
    this->first = first;
    this->last = last;
    this->level = level;

    next.resize(level, 0);
}

bool
FastCallRange::contains(CallNo call_no) const
{
    return (first <= call_no && last >= call_no);
}

bool
FastCallSet::empty(void) const
{
    return max_level == 0;
}

FastCallSet::FastCallSet(): head(0, 0, MAX_LEVEL)
{
    head.first = std::numeric_limits<CallNo>::max();
    head.last = std::numeric_limits<CallNo>::min();

    max_level = 0;
}

/*
 * Generate a random level number, distributed
 * so that each level is 1/4 as likely as the one before
 *
 * Note that level numbers run 1 <= level < MAX_LEVEL
 */
static int
random_level (void)
{
    /* tricky bit -- each bit is '1' 75% of the time */
    long int bits = os::random() | os::random();
    int	level = 1;

    while (level < MAX_LEVEL)
    {
	if (bits & 1)
	    break;
        level++;
	bits >>= 1;
    }

    return level;
}

void
FastCallSet::add(CallNo first, CallNo last)
{
    std::vector<FastCallRangePtr*> update (MAX_LEVEL);
    FastCallRange *node;
    FastCallRangePtr new_node;
    int i, level;

    /* Find node immediately before insertion point.
     * NOTE: FastCallRangePtr(), e.g., next[i](), returns FastCallRange* */
    node = &head;  // Can't reference &head as a FastCallRangePtr
    for (i = max_level - 1; i >= 0; i--) {
        while (node->next[i]() && first > node->next[i]->last) {
            node = node->next[i]();
        }
        update[i] = &node->next[i];
    }

    /* Can we contain first by expanding tail of current range by 1? */
    if (node != &head && node->last == first - 1) {

        new_node = FastCallRangePtr(node);
        new_node->last = last;
        goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;

    }

    /* Current range could not contain first, look at next. */
    node = node->next[0]();

    if (node) {
        /* Do nothing if new range is already entirely contained. */
        if (node->first <= first && node->last >= last) {
            return;
        }

        /* If last is already included, we can simply expand
         * node->first to fully include the range. */
        if (node->first <= last && node->last >= last) {
            node->first = first;
            return;
        }

        /* This is our candidate node if first is contained */
        if (node->first <= first && node->last >= first) {
            new_node = FastCallRangePtr(node);
            new_node->last = last;
            goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
        }
    }

    /* Not possible to expand any existing node, so create a new one. */

    level = random_level();

    /* Handle first node of this level. */
    if (level > max_level) {
        level = max_level + 1;
        update[max_level] = &head.next[max_level];
        max_level = level;
    }

    new_node = FastCallRangePtr(new FastCallRange(first, last, level));

    /* Perform insertion into all lists. */
    for (i = 0; i < level; i++) {
        new_node->next[i] = *update[i];
        *update[i] = new_node;
    }

MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES:
    FastCallRangePtr next = new_node->next[0];
    node = new_node();
    while (next() && next->first <= node->last + 1) {
        if (next->last > node->last)
            node->last = next->last;

        for (i = 0; i < node->level && i < next->level; i++) {
            node->next[i] = next->next[i];
        }

        for (; i < next->level; i++) {
            *update[i] = next->next[i];
        }

        next = node->next[0];
    }
}

void
FastCallSet::add(CallNo call_no)
{
    this->add(call_no, call_no);
}

bool
FastCallSet::contains(CallNo call_no) const
{
    FastCallRange *node;
    int i;

    node = const_cast<FastCallRange*>(&head);
    for (i = max_level - 1; i >= 0; i--) {
        while (node->next[i]() && call_no > node->next[i]->last) {
            node = node->next[i]();
        }
    }

    node = node->next[0]();

    if (node == NULL)
        return false;

    return node->contains(call_no);
}