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

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: Rename growTo to resizeStorageTo 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) {
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) {
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.
reed1 2014/02/07 18:00:05 * * note: does NOT modify fCount
iancottrell 2014/02/11 10:15:06 Done.
388 */
389 void resizeStorageToExact(int count) {
reed1 2014/02/07 18:00:05 SkASSERT(count >= 0);
iancottrell 2014/02/11 10:15:06 Done.
390 fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T));
391 #ifdef SK_DEBUG
392 fData = (ArrayT*)fArray;
393 #endif
394 fReserve = count;
395 }
361 396
362 if (fCount + extra > fReserve) { 397 /**
363 int size = fCount + extra + 4; 398 * Increase the storage allocation such that it can hold (fCount + extra)
364 size += size >> 2; 399 * elements.
365 400 * It never shrinks the allocation, and it may increase the allocation by
366 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); 401 * more than is strictly required, based on a private growth heuristic.
reed1 2014/02/07 18:00:05 * * note: does NOT modify fCount
iancottrell 2014/02/11 10:15:06 Done.
367 #ifdef SK_DEBUG 402 */
368 fData = (ArrayT*)fArray; 403 void resizeStorageToAtLeast(int count) {
369 #endif 404 SkASSERT(count > fReserve);
370 fReserve = size; 405 int space = count + 4;
371 } 406 space += space>>2;
372 fCount += extra; 407 resizeStorageToExact(space);
373 } 408 }
374 }; 409 };
375 410
376 #endif 411 #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