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

Side by Side Diff: include/core/SkTDArray.h

Issue 150663014: Change growth function for SkWriter32 (Closed) Base URL: https://skia.googlesource.com/skia.git@no_external_test
Patch Set: Add comment and assert. Created 6 years, 10 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 | « no previous file | src/core/SkWriter32.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 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
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) {
mtklein 2014/02/11 12:52:55 I suspect that we'll ultimately find we can get aw
iancottrell 2014/02/11 13:25:04 Done.
161 SkASSERT(count >= 0);
155 if (count > fReserve) { 162 if (count > fReserve) {
156 this->growBy(count - fCount); 163 resizeStorageToAtLeast(count);
157 } else {
158 fCount = count;
159 } 164 }
165 fCount = count;
166 }
167
168 /**
169 * Adjusts the number of elements in the array.
170 * This is the same as calling setCount(count() + delta).
171 */
172 void adjustCount(int delta) {
mtklein 2014/02/11 12:52:55 Can you move this down to private?
iancottrell 2014/02/11 13:25:04 Done.
173 setCount(fCount + delta);
174 }
175
176 /**
177 * Sets the number of elements in the array.
178 * If the array does not have space for count elements, it will increase
179 * the storage allocated to exactly the amount required, with no remaining
180 * reserved space.
181 * It will never shrink the shrink the storage.
182 */
183 void setCountExact(int count) {
184 if (count > fReserve) {
185 this->resizeStorageToExact(count);
186 }
187 fCount = count;
160 } 188 }
161 189
162 void setReserve(int reserve) { 190 void setReserve(int reserve) {
163 if (reserve > fReserve) { 191 if (reserve > fReserve) {
164 SkASSERT(reserve > fCount); 192 resizeStorageToAtLeast(reserve);
165 int count = fCount;
166 this->growBy(reserve - fCount);
167 fCount = count;
168 } 193 }
169 } 194 }
170 195
171 T* prepend() { 196 T* prepend() {
172 this->growBy(1); 197 this->adjustCount(1);
173 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); 198 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
174 return fArray; 199 return fArray;
175 } 200 }
176 201
177 T* append() { 202 T* append() {
178 return this->append(1, NULL); 203 return this->append(1, NULL);
179 } 204 }
180 T* append(int count, const T* src = NULL) { 205 T* append(int count, const T* src = NULL) {
181 int oldCount = fCount; 206 int oldCount = fCount;
182 if (count) { 207 if (count) {
183 SkASSERT(src == NULL || fArray == NULL || 208 SkASSERT(src == NULL || fArray == NULL ||
184 src + count <= fArray || fArray + oldCount <= src); 209 src + count <= fArray || fArray + oldCount <= src);
185 210
186 this->growBy(count); 211 this->adjustCount(count);
187 if (src) { 212 if (src) {
188 memcpy(fArray + oldCount, src, sizeof(T) * count); 213 memcpy(fArray + oldCount, src, sizeof(T) * count);
189 } 214 }
190 } 215 }
191 return fArray + oldCount; 216 return fArray + oldCount;
192 } 217 }
193 218
194 T* appendClear() { 219 T* appendClear() {
195 T* result = this->append(); 220 T* result = this->append();
196 *result = 0; 221 *result = 0;
197 return result; 222 return result;
198 } 223 }
199 224
200 T* insert(int index) { 225 T* insert(int index) {
201 return this->insert(index, 1, NULL); 226 return this->insert(index, 1, NULL);
202 } 227 }
203 T* insert(int index, int count, const T* src = NULL) { 228 T* insert(int index, int count, const T* src = NULL) {
204 SkASSERT(count); 229 SkASSERT(count);
205 SkASSERT(index <= fCount); 230 SkASSERT(index <= fCount);
206 size_t oldCount = fCount; 231 size_t oldCount = fCount;
207 this->growBy(count); 232 this->adjustCount(count);
208 T* dst = fArray + index; 233 T* dst = fArray + index;
209 memmove(dst + count, dst, sizeof(T) * (oldCount - index)); 234 memmove(dst + count, dst, sizeof(T) * (oldCount - index));
210 if (src) { 235 if (src) {
211 memcpy(dst, src, sizeof(T) * count); 236 memcpy(dst, src, sizeof(T) * count);
212 } 237 }
213 return dst; 238 return dst;
214 } 239 }
215 240
216 void remove(int index, int count = 1) { 241 void remove(int index, int count = 1) {
217 SkASSERT(index + count <= fCount); 242 SkASSERT(index + count <= fCount);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 enum { 374 enum {
350 kDebugArraySize = 16 375 kDebugArraySize = 16
351 }; 376 };
352 typedef T ArrayT[kDebugArraySize]; 377 typedef T ArrayT[kDebugArraySize];
353 ArrayT* fData; 378 ArrayT* fData;
354 #endif 379 #endif
355 T* fArray; 380 T* fArray;
356 int fReserve; 381 int fReserve;
357 int fCount; 382 int fCount;
358 383
359 void growBy(int extra) { 384 /**
360 SkASSERT(extra); 385 * This resizes the storage to *exactly* count elements, growing or
386 * shrinking the allocation as needed. It does not ASSERT anything about
387 * the previous allocation size, or about fCount.
388 *
389 * note: does NOT modify fCount
390 */
391 void resizeStorageToExact(int count) {
392 SkASSERT(count >= 0);
393 fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T));
394 #ifdef SK_DEBUG
395 fData = (ArrayT*)fArray;
396 #endif
397 fReserve = count;
398 }
361 399
362 if (fCount + extra > fReserve) { 400 /**
363 int size = fCount + extra + 4; 401 * Increase the storage allocation such that it can hold (fCount + extra)
364 size += size >> 2; 402 * elements.
365 403 * It never shrinks the allocation, and it may increase the allocation by
366 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); 404 * more than is strictly required, based on a private growth heuristic.
367 #ifdef SK_DEBUG 405 *
368 fData = (ArrayT*)fArray; 406 * note: does NOT modify fCount
369 #endif 407 */
370 fReserve = size; 408 void resizeStorageToAtLeast(int count) {
371 } 409 SkASSERT(count > fReserve);
372 fCount += extra; 410 int space = count + 4;
411 space += space>>2;
mtklein 2014/02/11 12:52:55 Is this CL blocking any others? If not, I'd be in
iancottrell 2014/02/11 13:25:04 It is blocking one more cl, yes. I have already do
412 resizeStorageToExact(space);
373 } 413 }
374 }; 414 };
375 415
376 #endif 416 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkWriter32.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698