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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/include/fxcrt/fx_basic.h"
#include "plex.h"
CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize)
: m_pHashTable(NULL),
m_nHashTableSize(17),
m_nCount(0),
m_pFreeList(NULL),
m_pBlocks(NULL),
m_nBlockSize(nBlockSize) {
ASSERT(m_nBlockSize > 0);
}
void CFX_MapPtrToPtr::RemoveAll() {
FX_Free(m_pHashTable);
m_pHashTable = NULL;
m_nCount = 0;
m_pFreeList = NULL;
m_pBlocks->FreeDataChain();
m_pBlocks = NULL;
}
CFX_MapPtrToPtr::~CFX_MapPtrToPtr() {
RemoveAll();
ASSERT(m_nCount == 0);
}
FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const {
return ((FX_DWORD)(uintptr_t)key) >> 4;
}
void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
void*& rKey,
void*& rValue) const {
ASSERT(m_pHashTable);
CAssoc* pAssocRet = (CAssoc*)rNextPosition;
ASSERT(pAssocRet);
if (pAssocRet == (CAssoc*)-1) {
for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) {
if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
break;
}
ASSERT(pAssocRet);
}
CAssoc* pAssocNext;
if ((pAssocNext = pAssocRet->pNext) == NULL) {
for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
nBucket < m_nHashTableSize; nBucket++) {
if ((pAssocNext = m_pHashTable[nBucket]) != NULL) {
break;
}
}
}
rNextPosition = (FX_POSITION)pAssocNext;
rKey = pAssocRet->key;
rValue = pAssocRet->value;
}
FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const {
FX_DWORD nHash;
CAssoc* pAssoc = GetAssocAt(key, nHash);
if (pAssoc == NULL) {
return FALSE;
}
rValue = pAssoc->value;
return TRUE;
}
void* CFX_MapPtrToPtr::GetValueAt(void* key) const {
FX_DWORD nHash;
CAssoc* pAssoc = GetAssocAt(key, nHash);
if (pAssoc == NULL) {
return NULL;
}
return pAssoc->value;
}
void*& CFX_MapPtrToPtr::operator[](void* key) {
FX_DWORD nHash;
CAssoc* pAssoc;
if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
if (m_pHashTable == NULL) {
InitHashTable(m_nHashTableSize);
}
pAssoc = NewAssoc();
pAssoc->key = key;
pAssoc->pNext = m_pHashTable[nHash];
m_pHashTable[nHash] = pAssoc;
}
return pAssoc->value;
}
CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::GetAssocAt(void* key,
FX_DWORD& nHash) const {
nHash = HashKey(key) % m_nHashTableSize;
if (m_pHashTable == NULL) {
return NULL;
}
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc; pAssoc = pAssoc->pNext) {
if (pAssoc->key == key)
return pAssoc;
}
return NULL;
}
CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() {
if (m_pFreeList == NULL) {
CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize,
sizeof(CFX_MapPtrToPtr::CAssoc));
CFX_MapPtrToPtr::CAssoc* pAssoc =
(CFX_MapPtrToPtr::CAssoc*)newBlock->data();
pAssoc += m_nBlockSize - 1;
for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) {
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
}
}
CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList;
m_pFreeList = m_pFreeList->pNext;
m_nCount++;
ASSERT(m_nCount > 0);
pAssoc->key = 0;
pAssoc->value = 0;
return pAssoc;
}
void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) {
ASSERT(m_nCount == 0);
ASSERT(nHashSize > 0);
FX_Free(m_pHashTable);
m_pHashTable = NULL;
if (bAllocNow) {
m_pHashTable = FX_Alloc(CAssoc*, nHashSize);
}
m_nHashTableSize = nHashSize;
}
FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) {
if (m_pHashTable == NULL) {
return FALSE;
}
CAssoc** ppAssocPrev;
ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
CAssoc* pAssoc;
for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) {
if (pAssoc->key == key) {
*ppAssocPrev = pAssoc->pNext;
FreeAssoc(pAssoc);
return TRUE;
}
ppAssocPrev = &pAssoc->pNext;
}
return FALSE;
}
void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) {
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
m_nCount--;
ASSERT(m_nCount >= 0);
if (m_nCount == 0) {
RemoveAll();
}
}
struct _CompactString {
uint8_t m_CompactLen;
uint8_t m_LenHigh;
uint8_t m_LenLow;
uint8_t m_Unused;
uint8_t* m_pBuffer;
};
static void _CompactStringRelease(_CompactString* pCompact) {
if (pCompact->m_CompactLen == 0xff) {
FX_Free(pCompact->m_pBuffer);
}
}
static FX_BOOL _CompactStringSame(_CompactString* pCompact,
const uint8_t* pStr,
int len) {
if (len < sizeof(_CompactString)) {
if (pCompact->m_CompactLen != len) {
return FALSE;
}
return FXSYS_memcmp(&pCompact->m_LenHigh, pStr, len) == 0;
}
if (pCompact->m_CompactLen != 0xff ||
pCompact->m_LenHigh * 256 + pCompact->m_LenLow != len) {
return FALSE;
}
return FXSYS_memcmp(pCompact->m_pBuffer, pStr, len) == 0;
}
static void _CompactStringStore(_CompactString* pCompact,
const uint8_t* pStr,
int len) {
if (len < (int)sizeof(_CompactString)) {
pCompact->m_CompactLen = (uint8_t)len;
FXSYS_memcpy(&pCompact->m_LenHigh, pStr, len);
return;
}
pCompact->m_CompactLen = 0xff;
pCompact->m_LenHigh = len / 256;
pCompact->m_LenLow = len % 256;
pCompact->m_pBuffer = FX_Alloc(uint8_t, len);
FXSYS_memcpy(pCompact->m_pBuffer, pStr, len);
}
static CFX_ByteStringC _CompactStringGet(_CompactString* pCompact) {
if (pCompact->m_CompactLen == 0xff) {
return CFX_ByteStringC(pCompact->m_pBuffer,
pCompact->m_LenHigh * 256 + pCompact->m_LenLow);
}
if (pCompact->m_CompactLen == 0xfe) {
return CFX_ByteStringC();
}
return CFX_ByteStringC(&pCompact->m_LenHigh, pCompact->m_CompactLen);
}
#define CMAP_ALLOC_STEP 8
#define CMAP_INDEX_SIZE 8
CFX_CMapByteStringToPtr::CFX_CMapByteStringToPtr()
: m_Buffer(sizeof(_CompactString) + sizeof(void*),
CMAP_ALLOC_STEP,
CMAP_INDEX_SIZE) {}
CFX_CMapByteStringToPtr::~CFX_CMapByteStringToPtr() {
RemoveAll();
}
void CFX_CMapByteStringToPtr::RemoveAll() {
int size = m_Buffer.GetSize();
for (int i = 0; i < size; i++) {
_CompactStringRelease((_CompactString*)m_Buffer.GetAt(i));
}
m_Buffer.RemoveAll();
}
FX_POSITION CFX_CMapByteStringToPtr::GetStartPosition() const {
int size = m_Buffer.GetSize();
for (int i = 0; i < size; i++) {
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
if (pKey->m_CompactLen != 0xfe) {
return (FX_POSITION)(uintptr_t)(i + 1);
}
}
return NULL;
}
void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
CFX_ByteString& rKey,
void*& rValue) const {
if (rNextPosition == NULL) {
return;
}
int index = (int)(uintptr_t)rNextPosition - 1;
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
rKey = _CompactStringGet(pKey);
rValue = *(void**)(pKey + 1);
index++;
int size = m_Buffer.GetSize();
while (index < size) {
pKey = (_CompactString*)m_Buffer.GetAt(index);
if (pKey->m_CompactLen != 0xfe) {
rNextPosition = (FX_POSITION)(uintptr_t)(index + 1);
return;
}
index++;
}
rNextPosition = NULL;
}
void* CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const {
if (rNextPosition == NULL) {
return NULL;
}
int index = (int)(uintptr_t)rNextPosition - 1;
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
void* rValue = *(void**)(pKey + 1);
index++;
int size = m_Buffer.GetSize();
while (index < size) {
pKey = (_CompactString*)m_Buffer.GetAt(index);
if (pKey->m_CompactLen != 0xfe) {
rNextPosition = (FX_POSITION)(uintptr_t)(index + 1);
return rValue;
}
index++;
}
rNextPosition = NULL;
return rValue;
}
FX_BOOL _CMapLookupCallback(void* param, void* pData) {
return !_CompactStringSame((_CompactString*)pData,
((CFX_ByteStringC*)param)->GetPtr(),
((CFX_ByteStringC*)param)->GetLength());
}
FX_BOOL CFX_CMapByteStringToPtr::Lookup(const CFX_ByteStringC& key,
void*& rValue) const {
void* p = m_Buffer.Iterate(_CMapLookupCallback, (void*)&key);
if (!p) {
return FALSE;
}
rValue = *(void**)((_CompactString*)p + 1);
return TRUE;
}
void CFX_CMapByteStringToPtr::SetAt(const CFX_ByteStringC& key, void* value) {
ASSERT(value);
int key_len = key.GetLength();
int size = m_Buffer.GetSize();
for (int index = 0; index < size; index++) {
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) {
continue;
}
*(void**)(pKey + 1) = value;
return;
}
for (int index = 0; index < size; index++) {
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
if (pKey->m_CompactLen) {
continue;
}
_CompactStringStore(pKey, key.GetPtr(), key_len);
*(void**)(pKey + 1) = value;
return;
}
_CompactString* pKey = (_CompactString*)m_Buffer.Add();
_CompactStringStore(pKey, key.GetPtr(), key_len);
*(void**)(pKey + 1) = value;
}
void CFX_CMapByteStringToPtr::AddValue(const CFX_ByteStringC& key,
void* value) {
ASSERT(value);
_CompactString* pKey = (_CompactString*)m_Buffer.Add();
_CompactStringStore(pKey, key.GetPtr(), key.GetLength());
*(void**)(pKey + 1) = value;
}
void CFX_CMapByteStringToPtr::RemoveKey(const CFX_ByteStringC& key) {
int key_len = key.GetLength();
int size = m_Buffer.GetSize();
for (int index = 0; index < size; index++) {
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) {
continue;
}
_CompactStringRelease(pKey);
pKey->m_CompactLen = 0xfe;
return;
}
}
int CFX_CMapByteStringToPtr::GetCount() const {
int count = 0;
int size = m_Buffer.GetSize();
for (int i = 0; i < size; i++) {
_CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
if (pKey->m_CompactLen != 0xfe) {
count++;
}
}
return count;
}
|