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
|
/* libs/opengles/Tokenizer.cpp
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include "Tokenizer.h"
// ----------------------------------------------------------------------------
namespace android {
ANDROID_BASIC_TYPES_TRAITS(Tokenizer::run_t)
Tokenizer::Tokenizer()
{
}
Tokenizer::Tokenizer(const Tokenizer& other)
: mRanges(other.mRanges)
{
}
Tokenizer::~Tokenizer()
{
}
uint32_t Tokenizer::acquire()
{
if (!mRanges.size() || mRanges[0].first) {
_insertTokenAt(0,0);
return 0;
}
// just extend the first run
const run_t& run = mRanges[0];
uint32_t token = run.first + run.length;
_insertTokenAt(token, 1);
return token;
}
bool Tokenizer::isAcquired(uint32_t token) const
{
return (_indexOrderOf(token) >= 0);
}
status_t Tokenizer::reserve(uint32_t token)
{
size_t o;
const ssize_t i = _indexOrderOf(token, &o);
if (i >= 0) {
return BAD_VALUE; // this token is already taken
}
ssize_t err = _insertTokenAt(token, o);
return (err<0) ? err : status_t(NO_ERROR);
}
status_t Tokenizer::release(uint32_t token)
{
const ssize_t i = _indexOrderOf(token);
if (i >= 0) {
const run_t& run = mRanges[i];
if ((token >= run.first) && (token < run.first+run.length)) {
// token in this range, we need to split
run_t& run = mRanges.editItemAt(i);
if ((token == run.first) || (token == run.first+run.length-1)) {
if (token == run.first) {
run.first += 1;
}
run.length -= 1;
if (run.length == 0) {
// XXX: should we systematically remove a run that's empty?
mRanges.removeItemsAt(i);
}
} else {
// split the run
run_t new_run;
new_run.first = token+1;
new_run.length = run.first+run.length - new_run.first;
run.length = token - run.first;
mRanges.insertAt(new_run, i+1);
}
return NO_ERROR;
}
}
return NAME_NOT_FOUND;
}
ssize_t Tokenizer::_indexOrderOf(uint32_t token, size_t* order) const
{
// binary search
ssize_t err = NAME_NOT_FOUND;
ssize_t l = 0;
ssize_t h = mRanges.size()-1;
ssize_t mid;
const run_t* a = mRanges.array();
while (l <= h) {
mid = l + (h - l)/2;
const run_t* const curr = a + mid;
int c = 0;
if (token < curr->first) c = 1;
else if (token >= curr->first+curr->length) c = -1;
if (c == 0) {
err = l = mid;
break;
} else if (c < 0) {
l = mid + 1;
} else {
h = mid - 1;
}
}
if (order) *order = l;
return err;
}
ssize_t Tokenizer::_insertTokenAt(uint32_t token, size_t index)
{
const size_t c = mRanges.size();
if (index >= 1) {
// do we need to merge with the previous run?
run_t& p = mRanges.editItemAt(index-1);
if (p.first+p.length == token) {
p.length += 1;
if (index < c) {
const run_t& n = mRanges[index];
if (token+1 == n.first) {
p.length += n.length;
mRanges.removeItemsAt(index);
}
}
return index;
}
}
if (index < c) {
// do we need to merge with the next run?
run_t& n = mRanges.editItemAt(index);
if (token+1 == n.first) {
n.first -= 1;
n.length += 1;
return index;
}
}
return mRanges.insertAt(run_t(token,1), index);
}
void Tokenizer::dump() const
{
const run_t* ranges = mRanges.array();
const size_t c = mRanges.size();
ALOGD("Tokenizer (%p, size = %zu)\n", this, c);
for (size_t i=0 ; i<c ; i++) {
ALOGD("%zu: (%u, %u)\n", i, ranges[i].first, ranges[i].length);
}
}
}; // namespace android
|