-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptrarray.cpp
267 lines (210 loc) · 5.26 KB
/
ptrarray.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//+-------------------------------------------------------------------------
//
// Microsoft Windows
// Copyright (C) Microsoft Corporation, 1995
//
// File: ptrarray.cpp
//
// Contents: Handles dynamic arrays of void *. Stolen from MFC
//
// History: 7-13-95 Davepl Created
//
//--------------------------------------------------------------------------
#include "precomp.h"
//
// Default contstructor just invokes normal constructor, using the
// current process' heap as the heap handle
//
CPtrArray::CPtrArray()
{
CPtrArray::CPtrArray(GetProcessHeap());
}
//
// Constructor save a handle to the heap supplied to use for future
// allocations
//
CPtrArray::CPtrArray(HANDLE hHeap)
{
m_hHeap = hHeap;
m_pData = NULL;
m_nSize = 0;
m_nMaxSize = 0;
m_nGrowBy = 0;
}
CPtrArray::~CPtrArray()
{
HeapFree(m_hHeap, 0, m_pData);
}
BOOL CPtrArray::SetSize(int nNewSize, int nGrowBy)
{
ASSERT(nNewSize >= 0);
//
// Set the new size
//
if (nGrowBy != -1)
{
m_nGrowBy = nGrowBy;
}
if ( -1 == nGrowBy )
{
nGrowBy = m_nGrowBy;
}
if (nNewSize == 0)
{
//
// Shrink to nothing
//
HeapFree(m_hHeap, 0, m_pData);
m_pData = NULL;
m_nSize = m_nMaxSize = 0;
}
else if (m_pData == NULL)
{
//
// Data array doesn't exist yet, allocate it now
//
LPVOID * pnew = (LPVOID *) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, nNewSize * sizeof(*m_pData));
if (pnew)
{
m_pData = pnew;
m_nSize = nNewSize;
m_nMaxSize = nNewSize;
}
else
{
return FALSE;
}
}
else if (nNewSize <= m_nMaxSize)
{
//
// It fits
//
if (nNewSize > m_nSize)
{
// initialize the new elements
ZeroMemory(&m_pData[m_nSize], (nNewSize-m_nSize) * sizeof(*m_pData));
}
m_nSize = nNewSize;
}
else
{
Assert( -1 != nGrowBy );
//
// It doesn't fit: grow the array
//
m_nGrowBy = nGrowBy;
if (nGrowBy == 0)
{
//
// Heuristically determine growth when nGrowBy == 0
// (this avoids heap fragmentation in many situations)
//
nGrowBy = min(1024, max(4, m_nSize / 8));
}
int nNewMax;
if (nNewSize < m_nMaxSize + nGrowBy)
{
nNewMax = m_nMaxSize + nGrowBy; // granularity
}
else
{
nNewMax = nNewSize; // no slush
}
ASSERT(nNewMax >= m_nMaxSize); // no wrap around
LPVOID * pNewData = (LPVOID *) HeapReAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_pData, nNewMax * sizeof(*m_pData));
if (NULL == pNewData)
{
return FALSE;
}
ASSERT(nNewSize > m_nSize);
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
return TRUE;
}
BOOL CPtrArray::FreeExtra()
{
if (m_nSize != m_nMaxSize)
{
//
// shrink to desired size
//
void** pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (void**) HeapAlloc(m_hHeap, 0, m_nSize * sizeof(*m_pData));
if (NULL == pNewData)
{
return FALSE;
}
//
// copy new data from old
//
CopyMemory(pNewData, m_pData, m_nSize * sizeof(*m_pData));
}
//
// get rid of old stuff (note: no destructors called)
//
HeapFree(m_hHeap, 0, m_pData);
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
return TRUE;
}
BOOL CPtrArray::InsertAt(int nIndex, void* newElement, int nCount)
{
ASSERT(nIndex >= 0); // will expand to meet need
ASSERT(nCount > 0); // zero or negative size not allowed
if (nIndex >= m_nSize)
{
//
// adding after the end of the array
//
if (FALSE == SetSize(nIndex + nCount)) // grow so nIndex is valid
{
return FALSE;
}
}
else
{
//
// inserting in the middle of the array
//
int nOldSize = m_nSize;
if (FALSE == SetSize(m_nSize + nCount)) // grow it to new size
{
return FALSE;
}
//
// shift old data up to fill gap
//
MoveMemory(&m_pData[nIndex+nCount], &m_pData[nIndex], (nOldSize-nIndex) * sizeof(*m_pData));
// re-init slots we copied from
ZeroMemory(&m_pData[nIndex], nCount * sizeof(*m_pData));
}
// insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
{
m_pData[nIndex++] = newElement;
}
return TRUE;
}
BOOL CPtrArray::InsertAt(int nStartIndex, CPtrArray* pNewArray)
{
ASSERT(nStartIndex >= 0);
if (pNewArray->GetSize() > 0)
{
if (FALSE == InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize()))
{
return FALSE;
}
for (int i = 0; i < pNewArray->GetSize(); i++)
{
SetAt(nStartIndex + i, pNewArray->GetAt(i));
}
}
return TRUE;
}