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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 | //- Class: ArrayBasedContainer
//- Owner: Paul Kinney
//- Checked by:
//- Version: $Id:
#include "ArrayBasedContainer.hpp"
#include "CubitDefines.h"
#include <cstring>
#include <cassert>
unsigned int ArrayBasedContainer::currentAllocatedMemory = 0;
unsigned int ArrayBasedContainer::maxAllocatedMemory = 0;
//- Constructor: Create a list with initial size {increment}. The list
//- will be grown by {increment} each time it is filled. Memory for the
//- list is not allocated until the first element is inserted using
//- {insertLink}.
//- If {increment} is zero, the default increment ({INCREMENT}) will be used
//- From an efficiency standpoint, it is very important that the
//- increment be set large enough to reduce the number of list
//- growths, but small enough to not waste memory.
//- It is more efficient to sligthly overestimate the increment than
//- to underestimate the increment.
ArrayBasedContainer::ArrayBasedContainer ( int increment )
{
if ( increment > 0)
{
listIncrement = increment;
}
else
{
listIncrement = INCREMENT;
}
itemCount = 0;
listLength = 0;
listArray = 0;
poolAllocated = CUBIT_FALSE;
listIsSorted = CUBIT_FALSE;
}
//- Copy Constructor
//- Uses memory from ArrayMemory if size is default
ArrayBasedContainer::ArrayBasedContainer(const ArrayBasedContainer& from)
{
listLength = from.listLength;
listIncrement = from.listIncrement;
poolAllocated = from.poolAllocated;
listIsSorted = from.listIsSorted;
listArray = NULL;
if (listLength)
{
if (listLength == INCREMENT && (int)listIncrement == INCREMENT) {
listArray = (void **)new ArrayMemory;
poolAllocated = CUBIT_TRUE;
}
else {
listArray = new void* [listLength];
poolAllocated = CUBIT_FALSE;
currentAllocatedMemory += listLength;
if (currentAllocatedMemory > maxAllocatedMemory)
maxAllocatedMemory = currentAllocatedMemory;
}
assert(listArray != 0);
}
itemCount = from.itemCount;
if (itemCount)
memcpy (listArray, from.listArray, itemCount*sizeof(void*));
}
ArrayBasedContainer::~ArrayBasedContainer()
{
if ( listLength )
{
if (poolAllocated) {
delete ((ArrayMemory*) listArray);
}
else {
delete [] listArray;
currentAllocatedMemory -= listLength;
}
listArray = NULL;
}
}
void ArrayBasedContainer::lengthen_list()
{
void **tempArray = 0;
if (listLength == 0 && (int)listIncrement == INCREMENT)
{
poolAllocated = CUBIT_TRUE;
listArray = (void **)new ArrayMemory;
assert(listArray != 0);
listLength = listIncrement;
return;
}
// Normal allocation from here to end.
tempArray = new void* [listLength + listIncrement];
currentAllocatedMemory += (listLength + listIncrement);
if (currentAllocatedMemory > maxAllocatedMemory)
maxAllocatedMemory = currentAllocatedMemory;
assert(tempArray != 0);
// copy the old stuff into the new array
if (listLength)
{
memcpy ( tempArray, listArray, listLength*sizeof(void*) );
// delete the old array
if (poolAllocated)
{
delete ((ArrayMemory*) listArray);
}
else {
delete [] listArray;
currentAllocatedMemory -= listLength;
}
}
// set the new equal to the old
listArray = tempArray;
// update data
poolAllocated = CUBIT_FALSE;
listLength += listIncrement;
listIncrement *= 2;
listIncrement = listIncrement > 1000000 ? 1000000 : listIncrement;
}
ArrayBasedContainer&
ArrayBasedContainer::operator=(const ArrayBasedContainer& from)
{
if (this == &from)
return *this;
// See if the existing listArray length is equal to the from listArray
// length. If it is, we can reuse it without deleting and newing.
if (listLength != from.listLength) {
if (listLength) {
if (poolAllocated) {
delete ((ArrayMemory*) listArray);
} else {
delete [] listArray;
currentAllocatedMemory -= listLength;
}
}
listLength = from.listLength;
if (listLength)
{
if (listLength == INCREMENT && (int)listIncrement == INCREMENT)
{
listArray = (void **)new ArrayMemory;
poolAllocated = CUBIT_TRUE;
}
else
{
listArray = new void* [listLength];
poolAllocated = CUBIT_FALSE;
currentAllocatedMemory += listLength;
if (currentAllocatedMemory > maxAllocatedMemory)
maxAllocatedMemory = currentAllocatedMemory;
}
assert(listArray != 0);
}
}
itemCount = from.itemCount;
listIncrement = from.listIncrement;
listIsSorted = from.listIsSorted;
if (itemCount)
memcpy (listArray, from.listArray, itemCount*sizeof(void*));
return *this;
}
ArrayBasedContainer&
ArrayBasedContainer::operator+=(const ArrayBasedContainer& from)
{
int tmp_itemCount = itemCount + from.itemCount;
if (tmp_itemCount >= listLength)
{
if ((listLength + (int)listIncrement) < tmp_itemCount)
{
listIncrement = tmp_itemCount - listLength;
}
lengthen_list();
}
if (from.itemCount == 1)
{
listArray[itemCount] = from.listArray[0];
}
else
{
if (from.itemCount)
memcpy (&listArray[itemCount], from.listArray,
from.itemCount*sizeof(void*));
}
itemCount += from.itemCount;
listIsSorted = listIsSorted && from.listIsSorted;
return *this;
}
ArrayBasedContainer&
ArrayBasedContainer::operator-=(const ArrayBasedContainer& from)
{
if ( !itemCount )
return *this;
int i, j;
void *item;<--- The scope of the variable 'item' can be reduced. [+]The scope of the variable 'item' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
for ( i = 0; i < itemCount; i++ )
{
item = listArray[i];
for ( j = from.itemCount; j--; )
if ( from.listArray[j] == item )
{
listArray[i] = NULL;
break;
}
}
// compress, virtual
compress();
return *this;
}
void ArrayBasedContainer::compress()
{
int i, j;
void *item;
for ( i = j = 0; i < itemCount; i++ )
if ((item = listArray[i]) != NULL) // = is ok
listArray[j++] = item;
}
unsigned int ArrayBasedContainer::current_allocated_memory()
{
return currentAllocatedMemory;
}
unsigned int ArrayBasedContainer::maximum_allocated_memory()
{
return maxAllocatedMemory;
}
|