OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/include/fxcrt/fx_basic.h" | 7 #include "core/include/fxcrt/fx_basic.h" |
8 #include "plex.h" | 8 #include "plex.h" |
9 | 9 |
10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) | 10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) |
(...skipping 16 matching lines...) Expand all Loading... |
27 CFX_MapPtrToPtr::~CFX_MapPtrToPtr() { | 27 CFX_MapPtrToPtr::~CFX_MapPtrToPtr() { |
28 RemoveAll(); | 28 RemoveAll(); |
29 ASSERT(m_nCount == 0); | 29 ASSERT(m_nCount == 0); |
30 } | 30 } |
31 FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const { | 31 FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const { |
32 return ((FX_DWORD)(uintptr_t)key) >> 4; | 32 return ((FX_DWORD)(uintptr_t)key) >> 4; |
33 } | 33 } |
34 void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition, | 34 void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition, |
35 void*& rKey, | 35 void*& rKey, |
36 void*& rValue) const { | 36 void*& rValue) const { |
37 ASSERT(m_pHashTable != NULL); | 37 ASSERT(m_pHashTable); |
38 CAssoc* pAssocRet = (CAssoc*)rNextPosition; | 38 CAssoc* pAssocRet = (CAssoc*)rNextPosition; |
39 ASSERT(pAssocRet != NULL); | 39 ASSERT(pAssocRet); |
40 if (pAssocRet == (CAssoc*)-1) { | 40 if (pAssocRet == (CAssoc*)-1) { |
41 for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) | 41 for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) { |
42 if ((pAssocRet = m_pHashTable[nBucket]) != NULL) { | 42 if ((pAssocRet = m_pHashTable[nBucket]) != NULL) |
43 break; | 43 break; |
44 } | 44 } |
45 ASSERT(pAssocRet != NULL); | 45 ASSERT(pAssocRet); |
46 } | 46 } |
47 CAssoc* pAssocNext; | 47 CAssoc* pAssocNext; |
48 if ((pAssocNext = pAssocRet->pNext) == NULL) { | 48 if ((pAssocNext = pAssocRet->pNext) == NULL) { |
49 for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1; | 49 for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1; |
50 nBucket < m_nHashTableSize; nBucket++) { | 50 nBucket < m_nHashTableSize; nBucket++) { |
51 if ((pAssocNext = m_pHashTable[nBucket]) != NULL) { | 51 if ((pAssocNext = m_pHashTable[nBucket]) != NULL) { |
52 break; | 52 break; |
53 } | 53 } |
54 } | 54 } |
55 } | 55 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 return pAssoc->value; | 89 return pAssoc->value; |
90 } | 90 } |
91 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::GetAssocAt(void* key, | 91 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::GetAssocAt(void* key, |
92 FX_DWORD& nHash) const { | 92 FX_DWORD& nHash) const { |
93 nHash = HashKey(key) % m_nHashTableSize; | 93 nHash = HashKey(key) % m_nHashTableSize; |
94 if (m_pHashTable == NULL) { | 94 if (m_pHashTable == NULL) { |
95 return NULL; | 95 return NULL; |
96 } | 96 } |
97 CAssoc* pAssoc; | 97 CAssoc* pAssoc; |
98 for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) { | 98 for (pAssoc = m_pHashTable[nHash]; pAssoc; pAssoc = pAssoc->pNext) { |
99 if (pAssoc->key == key) { | 99 if (pAssoc->key == key) |
100 return pAssoc; | 100 return pAssoc; |
101 } | |
102 } | 101 } |
103 return NULL; | 102 return NULL; |
104 } | 103 } |
105 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() { | 104 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() { |
106 if (m_pFreeList == NULL) { | 105 if (m_pFreeList == NULL) { |
107 CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize, | 106 CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize, |
108 sizeof(CFX_MapPtrToPtr::CAssoc)); | 107 sizeof(CFX_MapPtrToPtr::CAssoc)); |
109 CFX_MapPtrToPtr::CAssoc* pAssoc = | 108 CFX_MapPtrToPtr::CAssoc* pAssoc = |
110 (CFX_MapPtrToPtr::CAssoc*)newBlock->data(); | 109 (CFX_MapPtrToPtr::CAssoc*)newBlock->data(); |
111 pAssoc += m_nBlockSize - 1; | 110 pAssoc += m_nBlockSize - 1; |
112 for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { | 111 for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { |
113 pAssoc->pNext = m_pFreeList; | 112 pAssoc->pNext = m_pFreeList; |
114 m_pFreeList = pAssoc; | 113 m_pFreeList = pAssoc; |
115 } | 114 } |
116 } | 115 } |
117 ASSERT(m_pFreeList != NULL); | |
118 CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList; | 116 CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList; |
119 m_pFreeList = m_pFreeList->pNext; | 117 m_pFreeList = m_pFreeList->pNext; |
120 m_nCount++; | 118 m_nCount++; |
121 ASSERT(m_nCount > 0); | 119 ASSERT(m_nCount > 0); |
122 pAssoc->key = 0; | 120 pAssoc->key = 0; |
123 pAssoc->value = 0; | 121 pAssoc->value = 0; |
124 return pAssoc; | 122 return pAssoc; |
125 } | 123 } |
126 void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) { | 124 void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) { |
127 ASSERT(m_nCount == 0); | 125 ASSERT(m_nCount == 0); |
128 ASSERT(nHashSize > 0); | 126 ASSERT(nHashSize > 0); |
129 FX_Free(m_pHashTable); | 127 FX_Free(m_pHashTable); |
130 m_pHashTable = NULL; | 128 m_pHashTable = NULL; |
131 if (bAllocNow) { | 129 if (bAllocNow) { |
132 m_pHashTable = FX_Alloc(CAssoc*, nHashSize); | 130 m_pHashTable = FX_Alloc(CAssoc*, nHashSize); |
133 } | 131 } |
134 m_nHashTableSize = nHashSize; | 132 m_nHashTableSize = nHashSize; |
135 } | 133 } |
136 FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) { | 134 FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) { |
137 if (m_pHashTable == NULL) { | 135 if (m_pHashTable == NULL) { |
138 return FALSE; | 136 return FALSE; |
139 } | 137 } |
140 CAssoc** ppAssocPrev; | 138 CAssoc** ppAssocPrev; |
141 ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; | 139 ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; |
142 CAssoc* pAssoc; | 140 CAssoc* pAssoc; |
143 for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext) { | 141 for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) { |
144 if (pAssoc->key == key) { | 142 if (pAssoc->key == key) { |
145 *ppAssocPrev = pAssoc->pNext; | 143 *ppAssocPrev = pAssoc->pNext; |
146 FreeAssoc(pAssoc); | 144 FreeAssoc(pAssoc); |
147 return TRUE; | 145 return TRUE; |
148 } | 146 } |
149 ppAssocPrev = &pAssoc->pNext; | 147 ppAssocPrev = &pAssoc->pNext; |
150 } | 148 } |
151 return FALSE; | 149 return FALSE; |
152 } | 150 } |
153 void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) { | 151 void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 FX_BOOL CFX_CMapByteStringToPtr::Lookup(const CFX_ByteStringC& key, | 284 FX_BOOL CFX_CMapByteStringToPtr::Lookup(const CFX_ByteStringC& key, |
287 void*& rValue) const { | 285 void*& rValue) const { |
288 void* p = m_Buffer.Iterate(_CMapLookupCallback, (void*)&key); | 286 void* p = m_Buffer.Iterate(_CMapLookupCallback, (void*)&key); |
289 if (!p) { | 287 if (!p) { |
290 return FALSE; | 288 return FALSE; |
291 } | 289 } |
292 rValue = *(void**)((_CompactString*)p + 1); | 290 rValue = *(void**)((_CompactString*)p + 1); |
293 return TRUE; | 291 return TRUE; |
294 } | 292 } |
295 void CFX_CMapByteStringToPtr::SetAt(const CFX_ByteStringC& key, void* value) { | 293 void CFX_CMapByteStringToPtr::SetAt(const CFX_ByteStringC& key, void* value) { |
296 ASSERT(value != NULL); | 294 ASSERT(value); |
297 int index, key_len = key.GetLength(); | 295 int key_len = key.GetLength(); |
298 int size = m_Buffer.GetSize(); | 296 int size = m_Buffer.GetSize(); |
299 for (index = 0; index < size; index++) { | 297 for (int index = 0; index < size; index++) { |
300 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); | 298 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); |
301 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { | 299 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { |
302 continue; | 300 continue; |
303 } | 301 } |
304 *(void**)(pKey + 1) = value; | 302 *(void**)(pKey + 1) = value; |
305 return; | 303 return; |
306 } | 304 } |
307 for (index = 0; index < size; index++) { | 305 for (int index = 0; index < size; index++) { |
308 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); | 306 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); |
309 if (pKey->m_CompactLen) { | 307 if (pKey->m_CompactLen) { |
310 continue; | 308 continue; |
311 } | 309 } |
312 _CompactStringStore(pKey, key.GetPtr(), key_len); | 310 _CompactStringStore(pKey, key.GetPtr(), key_len); |
313 *(void**)(pKey + 1) = value; | 311 *(void**)(pKey + 1) = value; |
314 return; | 312 return; |
315 } | 313 } |
316 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); | 314 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); |
317 _CompactStringStore(pKey, key.GetPtr(), key_len); | 315 _CompactStringStore(pKey, key.GetPtr(), key_len); |
318 *(void**)(pKey + 1) = value; | 316 *(void**)(pKey + 1) = value; |
319 } | 317 } |
320 void CFX_CMapByteStringToPtr::AddValue(const CFX_ByteStringC& key, | 318 void CFX_CMapByteStringToPtr::AddValue(const CFX_ByteStringC& key, |
321 void* value) { | 319 void* value) { |
322 ASSERT(value != NULL); | 320 ASSERT(value); |
323 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); | 321 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); |
324 _CompactStringStore(pKey, key.GetPtr(), key.GetLength()); | 322 _CompactStringStore(pKey, key.GetPtr(), key.GetLength()); |
325 *(void**)(pKey + 1) = value; | 323 *(void**)(pKey + 1) = value; |
326 } | 324 } |
327 void CFX_CMapByteStringToPtr::RemoveKey(const CFX_ByteStringC& key) { | 325 void CFX_CMapByteStringToPtr::RemoveKey(const CFX_ByteStringC& key) { |
328 int key_len = key.GetLength(); | 326 int key_len = key.GetLength(); |
329 int size = m_Buffer.GetSize(); | 327 int size = m_Buffer.GetSize(); |
330 for (int index = 0; index < size; index++) { | 328 for (int index = 0; index < size; index++) { |
331 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); | 329 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); |
332 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { | 330 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { |
333 continue; | 331 continue; |
334 } | 332 } |
335 _CompactStringRelease(pKey); | 333 _CompactStringRelease(pKey); |
336 pKey->m_CompactLen = 0xfe; | 334 pKey->m_CompactLen = 0xfe; |
337 return; | 335 return; |
338 } | 336 } |
339 } | 337 } |
340 int CFX_CMapByteStringToPtr::GetCount() const { | 338 int CFX_CMapByteStringToPtr::GetCount() const { |
341 int count = 0; | 339 int count = 0; |
342 int size = m_Buffer.GetSize(); | 340 int size = m_Buffer.GetSize(); |
343 for (int i = 0; i < size; i++) { | 341 for (int i = 0; i < size; i++) { |
344 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); | 342 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); |
345 if (pKey->m_CompactLen != 0xfe) { | 343 if (pKey->m_CompactLen != 0xfe) { |
346 count++; | 344 count++; |
347 } | 345 } |
348 } | 346 } |
349 return count; | 347 return count; |
350 } | 348 } |
OLD | NEW |