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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 } | 54 } |
55 } | 55 } |
56 rNextPosition = (FX_POSITION)pAssocNext; | 56 rNextPosition = (FX_POSITION)pAssocNext; |
57 rKey = pAssocRet->key; | 57 rKey = pAssocRet->key; |
58 rValue = pAssocRet->value; | 58 rValue = pAssocRet->value; |
59 } | 59 } |
60 FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const { | 60 FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const { |
61 FX_DWORD nHash; | 61 FX_DWORD nHash; |
62 CAssoc* pAssoc = GetAssocAt(key, nHash); | 62 CAssoc* pAssoc = GetAssocAt(key, nHash); |
63 if (pAssoc == NULL) { | 63 if (!pAssoc) { |
64 return FALSE; | 64 return FALSE; |
65 } | 65 } |
66 rValue = pAssoc->value; | 66 rValue = pAssoc->value; |
67 return TRUE; | 67 return TRUE; |
68 } | 68 } |
69 void* CFX_MapPtrToPtr::GetValueAt(void* key) const { | 69 void* CFX_MapPtrToPtr::GetValueAt(void* key) const { |
70 FX_DWORD nHash; | 70 FX_DWORD nHash; |
71 CAssoc* pAssoc = GetAssocAt(key, nHash); | 71 CAssoc* pAssoc = GetAssocAt(key, nHash); |
72 if (pAssoc == NULL) { | 72 if (!pAssoc) { |
73 return NULL; | 73 return NULL; |
74 } | 74 } |
75 return pAssoc->value; | 75 return pAssoc->value; |
76 } | 76 } |
77 void*& CFX_MapPtrToPtr::operator[](void* key) { | 77 void*& CFX_MapPtrToPtr::operator[](void* key) { |
78 FX_DWORD nHash; | 78 FX_DWORD nHash; |
79 CAssoc* pAssoc; | 79 CAssoc* pAssoc; |
80 if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { | 80 if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { |
81 if (m_pHashTable == NULL) { | 81 if (!m_pHashTable) { |
82 InitHashTable(m_nHashTableSize); | 82 InitHashTable(m_nHashTableSize); |
83 } | 83 } |
84 pAssoc = NewAssoc(); | 84 pAssoc = NewAssoc(); |
85 pAssoc->key = key; | 85 pAssoc->key = key; |
86 pAssoc->pNext = m_pHashTable[nHash]; | 86 pAssoc->pNext = m_pHashTable[nHash]; |
87 m_pHashTable[nHash] = pAssoc; | 87 m_pHashTable[nHash] = pAssoc; |
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) { |
95 return NULL; | 95 return NULL; |
96 } | 96 } |
97 CAssoc* pAssoc; | 97 CAssoc* pAssoc; |
98 for (pAssoc = m_pHashTable[nHash]; pAssoc; 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 } | 101 } |
102 return NULL; | 102 return NULL; |
103 } | 103 } |
104 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() { | 104 CFX_MapPtrToPtr::CAssoc* CFX_MapPtrToPtr::NewAssoc() { |
105 if (m_pFreeList == NULL) { | 105 if (!m_pFreeList) { |
106 CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize, | 106 CFX_Plex* newBlock = CFX_Plex::Create(m_pBlocks, m_nBlockSize, |
107 sizeof(CFX_MapPtrToPtr::CAssoc)); | 107 sizeof(CFX_MapPtrToPtr::CAssoc)); |
108 CFX_MapPtrToPtr::CAssoc* pAssoc = | 108 CFX_MapPtrToPtr::CAssoc* pAssoc = |
109 (CFX_MapPtrToPtr::CAssoc*)newBlock->data(); | 109 (CFX_MapPtrToPtr::CAssoc*)newBlock->data(); |
110 pAssoc += m_nBlockSize - 1; | 110 pAssoc += m_nBlockSize - 1; |
111 for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { | 111 for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { |
112 pAssoc->pNext = m_pFreeList; | 112 pAssoc->pNext = m_pFreeList; |
113 m_pFreeList = pAssoc; | 113 m_pFreeList = pAssoc; |
114 } | 114 } |
115 } | 115 } |
116 CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList; | 116 CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList; |
117 m_pFreeList = m_pFreeList->pNext; | 117 m_pFreeList = m_pFreeList->pNext; |
118 m_nCount++; | 118 m_nCount++; |
119 ASSERT(m_nCount > 0); | 119 ASSERT(m_nCount > 0); |
120 pAssoc->key = 0; | 120 pAssoc->key = 0; |
121 pAssoc->value = 0; | 121 pAssoc->value = 0; |
122 return pAssoc; | 122 return pAssoc; |
123 } | 123 } |
124 void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) { | 124 void CFX_MapPtrToPtr::InitHashTable(FX_DWORD nHashSize, FX_BOOL bAllocNow) { |
125 ASSERT(m_nCount == 0); | 125 ASSERT(m_nCount == 0); |
126 ASSERT(nHashSize > 0); | 126 ASSERT(nHashSize > 0); |
127 FX_Free(m_pHashTable); | 127 FX_Free(m_pHashTable); |
128 m_pHashTable = NULL; | 128 m_pHashTable = NULL; |
129 if (bAllocNow) { | 129 if (bAllocNow) { |
130 m_pHashTable = FX_Alloc(CAssoc*, nHashSize); | 130 m_pHashTable = FX_Alloc(CAssoc*, nHashSize); |
131 } | 131 } |
132 m_nHashTableSize = nHashSize; | 132 m_nHashTableSize = nHashSize; |
133 } | 133 } |
134 FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) { | 134 FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) { |
135 if (m_pHashTable == NULL) { | 135 if (!m_pHashTable) { |
136 return FALSE; | 136 return FALSE; |
137 } | 137 } |
138 CAssoc** ppAssocPrev; | 138 CAssoc** ppAssocPrev; |
139 ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; | 139 ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; |
140 CAssoc* pAssoc; | 140 CAssoc* pAssoc; |
141 for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) { | 141 for (pAssoc = *ppAssocPrev; pAssoc; pAssoc = pAssoc->pNext) { |
142 if (pAssoc->key == key) { | 142 if (pAssoc->key == key) { |
143 *ppAssocPrev = pAssoc->pNext; | 143 *ppAssocPrev = pAssoc->pNext; |
144 FreeAssoc(pAssoc); | 144 FreeAssoc(pAssoc); |
145 return TRUE; | 145 return TRUE; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); | 230 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); |
231 if (pKey->m_CompactLen != 0xfe) { | 231 if (pKey->m_CompactLen != 0xfe) { |
232 return (FX_POSITION)(uintptr_t)(i + 1); | 232 return (FX_POSITION)(uintptr_t)(i + 1); |
233 } | 233 } |
234 } | 234 } |
235 return NULL; | 235 return NULL; |
236 } | 236 } |
237 void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, | 237 void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, |
238 CFX_ByteString& rKey, | 238 CFX_ByteString& rKey, |
239 void*& rValue) const { | 239 void*& rValue) const { |
240 if (rNextPosition == NULL) { | 240 if (!rNextPosition) { |
241 return; | 241 return; |
242 } | 242 } |
243 int index = (int)(uintptr_t)rNextPosition - 1; | 243 int index = (int)(uintptr_t)rNextPosition - 1; |
244 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); | 244 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); |
245 rKey = _CompactStringGet(pKey); | 245 rKey = _CompactStringGet(pKey); |
246 rValue = *(void**)(pKey + 1); | 246 rValue = *(void**)(pKey + 1); |
247 index++; | 247 index++; |
248 int size = m_Buffer.GetSize(); | 248 int size = m_Buffer.GetSize(); |
249 while (index < size) { | 249 while (index < size) { |
250 pKey = (_CompactString*)m_Buffer.GetAt(index); | 250 pKey = (_CompactString*)m_Buffer.GetAt(index); |
251 if (pKey->m_CompactLen != 0xfe) { | 251 if (pKey->m_CompactLen != 0xfe) { |
252 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1); | 252 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1); |
253 return; | 253 return; |
254 } | 254 } |
255 index++; | 255 index++; |
256 } | 256 } |
257 rNextPosition = NULL; | 257 rNextPosition = NULL; |
258 } | 258 } |
259 void* CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const { | 259 void* CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const { |
260 if (rNextPosition == NULL) { | 260 if (!rNextPosition) { |
261 return NULL; | 261 return NULL; |
262 } | 262 } |
263 int index = (int)(uintptr_t)rNextPosition - 1; | 263 int index = (int)(uintptr_t)rNextPosition - 1; |
264 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); | 264 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); |
265 void* rValue = *(void**)(pKey + 1); | 265 void* rValue = *(void**)(pKey + 1); |
266 index++; | 266 index++; |
267 int size = m_Buffer.GetSize(); | 267 int size = m_Buffer.GetSize(); |
268 while (index < size) { | 268 while (index < size) { |
269 pKey = (_CompactString*)m_Buffer.GetAt(index); | 269 pKey = (_CompactString*)m_Buffer.GetAt(index); |
270 if (pKey->m_CompactLen != 0xfe) { | 270 if (pKey->m_CompactLen != 0xfe) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 int count = 0; | 339 int count = 0; |
340 int size = m_Buffer.GetSize(); | 340 int size = m_Buffer.GetSize(); |
341 for (int i = 0; i < size; i++) { | 341 for (int i = 0; i < size; i++) { |
342 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); | 342 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); |
343 if (pKey->m_CompactLen != 0xfe) { | 343 if (pKey->m_CompactLen != 0xfe) { |
344 count++; | 344 count++; |
345 } | 345 } |
346 } | 346 } |
347 return count; | 347 return count; |
348 } | 348 } |
OLD | NEW |