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 "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
8 #include "../../../third_party/base/numerics/safe_math.h" | 8 #include "../../../third_party/base/numerics/safe_math.h" |
9 | 9 |
10 CFX_BasicArray::CFX_BasicArray(int unit_size) | 10 CFX_BasicArray::CFX_BasicArray(int unit_size) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSi
ze) * m_nUnitSize); | 45 FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSi
ze) * m_nUnitSize); |
46 } | 46 } |
47 m_nSize = nNewSize; | 47 m_nSize = nNewSize; |
48 } else { | 48 } else { |
49 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize; | 49 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize; |
50 pdfium::base::CheckedNumeric<int> totalSize = nNewMax; | 50 pdfium::base::CheckedNumeric<int> totalSize = nNewMax; |
51 totalSize *= m_nUnitSize; | 51 totalSize *= m_nUnitSize; |
52 if (!totalSize.IsValid() || nNewMax < m_nSize) { | 52 if (!totalSize.IsValid() || nNewMax < m_nSize) { |
53 return FALSE; | 53 return FALSE; |
54 } | 54 } |
55 FX_LPBYTE pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie()
); | 55 uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie())
; |
56 if (pNewData == NULL) { | 56 if (pNewData == NULL) { |
57 return FALSE; | 57 return FALSE; |
58 } | 58 } |
59 FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize)
* m_nUnitSize); | 59 FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize)
* m_nUnitSize); |
60 m_pData = pNewData; | 60 m_pData = pNewData; |
61 m_nSize = nNewSize; | 61 m_nSize = nNewSize; |
62 m_nMaxSize = nNewMax; | 62 m_nMaxSize = nNewMax; |
63 } | 63 } |
64 return TRUE; | 64 return TRUE; |
65 } | 65 } |
(...skipping 10 matching lines...) Expand all Loading... |
76 return TRUE; | 76 return TRUE; |
77 } | 77 } |
78 FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) | 78 FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) |
79 { | 79 { |
80 if (!SetSize(src.m_nSize)) { | 80 if (!SetSize(src.m_nSize)) { |
81 return FALSE; | 81 return FALSE; |
82 } | 82 } |
83 FXSYS_memcpy32(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); | 83 FXSYS_memcpy32(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); |
84 return TRUE; | 84 return TRUE; |
85 } | 85 } |
86 FX_LPBYTE CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) | 86 uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) |
87 { | 87 { |
88 if (nIndex < 0 || nCount <= 0) { | 88 if (nIndex < 0 || nCount <= 0) { |
89 return NULL; | 89 return NULL; |
90 } | 90 } |
91 if (nIndex >= m_nSize) { | 91 if (nIndex >= m_nSize) { |
92 if (!SetSize(nIndex + nCount)) { | 92 if (!SetSize(nIndex + nCount)) { |
93 return NULL; | 93 return NULL; |
94 } | 94 } |
95 } else { | 95 } else { |
96 int nOldSize = m_nSize; | 96 int nOldSize = m_nSize; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 tree_size *= m_IndexSize; | 252 tree_size *= m_IndexSize; |
253 } | 253 } |
254 void** pSpot = (void**)m_pIndex; | 254 void** pSpot = (void**)m_pIndex; |
255 for (i = 1; i < m_IndexDepth; i ++) { | 255 for (i = 1; i < m_IndexDepth; i ++) { |
256 pSpot = (void**)pSpot[seg_index / tree_size]; | 256 pSpot = (void**)pSpot[seg_index / tree_size]; |
257 seg_index = seg_index % tree_size; | 257 seg_index = seg_index % tree_size; |
258 tree_size /= m_IndexSize; | 258 tree_size /= m_IndexSize; |
259 } | 259 } |
260 return pSpot; | 260 return pSpot; |
261 } | 261 } |
262 void* CFX_BaseSegmentedArray::IterateSegment(FX_LPCBYTE pSegment, int count, FX_
BOOL (*callback)(void* param, void* pData), void* param) const | 262 void* CFX_BaseSegmentedArray::IterateSegment(const uint8_t* pSegment, int count,
FX_BOOL (*callback)(void* param, void* pData), void* param) const |
263 { | 263 { |
264 for (int i = 0; i < count; i ++) { | 264 for (int i = 0; i < count; i ++) { |
265 if (!callback(param, (void*)(pSegment + i * m_UnitSize))) { | 265 if (!callback(param, (void*)(pSegment + i * m_UnitSize))) { |
266 return (void*)(pSegment + i * m_UnitSize); | 266 return (void*)(pSegment + i * m_UnitSize); |
267 } | 267 } |
268 } | 268 } |
269 return NULL; | 269 return NULL; |
270 } | 270 } |
271 void* CFX_BaseSegmentedArray::IterateIndex(int level, int& start, void** pIndex,
FX_BOOL (*callback)(void* param, void* pData), void* param) const | 271 void* CFX_BaseSegmentedArray::IterateIndex(int level, int& start, void** pIndex,
FX_BOOL (*callback)(void* param, void* pData), void* param) const |
272 { | 272 { |
273 if (level == 0) { | 273 if (level == 0) { |
274 int count = m_DataSize - start; | 274 int count = m_DataSize - start; |
275 if (count > m_SegmentSize) { | 275 if (count > m_SegmentSize) { |
276 count = m_SegmentSize; | 276 count = m_SegmentSize; |
277 } | 277 } |
278 start += count; | 278 start += count; |
279 return IterateSegment((FX_LPCBYTE)pIndex, count, callback, param); | 279 return IterateSegment((const uint8_t*)pIndex, count, callback, param); |
280 } | 280 } |
281 for (int i = 0; i < m_IndexSize; i ++) { | 281 for (int i = 0; i < m_IndexSize; i ++) { |
282 if (pIndex[i] == NULL) { | 282 if (pIndex[i] == NULL) { |
283 continue; | 283 continue; |
284 } | 284 } |
285 void* p = IterateIndex(level - 1, start, (void**)pIndex[i], callback, pa
ram); | 285 void* p = IterateIndex(level - 1, start, (void**)pIndex[i], callback, pa
ram); |
286 if (p) { | 286 if (p) { |
287 return p; | 287 return p; |
288 } | 288 } |
289 } | 289 } |
290 return NULL; | 290 return NULL; |
291 } | 291 } |
292 void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, void* pDa
ta), void* param) const | 292 void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, void* pDa
ta), void* param) const |
293 { | 293 { |
294 if (m_pIndex == NULL) { | 294 if (m_pIndex == NULL) { |
295 return NULL; | 295 return NULL; |
296 } | 296 } |
297 int start = 0; | 297 int start = 0; |
298 return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param); | 298 return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param); |
299 } | 299 } |
300 void* CFX_BaseSegmentedArray::GetAt(int index) const | 300 void* CFX_BaseSegmentedArray::GetAt(int index) const |
301 { | 301 { |
302 if (index < 0 || index >= m_DataSize) { | 302 if (index < 0 || index >= m_DataSize) { |
303 return NULL; | 303 return NULL; |
304 } | 304 } |
305 if (m_IndexDepth == 0) { | 305 if (m_IndexDepth == 0) { |
306 return (FX_LPBYTE)m_pIndex + m_UnitSize * index; | 306 return (uint8_t*)m_pIndex + m_UnitSize * index; |
307 } | 307 } |
308 int seg_index = index / m_SegmentSize; | 308 int seg_index = index / m_SegmentSize; |
309 return (FX_LPBYTE)GetIndex(seg_index)[seg_index % m_IndexSize] + (index % m_
SegmentSize) * m_UnitSize; | 309 return (uint8_t*)GetIndex(seg_index)[seg_index % m_IndexSize] + (index % m_S
egmentSize) * m_UnitSize; |
310 } | 310 } |
311 void CFX_BaseSegmentedArray::Delete(int index, int count) | 311 void CFX_BaseSegmentedArray::Delete(int index, int count) |
312 { | 312 { |
313 if(index < 0 || count < 1 || index + count > m_DataSize) { | 313 if(index < 0 || count < 1 || index + count > m_DataSize) { |
314 return; | 314 return; |
315 } | 315 } |
316 int i; | 316 int i; |
317 for (i = index; i < m_DataSize - count; i ++) { | 317 for (i = index; i < m_DataSize - count; i ++) { |
318 uint8_t* pSrc = (uint8_t*)GetAt(i + count); | 318 uint8_t* pSrc = (uint8_t*)GetAt(i + count); |
319 uint8_t* pDest = (uint8_t*)GetAt(i); | 319 uint8_t* pDest = (uint8_t*)GetAt(i); |
(...skipping 10 matching lines...) Expand all Loading... |
330 FX_Free(pIndex[i % m_IndexSize]); | 330 FX_Free(pIndex[i % m_IndexSize]); |
331 pIndex[i % m_IndexSize] = NULL; | 331 pIndex[i % m_IndexSize] = NULL; |
332 } | 332 } |
333 } else { | 333 } else { |
334 FX_Free(m_pIndex); | 334 FX_Free(m_pIndex); |
335 m_pIndex = NULL; | 335 m_pIndex = NULL; |
336 } | 336 } |
337 } | 337 } |
338 m_DataSize -= count; | 338 m_DataSize -= count; |
339 } | 339 } |
OLD | NEW |