OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTDArray_DEFINED | 10 #ifndef SkTDArray_DEFINED |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 } else { | 144 } else { |
145 SkASSERT(fReserve == 0 && fCount == 0); | 145 SkASSERT(fReserve == 0 && fCount == 0); |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 void rewind() { | 149 void rewind() { |
150 // same as setCount(0) | 150 // same as setCount(0) |
151 fCount = 0; | 151 fCount = 0; |
152 } | 152 } |
153 | 153 |
154 /** | |
155 * Sets the number of elements in the array. | |
156 * If the array does not have space for count elements, it will increase | |
157 * the storage allocated to some amount greater than that required. | |
158 * It will never shrink the shrink the storage. | |
159 */ | |
154 void setCount(int count) { | 160 void setCount(int count) { |
155 if (count > fReserve) { | 161 if (count > fReserve) { |
156 this->growBy(count - fCount); | 162 this->growBy(count - fCount); |
157 } else { | 163 } else { |
reed1
2014/02/07 17:05:15
This is so hard to read (for me this time at least
| |
158 fCount = count; | 164 fCount = count; |
159 } | 165 } |
160 } | 166 } |
161 | 167 |
168 /** | |
169 * Sets the number of elements in the array. | |
170 * If the array does not have space for count elements, it will increase | |
171 * the storage allocated to exactly the amount required, with no remaining | |
172 * reserved space. | |
173 * It will never shrink the shrink the storage. | |
reed1
2014/02/07 17:05:15
... * this call allows the caller explicit control
| |
174 */ | |
175 void setCountExact(int count) { | |
176 if (count > fReserve) { | |
177 this->growTo(count); | |
178 } | |
179 fCount = count; | |
180 } | |
181 | |
162 void setReserve(int reserve) { | 182 void setReserve(int reserve) { |
163 if (reserve > fReserve) { | 183 if (reserve > fReserve) { |
164 SkASSERT(reserve > fCount); | 184 SkASSERT(reserve > fCount); |
165 int count = fCount; | 185 int count = fCount; |
166 this->growBy(reserve - fCount); | 186 this->growBy(reserve - fCount); |
167 fCount = count; | 187 fCount = count; |
168 } | 188 } |
169 } | 189 } |
170 | 190 |
171 T* prepend() { | 191 T* prepend() { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 enum { | 369 enum { |
350 kDebugArraySize = 16 | 370 kDebugArraySize = 16 |
351 }; | 371 }; |
352 typedef T ArrayT[kDebugArraySize]; | 372 typedef T ArrayT[kDebugArraySize]; |
353 ArrayT* fData; | 373 ArrayT* fData; |
354 #endif | 374 #endif |
355 T* fArray; | 375 T* fArray; |
356 int fReserve; | 376 int fReserve; |
357 int fCount; | 377 int fCount; |
358 | 378 |
379 /** | |
380 * This resizes the storage to *exactly* count elements, growing or | |
381 * shrinking the allocation as needed. It does not ASSERT anything about | |
382 * the previous allocation size, or about fCount. | |
383 */ | |
384 void growTo(int count) { | |
reed1
2014/02/07 17:05:15
rename to: resizeStorageTo() so we know what's goi
| |
385 fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T)); | |
386 #ifdef SK_DEBUG | |
387 fData = (ArrayT*)fArray; | |
388 #endif | |
389 fReserve = count; | |
390 } | |
391 | |
392 /** | |
393 * Increase the storage allocation such that it can hold (fCount + extra) | |
394 * elements. | |
395 * It never shrinks the allocation, and it may increase the allocation by | |
396 * more than is strictly required, based on a private growth heuristic. | |
397 */ | |
359 void growBy(int extra) { | 398 void growBy(int extra) { |
360 SkASSERT(extra); | 399 SkASSERT(extra); |
361 | 400 |
362 if (fCount + extra > fReserve) { | 401 if (fCount + extra > fReserve) { |
reed1
2014/02/07 17:05:15
Slightly unrelated to this CL, but I feel like we
| |
363 int size = fCount + extra + 4; | 402 int count = fCount + extra + 4; |
364 size += size >> 2; | 403 growTo(count + (count >> 2)); |
365 | |
366 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); | |
367 #ifdef SK_DEBUG | |
368 fData = (ArrayT*)fArray; | |
369 #endif | |
370 fReserve = size; | |
371 } | 404 } |
372 fCount += extra; | 405 fCount += extra; |
373 } | 406 } |
374 }; | 407 }; |
375 | 408 |
376 #endif | 409 #endif |
OLD | NEW |