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 "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) |
11 : m_pData(NULL), m_nSize(0), m_nMaxSize(0) { | 11 : m_pData(NULL), m_nSize(0), m_nMaxSize(0) { |
12 if (unit_size < 0 || unit_size > (1 << 28)) { | 12 if (unit_size < 0 || unit_size > (1 << 28)) { |
13 m_nUnitSize = 4; | 13 m_nUnitSize = 4; |
14 } else { | 14 } else { |
15 m_nUnitSize = unit_size; | 15 m_nUnitSize = unit_size; |
16 } | 16 } |
17 } | 17 } |
18 CFX_BasicArray::~CFX_BasicArray() { | 18 CFX_BasicArray::~CFX_BasicArray() { |
19 FX_Free(m_pData); | 19 FX_Free(m_pData); |
20 } | 20 } |
21 FX_BOOL CFX_BasicArray::SetSize(int nNewSize) { | 21 FX_BOOL CFX_BasicArray::SetSize(int nNewSize) { |
22 if (nNewSize <= 0) { | 22 if (nNewSize <= 0) { |
23 FX_Free(m_pData); | 23 FX_Free(m_pData); |
24 m_pData = NULL; | 24 m_pData = NULL; |
25 m_nSize = m_nMaxSize = 0; | 25 m_nSize = m_nMaxSize = 0; |
26 return 0 == nNewSize; | 26 return 0 == nNewSize; |
27 } | 27 } |
28 | 28 |
29 if (m_pData == NULL) { | 29 if (!m_pData) { |
30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize; | 30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize; |
31 totalSize *= m_nUnitSize; | 31 totalSize *= m_nUnitSize; |
32 if (!totalSize.IsValid()) { | 32 if (!totalSize.IsValid()) { |
33 m_nSize = m_nMaxSize = 0; | 33 m_nSize = m_nMaxSize = 0; |
34 return FALSE; | 34 return FALSE; |
35 } | 35 } |
36 m_pData = FX_Alloc(uint8_t, totalSize.ValueOrDie()); | 36 m_pData = FX_Alloc(uint8_t, totalSize.ValueOrDie()); |
37 m_nSize = m_nMaxSize = nNewSize; | 37 m_nSize = m_nMaxSize = nNewSize; |
38 } else if (nNewSize <= m_nMaxSize) { | 38 } else if (nNewSize <= m_nMaxSize) { |
39 if (nNewSize > m_nSize) { | 39 if (nNewSize > m_nSize) { |
40 FXSYS_memset(m_pData + m_nSize * m_nUnitSize, 0, | 40 FXSYS_memset(m_pData + m_nSize * m_nUnitSize, 0, |
41 (nNewSize - m_nSize) * m_nUnitSize); | 41 (nNewSize - m_nSize) * m_nUnitSize); |
42 } | 42 } |
43 m_nSize = nNewSize; | 43 m_nSize = nNewSize; |
44 } else { | 44 } else { |
45 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize; | 45 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize; |
46 pdfium::base::CheckedNumeric<int> totalSize = nNewMax; | 46 pdfium::base::CheckedNumeric<int> totalSize = nNewMax; |
47 totalSize *= m_nUnitSize; | 47 totalSize *= m_nUnitSize; |
48 if (!totalSize.IsValid() || nNewMax < m_nSize) { | 48 if (!totalSize.IsValid() || nNewMax < m_nSize) { |
49 return FALSE; | 49 return FALSE; |
50 } | 50 } |
51 uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie()); | 51 uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie()); |
52 if (pNewData == NULL) { | 52 if (!pNewData) { |
53 return FALSE; | 53 return FALSE; |
54 } | 54 } |
55 FXSYS_memset(pNewData + m_nSize * m_nUnitSize, 0, | 55 FXSYS_memset(pNewData + m_nSize * m_nUnitSize, 0, |
56 (nNewMax - m_nSize) * m_nUnitSize); | 56 (nNewMax - m_nSize) * m_nUnitSize); |
57 m_pData = pNewData; | 57 m_pData = pNewData; |
58 m_nSize = nNewSize; | 58 m_nSize = nNewSize; |
59 m_nMaxSize = nNewMax; | 59 m_nMaxSize = nNewMax; |
60 } | 60 } |
61 return TRUE; | 61 return TRUE; |
62 } | 62 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 if (nMoveCount) { | 108 if (nMoveCount) { |
109 FXSYS_memmove(m_pData + nIndex * m_nUnitSize, | 109 FXSYS_memmove(m_pData + nIndex * m_nUnitSize, |
110 m_pData + (nIndex + nCount) * m_nUnitSize, | 110 m_pData + (nIndex + nCount) * m_nUnitSize, |
111 nMoveCount * m_nUnitSize); | 111 nMoveCount * m_nUnitSize); |
112 } | 112 } |
113 m_nSize -= nCount; | 113 m_nSize -= nCount; |
114 return TRUE; | 114 return TRUE; |
115 } | 115 } |
116 FX_BOOL CFX_BasicArray::InsertAt(int nStartIndex, | 116 FX_BOOL CFX_BasicArray::InsertAt(int nStartIndex, |
117 const CFX_BasicArray* pNewArray) { | 117 const CFX_BasicArray* pNewArray) { |
118 if (pNewArray == NULL) { | 118 if (!pNewArray) { |
119 return FALSE; | 119 return FALSE; |
120 } | 120 } |
121 if (pNewArray->m_nSize == 0) { | 121 if (pNewArray->m_nSize == 0) { |
122 return TRUE; | 122 return TRUE; |
123 } | 123 } |
124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { | 124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { |
125 return FALSE; | 125 return FALSE; |
126 } | 126 } |
127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, | 127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, |
128 pNewArray->m_nSize * m_nUnitSize); | 128 pNewArray->m_nSize * m_nUnitSize); |
129 return TRUE; | 129 return TRUE; |
130 } | 130 } |
131 const void* CFX_BasicArray::GetDataPtr(int index) const { | 131 const void* CFX_BasicArray::GetDataPtr(int index) const { |
132 if (index < 0 || index >= m_nSize || m_pData == NULL) { | 132 if (index < 0 || index >= m_nSize || !m_pData) { |
133 return NULL; | 133 return NULL; |
134 } | 134 } |
135 return m_pData + index * m_nUnitSize; | 135 return m_pData + index * m_nUnitSize; |
136 } | 136 } |
137 CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, | 137 CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, |
138 int segment_units, | 138 int segment_units, |
139 int index_size) | 139 int index_size) |
140 : m_UnitSize(unit_size), | 140 : m_UnitSize(unit_size), |
141 m_SegmentSize(segment_units), | 141 m_SegmentSize(segment_units), |
142 m_IndexSize(index_size), | 142 m_IndexSize(index_size), |
143 m_IndexDepth(0), | 143 m_IndexDepth(0), |
144 m_DataSize(0), | 144 m_DataSize(0), |
145 m_pIndex(NULL) {} | 145 m_pIndex(NULL) {} |
146 void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, | 146 void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, |
147 int segment_units, | 147 int segment_units, |
148 int index_size) { | 148 int index_size) { |
149 ASSERT(m_DataSize == 0); | 149 ASSERT(m_DataSize == 0); |
150 m_UnitSize = unit_size; | 150 m_UnitSize = unit_size; |
151 m_SegmentSize = segment_units; | 151 m_SegmentSize = segment_units; |
152 m_IndexSize = index_size; | 152 m_IndexSize = index_size; |
153 } | 153 } |
154 CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray() { | 154 CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray() { |
155 RemoveAll(); | 155 RemoveAll(); |
156 } | 156 } |
157 static void _ClearIndex(int level, int size, void** pIndex) { | 157 static void _ClearIndex(int level, int size, void** pIndex) { |
158 if (level == 0) { | 158 if (level == 0) { |
159 FX_Free(pIndex); | 159 FX_Free(pIndex); |
160 return; | 160 return; |
161 } | 161 } |
162 for (int i = 0; i < size; i++) { | 162 for (int i = 0; i < size; ++i) { |
163 if (pIndex[i] == NULL) { | 163 if (pIndex[i]) |
164 continue; | 164 _ClearIndex(level - 1, size, (void**)pIndex[i]); |
165 } | |
166 _ClearIndex(level - 1, size, (void**)pIndex[i]); | |
167 } | 165 } |
168 FX_Free(pIndex); | 166 FX_Free(pIndex); |
169 } | 167 } |
170 void CFX_BaseSegmentedArray::RemoveAll() { | 168 void CFX_BaseSegmentedArray::RemoveAll() { |
171 if (m_pIndex == NULL) { | 169 if (!m_pIndex) { |
172 return; | 170 return; |
173 } | 171 } |
174 _ClearIndex(m_IndexDepth, m_IndexSize, (void**)m_pIndex); | 172 _ClearIndex(m_IndexDepth, m_IndexSize, (void**)m_pIndex); |
175 m_pIndex = NULL; | 173 m_pIndex = NULL; |
176 m_IndexDepth = 0; | 174 m_IndexDepth = 0; |
177 m_DataSize = 0; | 175 m_DataSize = 0; |
178 } | 176 } |
179 void* CFX_BaseSegmentedArray::Add() { | 177 void* CFX_BaseSegmentedArray::Add() { |
180 if (m_DataSize % m_SegmentSize) { | 178 if (m_DataSize % m_SegmentSize) { |
181 return GetAt(m_DataSize++); | 179 return GetAt(m_DataSize++); |
182 } | 180 } |
183 void* pSegment = FX_Alloc2D(uint8_t, m_UnitSize, m_SegmentSize); | 181 void* pSegment = FX_Alloc2D(uint8_t, m_UnitSize, m_SegmentSize); |
184 if (m_pIndex == NULL) { | 182 if (!m_pIndex) { |
185 m_pIndex = pSegment; | 183 m_pIndex = pSegment; |
186 m_DataSize++; | 184 m_DataSize++; |
187 return pSegment; | 185 return pSegment; |
188 } | 186 } |
189 if (m_IndexDepth == 0) { | 187 if (m_IndexDepth == 0) { |
190 void** pIndex = FX_Alloc(void*, m_IndexSize); | 188 void** pIndex = FX_Alloc(void*, m_IndexSize); |
191 pIndex[0] = m_pIndex; | 189 pIndex[0] = m_pIndex; |
192 pIndex[1] = pSegment; | 190 pIndex[1] = pSegment; |
193 m_pIndex = pIndex; | 191 m_pIndex = pIndex; |
194 m_DataSize++; | 192 m_DataSize++; |
(...skipping 15 matching lines...) Expand all Loading... |
210 if (m_DataSize == tree_size * m_SegmentSize) { | 208 if (m_DataSize == tree_size * m_SegmentSize) { |
211 void** pIndex = FX_Alloc(void*, m_IndexSize); | 209 void** pIndex = FX_Alloc(void*, m_IndexSize); |
212 pIndex[0] = m_pIndex; | 210 pIndex[0] = m_pIndex; |
213 m_pIndex = pIndex; | 211 m_pIndex = pIndex; |
214 m_IndexDepth++; | 212 m_IndexDepth++; |
215 } else { | 213 } else { |
216 tree_size /= m_IndexSize; | 214 tree_size /= m_IndexSize; |
217 } | 215 } |
218 void** pSpot = (void**)m_pIndex; | 216 void** pSpot = (void**)m_pIndex; |
219 for (i = 1; i < m_IndexDepth; i++) { | 217 for (i = 1; i < m_IndexDepth; i++) { |
220 if (pSpot[seg_index / tree_size] == NULL) { | 218 if (!pSpot[seg_index / tree_size]) { |
221 pSpot[seg_index / tree_size] = FX_Alloc(void*, m_IndexSize); | 219 pSpot[seg_index / tree_size] = FX_Alloc(void*, m_IndexSize); |
222 } | 220 } |
223 pSpot = (void**)pSpot[seg_index / tree_size]; | 221 pSpot = (void**)pSpot[seg_index / tree_size]; |
224 seg_index = seg_index % tree_size; | 222 seg_index = seg_index % tree_size; |
225 tree_size /= m_IndexSize; | 223 tree_size /= m_IndexSize; |
226 } | 224 } |
227 if (i < m_IndexDepth) { | 225 if (i < m_IndexDepth) { |
228 FX_Free(pSegment); | 226 FX_Free(pSegment); |
229 RemoveAll(); | 227 RemoveAll(); |
230 return NULL; | 228 return NULL; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 void* param) const { | 272 void* param) const { |
275 if (level == 0) { | 273 if (level == 0) { |
276 int count = m_DataSize - start; | 274 int count = m_DataSize - start; |
277 if (count > m_SegmentSize) { | 275 if (count > m_SegmentSize) { |
278 count = m_SegmentSize; | 276 count = m_SegmentSize; |
279 } | 277 } |
280 start += count; | 278 start += count; |
281 return IterateSegment((const uint8_t*)pIndex, count, callback, param); | 279 return IterateSegment((const uint8_t*)pIndex, count, callback, param); |
282 } | 280 } |
283 for (int i = 0; i < m_IndexSize; i++) { | 281 for (int i = 0; i < m_IndexSize; i++) { |
284 if (pIndex[i] == NULL) { | 282 if (!pIndex[i]) { |
285 continue; | 283 continue; |
286 } | 284 } |
287 void* p = | 285 void* p = |
288 IterateIndex(level - 1, start, (void**)pIndex[i], callback, param); | 286 IterateIndex(level - 1, start, (void**)pIndex[i], callback, param); |
289 if (p) { | 287 if (p) { |
290 return p; | 288 return p; |
291 } | 289 } |
292 } | 290 } |
293 return NULL; | 291 return NULL; |
294 } | 292 } |
295 void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, | 293 void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, |
296 void* pData), | 294 void* pData), |
297 void* param) const { | 295 void* param) const { |
298 if (m_pIndex == NULL) { | 296 if (!m_pIndex) { |
299 return NULL; | 297 return NULL; |
300 } | 298 } |
301 int start = 0; | 299 int start = 0; |
302 return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param); | 300 return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param); |
303 } | 301 } |
304 void* CFX_BaseSegmentedArray::GetAt(int index) const { | 302 void* CFX_BaseSegmentedArray::GetAt(int index) const { |
305 if (index < 0 || index >= m_DataSize) { | 303 if (index < 0 || index >= m_DataSize) { |
306 return NULL; | 304 return NULL; |
307 } | 305 } |
308 if (m_IndexDepth == 0) { | 306 if (m_IndexDepth == 0) { |
(...skipping 24 matching lines...) Expand all Loading... |
333 FX_Free(pIndex[i % m_IndexSize]); | 331 FX_Free(pIndex[i % m_IndexSize]); |
334 pIndex[i % m_IndexSize] = NULL; | 332 pIndex[i % m_IndexSize] = NULL; |
335 } | 333 } |
336 } else { | 334 } else { |
337 FX_Free(m_pIndex); | 335 FX_Free(m_pIndex); |
338 m_pIndex = NULL; | 336 m_pIndex = NULL; |
339 } | 337 } |
340 } | 338 } |
341 m_DataSize -= count; | 339 m_DataSize -= count; |
342 } | 340 } |
OLD | NEW |