Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(792)

Side by Side Diff: core/src/fxcrt/fx_basic_array.cpp

Issue 372473003: Remove custom memory manager (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Change malloc to calloc Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fxcrt/extension.h ('k') | core/src/fxcrt/fx_basic_bstring.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 CFX_BasicArray::CFX_BasicArray(int unit_size, IFX_Allocator* pAllocator) 8 CFX_BasicArray::CFX_BasicArray(int unit_size)
9 : m_pAllocator(pAllocator) 9 : m_pData(NULL)
10 , m_pData(NULL)
11 , m_nSize(0) 10 , m_nSize(0)
12 , m_nMaxSize(0) 11 , m_nMaxSize(0)
13 , m_nGrowBy(0) 12 , m_nGrowBy(0)
14 { 13 {
15 if (unit_size < 0 || unit_size > (1 << 28)) { 14 if (unit_size < 0 || unit_size > (1 << 28)) {
16 m_nUnitSize = 4; 15 m_nUnitSize = 4;
17 } else { 16 } else {
18 m_nUnitSize = unit_size; 17 m_nUnitSize = unit_size;
19 } 18 }
20 } 19 }
21 CFX_BasicArray::~CFX_BasicArray() 20 CFX_BasicArray::~CFX_BasicArray()
22 { 21 {
23 FX_Allocator_Free(m_pAllocator, m_pData); 22 FX_Free(m_pData);
24 } 23 }
25 FX_BOOL CFX_BasicArray::SetSize(int nNewSize, int nGrowBy) 24 FX_BOOL CFX_BasicArray::SetSize(int nNewSize, int nGrowBy)
26 { 25 {
27 if (nNewSize < 0 || nNewSize > (1 << 28) / m_nUnitSize) { 26 if (nNewSize < 0 || nNewSize > (1 << 28) / m_nUnitSize) {
28 if (m_pData != NULL) { 27 if (m_pData != NULL) {
29 FX_Allocator_Free(m_pAllocator, m_pData); 28 FX_Free(m_pData);
30 m_pData = NULL; 29 m_pData = NULL;
31 » » } 30 }
32 m_nSize = m_nMaxSize = 0; 31 m_nSize = m_nMaxSize = 0;
33 return FALSE; 32 return FALSE;
34 } 33 }
35 if (nGrowBy >= 0) { 34 if (nGrowBy >= 0) {
36 m_nGrowBy = nGrowBy; 35 m_nGrowBy = nGrowBy;
37 } 36 }
38 if (nNewSize == 0) { 37 if (nNewSize == 0) {
39 if (m_pData != NULL) { 38 if (m_pData != NULL) {
40 FX_Allocator_Free(m_pAllocator, m_pData); 39 FX_Free(m_pData);
41 m_pData = NULL; 40 m_pData = NULL;
42 } 41 }
43 m_nSize = m_nMaxSize = 0; 42 m_nSize = m_nMaxSize = 0;
44 } else if (m_pData == NULL) { 43 } else if (m_pData == NULL) {
45 m_pData = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, nNewSize * m_nUnitSi ze); 44 m_pData = FX_Alloc(FX_BYTE, nNewSize * m_nUnitSize);
46 if (!m_pData) { 45 if (!m_pData) {
47 m_nSize = m_nMaxSize = 0; 46 m_nSize = m_nMaxSize = 0;
48 return FALSE; 47 return FALSE;
49 } 48 }
50 FXSYS_memset32(m_pData, 0, nNewSize * m_nUnitSize);
51 m_nSize = m_nMaxSize = nNewSize; 49 m_nSize = m_nMaxSize = nNewSize;
52 } else if (nNewSize <= m_nMaxSize) { 50 } else if (nNewSize <= m_nMaxSize) {
53 if (nNewSize > m_nSize) { 51 if (nNewSize > m_nSize) {
54 FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSi ze) * m_nUnitSize); 52 FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSi ze) * m_nUnitSize);
55 } 53 }
56 m_nSize = nNewSize; 54 m_nSize = nNewSize;
57 } else { 55 } else {
58 int nGrowBy = m_nGrowBy; 56 int nGrowBy = m_nGrowBy;
59 if (nGrowBy == 0) { 57 if (nGrowBy == 0) {
60 nGrowBy = m_nSize / 8; 58 nGrowBy = m_nSize / 8;
61 nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy); 59 nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
62 } 60 }
63 int nNewMax; 61 int nNewMax;
64 if (nNewSize < m_nMaxSize + nGrowBy) { 62 if (nNewSize < m_nMaxSize + nGrowBy) {
65 nNewMax = m_nMaxSize + nGrowBy; 63 nNewMax = m_nMaxSize + nGrowBy;
66 } else { 64 } else {
67 nNewMax = nNewSize; 65 nNewMax = nNewSize;
68 } 66 }
69 FX_LPBYTE pNewData = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pData , nNewMax * m_nUnitSize); 67 FX_LPBYTE pNewData = FX_Realloc(FX_BYTE, m_pData, nNewMax * m_nUnitSize) ;
70 if (pNewData == NULL) { 68 if (pNewData == NULL) {
71 return FALSE; 69 return FALSE;
72 } 70 }
73 FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize) * m_nUnitSize); 71 FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize) * m_nUnitSize);
74 m_pData = pNewData; 72 m_pData = pNewData;
75 m_nSize = nNewSize; 73 m_nSize = nNewSize;
76 m_nMaxSize = nNewMax; 74 m_nMaxSize = nNewMax;
77 } 75 }
78 return TRUE; 76 return TRUE;
79 } 77 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 FXSYS_memcpy32(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, pNew Array->m_nSize * m_nUnitSize); 138 FXSYS_memcpy32(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, pNew Array->m_nSize * m_nUnitSize);
141 return TRUE; 139 return TRUE;
142 } 140 }
143 const void* CFX_BasicArray::GetDataPtr(int index) const 141 const void* CFX_BasicArray::GetDataPtr(int index) const
144 { 142 {
145 if (index < 0 || index >= m_nSize || m_pData == NULL) { 143 if (index < 0 || index >= m_nSize || m_pData == NULL) {
146 return NULL; 144 return NULL;
147 } 145 }
148 return m_pData + index * m_nUnitSize; 146 return m_pData + index * m_nUnitSize;
149 } 147 }
150 CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, int segment_units, int index_size, IFX_Allocator* pAllocator) 148 CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, int segment_units, int index_size)
151 : m_pAllocator(pAllocator) 149 : m_UnitSize(unit_size)
152 , m_UnitSize(unit_size)
153 , m_SegmentSize(segment_units) 150 , m_SegmentSize(segment_units)
154 , m_IndexSize(index_size) 151 , m_IndexSize(index_size)
155 , m_IndexDepth(0) 152 , m_IndexDepth(0)
156 , m_DataSize(0) 153 , m_DataSize(0)
157 , m_pIndex(NULL) 154 , m_pIndex(NULL)
158 { 155 {
159 } 156 }
160 void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, int segment_units, int i ndex_size) 157 void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, int segment_units, int i ndex_size)
161 { 158 {
162 ASSERT(m_DataSize == 0); 159 ASSERT(m_DataSize == 0);
163 m_UnitSize = unit_size; 160 m_UnitSize = unit_size;
164 m_SegmentSize = segment_units; 161 m_SegmentSize = segment_units;
165 m_IndexSize = index_size; 162 m_IndexSize = index_size;
166 } 163 }
167 CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray() 164 CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray()
168 { 165 {
169 RemoveAll(); 166 RemoveAll();
170 } 167 }
171 static void _ClearIndex(IFX_Allocator* pAllcator, int level, int size, void** pI ndex) 168 static void _ClearIndex(int level, int size, void** pIndex)
172 { 169 {
173 if (level == 0) { 170 if (level == 0) {
174 FX_Allocator_Free(pAllcator, pIndex); 171 FX_Free(pIndex);
175 return; 172 return;
176 } 173 }
177 for (int i = 0; i < size; i ++) { 174 for (int i = 0; i < size; i++) {
178 if (pIndex[i] == NULL) { 175 if (pIndex[i] == NULL) {
179 continue; 176 continue;
180 } 177 }
181 _ClearIndex(pAllcator, level - 1, size, (void**)pIndex[i]); 178 _ClearIndex(level - 1, size, (void**)pIndex[i]);
182 } 179 }
183 FX_Allocator_Free(pAllcator, pIndex); 180 FX_Free(pIndex);
184 } 181 }
185 void CFX_BaseSegmentedArray::RemoveAll() 182 void CFX_BaseSegmentedArray::RemoveAll()
186 { 183 {
187 if (m_pIndex == NULL) { 184 if (m_pIndex == NULL) {
188 return; 185 return;
189 } 186 }
190 _ClearIndex(m_pAllocator, m_IndexDepth, m_IndexSize, (void**)m_pIndex); 187 _ClearIndex(m_IndexDepth, m_IndexSize, (void**)m_pIndex);
191 m_pIndex = NULL; 188 m_pIndex = NULL;
192 m_IndexDepth = 0; 189 m_IndexDepth = 0;
193 m_DataSize = 0; 190 m_DataSize = 0;
194 } 191 }
195 void* CFX_BaseSegmentedArray::Add() 192 void* CFX_BaseSegmentedArray::Add()
196 { 193 {
197 if (m_DataSize % m_SegmentSize) { 194 if (m_DataSize % m_SegmentSize) {
198 return GetAt(m_DataSize ++); 195 return GetAt(m_DataSize ++);
199 } 196 }
200 void* pSegment = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_UnitSize * m_Se gmentSize); 197 void* pSegment = FX_Alloc(FX_BYTE, m_UnitSize * m_SegmentSize);
201 if (!pSegment) { 198 if (!pSegment) {
202 return NULL; 199 return NULL;
203 } 200 }
204 if (m_pIndex == NULL) { 201 if (m_pIndex == NULL) {
205 m_pIndex = pSegment; 202 m_pIndex = pSegment;
206 m_DataSize ++; 203 m_DataSize ++;
207 return pSegment; 204 return pSegment;
208 } 205 }
209 if (m_IndexDepth == 0) { 206 if (m_IndexDepth == 0) {
210 void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexS ize); 207 void** pIndex = (void**)FX_Alloc(void*, m_IndexSize);
211 if (pIndex == NULL) { 208 if (pIndex == NULL) {
212 FX_Allocator_Free(m_pAllocator, pSegment); 209 FX_Free(pSegment);
213 return NULL; 210 return NULL;
214 } 211 }
215 FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize);
216 pIndex[0] = m_pIndex; 212 pIndex[0] = m_pIndex;
217 pIndex[1] = pSegment; 213 pIndex[1] = pSegment;
218 m_pIndex = pIndex; 214 m_pIndex = pIndex;
219 m_DataSize ++; 215 m_DataSize ++;
220 m_IndexDepth ++; 216 m_IndexDepth ++;
221 return pSegment; 217 return pSegment;
222 } 218 }
223 int seg_index = m_DataSize / m_SegmentSize; 219 int seg_index = m_DataSize / m_SegmentSize;
224 if (seg_index % m_IndexSize) { 220 if (seg_index % m_IndexSize) {
225 void** pIndex = GetIndex(seg_index); 221 void** pIndex = GetIndex(seg_index);
226 pIndex[seg_index % m_IndexSize] = pSegment; 222 pIndex[seg_index % m_IndexSize] = pSegment;
227 m_DataSize ++; 223 m_DataSize ++;
228 return pSegment; 224 return pSegment;
229 } 225 }
230 int tree_size = 1; 226 int tree_size = 1;
231 int i; 227 int i;
232 for (i = 0; i < m_IndexDepth; i ++) { 228 for (i = 0; i < m_IndexDepth; i ++) {
233 tree_size *= m_IndexSize; 229 tree_size *= m_IndexSize;
234 } 230 }
235 if (m_DataSize == tree_size * m_SegmentSize) { 231 if (m_DataSize == tree_size * m_SegmentSize) {
236 void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexS ize); 232 void** pIndex = (void**)FX_Alloc(void*, m_IndexSize);
237 if (pIndex == NULL) { 233 if (pIndex == NULL) {
238 FX_Allocator_Free(m_pAllocator, pSegment); 234 FX_Free(pSegment);
239 return NULL; 235 return NULL;
240 } 236 }
241 FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize);
242 pIndex[0] = m_pIndex; 237 pIndex[0] = m_pIndex;
243 m_pIndex = pIndex; 238 m_pIndex = pIndex;
244 m_IndexDepth ++; 239 m_IndexDepth ++;
245 } else { 240 } else {
246 tree_size /= m_IndexSize; 241 tree_size /= m_IndexSize;
247 } 242 }
248 void** pSpot = (void**)m_pIndex; 243 void** pSpot = (void**)m_pIndex;
249 for (i = 1; i < m_IndexDepth; i ++) { 244 for (i = 1; i < m_IndexDepth; i ++) {
250 if (pSpot[seg_index / tree_size] == NULL) { 245 if (pSpot[seg_index / tree_size] == NULL) {
251 pSpot[seg_index / tree_size] = (void*)FX_Allocator_Alloc(m_pAllocato r, void*, m_IndexSize); 246 pSpot[seg_index / tree_size] = (void*)FX_Alloc(void*, m_IndexSize);
252 if (pSpot[seg_index / tree_size] == NULL) { 247 if (pSpot[seg_index / tree_size] == NULL) {
253 break; 248 break;
254 } 249 }
255 FXSYS_memset32(pSpot[seg_index / tree_size], 0, sizeof(void*) * m_In dexSize);
256 } 250 }
257 pSpot = (void**)pSpot[seg_index / tree_size]; 251 pSpot = (void**)pSpot[seg_index / tree_size];
258 seg_index = seg_index % tree_size; 252 seg_index = seg_index % tree_size;
259 tree_size /= m_IndexSize; 253 tree_size /= m_IndexSize;
260 } 254 }
261 if (i < m_IndexDepth) { 255 if (i < m_IndexDepth) {
262 FX_Allocator_Free(m_pAllocator, pSegment); 256 FX_Free(pSegment);
263 RemoveAll(); 257 RemoveAll();
264 return NULL; 258 return NULL;
265 } 259 }
266 pSpot[seg_index % m_IndexSize] = pSegment; 260 pSpot[seg_index % m_IndexSize] = pSegment;
267 m_DataSize ++; 261 m_DataSize ++;
268 return pSegment; 262 return pSegment;
269 } 263 }
270 void** CFX_BaseSegmentedArray::GetIndex(int seg_index) const 264 void** CFX_BaseSegmentedArray::GetIndex(int seg_index) const
271 { 265 {
272 ASSERT(m_IndexDepth != 0); 266 ASSERT(m_IndexDepth != 0);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 for (int j = 0; j < m_UnitSize; j ++) { 343 for (int j = 0; j < m_UnitSize; j ++) {
350 pDest[j] = pSrc[j]; 344 pDest[j] = pSrc[j];
351 } 345 }
352 } 346 }
353 int new_segs = (m_DataSize - count + m_SegmentSize - 1) / m_SegmentSize; 347 int new_segs = (m_DataSize - count + m_SegmentSize - 1) / m_SegmentSize;
354 int old_segs = (m_DataSize + m_SegmentSize - 1) / m_SegmentSize; 348 int old_segs = (m_DataSize + m_SegmentSize - 1) / m_SegmentSize;
355 if (new_segs < old_segs) { 349 if (new_segs < old_segs) {
356 if(m_IndexDepth) { 350 if(m_IndexDepth) {
357 for (i = new_segs; i < old_segs; i ++) { 351 for (i = new_segs; i < old_segs; i ++) {
358 void** pIndex = GetIndex(i); 352 void** pIndex = GetIndex(i);
359 FX_Allocator_Free(m_pAllocator, pIndex[i % m_IndexSize]); 353 FX_Free(pIndex[i % m_IndexSize]);
360 pIndex[i % m_IndexSize] = NULL; 354 pIndex[i % m_IndexSize] = NULL;
361 } 355 }
362 } else { 356 } else {
363 FX_Allocator_Free(m_pAllocator, m_pIndex); 357 FX_Free(m_pIndex);
364 m_pIndex = NULL; 358 m_pIndex = NULL;
365 } 359 }
366 } 360 }
367 m_DataSize -= count; 361 m_DataSize -= count;
368 } 362 }
OLDNEW
« no previous file with comments | « core/src/fxcrt/extension.h ('k') | core/src/fxcrt/fx_basic_bstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698